@metamask-previews/multichain-account-controller 0.0.0-preview-0e455278 → 0.0.0-preview-dccbb1e2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/MultichainAccountController.cjs +2 -0
- package/dist/MultichainAccountController.cjs.map +1 -1
- package/dist/MultichainAccountController.d.cts +2 -29
- package/dist/MultichainAccountController.d.cts.map +1 -1
- package/dist/MultichainAccountController.d.mts +2 -29
- package/dist/MultichainAccountController.d.mts.map +1 -1
- package/dist/MultichainAccountController.mjs +2 -0
- package/dist/MultichainAccountController.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +1 -0
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +1 -0
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/package.json +1 -1
@@ -39,6 +39,8 @@ class MultichainAccountController {
|
|
39
39
|
new EvmAccountProvider_1.EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
|
40
40
|
new SolAccountProvider_1.SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
|
41
41
|
], "f");
|
42
|
+
}
|
43
|
+
init() {
|
42
44
|
// Gather all entropy sources first.
|
43
45
|
const entropySources = new Set();
|
44
46
|
for (const provider of __classPrivateFieldGet(this, _MultichainAccountController_providers, "f")) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"MultichainAccountController.cjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;
|
1
|
+
{"version":3,"file":"MultichainAccountController.cjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,6EAK0C;AAE1C,2EAAoE;AACpE,2EAAoE;AAUpE;;GAEG;AACH,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C3B,CAAC;IAED,qBAAqB,CAAC,EACpB,aAAa,GAGd;QACC,OAAO,uBAAA,IAAI,sFAAW,MAAf,IAAI,EAAY,aAAa,CAAC,CAAC,QAAQ,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,2BAA2B,CAAC,EAChC,aAAa,GAGd;QACC,OAAO,MAAM,uBAAA,IAAI,sFAAW,MAAf,IAAI,EAAY,aAAa,CAAC,CAAC,2BAA2B,EAAE,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,mCAAmC,CAAC,EACxC,aAAa,GAGd;QACC,OAAO,MAAM,uBAAA,IAAI,sFAAW,MAAf,IAAI,EACf,aAAa,CACd,CAAC,mCAAmC,EAAE,CAAC;IAC1C,CAAC;CACF;AArGD,kEAqGC;uTAxDY,aAA8B;IACvC,MAAM,MAAM,GAAG,uBAAA,IAAI,4CAAS,CAAC,GAAG,CAC9B,IAAA,oDAA2B,EAAC,aAAa,CAAC,CAC3C,CAAC;IAEF,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;KACH;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { EntropySourceId } from '@metamask/keyring-api';\nimport type {\n AccountProvider,\n MultichainAccountWalletId,\n} from '@metamask/multichain-account-api';\nimport {\n MultichainAccountWalletAdapter,\n toMultichainAccountWalletId,\n type MultichainAccount,\n type MultichainAccountWallet,\n} from '@metamask/multichain-account-api';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\nimport type { MultichainAccountControllerMessenger } from './types';\n\n/**\n * The options that {@link MultichainAccountController} takes.\n */\ntype MultichainAccountControllerOptions = {\n messenger: MultichainAccountControllerMessenger;\n};\n\n/**\n * Stateless controller to expose multichain accounts capabilities.\n */\nexport class MultichainAccountController {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #providers: AccountProvider[];\n\n readonly #wallets: Map<MultichainAccountWalletId, MultichainAccountWallet>;\n\n /**\n * Constructs a new MultichainAccountController.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountController.\n */\n constructor({ messenger }: MultichainAccountControllerOptions) {\n this.#messenger = messenger;\n this.#wallets = new Map();\n // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.\n this.#providers = [\n new EvmAccountProvider(this.#messenger),\n new SolAccountProvider(this.#messenger),\n ];\n }\n\n init(): void {\n // Gather all entropy sources first.\n const entropySources = new Set<EntropySourceId>();\n for (const provider of this.#providers) {\n for (const entropySource of provider.getEntropySources()) {\n entropySources.add(entropySource);\n }\n }\n\n for (const entropySource of entropySources) {\n // This will automatically create all multichain accounts for that wallet (based\n // on the accounts owned by each account providers).\n const wallet = new MultichainAccountWalletAdapter({\n entropySource,\n providers: this.#providers,\n });\n\n this.#wallets.set(wallet.id, wallet);\n }\n }\n\n #getWallet(entropySource: EntropySourceId): MultichainAccountWallet {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error(\n 'Unknown wallet, not wallet matching this entropy source',\n );\n }\n\n return wallet;\n }\n\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount {\n const multichainAccount =\n this.#getWallet(entropySource).accounts[groupIndex];\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount[] {\n return this.#getWallet(entropySource).accounts;\n }\n\n async createNextMultichainAccount({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount> {\n return await this.#getWallet(entropySource).createNextMultichainAccount();\n }\n\n async discoverAndCreateMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount[]> {\n return await this.#getWallet(\n entropySource,\n ).discoverAndCreateMultichainAccounts();\n }\n}\n"]}
|
@@ -1,34 +1,6 @@
|
|
1
|
-
import type { AccountsControllerGetAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
|
2
|
-
import type { RestrictedMessenger } from "@metamask/base-controller";
|
3
1
|
import type { EntropySourceId } from "@metamask/keyring-api";
|
4
|
-
import type { KeyringControllerWithKeyringAction } from "@metamask/keyring-controller";
|
5
2
|
import { type MultichainAccount } from "@metamask/multichain-account-api";
|
6
|
-
import type {
|
7
|
-
/**
|
8
|
-
* All actions that {@link MultichainAccountController} registers so that other
|
9
|
-
* modules can call them.
|
10
|
-
*/
|
11
|
-
export type MultichainAccountControllerActions = never;
|
12
|
-
/**
|
13
|
-
* All events that {@link MultichainAccountController} publishes so that other modules
|
14
|
-
* can subscribe to them.
|
15
|
-
*/
|
16
|
-
export type MultichainAccountControllerEvents = never;
|
17
|
-
/**
|
18
|
-
* All actions registered by other modules that {@link MultichainAccountController}
|
19
|
-
* calls.
|
20
|
-
*/
|
21
|
-
export type AllowedActions = AccountsControllerListMultichainAccountsAction | AccountsControllerGetAccountAction | AccountsControllerGetAccountByAddressAction | SnapControllerHandleSnapRequestAction | KeyringControllerWithKeyringAction;
|
22
|
-
/**
|
23
|
-
* All events published by other modules that {@link MultichainAccountController}
|
24
|
-
* subscribes to.
|
25
|
-
*/
|
26
|
-
export type AllowedEvents = never;
|
27
|
-
/**
|
28
|
-
* The messenger restricted to actions and events that
|
29
|
-
* {@link MultichainAccountController} needs to access.
|
30
|
-
*/
|
31
|
-
export type MultichainAccountControllerMessenger = RestrictedMessenger<'MultichainAccountController', MultichainAccountControllerActions | AllowedActions, MultichainAccountControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
3
|
+
import type { MultichainAccountControllerMessenger } from "./types.cjs";
|
32
4
|
/**
|
33
5
|
* The options that {@link MultichainAccountController} takes.
|
34
6
|
*/
|
@@ -48,6 +20,7 @@ export declare class MultichainAccountController {
|
|
48
20
|
* MultichainAccountController.
|
49
21
|
*/
|
50
22
|
constructor({ messenger }: MultichainAccountControllerOptions);
|
23
|
+
init(): void;
|
51
24
|
getMultichainAccount({ entropySource, groupIndex, }: {
|
52
25
|
entropySource: EntropySourceId;
|
53
26
|
groupIndex: number;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"MultichainAccountController.d.cts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"MultichainAccountController.d.cts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAK7D,OAAO,EAGL,KAAK,iBAAiB,EAEvB,yCAAyC;AAI1C,OAAO,KAAK,EAAE,oCAAoC,EAAE,oBAAgB;AAEpE;;GAEG;AACH,KAAK,kCAAkC,GAAG;IACxC,SAAS,EAAE,oCAAoC,CAAC;CACjD,CAAC;AAEF;;GAEG;AACH,qBAAa,2BAA2B;;IAOtC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,kCAAkC;IAU7D,IAAI,IAAI,IAAI;IAmCZ,oBAAoB,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,iBAAiB;IAWrB,qBAAqB,CAAC,EACpB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,iBAAiB,EAAE;IAIjB,2BAA2B,CAAC,EAChC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAIxB,mCAAmC,CAAC,EACxC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;CAKjC"}
|
@@ -1,34 +1,6 @@
|
|
1
|
-
import type { AccountsControllerGetAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
|
2
|
-
import type { RestrictedMessenger } from "@metamask/base-controller";
|
3
1
|
import type { EntropySourceId } from "@metamask/keyring-api";
|
4
|
-
import type { KeyringControllerWithKeyringAction } from "@metamask/keyring-controller";
|
5
2
|
import { type MultichainAccount } from "@metamask/multichain-account-api";
|
6
|
-
import type {
|
7
|
-
/**
|
8
|
-
* All actions that {@link MultichainAccountController} registers so that other
|
9
|
-
* modules can call them.
|
10
|
-
*/
|
11
|
-
export type MultichainAccountControllerActions = never;
|
12
|
-
/**
|
13
|
-
* All events that {@link MultichainAccountController} publishes so that other modules
|
14
|
-
* can subscribe to them.
|
15
|
-
*/
|
16
|
-
export type MultichainAccountControllerEvents = never;
|
17
|
-
/**
|
18
|
-
* All actions registered by other modules that {@link MultichainAccountController}
|
19
|
-
* calls.
|
20
|
-
*/
|
21
|
-
export type AllowedActions = AccountsControllerListMultichainAccountsAction | AccountsControllerGetAccountAction | AccountsControllerGetAccountByAddressAction | SnapControllerHandleSnapRequestAction | KeyringControllerWithKeyringAction;
|
22
|
-
/**
|
23
|
-
* All events published by other modules that {@link MultichainAccountController}
|
24
|
-
* subscribes to.
|
25
|
-
*/
|
26
|
-
export type AllowedEvents = never;
|
27
|
-
/**
|
28
|
-
* The messenger restricted to actions and events that
|
29
|
-
* {@link MultichainAccountController} needs to access.
|
30
|
-
*/
|
31
|
-
export type MultichainAccountControllerMessenger = RestrictedMessenger<'MultichainAccountController', MultichainAccountControllerActions | AllowedActions, MultichainAccountControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
3
|
+
import type { MultichainAccountControllerMessenger } from "./types.mjs";
|
32
4
|
/**
|
33
5
|
* The options that {@link MultichainAccountController} takes.
|
34
6
|
*/
|
@@ -48,6 +20,7 @@ export declare class MultichainAccountController {
|
|
48
20
|
* MultichainAccountController.
|
49
21
|
*/
|
50
22
|
constructor({ messenger }: MultichainAccountControllerOptions);
|
23
|
+
init(): void;
|
51
24
|
getMultichainAccount({ entropySource, groupIndex, }: {
|
52
25
|
entropySource: EntropySourceId;
|
53
26
|
groupIndex: number;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"MultichainAccountController.d.mts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"MultichainAccountController.d.mts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAK7D,OAAO,EAGL,KAAK,iBAAiB,EAEvB,yCAAyC;AAI1C,OAAO,KAAK,EAAE,oCAAoC,EAAE,oBAAgB;AAEpE;;GAEG;AACH,KAAK,kCAAkC,GAAG;IACxC,SAAS,EAAE,oCAAoC,CAAC;CACjD,CAAC;AAEF;;GAEG;AACH,qBAAa,2BAA2B;;IAOtC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,kCAAkC;IAU7D,IAAI,IAAI,IAAI;IAmCZ,oBAAoB,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,iBAAiB;IAWrB,qBAAqB,CAAC,EACpB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,iBAAiB,EAAE;IAIjB,2BAA2B,CAAC,EAChC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAIxB,mCAAmC,CAAC,EACxC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;CAKjC"}
|
@@ -36,6 +36,8 @@ export class MultichainAccountController {
|
|
36
36
|
new EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
|
37
37
|
new SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
|
38
38
|
], "f");
|
39
|
+
}
|
40
|
+
init() {
|
39
41
|
// Gather all entropy sources first.
|
40
42
|
const entropySources = new Set();
|
41
43
|
for (const provider of __classPrivateFieldGet(this, _MultichainAccountController_providers, "f")) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"MultichainAccountController.mjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
1
|
+
{"version":3,"file":"MultichainAccountController.mjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,EAG5B,yCAAyC;AAE1C,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AACpE,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AAUpE;;GAEG;AACH,MAAM,OAAO,2BAA2B;IAOtC;;;;;;OAMG;IACH,YAAY,EAAE,SAAS,EAAsC;;QAbpD,yDAAiD;QAEjD,yDAA8B;QAE9B,uDAAkE;QAUzE,uBAAA,IAAI,0CAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wCAAY,IAAI,GAAG,EAAE,MAAA,CAAC;QAC1B,mFAAmF;QACnF,uBAAA,IAAI,0CAAc;YAChB,IAAI,kBAAkB,CAAC,uBAAA,IAAI,8CAAW,CAAC;YACvC,IAAI,kBAAkB,CAAC,uBAAA,IAAI,8CAAW,CAAC;SACxC,MAAA,CAAC;IACJ,CAAC;IAED,IAAI;QACF,oCAAoC;QACpC,MAAM,cAAc,GAAG,IAAI,GAAG,EAAmB,CAAC;QAClD,KAAK,MAAM,QAAQ,IAAI,uBAAA,IAAI,8CAAW,EAAE;YACtC,KAAK,MAAM,aAAa,IAAI,QAAQ,CAAC,iBAAiB,EA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F,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;KACH;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { EntropySourceId } from '@metamask/keyring-api';\nimport type {\n AccountProvider,\n MultichainAccountWalletId,\n} from '@metamask/multichain-account-api';\nimport {\n MultichainAccountWalletAdapter,\n toMultichainAccountWalletId,\n type MultichainAccount,\n type MultichainAccountWallet,\n} from '@metamask/multichain-account-api';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\nimport type { MultichainAccountControllerMessenger } from './types';\n\n/**\n * The options that {@link MultichainAccountController} takes.\n */\ntype MultichainAccountControllerOptions = {\n messenger: MultichainAccountControllerMessenger;\n};\n\n/**\n * Stateless controller to expose multichain accounts capabilities.\n */\nexport class MultichainAccountController {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #providers: AccountProvider[];\n\n readonly #wallets: Map<MultichainAccountWalletId, MultichainAccountWallet>;\n\n /**\n * Constructs a new MultichainAccountController.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountController.\n */\n constructor({ messenger }: MultichainAccountControllerOptions) {\n this.#messenger = messenger;\n this.#wallets = new Map();\n // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.\n this.#providers = [\n new EvmAccountProvider(this.#messenger),\n new SolAccountProvider(this.#messenger),\n ];\n }\n\n init(): void {\n // Gather all entropy sources first.\n const entropySources = new Set<EntropySourceId>();\n for (const provider of this.#providers) {\n for (const entropySource of provider.getEntropySources()) {\n entropySources.add(entropySource);\n }\n }\n\n for (const entropySource of entropySources) {\n // This will automatically create all multichain accounts for that wallet (based\n // on the accounts owned by each account providers).\n const wallet = new MultichainAccountWalletAdapter({\n entropySource,\n providers: this.#providers,\n });\n\n this.#wallets.set(wallet.id, wallet);\n }\n }\n\n #getWallet(entropySource: EntropySourceId): MultichainAccountWallet {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error(\n 'Unknown wallet, not wallet matching this entropy source',\n );\n }\n\n return wallet;\n }\n\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount {\n const multichainAccount =\n this.#getWallet(entropySource).accounts[groupIndex];\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount[] {\n return this.#getWallet(entropySource).accounts;\n }\n\n async createNextMultichainAccount({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount> {\n return await this.#getWallet(entropySource).createNextMultichainAccount();\n }\n\n async discoverAndCreateMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount[]> {\n return await this.#getWallet(\n entropySource,\n ).discoverAndCreateMultichainAccounts();\n }\n}\n"]}
|
@@ -88,6 +88,7 @@ _SolAccountProvider_messenger = new WeakMap(), _SolAccountProvider_client = new
|
|
88
88
|
const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {
|
89
89
|
displayAccountNameSuggestion: false,
|
90
90
|
displayConfirmation: false,
|
91
|
+
setSelectedAccount: false,
|
91
92
|
});
|
92
93
|
// FIXME: This part of the flow is truly async, so when the `KeyringClient`
|
93
94
|
// returns the `KeyringAccount`, its `InternalAccount` won't be "ready"
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAI+B;AAK/B,qEAA4D;AAK5D,uEAA8D;AAG9D,uDAAoD;AAYpD,+CAA+C;AAC/C,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,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AO,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE;YACvC,OAAO,CAAC,IAAI,CACV,oFAAoF,CACrF,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAyB,CAAC,CAAC,0DAA0D;AAC1F,CAAC","sourcesContent":["import type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport {\n SolAccountType,\n SolScope,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { AccountProvider } from '@metamask/multichain-account-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport type { MultichainAccountControllerMessenger } from '../types';\n\ntype SolInternalAccount = InternalAccount & {\n options: {\n index: number;\n entropySource: EntropySourceId;\n };\n};\n\n// eslint-disable-next-line jsdoc/require-jsdoc\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nconst SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\nexport class SolAccountProvider implements AccountProvider {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #client: KeyringClient;\n\n constructor(messenger: MultichainAccountControllerMessenger) {\n this.#messenger = messenger;\n\n // TODO: Change this once we introduce 1 Snap keyring per Snaps.\n this.#client = this.#getKeyringClientFromSnapId(SOLANA_SNAP_ID);\n }\n\n async #withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.#messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) => {\n const response = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Promise<Json>;\n },\n });\n }\n\n async #createAccount(opts: {\n entropySource: EntropySourceId;\n derivationPath: `m/${string}`;\n }) {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const createAccount = await this.#withKeyring<\n SnapKeyring,\n SnapKeyring['createAccount']\n >({ type: KeyringTypes.snap }, async ({ keyring }) =>\n keyring.createAccount.bind(keyring),\n );\n\n const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n });\n\n // FIXME: This part of the flow is truly async, so when the `KeyringClient`\n // returns the `KeyringAccount`, its `InternalAccount` won't be \"ready\"\n // right away. For now we just re-create a fake `InternalAccount` and\n // we might have to rely solely on `account.id`.\n\n // Actually get the associated `InternalAccount`.\n // const account = this.#messenger.call(\n // 'AccountsController:getAccount',\n // keyringAccount.id,\n // );\n\n const account: InternalAccount = {\n ...keyringAccount,\n metadata: {\n name: `Solana account -- ${keyringAccount.options.index}`,\n importTime: 0,\n keyring: {\n type: KeyringTypes.snap,\n },\n },\n };\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const account = await this.#createAccount({\n entropySource,\n derivationPath: `m/44'/501'/${groupIndex}'/0'`,\n });\n\n return [account];\n }\n\n async discoverAndCreateAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const discoveredAccounts = await this.#client.discoverAccounts(\n [SolScope.Mainnet, SolScope.Testnet],\n entropySource,\n groupIndex,\n );\n\n return await Promise.all(\n discoveredAccounts.map(\n async ({ derivationPath }) =>\n await this.#createAccount({ entropySource, derivationPath }),\n ),\n );\n }\n\n #getAccounts(): SolInternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts')\n .filter((account) => {\n // We only check for EOA accounts for multichain accounts.\n if (\n account.type !== SolAccountType.DataAccount ||\n account.metadata.keyring.type !== (KeyringTypes.snap as string)\n ) {\n return false;\n }\n\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found a Solana account with no entropy source: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n // TODO: We need to add this index for native accounts too!\n if (account.options.index === undefined) {\n console.warn(\n \"! Found a Solana account with no index: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n return true;\n }) as SolInternalAccount[]; // Safe, we did check for options fields during filtering.\n }\n\n getAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): InternalAccount[] {\n return this.#getAccounts().filter((account) => {\n return (\n account.options.entropySource === entropySource &&\n account.options.index === groupIndex\n );\n });\n }\n\n getEntropySources(): EntropySourceId[] {\n const entropySources = new Set<EntropySourceId>();\n\n for (const account of this.#getAccounts()) {\n entropySources.add(account.options.entropySource);\n }\n\n return Array.from(entropySources);\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"SolAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAI+B;AAK/B,qE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AI,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE;YAClC,OAAO,CAAC,IAAI,CACV,6FAA6F,CAC9F,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,2DAA2D;QAC3D,IAAI,OAAO,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE;YACvC,OAAO,CAAC,IAAI,CACV,oFAAoF,CACrF,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAyB,CAAC,CAAC,0DAA0D;AAC1F,CAAC","sourcesContent":["import type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport {\n SolAccountType,\n SolScope,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { AccountProvider } from '@metamask/multichain-account-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport type { MultichainAccountControllerMessenger } from '../types';\n\ntype SolInternalAccount = InternalAccount & {\n options: {\n index: number;\n entropySource: EntropySourceId;\n };\n};\n\n// eslint-disable-next-line jsdoc/require-jsdoc\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nconst SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\nexport class SolAccountProvider implements AccountProvider {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #client: KeyringClient;\n\n constructor(messenger: MultichainAccountControllerMessenger) {\n this.#messenger = messenger;\n\n // TODO: Change this once we introduce 1 Snap keyring per Snaps.\n this.#client = this.#getKeyringClientFromSnapId(SOLANA_SNAP_ID);\n }\n\n async #withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.#messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) => {\n const response = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Promise<Json>;\n },\n });\n }\n\n async #createAccount(opts: {\n entropySource: EntropySourceId;\n derivationPath: `m/${string}`;\n }) {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const createAccount = await this.#withKeyring<\n SnapKeyring,\n SnapKeyring['createAccount']\n >({ type: KeyringTypes.snap }, async ({ keyring }) =>\n keyring.createAccount.bind(keyring),\n );\n\n const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n });\n\n // FIXME: This part of the flow is truly async, so when the `KeyringClient`\n // returns the `KeyringAccount`, its `InternalAccount` won't be \"ready\"\n // right away. For now we just re-create a fake `InternalAccount` and\n // we might have to rely solely on `account.id`.\n\n // Actually get the associated `InternalAccount`.\n // const account = this.#messenger.call(\n // 'AccountsController:getAccount',\n // keyringAccount.id,\n // );\n\n const account: InternalAccount = {\n ...keyringAccount,\n metadata: {\n name: `Solana account -- ${keyringAccount.options.index}`,\n importTime: 0,\n keyring: {\n type: KeyringTypes.snap,\n },\n },\n };\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const account = await this.#createAccount({\n entropySource,\n derivationPath: `m/44'/501'/${groupIndex}'/0'`,\n });\n\n return [account];\n }\n\n async discoverAndCreateAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const discoveredAccounts = await this.#client.discoverAccounts(\n [SolScope.Mainnet, SolScope.Testnet],\n entropySource,\n groupIndex,\n );\n\n return await Promise.all(\n discoveredAccounts.map(\n async ({ derivationPath }) =>\n await this.#createAccount({ entropySource, derivationPath }),\n ),\n );\n }\n\n #getAccounts(): SolInternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts')\n .filter((account) => {\n // We only check for EOA accounts for multichain accounts.\n if (\n account.type !== SolAccountType.DataAccount ||\n account.metadata.keyring.type !== (KeyringTypes.snap as string)\n ) {\n return false;\n }\n\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found a Solana account with no entropy source: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n // TODO: We need to add this index for native accounts too!\n if (account.options.index === undefined) {\n console.warn(\n \"! Found a Solana account with no index: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n return true;\n }) as SolInternalAccount[]; // Safe, we did check for options fields during filtering.\n }\n\n getAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): InternalAccount[] {\n return this.#getAccounts().filter((account) => {\n return (\n account.options.entropySource === entropySource &&\n account.options.index === groupIndex\n );\n });\n }\n\n getEntropySources(): EntropySourceId[] {\n const entropySources = new Set<EntropySourceId>();\n\n for (const account of this.#getAccounts()) {\n entropySources.add(account.options.entropySource);\n }\n\n return Array.from(entropySources);\n }\n}\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,eAAe,EACrB,8BAA8B;AAM/B,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAGxE,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,KAAK,EAAE,oCAAoC,EAAE,qBAAiB;AAmBrE,qBAAa,kBAAmB,YAAW,eAAe;;gBAK5C,SAAS,EAAE,oCAAoC;
|
1
|
+
{"version":3,"file":"SolAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,eAAe,EACrB,8BAA8B;AAM/B,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAGxE,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,KAAK,EAAE,oCAAoC,EAAE,qBAAiB;AAmBrE,qBAAa,kBAAmB,YAAW,eAAe;;gBAK5C,SAAS,EAAE,oCAAoC;IAkGrD,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;;;;;IASK,yBAAyB,CAAC,EAC9B,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;;;;;IA+CD,WAAW,CAAC,EACV,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,eAAe,EAAE;IASrB,iBAAiB,IAAI,eAAe,EAAE;CASvC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,eAAe,EACrB,8BAA8B;AAM/B,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAGxE,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,KAAK,EAAE,oCAAoC,EAAE,qBAAiB;AAmBrE,qBAAa,kBAAmB,YAAW,eAAe;;gBAK5C,SAAS,EAAE,oCAAoC;
|
1
|
+
{"version":3,"file":"SolAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,eAAe,EACrB,8BAA8B;AAM/B,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAGxE,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,KAAK,EAAE,oCAAoC,EAAE,qBAAiB;AAmBrE,qBAAa,kBAAmB,YAAW,eAAe;;gBAK5C,SAAS,EAAE,oCAAoC;IAkGrD,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;;;;;IASK,yBAAyB,CAAC,EAC9B,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;;;;;IA+CD,WAAW,CAAC,EACV,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,eAAe,EAAE;IASrB,iBAAiB,IAAI,eAAe,EAAE;CASvC"}
|
@@ -84,6 +84,7 @@ _SolAccountProvider_messenger = new WeakMap(), _SolAccountProvider_client = new
|
|
84
84
|
const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {
|
85
85
|
displayAccountNameSuggestion: false,
|
86
86
|
displayConfirmation: false,
|
87
|
+
setSelectedAccount: false,
|
87
88
|
});
|
88
89
|
// FIXME: This part of the flow is truly async, so when the `KeyringClient`
|
89
90
|
// returns the `KeyringAccount`, its `InternalAccount` won't be "ready"
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,cAAc,EACd,QAAQ,EAET,8BAA8B;AAK/B,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAK5D,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAG9D,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAYpD,+CAA+C;AAC/C,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAM,cAAc,GAAG,kCAA4C,CAAC;AACpE,MAAM,OAAO,kBAAkB;IAK7B,YAAY,SAA+C;;QAJlD,gDAAiD;QAEjD,6CAAuB;QAG9B,uBAAA,IAAI,iCAAc,SAAS,MAAA,CAAC;QAE5B,gEAAgE;QAChE,uBAAA,IAAI,8BAAW,uBAAA,IAAI,qFAA4B,MAAhC,IAAI,EAA6B,cAAc,CAAC,MAAA,CAAC;IAClE,CAAC;IA4FD,KAAK,CAAC,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,OAAO,GAAG,MAAM,uBAAA,IAAI,wEAAe,MAAnB,IA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;QAC/B,GAAG,cAAc;QACjB,QAAQ,EAAE;YACR,IAAI,EAAE,qBAAqB,cAAc,CAAC,OAAO,CAAC,KAAK,EAAE;YACzD,UAAU,EAAE,CAAC;YACb,OAAO,EAAE;gBACP,IAAI,EAAE,YAAY,CAAC,IAAI;aACxB;SACF;KACF,CAAC;IAEF,gDAAgD;IAChD,2BAA2B,CAAC,OAAO,CAAC,CAAC;IAErC,OAAO,OAAO,CAAC;AACjB,CAAC;IAuCC,OAAO,uBAAA,IAAI,qCAAW;SACnB,IAAI,CAAC,2CAA2C,CAAC;SACjD,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE;QAClB,0DAA0D;QAC1D,IACE,OAAO,CAAC,IAAI,KAAK,cAAc,CAAC,WAAW;YAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAM,YAAY,CAAC,IAAe,EAC/D;YACA,OAAO,KAAK,CAAC;SACd;QAED,iHAAiH;QACjH,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE;YAClC,OAAO,CAAC,IAAI,CACV,6FAA6F,CAC9F,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,2DAA2D;QAC3D,IAAI,OAAO,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE;YACvC,OAAO,CAAC,IAAI,CACV,oFAAoF,CACrF,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAyB,CAAC,CAAC,0DAA0D;AAC1F,CAAC","sourcesContent":["import type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport {\n SolAccountType,\n SolScope,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { AccountProvider } from '@metamask/multichain-account-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport type { MultichainAccountControllerMessenger } from '../types';\n\ntype SolInternalAccount = InternalAccount & {\n options: {\n index: number;\n entropySource: EntropySourceId;\n };\n};\n\n// eslint-disable-next-line jsdoc/require-jsdoc\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nconst SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\nexport class SolAccountProvider implements AccountProvider {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #client: KeyringClient;\n\n constructor(messenger: MultichainAccountControllerMessenger) {\n this.#messenger = messenger;\n\n // TODO: Change this once we introduce 1 Snap keyring per Snaps.\n this.#client = this.#getKeyringClientFromSnapId(SOLANA_SNAP_ID);\n }\n\n async #withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.#messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) => {\n const response = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Promise<Json>;\n },\n });\n }\n\n async #createAccount(opts: {\n entropySource: EntropySourceId;\n derivationPath: `m/${string}`;\n }) {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const createAccount = await this.#withKeyring<\n SnapKeyring,\n SnapKeyring['createAccount']\n >({ type: KeyringTypes.snap }, async ({ keyring }) =>\n keyring.createAccount.bind(keyring),\n );\n\n const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n });\n\n // FIXME: This part of the flow is truly async, so when the `KeyringClient`\n // returns the `KeyringAccount`, its `InternalAccount` won't be \"ready\"\n // right away. For now we just re-create a fake `InternalAccount` and\n // we might have to rely solely on `account.id`.\n\n // Actually get the associated `InternalAccount`.\n // const account = this.#messenger.call(\n // 'AccountsController:getAccount',\n // keyringAccount.id,\n // );\n\n const account: InternalAccount = {\n ...keyringAccount,\n metadata: {\n name: `Solana account -- ${keyringAccount.options.index}`,\n importTime: 0,\n keyring: {\n type: KeyringTypes.snap,\n },\n },\n };\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const account = await this.#createAccount({\n entropySource,\n derivationPath: `m/44'/501'/${groupIndex}'/0'`,\n });\n\n return [account];\n }\n\n async discoverAndCreateAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const discoveredAccounts = await this.#client.discoverAccounts(\n [SolScope.Mainnet, SolScope.Testnet],\n entropySource,\n groupIndex,\n );\n\n return await Promise.all(\n discoveredAccounts.map(\n async ({ derivationPath }) =>\n await this.#createAccount({ entropySource, derivationPath }),\n ),\n );\n }\n\n #getAccounts(): SolInternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts')\n .filter((account) => {\n // We only check for EOA accounts for multichain accounts.\n if (\n account.type !== SolAccountType.DataAccount ||\n account.metadata.keyring.type !== (KeyringTypes.snap as string)\n ) {\n return false;\n }\n\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found a Solana account with no entropy source: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n // TODO: We need to add this index for native accounts too!\n if (account.options.index === undefined) {\n console.warn(\n \"! Found a Solana account with no index: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n return true;\n }) as SolInternalAccount[]; // Safe, we did check for options fields during filtering.\n }\n\n getAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): InternalAccount[] {\n return this.#getAccounts().filter((account) => {\n return (\n account.options.entropySource === entropySource &&\n account.options.index === groupIndex\n );\n });\n }\n\n getEntropySources(): EntropySourceId[] {\n const entropySources = new Set<EntropySourceId>();\n\n for (const account of this.#getAccounts()) {\n entropySources.add(account.options.entropySource);\n }\n\n return Array.from(entropySources);\n }\n}\n"]}
|
1
|
+
{"version":3,"file":"SolAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,cAAc,EACd,QAAQ,EAET,8BAA8B;AAK/B,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAK5D,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAG9D,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAYpD,+CAA+C;AAC/C,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAM,cAAc,GAAG,kCAA4C,CAAC;AACpE,MAAM,OAAO,kBAAkB;IAK7B,YAAY,SAA+C;;QAJlD,gDAAiD;QAEjD,6CAAuB;QAG9B,uBAAA,IAAI,iCAAc,SAAS,MAAA,CAAC;QAE5B,gEAAgE;QAChE,uBAAA,IA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\n SolAccountType,\n SolScope,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { AccountProvider } from '@metamask/multichain-account-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport type { MultichainAccountControllerMessenger } from '../types';\n\ntype SolInternalAccount = InternalAccount & {\n options: {\n index: number;\n entropySource: EntropySourceId;\n };\n};\n\n// eslint-disable-next-line jsdoc/require-jsdoc\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nconst SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\nexport class SolAccountProvider implements AccountProvider {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #client: KeyringClient;\n\n constructor(messenger: MultichainAccountControllerMessenger) {\n this.#messenger = messenger;\n\n // TODO: Change this once we introduce 1 Snap keyring per Snaps.\n this.#client = this.#getKeyringClientFromSnapId(SOLANA_SNAP_ID);\n }\n\n async #withKeyring<SelectedKeyring, CallbackResult = void>(\n selector: KeyringSelector,\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SelectedKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n const result = await this.#messenger.call(\n 'KeyringController:withKeyring',\n selector,\n ({ keyring, metadata }) =>\n operation({\n keyring: keyring as SelectedKeyring,\n metadata,\n }),\n );\n\n return result as CallbackResult;\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) => {\n const response = await this.#messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Promise<Json>;\n },\n });\n }\n\n async #createAccount(opts: {\n entropySource: EntropySourceId;\n derivationPath: `m/${string}`;\n }) {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const createAccount = await this.#withKeyring<\n SnapKeyring,\n SnapKeyring['createAccount']\n >({ type: KeyringTypes.snap }, async ({ keyring }) =>\n keyring.createAccount.bind(keyring),\n );\n\n const keyringAccount = await createAccount(SOLANA_SNAP_ID, opts, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n });\n\n // FIXME: This part of the flow is truly async, so when the `KeyringClient`\n // returns the `KeyringAccount`, its `InternalAccount` won't be \"ready\"\n // right away. For now we just re-create a fake `InternalAccount` and\n // we might have to rely solely on `account.id`.\n\n // Actually get the associated `InternalAccount`.\n // const account = this.#messenger.call(\n // 'AccountsController:getAccount',\n // keyringAccount.id,\n // );\n\n const account: InternalAccount = {\n ...keyringAccount,\n metadata: {\n name: `Solana account -- ${keyringAccount.options.index}`,\n importTime: 0,\n keyring: {\n type: KeyringTypes.snap,\n },\n },\n };\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const account = await this.#createAccount({\n entropySource,\n derivationPath: `m/44'/501'/${groupIndex}'/0'`,\n });\n\n return [account];\n }\n\n async discoverAndCreateAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const discoveredAccounts = await this.#client.discoverAccounts(\n [SolScope.Mainnet, SolScope.Testnet],\n entropySource,\n groupIndex,\n );\n\n return await Promise.all(\n discoveredAccounts.map(\n async ({ derivationPath }) =>\n await this.#createAccount({ entropySource, derivationPath }),\n ),\n );\n }\n\n #getAccounts(): SolInternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts')\n .filter((account) => {\n // We only check for EOA accounts for multichain accounts.\n if (\n account.type !== SolAccountType.DataAccount ||\n account.metadata.keyring.type !== (KeyringTypes.snap as string)\n ) {\n return false;\n }\n\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found a Solana account with no entropy source: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n // TODO: We need to add this index for native accounts too!\n if (account.options.index === undefined) {\n console.warn(\n \"! Found a Solana account with no index: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n return true;\n }) as SolInternalAccount[]; // Safe, we did check for options fields during filtering.\n }\n\n getAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): InternalAccount[] {\n return this.#getAccounts().filter((account) => {\n return (\n account.options.entropySource === entropySource &&\n account.options.index === groupIndex\n );\n });\n }\n\n getEntropySources(): EntropySourceId[] {\n const entropySources = new Set<EntropySourceId>();\n\n for (const account of this.#getAccounts()) {\n entropySources.add(account.options.entropySource);\n }\n\n return Array.from(entropySources);\n }\n}\n"]}
|