@metamask/multichain-account-service
Version:
Service to manage multichain accounts
1 lines • 4.2 kB
Source Map (JSON)
{"version":3,"file":"SnapKeyringClient.cjs","sourceRoot":"","sources":["../../src/providers/SnapKeyringClient.ts"],"names":[],"mappings":";;;AACA,uEAA8D;AAC9D,yDAAoF;AAmDpF;;;;;;;GAOG;AACH,SAAgB,uBAAuB,CACrC,MAAc,EACd,IAAa;IAEb,IAAI,IAAI,EAAE,CAAC;QACT,MAAM,MAAM,GAAG,IAAI,kBAAe,CAAC,MAAM,CAAC,CAAC;QAC3C,OAAO;YACL,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE;YAC7C,aAAa,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;YACrD,gBAAgB,EAAE,KAAK,IAAkC,EAAE;gBACzD,yEAAyE;gBACzE,sEAAsE;gBACtE,MAAM,IAAI,KAAK,CACb,4DAA4D,CAC7D,CAAC;YACJ,CAAC;SACF,CAAC;IACJ,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,mCAAa,CAAC,MAAM,CAAC,CAAC;IACzC,OAAO;QACL,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC,YAAY,EAAE;QAC9C,aAAa,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QACrD,gBAAgB,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,EAAE,CAC5D,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,aAAa,EAAE,UAAU,CAAC;KAC7D,CAAC;AACJ,CAAC;AA1BD,0DA0BC","sourcesContent":["import type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport { KeyringClient as KeyringClientV2 } from '@metamask/keyring-snap-client/v2';\nimport type { CaipChainId } from '@metamask/utils';\n\n/**\n * Transport used by both the v1 and v2 keyring clients. Matches the object the\n * {@link KeyringClient} constructor expects.\n */\nexport type Sender = ConstructorParameters<typeof KeyringClient>[0];\n\ntype DiscoveredAccount = Awaited<\n ReturnType<KeyringClient['discoverAccounts']>\n>[number];\n\n/**\n * Thin abstraction over the v1 and v2 keyring RPC clients so that provider call\n * sites (account re-sync and v1 discovery) don't have to branch on the keyring\n * protocol version. The v1 and v2 clients expose different method names (e.g.\n * v1 `listAccounts` vs v2 `getAccounts`), and v2 has no `discoverAccounts` — v2\n * discovery flows through `createAccounts({ bip44:discover })` on the bridge\n * keyring instead.\n */\nexport type SnapKeyringClient = {\n /**\n * Returns the accounts the Snap currently holds.\n */\n getAccounts(): Promise<KeyringAccount[]>;\n\n /**\n * Deletes an account by id from the Snap.\n *\n * @param id - The id of the account to delete.\n */\n deleteAccount(id: string): Promise<void>;\n\n /**\n * Discovers accounts for the given entropy source and group index.\n *\n * Only supported on the v1 client; the v2 client throws, since v2 discovery\n * goes through `createAccounts({ bip44:discover })`.\n *\n * @param scopes - The scopes to discover accounts on.\n * @param entropySource - The entropy source to discover accounts for.\n * @param groupIndex - The group index to discover accounts for.\n */\n discoverAccounts(\n scopes: CaipChainId[],\n entropySource: EntropySourceId,\n groupIndex: number,\n ): Promise<DiscoveredAccount[]>;\n};\n\n/**\n * Builds a version-agnostic {@link SnapKeyringClient} backed by either the v1\n * or v2 keyring client, based on the Snap's declared capabilities.\n *\n * @param sender - The transport used to talk to the Snap.\n * @param isV2 - Whether to back the client with the v2 keyring client.\n * @returns A {@link SnapKeyringClient}.\n */\nexport function createSnapKeyringClient(\n sender: Sender,\n isV2: boolean,\n): SnapKeyringClient {\n if (isV2) {\n const client = new KeyringClientV2(sender);\n return {\n getAccounts: async () => client.getAccounts(),\n deleteAccount: async (id) => client.deleteAccount(id),\n discoverAccounts: async (): Promise<DiscoveredAccount[]> => {\n // v2 discovery is driven by `createAccounts({ bip44:discover })` through\n // the bridge keyring, so the client is never used for discovery here.\n throw new Error(\n 'discoverAccounts is not supported on the v2 keyring client',\n );\n },\n };\n }\n\n const client = new KeyringClient(sender);\n return {\n getAccounts: async () => client.listAccounts(),\n deleteAccount: async (id) => client.deleteAccount(id),\n discoverAccounts: async (scopes, entropySource, groupIndex) =>\n client.discoverAccounts(scopes, entropySource, groupIndex),\n };\n}\n"]}