@metamask-previews/multichain-account-service 2.0.0-preview-7df8698e → 2.0.0-preview-51709948
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 +4 -0
- package/dist/MultichainAccountService.cjs +8 -5
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +4 -3
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +4 -3
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +8 -5
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/analytics/index.cjs +18 -0
- package/dist/analytics/index.cjs.map +1 -0
- package/dist/analytics/index.d.cts +2 -0
- package/dist/analytics/index.d.cts.map +1 -0
- package/dist/analytics/index.d.mts +2 -0
- package/dist/analytics/index.d.mts.map +1 -0
- package/dist/analytics/index.mjs +2 -0
- package/dist/analytics/index.mjs.map +1 -0
- package/dist/analytics/traces.cjs +21 -0
- package/dist/analytics/traces.cjs.map +1 -0
- package/dist/analytics/traces.d.cts +13 -0
- package/dist/analytics/traces.d.cts.map +1 -0
- package/dist/analytics/traces.d.mts +13 -0
- package/dist/analytics/traces.d.mts.map +1 -0
- package/dist/analytics/traces.mjs +17 -0
- package/dist/analytics/traces.mjs.map +1 -0
- package/dist/constants/traces.cjs +9 -0
- package/dist/constants/traces.cjs.map +1 -0
- package/dist/constants/traces.d.cts +5 -0
- package/dist/constants/traces.d.cts.map +1 -0
- package/dist/constants/traces.d.mts +5 -0
- package/dist/constants/traces.d.mts.map +1 -0
- package/dist/constants/traces.mjs +6 -0
- package/dist/constants/traces.mjs.map +1 -0
- package/dist/providers/AccountProviderWrapper.cjs +24 -2
- package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.cts +3 -1
- package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.mts +3 -1
- package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
- package/dist/providers/AccountProviderWrapper.mjs +24 -2
- package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs +33 -22
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +3 -2
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +3 -2
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +33 -22
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +4 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +4 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.mjs","sourceRoot":"","sources":["../../src/analytics/traces.ts"],"names":[],"mappings":"AAMA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAkB,KAAK,EAC/C,QAAsB,EACtB,EAA2C,EACtB,EAAE;IACvB,IAAI,CAAC,EAAE,EAAE;QACP,OAAO,SAAuB,CAAC;KAChC;IACD,OAAO,MAAM,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;AACrC,CAAC,CAAC","sourcesContent":["import type {\n TraceCallback,\n TraceContext,\n TraceRequest,\n} from '@metamask/controller-utils';\n\n/**\n * Fallback function for tracing.\n * This function is used when no specific trace function is provided.\n * It executes the provided function in a trace context if available.\n *\n * @param _request - The trace request containing additional data and context.\n * @param fn - The function to execute within the trace context.\n * @returns A promise that resolves to the result of the executed function.\n * If no function is provided, it resolves to undefined.\n */\nexport const traceFallback: TraceCallback = async <ReturnType>(\n _request: TraceRequest,\n fn?: (context?: TraceContext) => ReturnType,\n): Promise<ReturnType> => {\n if (!fn) {\n return undefined as ReturnType;\n }\n return await Promise.resolve(fn());\n};\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TraceName = void 0;
|
|
4
|
+
var TraceName;
|
|
5
|
+
(function (TraceName) {
|
|
6
|
+
TraceName["SnapDiscoverAccounts"] = "Snap Discover Accounts";
|
|
7
|
+
TraceName["EvmDiscoverAccounts"] = "EVM Discover Accounts";
|
|
8
|
+
})(TraceName || (exports.TraceName = TraceName = {}));
|
|
9
|
+
//# sourceMappingURL=traces.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.cjs","sourceRoot":"","sources":["../../src/constants/traces.ts"],"names":[],"mappings":";;;AAAA,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,4DAAiD,CAAA;IACjD,0DAA+C,CAAA;AACjD,CAAC,EAHW,SAAS,yBAAT,SAAS,QAGpB","sourcesContent":["export enum TraceName {\n 'SnapDiscoverAccounts' = 'Snap Discover Accounts',\n 'EvmDiscoverAccounts' = 'EVM Discover Accounts',\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.d.cts","sourceRoot":"","sources":["../../src/constants/traces.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACnB,sBAAsB,2BAA2B;IACjD,qBAAqB,0BAA0B;CAChD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.d.mts","sourceRoot":"","sources":["../../src/constants/traces.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACnB,sBAAsB,2BAA2B;IACjD,qBAAqB,0BAA0B;CAChD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.mjs","sourceRoot":"","sources":["../../src/constants/traces.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,4DAAiD,CAAA;IACjD,0DAA+C,CAAA;AACjD,CAAC,EAHW,SAAS,KAAT,SAAS,QAGpB","sourcesContent":["export enum TraceName {\n 'SnapDiscoverAccounts' = 'Snap Discover Accounts',\n 'EvmDiscoverAccounts' = 'EVM Discover Accounts',\n}\n"]}
|
|
@@ -1,16 +1,32 @@
|
|
|
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 _AccountProviderWrapper_trace;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
exports.isAccountProviderWrapper = exports.AccountProviderWrapper = void 0;
|
|
4
16
|
const BaseBip44AccountProvider_1 = require("./BaseBip44AccountProvider.cjs");
|
|
17
|
+
const analytics_1 = require("../analytics/index.cjs");
|
|
18
|
+
const traces_1 = require("../constants/traces.cjs");
|
|
5
19
|
/**
|
|
6
20
|
* A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.
|
|
7
21
|
* When disabled, the provider will not create new accounts and return empty results.
|
|
8
22
|
*/
|
|
9
23
|
class AccountProviderWrapper extends BaseBip44AccountProvider_1.BaseBip44AccountProvider {
|
|
10
|
-
constructor(messenger, provider) {
|
|
24
|
+
constructor(messenger, provider, trace) {
|
|
11
25
|
super(messenger);
|
|
12
26
|
this.isEnabled = true;
|
|
27
|
+
_AccountProviderWrapper_trace.set(this, void 0);
|
|
13
28
|
this.provider = provider;
|
|
29
|
+
__classPrivateFieldSet(this, _AccountProviderWrapper_trace, trace ?? analytics_1.traceFallback, "f");
|
|
14
30
|
}
|
|
15
31
|
getName() {
|
|
16
32
|
return this.provider.getName();
|
|
@@ -83,10 +99,16 @@ class AccountProviderWrapper extends BaseBip44AccountProvider_1.BaseBip44Account
|
|
|
83
99
|
if (!this.isEnabled) {
|
|
84
100
|
return [];
|
|
85
101
|
}
|
|
86
|
-
return this.
|
|
102
|
+
return __classPrivateFieldGet(this, _AccountProviderWrapper_trace, "f").call(this, {
|
|
103
|
+
name: traces_1.TraceName.SnapDiscoverAccounts,
|
|
104
|
+
data: {
|
|
105
|
+
providerName: this.getName(),
|
|
106
|
+
},
|
|
107
|
+
}, async () => this.provider.discoverAccounts(options));
|
|
87
108
|
}
|
|
88
109
|
}
|
|
89
110
|
exports.AccountProviderWrapper = AccountProviderWrapper;
|
|
111
|
+
_AccountProviderWrapper_trace = new WeakMap();
|
|
90
112
|
/**
|
|
91
113
|
* Simple type guard to check if a provider is wrapped.
|
|
92
114
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.cjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.cjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,6EAAsE;AACtE,sDAA6C;AAC7C,oDAAgD;AAGhD;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,mDAAwB;IAOlE,YACE,SAA4C,EA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;QACD,OAAO,uBAAA,IAAI,qCAAO,MAAX,IAAI,EACT;YACE,IAAI,EAAE,kBAAS,CAAC,oBAAoB;YACpC,IAAI,EAAE;gBACJ,YAAY,EAAE,IAAI,CAAC,OAAO,EAAE;aAC7B;SACF,EACD,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,CACpD,CAAC;IACJ,CAAC;CACF;AAhHD,wDAgHC;;AAED;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,QAAiB;IAEjB,OAAO,QAAQ,YAAY,sBAAsB,CAAC;AACpD,CAAC;AAJD,4DAIC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.\n * When disabled, the provider will not create new accounts and return empty results.\n */\nexport class AccountProviderWrapper extends BaseBip44AccountProvider {\n private isEnabled: boolean = true;\n\n private readonly provider: BaseBip44AccountProvider;\n\n readonly #trace: TraceCallback;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n provider: BaseBip44AccountProvider,\n trace?: TraceCallback,\n ) {\n super(messenger);\n this.provider = provider;\n this.#trace = trace ?? traceFallback;\n }\n\n override getName(): string {\n return this.provider.getName();\n }\n\n /**\n * Set the enabled state for this provider.\n *\n * @param enabled - Whether the provider should be enabled.\n */\n setEnabled(enabled: boolean): void {\n this.isEnabled = enabled;\n }\n\n /**\n * Override getAccounts to return empty array when disabled.\n *\n * @returns Array of accounts, or empty array if disabled.\n */\n override getAccounts(): Bip44Account<KeyringAccount>[] {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.getAccounts();\n }\n\n /**\n * Override getAccount to throw when disabled.\n *\n * @param id - The account ID to retrieve.\n * @returns The account with the specified ID.\n * @throws When disabled or account not found.\n */\n override getAccount(\n id: Bip44Account<KeyringAccount>['id'],\n ): Bip44Account<KeyringAccount> {\n if (!this.isEnabled) {\n throw new Error('Provider is disabled');\n }\n return this.provider.getAccount(id);\n }\n\n /**\n * Implement abstract method: Check if account is compatible.\n * Delegates directly to wrapped provider - no runtime checks needed!\n *\n * @param account - The account to check.\n * @returns True if the account is compatible.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean {\n return this.provider.isAccountCompatible(account);\n }\n\n /**\n * Implement abstract method: Create accounts, returns empty array when disabled.\n *\n * @param options - Account creation options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\n }\n\n /**\n * Implement abstract method: Discover and create accounts, returns empty array when disabled.\n *\n * @param options - Account discovery options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to discovered accounts, or empty array if disabled.\n */\n async discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.#trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n providerName: this.getName(),\n },\n },\n async () => this.provider.discoverAccounts(options),\n );\n }\n}\n\n/**\n * Simple type guard to check if a provider is wrapped.\n *\n * @param provider - The provider to check.\n * @returns True if the provider is an AccountProviderWrapper.\n */\nexport function isAccountProviderWrapper(\n provider: unknown,\n): provider is AccountProviderWrapper {\n return provider instanceof AccountProviderWrapper;\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
|
2
3
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
4
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.cjs";
|
|
4
5
|
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
@@ -7,9 +8,10 @@ import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
|
7
8
|
* When disabled, the provider will not create new accounts and return empty results.
|
|
8
9
|
*/
|
|
9
10
|
export declare class AccountProviderWrapper extends BaseBip44AccountProvider {
|
|
11
|
+
#private;
|
|
10
12
|
private isEnabled;
|
|
11
13
|
private readonly provider;
|
|
12
|
-
constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider);
|
|
14
|
+
constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider, trace?: TraceCallback);
|
|
13
15
|
getName(): string;
|
|
14
16
|
/**
|
|
15
17
|
* Set the enabled state for this provider.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.d.cts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.d.cts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,wBAAwB;;IAClE,OAAO,CAAC,SAAS,CAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAKlD,SAAS,EAAE,iCAAiC,EAC5C,QAAQ,EAAE,wBAAwB,EAClC,KAAK,CAAC,EAAE,aAAa;IAOd,OAAO,IAAI,MAAM;IAI1B;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC;;;;OAIG;IACM,WAAW,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE;IAOtD;;;;;;OAMG;IACM,UAAU,CACjB,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GACrC,YAAY,CAAC,cAAc,CAAC;IAO/B;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAInE;;;;;;;OAOG;IACG,cAAc,CAAC,OAAO,EAAE;QAC5B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAO3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,OAAO,EAAE;QAC9B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAc5C;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,OAAO,GAChB,QAAQ,IAAI,sBAAsB,CAEpC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
|
2
3
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
4
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
4
5
|
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
@@ -7,9 +8,10 @@ import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
|
7
8
|
* When disabled, the provider will not create new accounts and return empty results.
|
|
8
9
|
*/
|
|
9
10
|
export declare class AccountProviderWrapper extends BaseBip44AccountProvider {
|
|
11
|
+
#private;
|
|
10
12
|
private isEnabled;
|
|
11
13
|
private readonly provider;
|
|
12
|
-
constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider);
|
|
14
|
+
constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider, trace?: TraceCallback);
|
|
13
15
|
getName(): string;
|
|
14
16
|
/**
|
|
15
17
|
* Set the enabled state for this provider.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.d.mts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.d.mts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,wBAAwB;;IAClE,OAAO,CAAC,SAAS,CAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAKlD,SAAS,EAAE,iCAAiC,EAC5C,QAAQ,EAAE,wBAAwB,EAClC,KAAK,CAAC,EAAE,aAAa;IAOd,OAAO,IAAI,MAAM;IAI1B;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC;;;;OAIG;IACM,WAAW,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE;IAOtD;;;;;;OAMG;IACM,UAAU,CACjB,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GACrC,YAAY,CAAC,cAAc,CAAC;IAO/B;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAInE;;;;;;;OAOG;IACG,cAAc,CAAC,OAAO,EAAE;QAC5B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAO3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,OAAO,EAAE;QAC9B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAc5C;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,OAAO,GAChB,QAAQ,IAAI,sBAAsB,CAEpC"}
|
|
@@ -1,13 +1,29 @@
|
|
|
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 _AccountProviderWrapper_trace;
|
|
1
13
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
14
|
+
import { traceFallback } from "../analytics/index.mjs";
|
|
15
|
+
import { TraceName } from "../constants/traces.mjs";
|
|
2
16
|
/**
|
|
3
17
|
* A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.
|
|
4
18
|
* When disabled, the provider will not create new accounts and return empty results.
|
|
5
19
|
*/
|
|
6
20
|
export class AccountProviderWrapper extends BaseBip44AccountProvider {
|
|
7
|
-
constructor(messenger, provider) {
|
|
21
|
+
constructor(messenger, provider, trace) {
|
|
8
22
|
super(messenger);
|
|
9
23
|
this.isEnabled = true;
|
|
24
|
+
_AccountProviderWrapper_trace.set(this, void 0);
|
|
10
25
|
this.provider = provider;
|
|
26
|
+
__classPrivateFieldSet(this, _AccountProviderWrapper_trace, trace ?? traceFallback, "f");
|
|
11
27
|
}
|
|
12
28
|
getName() {
|
|
13
29
|
return this.provider.getName();
|
|
@@ -80,9 +96,15 @@ export class AccountProviderWrapper extends BaseBip44AccountProvider {
|
|
|
80
96
|
if (!this.isEnabled) {
|
|
81
97
|
return [];
|
|
82
98
|
}
|
|
83
|
-
return this.
|
|
99
|
+
return __classPrivateFieldGet(this, _AccountProviderWrapper_trace, "f").call(this, {
|
|
100
|
+
name: TraceName.SnapDiscoverAccounts,
|
|
101
|
+
data: {
|
|
102
|
+
providerName: this.getName(),
|
|
103
|
+
},
|
|
104
|
+
}, async () => this.provider.discoverAccounts(options));
|
|
84
105
|
}
|
|
85
106
|
}
|
|
107
|
+
_AccountProviderWrapper_trace = new WeakMap();
|
|
86
108
|
/**
|
|
87
109
|
* Simple type guard to check if a provider is wrapped.
|
|
88
110
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.mjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.mjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,aAAa,EAAE,+BAAqB;AAC7C,OAAO,EAAE,SAAS,EAAE,gCAA4B;AAGhD;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,wBAAwB;IAOlE,YACE,SAA4C,EAC5C,QAAkC,EAClC,KAAqB;QAErB,KAAK,CAAC,SAAS,CAAC,CAAC;QAXX,cAAS,GAAY,IAAI,CAAC;QAIzB,gDAAsB;QAQ7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,uBAAA,IAAI,iCAAU,KAAK,IAAI,aAAa,MAAA,CAAC;IACvC,CAAC;IAEQ,OAAO;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACM,WAAW;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACM,UAAU,CACjB,EAAsC;QAEtC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACzC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAqC;QACvD,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,cAAc,CAAC,OAGpB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAGtB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,uBAAA,IAAI,qCAAO,MAAX,IAAI,EACT;YACE,IAAI,EAAE,SAAS,CAAC,oBAAoB;YACpC,IAAI,EAAE;gBACJ,YAAY,EAAE,IAAI,CAAC,OAAO,EAAE;aAC7B;SACF,EACD,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,CACpD,CAAC;IACJ,CAAC;CACF;;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAiB;IAEjB,OAAO,QAAQ,YAAY,sBAAsB,CAAC;AACpD,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.\n * When disabled, the provider will not create new accounts and return empty results.\n */\nexport class AccountProviderWrapper extends BaseBip44AccountProvider {\n private isEnabled: boolean = true;\n\n private readonly provider: BaseBip44AccountProvider;\n\n readonly #trace: TraceCallback;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n provider: BaseBip44AccountProvider,\n trace?: TraceCallback,\n ) {\n super(messenger);\n this.provider = provider;\n this.#trace = trace ?? traceFallback;\n }\n\n override getName(): string {\n return this.provider.getName();\n }\n\n /**\n * Set the enabled state for this provider.\n *\n * @param enabled - Whether the provider should be enabled.\n */\n setEnabled(enabled: boolean): void {\n this.isEnabled = enabled;\n }\n\n /**\n * Override getAccounts to return empty array when disabled.\n *\n * @returns Array of accounts, or empty array if disabled.\n */\n override getAccounts(): Bip44Account<KeyringAccount>[] {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.getAccounts();\n }\n\n /**\n * Override getAccount to throw when disabled.\n *\n * @param id - The account ID to retrieve.\n * @returns The account with the specified ID.\n * @throws When disabled or account not found.\n */\n override getAccount(\n id: Bip44Account<KeyringAccount>['id'],\n ): Bip44Account<KeyringAccount> {\n if (!this.isEnabled) {\n throw new Error('Provider is disabled');\n }\n return this.provider.getAccount(id);\n }\n\n /**\n * Implement abstract method: Check if account is compatible.\n * Delegates directly to wrapped provider - no runtime checks needed!\n *\n * @param account - The account to check.\n * @returns True if the account is compatible.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean {\n return this.provider.isAccountCompatible(account);\n }\n\n /**\n * Implement abstract method: Create accounts, returns empty array when disabled.\n *\n * @param options - Account creation options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\n }\n\n /**\n * Implement abstract method: Discover and create accounts, returns empty array when disabled.\n *\n * @param options - Account discovery options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to discovered accounts, or empty array if disabled.\n */\n async discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.#trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n providerName: this.getName(),\n },\n },\n async () => this.provider.discoverAccounts(options),\n );\n }\n}\n\n/**\n * Simple type guard to check if a provider is wrapped.\n *\n * @param provider - The provider to check.\n * @returns True if the provider is an AccountProviderWrapper.\n */\nexport function isAccountProviderWrapper(\n provider: unknown,\n): provider is AccountProviderWrapper {\n return provider instanceof AccountProviderWrapper;\n}\n"]}
|
|
@@ -10,13 +10,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
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
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
|
|
13
|
+
var _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_trace, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.EvmAccountProvider = exports.EVM_ACCOUNT_PROVIDER_NAME = void 0;
|
|
16
16
|
const util_1 = require("@ethereumjs/util");
|
|
17
17
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
18
18
|
const keyring_controller_1 = require("@metamask/keyring-controller");
|
|
19
19
|
const utils_1 = require("@metamask/utils");
|
|
20
|
+
const analytics_1 = require("../analytics/index.cjs");
|
|
21
|
+
const traces_1 = require("../constants/traces.cjs");
|
|
20
22
|
const BaseBip44AccountProvider_1 = require("./BaseBip44AccountProvider.cjs");
|
|
21
23
|
const utils_2 = require("./utils.cjs");
|
|
22
24
|
const ETH_MAINNET_CHAIN_ID = '0x1';
|
|
@@ -39,11 +41,13 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
|
|
|
39
41
|
timeoutMs: 500,
|
|
40
42
|
backOffMs: 500,
|
|
41
43
|
},
|
|
42
|
-
}) {
|
|
44
|
+
}, trace) {
|
|
43
45
|
super(messenger);
|
|
44
46
|
_EvmAccountProvider_instances.add(this);
|
|
45
47
|
_EvmAccountProvider_config.set(this, void 0);
|
|
48
|
+
_EvmAccountProvider_trace.set(this, void 0);
|
|
46
49
|
__classPrivateFieldSet(this, _EvmAccountProvider_config, config, "f");
|
|
50
|
+
__classPrivateFieldSet(this, _EvmAccountProvider_trace, trace ?? analytics_1.traceFallback, "f");
|
|
47
51
|
}
|
|
48
52
|
isAccountCompatible(account) {
|
|
49
53
|
return (account.type === keyring_api_1.EthAccountType.Eoa &&
|
|
@@ -84,30 +88,37 @@ class EvmAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountProv
|
|
|
84
88
|
* @returns The accounts for the EVM provider.
|
|
85
89
|
*/
|
|
86
90
|
async discoverAccounts(opts) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
return __classPrivateFieldGet(this, _EvmAccountProvider_trace, "f").call(this, {
|
|
92
|
+
name: traces_1.TraceName.EvmDiscoverAccounts,
|
|
93
|
+
data: {
|
|
94
|
+
providerName: this.getName(),
|
|
95
|
+
},
|
|
96
|
+
}, async () => {
|
|
97
|
+
const provider = this.getEvmProvider();
|
|
98
|
+
const { entropySource, groupIndex } = opts;
|
|
99
|
+
const addressFromGroupIndex = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAddressFromGroupIndex).call(this, {
|
|
100
|
+
entropySource,
|
|
101
|
+
groupIndex,
|
|
102
|
+
});
|
|
103
|
+
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, addressFromGroupIndex);
|
|
104
|
+
if (count === 0) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
// We have some activity on this address, we try to create the account.
|
|
108
|
+
const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
|
|
109
|
+
entropySource,
|
|
110
|
+
groupIndex,
|
|
111
|
+
});
|
|
112
|
+
(0, utils_1.assert)(addressFromGroupIndex === address, 'Created account does not match address from group index.');
|
|
113
|
+
const account = this.messenger.call('AccountsController:getAccountByAddress', address);
|
|
114
|
+
assertInternalAccountExists(account);
|
|
115
|
+
(0, BaseBip44AccountProvider_1.assertIsBip44Account)(account);
|
|
116
|
+
return [account];
|
|
92
117
|
});
|
|
93
|
-
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, addressFromGroupIndex);
|
|
94
|
-
if (count === 0) {
|
|
95
|
-
return [];
|
|
96
|
-
}
|
|
97
|
-
// We have some activity on this address, we try to create the account.
|
|
98
|
-
const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
|
|
99
|
-
entropySource,
|
|
100
|
-
groupIndex,
|
|
101
|
-
});
|
|
102
|
-
(0, utils_1.assert)(addressFromGroupIndex === address, 'Created account does not match address from group index.');
|
|
103
|
-
const account = this.messenger.call('AccountsController:getAccountByAddress', address);
|
|
104
|
-
assertInternalAccountExists(account);
|
|
105
|
-
(0, BaseBip44AccountProvider_1.assertIsBip44Account)(account);
|
|
106
|
-
return [account];
|
|
107
118
|
}
|
|
108
119
|
}
|
|
109
120
|
exports.EvmAccountProvider = EvmAccountProvider;
|
|
110
|
-
_EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
|
|
121
|
+
_EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
|
|
111
122
|
const result = await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
|
|
112
123
|
const existing = await keyring.getAccounts();
|
|
113
124
|
if (groupIndex < existing.length) {
|
|
@@ -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,MAAA,CAAC;IACxB,CAAC;IAED,mBAAmB,CAAC,OAAsC;QACxD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,GAAG;YACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CA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AE,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":";;;;;;;;;;;;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metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\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 { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../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 readonly #trace: TraceCallback;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: EvmAccountProviderConfig = {\n discovery: {\n maxAttempts: 3,\n timeoutMs: 500,\n backOffMs: 500,\n },\n },\n trace?: TraceCallback,\n ) {\n super(messenger);\n this.#config = config;\n this.#trace = trace ?? traceFallback;\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 return this.#trace(\n {\n name: TraceName.EvmDiscoverAccounts,\n data: {\n providerName: this.getName(),\n },\n },\n async () => {\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 }\n}\n"]}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
|
2
3
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
4
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
5
|
import type { Provider } from "@metamask/network-controller";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
6
7
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.cjs";
|
|
7
8
|
export type EvmAccountProviderConfig = {
|
|
8
9
|
discovery: {
|
|
@@ -15,7 +16,7 @@ export declare const EVM_ACCOUNT_PROVIDER_NAME: "EVM";
|
|
|
15
16
|
export declare class EvmAccountProvider extends BaseBip44AccountProvider {
|
|
16
17
|
#private;
|
|
17
18
|
static NAME: "EVM";
|
|
18
|
-
constructor(messenger: MultichainAccountServiceMessenger, config?: EvmAccountProviderConfig);
|
|
19
|
+
constructor(messenger: MultichainAccountServiceMessenger, config?: EvmAccountProviderConfig, trace?: TraceCallback);
|
|
19
20
|
isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;
|
|
20
21
|
getName(): string;
|
|
21
22
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAEhE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAI7D,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,OAAO,EAGL,wBAAwB,EACzB,uCAAmC;AAmBpC,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;gBAOtC,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAMP,EACD,KAAK,CAAC,EAAE,aAAa;IAOvB,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;CA6C5C"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
+
import type { TraceCallback } from "@metamask/controller-utils";
|
|
2
3
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
3
4
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
4
5
|
import type { Provider } from "@metamask/network-controller";
|
|
5
|
-
import type { MultichainAccountServiceMessenger } from "
|
|
6
|
+
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
6
7
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
7
8
|
export type EvmAccountProviderConfig = {
|
|
8
9
|
discovery: {
|
|
@@ -15,7 +16,7 @@ export declare const EVM_ACCOUNT_PROVIDER_NAME: "EVM";
|
|
|
15
16
|
export declare class EvmAccountProvider extends BaseBip44AccountProvider {
|
|
16
17
|
#private;
|
|
17
18
|
static NAME: "EVM";
|
|
18
|
-
constructor(messenger: MultichainAccountServiceMessenger, config?: EvmAccountProviderConfig);
|
|
19
|
+
constructor(messenger: MultichainAccountServiceMessenger, config?: EvmAccountProviderConfig, trace?: TraceCallback);
|
|
19
20
|
isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;
|
|
20
21
|
getName(): string;
|
|
21
22
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EvmAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;
|
|
1
|
+
{"version":3,"file":"EvmAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAEhE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAEV,eAAe,EAChB,uCAAuC;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,qCAAqC;AAI7D,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,OAAO,EAGL,wBAAwB,EACzB,uCAAmC;AAmBpC,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;gBAOtC,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAMP,EACD,KAAK,CAAC,EAAE,aAAa;IAOvB,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;CA6C5C"}
|
|
@@ -9,11 +9,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
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
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
|
|
12
|
+
var _EvmAccountProvider_instances, _EvmAccountProvider_config, _EvmAccountProvider_trace, _EvmAccountProvider_createAccount, _EvmAccountProvider_getTransactionCount, _EvmAccountProvider_getAddressFromGroupIndex;
|
|
13
13
|
import { publicToAddress } from "@ethereumjs/util";
|
|
14
14
|
import { EthAccountType } from "@metamask/keyring-api";
|
|
15
15
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
16
16
|
import { add0x, assert, bytesToHex } from "@metamask/utils";
|
|
17
|
+
import { traceFallback } from "../analytics/index.mjs";
|
|
18
|
+
import { TraceName } from "../constants/traces.mjs";
|
|
17
19
|
import { assertAreBip44Accounts, assertIsBip44Account, BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
18
20
|
import { withRetry, withTimeout } from "./utils.mjs";
|
|
19
21
|
const ETH_MAINNET_CHAIN_ID = '0x1';
|
|
@@ -36,11 +38,13 @@ export class EvmAccountProvider extends BaseBip44AccountProvider {
|
|
|
36
38
|
timeoutMs: 500,
|
|
37
39
|
backOffMs: 500,
|
|
38
40
|
},
|
|
39
|
-
}) {
|
|
41
|
+
}, trace) {
|
|
40
42
|
super(messenger);
|
|
41
43
|
_EvmAccountProvider_instances.add(this);
|
|
42
44
|
_EvmAccountProvider_config.set(this, void 0);
|
|
45
|
+
_EvmAccountProvider_trace.set(this, void 0);
|
|
43
46
|
__classPrivateFieldSet(this, _EvmAccountProvider_config, config, "f");
|
|
47
|
+
__classPrivateFieldSet(this, _EvmAccountProvider_trace, trace ?? traceFallback, "f");
|
|
44
48
|
}
|
|
45
49
|
isAccountCompatible(account) {
|
|
46
50
|
return (account.type === EthAccountType.Eoa &&
|
|
@@ -81,29 +85,36 @@ export class EvmAccountProvider extends BaseBip44AccountProvider {
|
|
|
81
85
|
* @returns The accounts for the EVM provider.
|
|
82
86
|
*/
|
|
83
87
|
async discoverAccounts(opts) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
return __classPrivateFieldGet(this, _EvmAccountProvider_trace, "f").call(this, {
|
|
89
|
+
name: TraceName.EvmDiscoverAccounts,
|
|
90
|
+
data: {
|
|
91
|
+
providerName: this.getName(),
|
|
92
|
+
},
|
|
93
|
+
}, async () => {
|
|
94
|
+
const provider = this.getEvmProvider();
|
|
95
|
+
const { entropySource, groupIndex } = opts;
|
|
96
|
+
const addressFromGroupIndex = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAddressFromGroupIndex).call(this, {
|
|
97
|
+
entropySource,
|
|
98
|
+
groupIndex,
|
|
99
|
+
});
|
|
100
|
+
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, addressFromGroupIndex);
|
|
101
|
+
if (count === 0) {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
// We have some activity on this address, we try to create the account.
|
|
105
|
+
const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
|
|
106
|
+
entropySource,
|
|
107
|
+
groupIndex,
|
|
108
|
+
});
|
|
109
|
+
assert(addressFromGroupIndex === address, 'Created account does not match address from group index.');
|
|
110
|
+
const account = this.messenger.call('AccountsController:getAccountByAddress', address);
|
|
111
|
+
assertInternalAccountExists(account);
|
|
112
|
+
assertIsBip44Account(account);
|
|
113
|
+
return [account];
|
|
89
114
|
});
|
|
90
|
-
const count = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getTransactionCount).call(this, provider, addressFromGroupIndex);
|
|
91
|
-
if (count === 0) {
|
|
92
|
-
return [];
|
|
93
|
-
}
|
|
94
|
-
// We have some activity on this address, we try to create the account.
|
|
95
|
-
const [address] = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_createAccount).call(this, {
|
|
96
|
-
entropySource,
|
|
97
|
-
groupIndex,
|
|
98
|
-
});
|
|
99
|
-
assert(addressFromGroupIndex === address, 'Created account does not match address from group index.');
|
|
100
|
-
const account = this.messenger.call('AccountsController:getAccountByAddress', address);
|
|
101
|
-
assertInternalAccountExists(account);
|
|
102
|
-
assertIsBip44Account(account);
|
|
103
|
-
return [account];
|
|
104
115
|
}
|
|
105
116
|
}
|
|
106
|
-
_EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
|
|
117
|
+
_EvmAccountProvider_config = new WeakMap(), _EvmAccountProvider_trace = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_createAccount = async function _EvmAccountProvider_createAccount({ entropySource, groupIndex, throwOnGap = false, }) {
|
|
107
118
|
const result = await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
|
|
108
119
|
const existing = await keyring.getAccounts();
|
|
109
120
|
if (groupIndex < existing.length) {
|