@metamask-previews/multichain-account-service 2.0.1-preview-60778d92 → 2.1.0-preview-578568f
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/CHANGELOG.md +10 -1
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +2 -2
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +2 -2
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +70 -51
- package/dist/MultichainAccountWallet.cjs.map +1 -1
- package/dist/MultichainAccountWallet.d.cts.map +1 -1
- package/dist/MultichainAccountWallet.d.mts.map +1 -1
- package/dist/MultichainAccountWallet.mjs +70 -51
- package/dist/MultichainAccountWallet.mjs.map +1 -1
- package/dist/providers/BtcAccountProvider.cjs +17 -17
- package/dist/providers/BtcAccountProvider.cjs.map +1 -1
- package/dist/providers/BtcAccountProvider.d.cts +2 -11
- package/dist/providers/BtcAccountProvider.d.cts.map +1 -1
- package/dist/providers/BtcAccountProvider.d.mts +2 -11
- package/dist/providers/BtcAccountProvider.d.mts.map +1 -1
- package/dist/providers/BtcAccountProvider.mjs +17 -17
- package/dist/providers/BtcAccountProvider.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +1 -1
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +1 -1
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/providers/SnapAccountProvider.cjs +39 -1
- package/dist/providers/SnapAccountProvider.cjs.map +1 -1
- package/dist/providers/SnapAccountProvider.d.cts +24 -2
- package/dist/providers/SnapAccountProvider.d.cts.map +1 -1
- package/dist/providers/SnapAccountProvider.d.mts +24 -2
- package/dist/providers/SnapAccountProvider.d.mts.map +1 -1
- package/dist/providers/SnapAccountProvider.mjs +39 -1
- package/dist/providers/SnapAccountProvider.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +16 -15
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts +3 -12
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts +3 -12
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +16 -15
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/providers/TrxAccountProvider.cjs +17 -17
- package/dist/providers/TrxAccountProvider.cjs.map +1 -1
- package/dist/providers/TrxAccountProvider.d.cts +3 -12
- package/dist/providers/TrxAccountProvider.d.cts.map +1 -1
- package/dist/providers/TrxAccountProvider.d.mts +3 -12
- package/dist/providers/TrxAccountProvider.d.mts.map +1 -1
- package/dist/providers/TrxAccountProvider.mjs +17 -17
- package/dist/providers/TrxAccountProvider.mjs.map +1 -1
- package/dist/utils.cjs +14 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +2 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +2 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +10 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +3 -1
|
@@ -3,18 +3,9 @@ import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { SnapId } from "@metamask/snaps-sdk";
|
|
5
5
|
import type { Json } from "@metamask/utils";
|
|
6
|
-
import { SnapAccountProvider } from "./SnapAccountProvider.cjs";
|
|
6
|
+
import { SnapAccountProvider, type SnapAccountProviderConfig } from "./SnapAccountProvider.cjs";
|
|
7
7
|
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
8
|
-
export type BtcAccountProviderConfig =
|
|
9
|
-
discovery: {
|
|
10
|
-
maxAttempts: number;
|
|
11
|
-
timeoutMs: number;
|
|
12
|
-
backOffMs: number;
|
|
13
|
-
};
|
|
14
|
-
createAccounts: {
|
|
15
|
-
timeoutMs: number;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
8
|
+
export type BtcAccountProviderConfig = SnapAccountProviderConfig;
|
|
18
9
|
export declare const BTC_ACCOUNT_PROVIDER_NAME: "Bitcoin";
|
|
19
10
|
export declare class BtcAccountProvider extends SnapAccountProvider {
|
|
20
11
|
#private;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BtcAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,8BAA8B;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,
|
|
1
|
+
{"version":3,"file":"BtcAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,8BAA8B;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,EACL,mBAAmB,EACnB,KAAK,yBAAyB,EAC/B,kCAA8B;AAE/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,WAAqB,CAAC;AAE5D,qBAAa,kBAAmB,SAAQ,mBAAmB;;IACzD,MAAM,CAAC,IAAI,YAA6B;IAExC,MAAM,CAAC,WAAW,SAAiD;gBAKjE,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBASP;IAQH,OAAO,IAAI,MAAM;IAqBjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAO9D,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,EAAE,KAAK,GAClB,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAmBrC,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;CA4BF"}
|
|
@@ -3,18 +3,9 @@ import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { SnapId } from "@metamask/snaps-sdk";
|
|
5
5
|
import type { Json } from "@metamask/utils";
|
|
6
|
-
import { SnapAccountProvider } from "./SnapAccountProvider.mjs";
|
|
6
|
+
import { SnapAccountProvider, type SnapAccountProviderConfig } from "./SnapAccountProvider.mjs";
|
|
7
7
|
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
8
|
-
export type BtcAccountProviderConfig =
|
|
9
|
-
discovery: {
|
|
10
|
-
maxAttempts: number;
|
|
11
|
-
timeoutMs: number;
|
|
12
|
-
backOffMs: number;
|
|
13
|
-
};
|
|
14
|
-
createAccounts: {
|
|
15
|
-
timeoutMs: number;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
8
|
+
export type BtcAccountProviderConfig = SnapAccountProviderConfig;
|
|
18
9
|
export declare const BTC_ACCOUNT_PROVIDER_NAME: "Bitcoin";
|
|
19
10
|
export declare class BtcAccountProvider extends SnapAccountProvider {
|
|
20
11
|
#private;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BtcAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,8BAA8B;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,
|
|
1
|
+
{"version":3,"file":"BtcAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,YAAY,EAAE,8BAA8B;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,KAAK,EAAE,IAAI,EAAkB,wBAAwB;AAE5D,OAAO,EACL,mBAAmB,EACnB,KAAK,yBAAyB,EAC/B,kCAA8B;AAE/B,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,WAAqB,CAAC;AAE5D,qBAAa,kBAAmB,SAAQ,mBAAmB;;IACzD,MAAM,CAAC,IAAI,YAA6B;IAExC,MAAM,CAAC,WAAW,SAAiD;gBAKjE,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBASP;IAQH,OAAO,IAAI,MAAM;IAqBjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAO9D,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,EAAE,KAAK,GAClB,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAmBrC,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;;;;;;;;CA4BF"}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _BtcAccountProvider_instances, _BtcAccountProvider_client,
|
|
12
|
+
var _BtcAccountProvider_instances, _BtcAccountProvider_client, _BtcAccountProvider_getKeyringClientFromSnapId;
|
|
13
13
|
import { assertIsBip44Account } from "@metamask/account-api";
|
|
14
14
|
import { BtcAccountType, BtcScope } from "@metamask/keyring-api";
|
|
15
15
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
@@ -28,12 +28,10 @@ export class BtcAccountProvider extends SnapAccountProvider {
|
|
|
28
28
|
backOffMs: 1000,
|
|
29
29
|
},
|
|
30
30
|
}) {
|
|
31
|
-
super(BtcAccountProvider.BTC_SNAP_ID, messenger);
|
|
31
|
+
super(BtcAccountProvider.BTC_SNAP_ID, messenger, config);
|
|
32
32
|
_BtcAccountProvider_instances.add(this);
|
|
33
33
|
_BtcAccountProvider_client.set(this, void 0);
|
|
34
|
-
_BtcAccountProvider_config.set(this, void 0);
|
|
35
34
|
__classPrivateFieldSet(this, _BtcAccountProvider_client, __classPrivateFieldGet(this, _BtcAccountProvider_instances, "m", _BtcAccountProvider_getKeyringClientFromSnapId).call(this, BtcAccountProvider.BTC_SNAP_ID), "f");
|
|
36
|
-
__classPrivateFieldSet(this, _BtcAccountProvider_config, config, "f");
|
|
37
35
|
}
|
|
38
36
|
getName() {
|
|
39
37
|
return BtcAccountProvider.NAME;
|
|
@@ -43,20 +41,22 @@ export class BtcAccountProvider extends SnapAccountProvider {
|
|
|
43
41
|
Object.values(BtcAccountType).includes(account.type));
|
|
44
42
|
}
|
|
45
43
|
async createAccounts({ entropySource, groupIndex: index, }) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
return this.withMaxConcurrency(async () => {
|
|
45
|
+
const createAccount = await this.getRestrictedSnapAccountCreator();
|
|
46
|
+
const account = await withTimeout(createAccount({
|
|
47
|
+
entropySource,
|
|
48
|
+
index,
|
|
49
|
+
addressType: BtcAccountType.P2wpkh,
|
|
50
|
+
scope: BtcScope.Mainnet,
|
|
51
|
+
}), this.config.createAccounts.timeoutMs);
|
|
52
|
+
assertIsBip44Account(account);
|
|
53
|
+
return [account];
|
|
54
|
+
});
|
|
55
55
|
}
|
|
56
56
|
async discoverAccounts({ entropySource, groupIndex, }) {
|
|
57
|
-
const discoveredAccounts = await withRetry(() => withTimeout(__classPrivateFieldGet(this, _BtcAccountProvider_client, "f").discoverAccounts([BtcScope.Mainnet], entropySource, groupIndex),
|
|
58
|
-
maxAttempts:
|
|
59
|
-
backOffMs:
|
|
57
|
+
const discoveredAccounts = await withRetry(() => withTimeout(__classPrivateFieldGet(this, _BtcAccountProvider_client, "f").discoverAccounts([BtcScope.Mainnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
|
|
58
|
+
maxAttempts: this.config.discovery.maxAttempts,
|
|
59
|
+
backOffMs: this.config.discovery.backOffMs,
|
|
60
60
|
});
|
|
61
61
|
if (!Array.isArray(discoveredAccounts) || discoveredAccounts.length === 0) {
|
|
62
62
|
return [];
|
|
@@ -68,7 +68,7 @@ export class BtcAccountProvider extends SnapAccountProvider {
|
|
|
68
68
|
return createdAccounts;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
_BtcAccountProvider_client = new WeakMap(),
|
|
71
|
+
_BtcAccountProvider_client = new WeakMap(), _BtcAccountProvider_instances = new WeakSet(), _BtcAccountProvider_getKeyringClientFromSnapId = function _BtcAccountProvider_getKeyringClientFromSnapId(snapId) {
|
|
72
72
|
return new KeyringClient({
|
|
73
73
|
send: async (request) => {
|
|
74
74
|
const response = await this.messenger.call('SnapController:handleRequest', {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAqB,8BAA8B;AAEhF,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,8BAA8B;AAEjE,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAE9D,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAGpD,OAAO,
|
|
1
|
+
{"version":3,"file":"BtcAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BtcAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAqB,8BAA8B;AAEhF,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,8BAA8B;AAEjE,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAE9D,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAGpD,OAAO,EACL,mBAAmB,EAEpB,kCAA8B;AAC/B,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,oBAAgB;AAKjD,MAAM,CAAC,MAAM,yBAAyB,GAAG,SAAkB,CAAC;AAE5D,MAAM,OAAO,kBAAmB,SAAQ,mBAAmB;IAOzD,YACE,SAA4C,EAC5C,SAAmC;QAC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S,CACxC,GAAG,EAAE,CACH,WAAW,CACT,uBAAA,IAAI,kCAAQ,CAAC,gBAAgB,CAC3B,CAAC,QAAQ,CAAC,OAAO,CAAC,EAClB,aAAa,EACb,UAAU,CACX,EACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAChC,EACH;YACE,WAAW,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,WAAW;YAC9C,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS;SAC3C,CACF,CAAC;QAEF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAAE;YACzE,OAAO,EAAE,CAAC;SACX;QAED,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;YAChD,aAAa;YACb,UAAU;SACX,CAAC,CAAC;QAEH,OAAO,eAAe,CAAC;IACzB,CAAC;;oMAlF2B,MAAc;IACxC,OAAO,IAAI,aAAa,CAAC;QACvB,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAE,EAAE;YACtC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACxC,8BAA8B,EAC9B;gBACE,MAAM,EAAE,MAAgB;gBACxB,MAAM,EAAE,UAAU;gBAClB,OAAO,EAAE,WAAW,CAAC,gBAAgB;gBACrC,OAAO;aACR,CACF,CAAC;YACF,OAAO,QAAgB,CAAC;QAC1B,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AA5CM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,8BAAW,GAAG,mCAA6C,AAAhD,CAAiD","sourcesContent":["import { assertIsBip44Account, type Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { BtcAccountType, BtcScope } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport {\n SnapAccountProvider,\n type SnapAccountProviderConfig,\n} from './SnapAccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type BtcAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const BTC_ACCOUNT_PROVIDER_NAME = 'Bitcoin' as const;\n\nexport class BtcAccountProvider extends SnapAccountProvider {\n static NAME = BTC_ACCOUNT_PROVIDER_NAME;\n\n static BTC_SNAP_ID = 'npm:@metamask/bitcoin-wallet-snap' as SnapId;\n\n readonly #client: KeyringClient;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: BtcAccountProviderConfig = {\n createAccounts: {\n timeoutMs: 3000,\n },\n discovery: {\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n },\n ) {\n super(BtcAccountProvider.BTC_SNAP_ID, messenger, config);\n this.#client = this.#getKeyringClientFromSnapId(\n BtcAccountProvider.BTC_SNAP_ID,\n );\n }\n\n getName(): string {\n return BtcAccountProvider.NAME;\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 Json;\n },\n });\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === BtcAccountType.P2wpkh &&\n Object.values<string>(BtcAccountType).includes(account.type)\n );\n }\n\n async createAccounts({\n entropySource,\n groupIndex: index,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n\n const account = await withTimeout(\n createAccount({\n entropySource,\n index,\n addressType: BtcAccountType.P2wpkh,\n scope: BtcScope.Mainnet,\n }),\n this.config.createAccounts.timeoutMs,\n );\n\n assertIsBip44Account(account);\n return [account];\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.#client.discoverAccounts(\n [BtcScope.Mainnet],\n entropySource,\n groupIndex,\n ),\n this.config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.config.discovery.maxAttempts,\n backOffMs: this.config.discovery.backOffMs,\n },\n );\n\n if (!Array.isArray(discoveredAccounts) || discoveredAccounts.length === 0) {\n return [];\n }\n\n const createdAccounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n\n return createdAccounts;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmD;AAInD,uDAAuD;AACvD,qEAA4D;AAM5D,2CAAsE;AAGtE,6EAIoC;AACpC,uCAAiD;AAEjD,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAUY,QAAA,yBAAyB,GAAG,KAAc,CAAC;AAExD,MAAa,kBAAmB,SAAQ,mDAAwB;IAK9D,YACE,SAA4C,EAC5C,SAAmC;QACjC,SAAS,EAAE;YACT,WAAW,EAAE,CAAC;YACd,SAAS,EAAE,GAAG;YACd,SAAS,EAAE,GAAG;SACf;KACF;QAED,KAAK,CAAC,SAAS,CAAC,CAAC;;QAZV,6CAAkC;QAazC,uBAAA,IAAI,8BAAW,MAAM,M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AA,cAAM,EAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,IAAA,aAAK,EACV,IAAA,kBAAU,EAAC,IAAA,sBAAe,EAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAzJM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\nimport type { MultichainAccountServiceMessenger } from 'src/types';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\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\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n ) {\n super(messenger);\n this.#config = config;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n return accountsArray;\n }\n\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n return [account];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmD;AAInD,uDAAuD;AACvD,qEAA4D;AAM5D,2CAAsE;AAEtE,6EAIoC;AACpC,uCAAiD;AAGjD,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAUY,QAAA,yBAAyB,GAAG,KAAc,CAAC;AAExD,MAAa,kBAAmB,SAAQ,mDAAwB;IAK9D,YACE,SAA4C,EAC5C,SAAmC;QACjC,SAAS,EAAE;YACT,WAAW,EAAE,CAAC;YACd,SAAS,EAAE,GAAG;YACd,SAAS,EAAE,GAAG;SACf;KACF;QAED,KAAK,CAAC,SAAS,CAAC,CAAC;;QAZV,6CAAkC;QAazC,uBAAA,IAAI,8BAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,GAAG;YACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAM,iCAAY,CAAC,EAAa,CAC9D,CAAC;IACJ,CAAC;IAED,OAAO;QACL,OAAO,kBAAkB,CAAC,IAAI,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,cAAc;QACZ,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACzC,gDAAgD,EAChD,oBAAoB,CACrB,CAAC;QACF,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACtC,wCAAwC,EACxC,eAAe,CAChB,CAAC;QACF,OAAO,QAAQ,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SAAS,CAAC,WAAW;QAC/C,SAAS,EAAE,uBAAA,IAAI,kCAAQ,CAAC,SAAS,CAAC,SAAS;KAC5C,CACF,CAAC;IAEF,OAAO,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;AAChC,CAAC,iDAED,KAAK,uDAA2B,EAC9B,aAAa,EACb,UAAU,GAIX;IACC,4FAA4F;IAC5F,mCAAmC;IACnC,OAAO,MAAM,IAAI,CAAC,WAAW,CAC3B,EAAE,EAAE,EAAE,aAAa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,+EAA+E;QAC/E,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;QAC7C,IAAI,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE;YAChC,OAAO,QAAQ,CAAC,UAAU,CAAC,CAAC;SAC7B;QAED,8EAA8E;QAC9E,IAAA,cAAM,EAAC,OAAO,CAAC,IAAI,EAAE,oCAAoC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACnD,IAAA,cAAM,EAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,IAAA,aAAK,EACV,IAAA,kBAAU,EAAC,IAAA,sBAAe,EAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAzJM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\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\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n ) {\n super(messenger);\n this.#config = config;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n return accountsArray;\n }\n\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n return [account];\n }\n}\n"]}
|
|
@@ -2,8 +2,8 @@ import type { Bip44Account } from "@metamask/account-api";
|
|
|
2
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { Provider } from "@metamask/network-controller";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "src/types";
|
|
6
5
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.cjs";
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
7
7
|
export type EvmAccountProviderConfig = {
|
|
8
8
|
discovery: {
|
|
9
9
|
maxAttempts: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAG7D,OAAO,EAGL,wBAAwB,EACzB,uCAAmC;AAEpC,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAkBlE,MAAM,MAAM,wBAAwB,GAAG;IACrC,SAAS,EAAE;QACT,WAAW,EAAE,MAAM,CAAC;QACpB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH,CAAC;AAEF,eAAO,MAAM,yBAAyB,OAAiB,CAAC;AAExD,qBAAa,kBAAmB,SAAQ,wBAAwB;;IAC9D,MAAM,CAAC,IAAI,QAA6B;gBAKtC,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAMP;IAMH,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAOpE,OAAO,IAAI,MAAM;IAIjB;;;;OAIG;IACH,cAAc,IAAI,QAAQ;IA0CpB,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAyE3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,IAAI,EAAE;QAC3B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAmC5C"}
|
|
@@ -2,8 +2,8 @@ import type { Bip44Account } from "@metamask/account-api";
|
|
|
2
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { Provider } from "@metamask/network-controller";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "src/types";
|
|
6
5
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
7
7
|
export type EvmAccountProviderConfig = {
|
|
8
8
|
discovery: {
|
|
9
9
|
maxAttempts: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAG7D,OAAO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AED,KAAK,uDAA2B,EAC9B,aAAa,EACb,UAAU,GAIX;IACC,4FAA4F;IAC5F,mCAAmC;IACnC,OAAO,MAAM,IAAI,CAAC,WAAW,CAC3B,EAAE,EAAE,EAAE,aAAa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;QACpB,+EAA+E;QAC/E,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;QAC7C,IAAI,UAAU,GAAG,QAAQ,CAAC,MAAM,EAAE;YAChC,OAAO,QAAQ,CAAC,UAAU,CAAC,CAAC;SAC7B;QAED,8EAA8E;QAC9E,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,oCAAoC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACnD,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,KAAK,CACV,UAAU,CAAC,eAAe,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAzJM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\nimport type { MultichainAccountServiceMessenger } from 'src/types';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\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\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n ) {\n super(messenger);\n this.#config = config;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n return accountsArray;\n }\n\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n return [account];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,yBAAyB;AAInD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AACvD,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAM5D,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAY,wBAAwB;AAEtE,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACzB,uCAAmC;AACpC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,oBAAgB;AAGjD,MAAM,oBAAoB,GAAG,KAAK,CAAC;AAEnC;;;;;GAKG;AACH,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAUD,MAAM,CAAC,MAAM,yBAAyB,GAAG,KAAc,CAAC;AAExD,MAAM,OAAO,kBAAmB,SA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AC7B;QAED,8EAA8E;QAC9E,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,oCAAoC,CAAC,CAAC;QAC3D,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACnD,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC;QAEzD,OAAO,KAAK,CACV,UAAU,CAAC,eAAe,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CACjE,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAzJM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B","sourcesContent":["import { publicToAddress } from '@ethereumjs/util';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { HdKeyring } from '@metamask/eth-hd-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { Provider } from '@metamask/network-controller';\nimport { add0x, assert, bytesToHex, type Hex } from '@metamask/utils';\n\nimport {\n assertAreBip44Accounts,\n assertIsBip44Account,\n BaseBip44AccountProvider,\n} from './BaseBip44AccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nconst ETH_MAINNET_CHAIN_ID = '0x1';\n\n/**\n * Asserts an internal account exists.\n *\n * @param account - The internal account to check.\n * @throws An error if the internal account does not exist.\n */\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\nexport type EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n};\n\nexport const EVM_ACCOUNT_PROVIDER_NAME = 'EVM' as const;\n\nexport class EvmAccountProvider extends BaseBip44AccountProvider {\n static NAME = EVM_ACCOUNT_PROVIDER_NAME;\n\n readonly #config: EvmAccountProviderConfig;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n ) {\n super(messenger);\n this.#config = config;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === EthAccountType.Eoa &&\n account.metadata.keyring.type === (KeyringTypes.hd as string)\n );\n }\n\n getName(): string {\n return EvmAccountProvider.NAME;\n }\n\n /**\n * Get the EVM provider.\n *\n * @returns The EVM provider.\n */\n getEvmProvider(): Provider {\n const networkClientId = this.messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n ETH_MAINNET_CHAIN_ID,\n );\n const { provider } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n return provider;\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n throwOnGap = false,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n throwOnGap?: boolean;\n }): Promise<[Hex, boolean]> {\n const result = await this.withKeyring<EthKeyring, [Hex, boolean]>(\n { id: entropySource },\n async ({ keyring }) => {\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return [existing[groupIndex], false];\n }\n\n // If the throwOnGap flag is set, we throw an error to prevent index gaps.\n if (throwOnGap && groupIndex !== existing.length) {\n throw new Error('Trying to create too many accounts');\n }\n\n const [added] = await keyring.addAccounts(1);\n return [added, true];\n },\n );\n\n return result;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n throwOnGap: true,\n });\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n const accountsArray = [account];\n assertAreBip44Accounts(accountsArray);\n\n return accountsArray;\n }\n\n async #getTransactionCount(\n provider: Provider,\n address: Hex,\n ): Promise<number> {\n const countHex = await withRetry<Hex>(\n () =>\n withTimeout(\n provider.request({\n method: 'eth_getTransactionCount',\n params: [address, 'latest'],\n }),\n this.#config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.#config.discovery.maxAttempts,\n backOffMs: this.#config.discovery.backOffMs,\n },\n );\n\n return parseInt(countHex, 16);\n }\n\n async #getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Hex> {\n // NOTE: To avoid exposing this function at keyring level, we just re-use its internal state\n // and compute the derivation here.\n return await this.withKeyring<HdKeyring, Hex>(\n { id: entropySource },\n async ({ keyring }) => {\n // If the account already exist, do not re-derive and just re-use that account.\n const existing = await keyring.getAccounts();\n if (groupIndex < existing.length) {\n return existing[groupIndex];\n }\n\n // If not, then we just \"peek\" the next address to avoid creating the account.\n assert(keyring.root, 'Expected HD keyring.root to be set');\n const hdKey = keyring.root.deriveChild(groupIndex);\n assert(hdKey.publicKey, 'Expected public key to be set');\n\n return add0x(\n bytesToHex(publicToAddress(hdKey.publicKey, true)).toLowerCase(),\n );\n },\n );\n }\n\n /**\n * Discover and create accounts for the EVM provider.\n *\n * @param opts - The options for the discovery and creation of accounts.\n * @param opts.entropySource - The entropy source to use for the discovery and creation of accounts.\n * @param opts.groupIndex - The index of the group to create the accounts for.\n * @returns The accounts for the EVM provider.\n */\n async discoverAccounts(opts: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n const provider = this.getEvmProvider();\n const { entropySource, groupIndex } = opts;\n\n const addressFromGroupIndex = await this.#getAddressFromGroupIndex({\n entropySource,\n groupIndex,\n });\n\n const count = await this.#getTransactionCount(\n provider,\n addressFromGroupIndex,\n );\n if (count === 0) {\n return [];\n }\n\n // We have some activity on this address, we try to create the account.\n const [address] = await this.#createAccount({\n entropySource,\n groupIndex,\n });\n assert(\n addressFromGroupIndex === address,\n 'Created account does not match address from group index.',\n );\n\n const account = this.messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n assertInternalAccountExists(account);\n assertIsBip44Account(account);\n return [account];\n }\n}\n"]}
|
|
@@ -1,12 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _SnapAccountProvider_queue;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.isSnapAccountProvider = exports.SnapAccountProvider = void 0;
|
|
4
16
|
const keyring_controller_1 = require("@metamask/keyring-controller");
|
|
17
|
+
const async_mutex_1 = require("async-mutex");
|
|
5
18
|
const BaseBip44AccountProvider_1 = require("./BaseBip44AccountProvider.cjs");
|
|
6
19
|
class SnapAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProvider {
|
|
7
|
-
constructor(snapId, messenger) {
|
|
20
|
+
constructor(snapId, messenger, config) {
|
|
8
21
|
super(messenger);
|
|
22
|
+
_SnapAccountProvider_queue.set(this, void 0);
|
|
9
23
|
this.snapId = snapId;
|
|
24
|
+
const maxConcurrency = config.maxConcurrency ?? Infinity;
|
|
25
|
+
this.config = {
|
|
26
|
+
...config,
|
|
27
|
+
maxConcurrency,
|
|
28
|
+
};
|
|
29
|
+
// Create semaphore only if concurrency is limited
|
|
30
|
+
if (isFinite(maxConcurrency)) {
|
|
31
|
+
__classPrivateFieldSet(this, _SnapAccountProvider_queue, new async_mutex_1.Semaphore(maxConcurrency), "f");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
|
|
36
|
+
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
|
|
37
|
+
* Otherwise, it's queued through the semaphore to respect the concurrency limit.
|
|
38
|
+
*
|
|
39
|
+
* @param operation - The async operation to execute.
|
|
40
|
+
* @returns The result of the operation.
|
|
41
|
+
*/
|
|
42
|
+
async withMaxConcurrency(operation) {
|
|
43
|
+
if (__classPrivateFieldGet(this, _SnapAccountProvider_queue, "f")) {
|
|
44
|
+
return __classPrivateFieldGet(this, _SnapAccountProvider_queue, "f").runExclusive(operation);
|
|
45
|
+
}
|
|
46
|
+
return operation();
|
|
10
47
|
}
|
|
11
48
|
async getRestrictedSnapAccountCreator() {
|
|
12
49
|
// NOTE: We're not supposed to make the keyring instance escape `withKeyring` but
|
|
@@ -24,6 +61,7 @@ class SnapAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountPro
|
|
|
24
61
|
}
|
|
25
62
|
}
|
|
26
63
|
exports.SnapAccountProvider = SnapAccountProvider;
|
|
64
|
+
_SnapAccountProvider_queue = new WeakMap();
|
|
27
65
|
const isSnapAccountProvider = (provider) => {
|
|
28
66
|
return provider instanceof SnapAccountProvider;
|
|
29
67
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,qEAA4D;AAG5D,6CAAwC;AAExC,6EAAsE;AAmBtE,MAAsB,mBAAoB,SAAQ,mDAAwB;IAOxE,YACE,MAAc,EACd,SAA4C,EAC5C,MAAiC;QAEjC,KAAK,CAAC,SAAS,CAAC,CAAC;QAPV,6CAAmB;QAS1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,QAAQ,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,cAAc;SACf,CAAC;QAEF,kDAAkD;QAClD,IAAI,QAAQ,CAAC,cAAc,CAAC,EAAE;YAC5B,uBAAA,IAAI,8BAAU,IAAI,uBAAS,CAAC,cAAc,CAAC,MAAA,CAAC;SAC7C;IACH,CAAC;IAED;;;;;;;OAOG;IACO,KAAK,CAAC,kBAAkB,CAChC,SAA2B;QAE3B,IAAI,uBAAA,IAAI,kCAAO,EAAE;YACf,OAAO,uBAAA,IAAI,kCAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;SAC5C;QACD,OAAO,SAAS,EAAE,CAAC;IACrB,CAAC;IAES,KAAK,CAAC,+BAA+B;QAC7C,iFAAiF;QACjF,gFAAgF;QAChF,sCAAsC;QACtC,+EAA+E;QAC/E,iFAAiF;QACjF,wDAAwD;QACxD,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,WAAW,CAG1C,EAAE,IAAI,EAAE,iCAAY,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CACnD,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CACpC,CAAC;QAEF,OAAO,CAAC,OAAO,EAAE,EAAE,CACjB,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE;YAClC,4BAA4B,EAAE,KAAK;YACnC,mBAAmB,EAAE,KAAK;YAC1B,kBAAkB,EAAE,KAAK;SAC1B,CAAC,CAAC;IACP,CAAC;CAaF;AA9ED,kDA8EC;;AAEM,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC;AAJW,QAAA,qBAAqB,yBAIhC","sourcesContent":["import { type Bip44Account } from '@metamask/account-api';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Json, SnapId } from '@metamask/snaps-sdk';\nimport { Semaphore } from 'async-mutex';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type RestrictedSnapKeyringCreateAccount = (\n options: Record<string, Json>,\n) => Promise<KeyringAccount>;\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n timeoutMs: number;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #queue?: Semaphore;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<T>(\n operation: () => Promise<T>,\n ): Promise<T> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async getRestrictedSnapAccountCreator(): Promise<RestrictedSnapKeyringCreateAccount> {\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 return (options) =>\n createAccount(this.snapId, options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n abstract createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|
|
@@ -2,12 +2,34 @@ import { type Bip44Account } from "@metamask/account-api";
|
|
|
2
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { Json, SnapId } from "@metamask/snaps-sdk";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "src/types";
|
|
6
5
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.cjs";
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
7
7
|
export type RestrictedSnapKeyringCreateAccount = (options: Record<string, Json>) => Promise<KeyringAccount>;
|
|
8
|
+
export type SnapAccountProviderConfig = {
|
|
9
|
+
maxConcurrency?: number;
|
|
10
|
+
discovery: {
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
timeoutMs: number;
|
|
13
|
+
backOffMs: number;
|
|
14
|
+
};
|
|
15
|
+
createAccounts: {
|
|
16
|
+
timeoutMs: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
8
19
|
export declare abstract class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
20
|
+
#private;
|
|
9
21
|
readonly snapId: SnapId;
|
|
10
|
-
|
|
22
|
+
protected readonly config: SnapAccountProviderConfig;
|
|
23
|
+
constructor(snapId: SnapId, messenger: MultichainAccountServiceMessenger, config: SnapAccountProviderConfig);
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
|
|
26
|
+
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
|
|
27
|
+
* Otherwise, it's queued through the semaphore to respect the concurrency limit.
|
|
28
|
+
*
|
|
29
|
+
* @param operation - The async operation to execute.
|
|
30
|
+
* @returns The result of the operation.
|
|
31
|
+
*/
|
|
32
|
+
protected withMaxConcurrency<T>(operation: () => Promise<T>): Promise<T>;
|
|
11
33
|
protected getRestrictedSnapAccountCreator(): Promise<RestrictedSnapKeyringCreateAccount>;
|
|
12
34
|
abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;
|
|
13
35
|
abstract createAccounts(options: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE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CAAC,CAAC;cAOG,+BAA+B,IAAI,OAAO,CAAC,kCAAkC,CAAC;IAsB9F,QAAQ,CAAC,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAE7E,QAAQ,CAAC,cAAc,CAAC,OAAO,EAAE;QAC/B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAE3C,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE;QACjC,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAC5C;AAED,eAAO,MAAM,qBAAqB,aACtB,OAAO,oCAGlB,CAAC"}
|
|
@@ -2,12 +2,34 @@ import { type Bip44Account } from "@metamask/account-api";
|
|
|
2
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
3
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
4
|
import type { Json, SnapId } from "@metamask/snaps-sdk";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "src/types";
|
|
6
5
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
7
7
|
export type RestrictedSnapKeyringCreateAccount = (options: Record<string, Json>) => Promise<KeyringAccount>;
|
|
8
|
+
export type SnapAccountProviderConfig = {
|
|
9
|
+
maxConcurrency?: number;
|
|
10
|
+
discovery: {
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
timeoutMs: number;
|
|
13
|
+
backOffMs: number;
|
|
14
|
+
};
|
|
15
|
+
createAccounts: {
|
|
16
|
+
timeoutMs: number;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
8
19
|
export declare abstract class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
20
|
+
#private;
|
|
9
21
|
readonly snapId: SnapId;
|
|
10
|
-
|
|
22
|
+
protected readonly config: SnapAccountProviderConfig;
|
|
23
|
+
constructor(snapId: SnapId, messenger: MultichainAccountServiceMessenger, config: SnapAccountProviderConfig);
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
|
|
26
|
+
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
|
|
27
|
+
* Otherwise, it's queued through the semaphore to respect the concurrency limit.
|
|
28
|
+
*
|
|
29
|
+
* @param operation - The async operation to execute.
|
|
30
|
+
* @returns The result of the operation.
|
|
31
|
+
*/
|
|
32
|
+
protected withMaxConcurrency<T>(operation: () => Promise<T>): Promise<T>;
|
|
11
33
|
protected getRestrictedSnapAccountCreator(): Promise<RestrictedSnapKeyringCreateAccount>;
|
|
12
34
|
abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;
|
|
13
35
|
abstract createAccounts(options: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,4BAA4B;
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,8BAA8B;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,4B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ACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAC5C;AAED,eAAO,MAAM,qBAAqB,aACtB,OAAO,oCAGlB,CAAC"}
|
|
@@ -1,9 +1,46 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _SnapAccountProvider_queue;
|
|
1
13
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
14
|
+
import { Semaphore } from "async-mutex";
|
|
2
15
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
3
16
|
export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
4
|
-
constructor(snapId, messenger) {
|
|
17
|
+
constructor(snapId, messenger, config) {
|
|
5
18
|
super(messenger);
|
|
19
|
+
_SnapAccountProvider_queue.set(this, void 0);
|
|
6
20
|
this.snapId = snapId;
|
|
21
|
+
const maxConcurrency = config.maxConcurrency ?? Infinity;
|
|
22
|
+
this.config = {
|
|
23
|
+
...config,
|
|
24
|
+
maxConcurrency,
|
|
25
|
+
};
|
|
26
|
+
// Create semaphore only if concurrency is limited
|
|
27
|
+
if (isFinite(maxConcurrency)) {
|
|
28
|
+
__classPrivateFieldSet(this, _SnapAccountProvider_queue, new Semaphore(maxConcurrency), "f");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
|
|
33
|
+
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
|
|
34
|
+
* Otherwise, it's queued through the semaphore to respect the concurrency limit.
|
|
35
|
+
*
|
|
36
|
+
* @param operation - The async operation to execute.
|
|
37
|
+
* @returns The result of the operation.
|
|
38
|
+
*/
|
|
39
|
+
async withMaxConcurrency(operation) {
|
|
40
|
+
if (__classPrivateFieldGet(this, _SnapAccountProvider_queue, "f")) {
|
|
41
|
+
return __classPrivateFieldGet(this, _SnapAccountProvider_queue, "f").runExclusive(operation);
|
|
42
|
+
}
|
|
43
|
+
return operation();
|
|
7
44
|
}
|
|
8
45
|
async getRestrictedSnapAccountCreator() {
|
|
9
46
|
// NOTE: We're not supposed to make the keyring instance escape `withKeyring` but
|
|
@@ -20,6 +57,7 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
20
57
|
});
|
|
21
58
|
}
|
|
22
59
|
}
|
|
60
|
+
_SnapAccountProvider_queue = new WeakMap();
|
|
23
61
|
export const isSnapAccountProvider = (provider) => {
|
|
24
62
|
return provider instanceof SnapAccountProvider;
|
|
25
63
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAG5D,OAAO,EAA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K;SAC1B,CAAC,CAAC;IACP,CAAC;CAaF;;AAED,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC","sourcesContent":["import { type Bip44Account } from '@metamask/account-api';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Json, SnapId } from '@metamask/snaps-sdk';\nimport { Semaphore } from 'async-mutex';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type RestrictedSnapKeyringCreateAccount = (\n options: Record<string, Json>,\n) => Promise<KeyringAccount>;\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n timeoutMs: number;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #queue?: Semaphore;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<T>(\n operation: () => Promise<T>,\n ): Promise<T> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async getRestrictedSnapAccountCreator(): Promise<RestrictedSnapKeyringCreateAccount> {\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 return (options) =>\n createAccount(this.snapId, options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n abstract createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|