Repository: onflow/fcl-js
Pull request #2079 (2025-01-27 19:46:35)

Setup eth accounts handler

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: 0b586b35dce5f99a7ff3ca11b1446a7f57e12ee5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/onflow/fcl-js/new/cf/eth-accounts?filename=.changeset/tall-socks-build.md&value=---%0A%22%40onflow%2Ffcl-ethereum-provider%22%3A%20patch%0A---%0A%0ASetup%20eth%20accounts%20handler%0A)



Patch:
diff --git a/packages/fcl-ethereum-provider/src/create-provider.ts b/packages/fcl-ethereum-provider/src/create-provider.ts
index 39ef0c61..ec0dc8bc 100644
--- a/packages/fcl-ethereum-provider/src/create-provider.ts
+++ b/packages/fcl-ethereum-provider/src/create-provider.ts
@@ -31,7 +31,7 @@ export function createProvider(config: {
   gateway?: string
 }): Eip1193Provider {
   const accountManager = new AccountManager(config.user)
-  const rpcProcessor = new RpcProcessor()
+  const rpcProcessor = new RpcProcessor(accountManager)
   const eventProcessor = new EventDispatcher()
   const provider = new FclEthereumProvider(rpcProcessor, eventProcessor)
   return provider
diff --git a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
index 16808189..4848ce60 100644
--- a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
+++ b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.ts
@@ -1,3 +1,9 @@
-export function eth_accounts() {
+import {AccountManager} from "../../accounts/account-manager"
+
+export function ethAccounts(accountManager: AccountManager): string[] {
+  return accountManager.getAccounts()
+}
+
+export function ethRequestAccounts() {
   throw new Error("Not implemented")
 }
diff --git a/packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts b/packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts
index 48e8204a..eda4bbc6 100644
--- a/packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts
+++ b/packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts
@@ -1,15 +1,14 @@
 import {ProviderRequest} from "../types/provider"
-import {eth_accounts} from "./handlers/eth-accounts"
-
-const handlers = {
-  eth_accounts,
-}
+import {ethAccounts} from "./handlers/eth-accounts"
+import {AccountManager} from "../accounts/account-manager"
 
 export class RpcProcessor {
-  constructor() {}
+  constructor(private accountManager: AccountManager) {}
 
   async handleRequest({method, params}: ProviderRequest): Promise<any> {
     switch (method) {
+      case "eth_accounts":
+        return ethAccounts(this.accountManager)
       case "eth_requestAccounts":
         throw new Error("Not implemented")
       default:


Test patch:
diff --git a/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts
new file mode 100644
index 00000000..2d8135b8
--- /dev/null
+++ b/packages/fcl-ethereum-provider/src/rpc/handlers/eth-accounts.test.ts
@@ -0,0 +1,31 @@
+import {ethAccounts} from "./eth-accounts"
+import {AccountManager} from "../../accounts/account-manager"
+
+describe("ethAccounts handler", () => {
+  let accountManagerMock: jest.Mocked<AccountManager>
+
+  beforeEach(() => {
+    accountManagerMock = {
+      getAccounts: jest.fn(),
+      subscribe: jest.fn(),
+    } as unknown as jest.Mocked<AccountManager>
+  })
+
+  it("should return accounts from the AccountManager", () => {
+    accountManagerMock.getAccounts.mockReturnValue(["0x1234...", "0x5678..."])
+
+    const accounts = ethAccounts(accountManagerMock)
+
+    expect(accounts).toEqual(["0x1234...", "0x5678..."])
+    expect(accountManagerMock.getAccounts).toHaveBeenCalled()
+  })
+
+  it("should return an empty array if no accounts are available", () => {
+    accountManagerMock.getAccounts.mockReturnValue([])
+
+    const accounts = ethAccounts(accountManagerMock)
+
+    expect(accounts).toEqual([])
+    expect(accountManagerMock.getAccounts).toHaveBeenCalled()
+  })
+})