@metamask/snaps-rpc-methods 11.13.1 → 12.0.0
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/restricted/getBip32Entropy.cjs +16 -3
- package/dist/restricted/getBip32Entropy.cjs.map +1 -1
- package/dist/restricted/getBip32Entropy.d.cts +11 -1
- package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip32Entropy.d.mts +11 -1
- package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip32Entropy.mjs +17 -4
- package/dist/restricted/getBip32Entropy.mjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.cjs +19 -3
- package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.cts +11 -1
- package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
- package/dist/restricted/getBip32PublicKey.d.mts +11 -1
- package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
- package/dist/restricted/getBip32PublicKey.mjs +20 -4
- package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
- package/dist/restricted/getBip44Entropy.cjs +8 -5
- package/dist/restricted/getBip44Entropy.cjs.map +1 -1
- package/dist/restricted/getBip44Entropy.d.cts +6 -6
- package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
- package/dist/restricted/getBip44Entropy.d.mts +6 -6
- package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
- package/dist/restricted/getBip44Entropy.mjs +9 -6
- package/dist/restricted/getBip44Entropy.mjs.map +1 -1
- package/dist/restricted/getEntropy.cjs +7 -7
- package/dist/restricted/getEntropy.cjs.map +1 -1
- package/dist/restricted/getEntropy.d.cts +7 -7
- package/dist/restricted/getEntropy.d.cts.map +1 -1
- package/dist/restricted/getEntropy.d.mts +7 -7
- package/dist/restricted/getEntropy.d.mts.map +1 -1
- package/dist/restricted/getEntropy.mjs +8 -8
- package/dist/restricted/getEntropy.mjs.map +1 -1
- package/dist/restricted/manageState.cjs +4 -4
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +3 -3
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +3 -3
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +5 -5
- package/dist/restricted/manageState.mjs.map +1 -1
- package/dist/utils.cjs +68 -23
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +40 -16
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +40 -16
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +63 -19
- package/dist/utils.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [12.0.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING:** Use mnemonic seed for `snap_get*Entropy` RPC methods ([#3220](https://github.com/MetaMask/snaps/pull/3220))
|
|
15
|
+
- A `getMnemonicSeed` hook is now required to support the RPC methods.
|
|
16
|
+
- Use mnemonic seed for state encryption key derivation ([#3217](https://github.com/MetaMask/snaps/pull/3217))
|
|
17
|
+
|
|
10
18
|
## [11.13.1]
|
|
11
19
|
|
|
12
20
|
### Fixed
|
|
@@ -406,7 +414,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
406
414
|
- The version of the package no longer needs to match the version of all other
|
|
407
415
|
MetaMask Snaps packages.
|
|
408
416
|
|
|
409
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@
|
|
417
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@12.0.0...HEAD
|
|
418
|
+
[12.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.13.1...@metamask/snaps-rpc-methods@12.0.0
|
|
410
419
|
[11.13.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.13.0...@metamask/snaps-rpc-methods@11.13.1
|
|
411
420
|
[11.13.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.12.0...@metamask/snaps-rpc-methods@11.13.0
|
|
412
421
|
[11.12.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.11.0...@metamask/snaps-rpc-methods@11.12.0
|
|
@@ -35,6 +35,7 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
35
35
|
};
|
|
36
36
|
const methodHooks = {
|
|
37
37
|
getMnemonic: true,
|
|
38
|
+
getMnemonicSeed: true,
|
|
38
39
|
getUnlockPromise: true,
|
|
39
40
|
getClientCryptography: true,
|
|
40
41
|
};
|
|
@@ -48,6 +49,7 @@ exports.getBip32EntropyBuilder = Object.freeze({
|
|
|
48
49
|
*
|
|
49
50
|
* @param hooks - The RPC method hooks.
|
|
50
51
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
52
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
51
53
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
52
54
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
53
55
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -55,13 +57,24 @@ exports.getBip32EntropyBuilder = Object.freeze({
|
|
|
55
57
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
56
58
|
* @throws If the params are invalid.
|
|
57
59
|
*/
|
|
58
|
-
function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
|
|
60
|
+
function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
|
|
59
61
|
return async function getBip32Entropy(args) {
|
|
60
62
|
await getUnlockPromise(true);
|
|
61
63
|
const { params } = args;
|
|
62
64
|
(0, utils_1.assert)(params);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
// Using the seed is much faster, but we can only do it for these specific curves.
|
|
66
|
+
if (params.curve === 'secp256k1' || params.curve === 'ed25519') {
|
|
67
|
+
const seed = await (0, utils_2.getValueFromEntropySource)(getMnemonicSeed, params.source);
|
|
68
|
+
const node = await (0, utils_2.getNodeFromSeed)({
|
|
69
|
+
curve: params.curve,
|
|
70
|
+
path: params.path,
|
|
71
|
+
seed,
|
|
72
|
+
cryptographicFunctions: getClientCryptography(),
|
|
73
|
+
});
|
|
74
|
+
return node.toJSON();
|
|
75
|
+
}
|
|
76
|
+
const secretRecoveryPhrase = await (0, utils_2.getValueFromEntropySource)(getMnemonic, params.source);
|
|
77
|
+
const node = await (0, utils_2.getNodeFromMnemonic)({
|
|
65
78
|
curve: params.curve,
|
|
66
79
|
path: params.path,
|
|
67
80
|
secretRecoveryPhrase,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":";;;AAOA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAEvD,2CAAyC;AAGzC,
|
|
1
|
+
{"version":3,"file":"getBip32Entropy.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":";;;AAOA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAAuD;AAEvD,2CAAyC;AAGzC,wCAIkB;AAElB,MAAM,UAAU,GAAG,sBAAsB,CAAC;AAoD1C;;;;;;;;GAQG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,EAA8C,EAAE,EAAE;IAClE,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,gBAAgB;QAC/C,UAAU;QACV,cAAc,EAAE,CAAC,4BAAc,CA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,EAAC;gBACjC,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI;gBACJ,sBAAsB,EAAE,qBAAqB,EAAE;aAChD,CAAC,CAAC;YAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,oBAAoB,GAAG,MAAM,IAAA,iCAAyB,EAC1D,WAAW,EACX,MAAM,CAAC,MAAM,CACd,CAAC;QAEF,MAAM,IAAI,GAAG,MAAM,IAAA,2BAAmB,EAAC;YACrC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AA7CD,sEA6CC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32EntropyParams,\n GetBip32EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n getNodeFromMnemonic,\n getNodeFromSeed,\n getValueFromEntropySource,\n} from '../utils';\n\nconst targetName = 'snap_getBip32Entropy';\n\nexport type GetBip32EntropyMethodHooks = {\n /**\n * Get the mnemonic of the provided source. If no source is provided, the\n * mnemonic of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Get the mnemonic seed of the provided source. If no source is provided, the\n * mnemonic seed of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic seed of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32EntropySpecificationBuilderOptions = {\n methodHooks: GetBip32EntropyMethodHooks;\n};\n\ntype GetBip32EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip32Entropy` permission.\n * `snap_getBip32Entropy` lets the Snap control private keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32EntropySpecificationBuilderOptions,\n GetBip32EntropySpecification\n> = ({ methodHooks }: GetBip32EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32EntropyMethodHooks> = {\n getMnemonic: true,\n getMnemonicSeed: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip32EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `JsonSLIP10Node`.\n * @throws If the params are invalid.\n */\nexport function getBip32EntropyImplementation({\n getMnemonic,\n getMnemonicSeed,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32EntropyMethodHooks) {\n return async function getBip32Entropy(\n args: RestrictedMethodOptions<GetBip32EntropyParams>,\n ): Promise<GetBip32EntropyResult> {\n await getUnlockPromise(true);\n\n const { params } = args;\n assert(params);\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed,\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic,\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n };\n}\n"]}
|
|
@@ -15,6 +15,15 @@ export type GetBip32EntropyMethodHooks = {
|
|
|
15
15
|
* source is provided.
|
|
16
16
|
*/
|
|
17
17
|
getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the mnemonic seed of the provided source. If no source is provided, the
|
|
20
|
+
* mnemonic seed of the primary keyring will be returned.
|
|
21
|
+
*
|
|
22
|
+
* @param source - The optional ID of the source to get the mnemonic of.
|
|
23
|
+
* @returns The mnemonic seed of the provided source, or the default source if no
|
|
24
|
+
* source is provided.
|
|
25
|
+
*/
|
|
26
|
+
getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
27
|
/**
|
|
19
28
|
* Waits for the extension to be unlocked.
|
|
20
29
|
*
|
|
@@ -49,6 +58,7 @@ export declare const getBip32EntropyBuilder: Readonly<{
|
|
|
49
58
|
*
|
|
50
59
|
* @param hooks - The RPC method hooks.
|
|
51
60
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
61
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
52
62
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
53
63
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
54
64
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -56,6 +66,6 @@ export declare const getBip32EntropyBuilder: Readonly<{
|
|
|
56
66
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
57
67
|
* @throws If the params are invalid.
|
|
58
68
|
*/
|
|
59
|
-
export declare function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip32EntropyParams>) => Promise<GetBip32EntropyResult>;
|
|
69
|
+
export declare function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip32EntropyParams>) => Promise<GetBip32EntropyResult>;
|
|
60
70
|
export {};
|
|
61
71
|
//# sourceMappingURL=getBip32Entropy.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32Entropy.d.cts","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;
|
|
1
|
+
{"version":3,"file":"getBip32Entropy.d.cts","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAOlD,QAAA,MAAM,UAAU,yBAAyB,CAAC;AAE1C,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;;;;OAOG;IACH,WAAW,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;IAElE;;;;;;;OAOG;IACH,eAAe,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;IAEtE;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtE;;;;;;OAMG;IACH,qBAAqB,EAAE,MAAM,sBAAsB,GAAG,SAAS,CAAC;CACjE,CAAC;AAEF,KAAK,0CAA0C,GAAG;IAChD,WAAW,EAAE,0BAA0B,CAAC;CACzC,CAAC;AAkDF,eAAO,MAAM,sBAAsB;;;wBA/CjB,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,oCAAoC,CAAC;wBACtD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;;;EA+C/B,CAAC;AAEZ;;;;;;;;;;;;GAYG;AACH,wBAAgB,6BAA6B,CAAC,EAC5C,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,0BAA0B,UAEnB,wBAAwB,qBAAqB,CAAC,KACnD,QAAQ,qBAAqB,CAAC,CAqClC"}
|
|
@@ -15,6 +15,15 @@ export type GetBip32EntropyMethodHooks = {
|
|
|
15
15
|
* source is provided.
|
|
16
16
|
*/
|
|
17
17
|
getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the mnemonic seed of the provided source. If no source is provided, the
|
|
20
|
+
* mnemonic seed of the primary keyring will be returned.
|
|
21
|
+
*
|
|
22
|
+
* @param source - The optional ID of the source to get the mnemonic of.
|
|
23
|
+
* @returns The mnemonic seed of the provided source, or the default source if no
|
|
24
|
+
* source is provided.
|
|
25
|
+
*/
|
|
26
|
+
getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
27
|
/**
|
|
19
28
|
* Waits for the extension to be unlocked.
|
|
20
29
|
*
|
|
@@ -49,6 +58,7 @@ export declare const getBip32EntropyBuilder: Readonly<{
|
|
|
49
58
|
*
|
|
50
59
|
* @param hooks - The RPC method hooks.
|
|
51
60
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
61
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
52
62
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
53
63
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
54
64
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -56,6 +66,6 @@ export declare const getBip32EntropyBuilder: Readonly<{
|
|
|
56
66
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
57
67
|
* @throws If the params are invalid.
|
|
58
68
|
*/
|
|
59
|
-
export declare function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip32EntropyParams>) => Promise<GetBip32EntropyResult>;
|
|
69
|
+
export declare function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }: GetBip32EntropyMethodHooks): (args: RestrictedMethodOptions<GetBip32EntropyParams>) => Promise<GetBip32EntropyResult>;
|
|
60
70
|
export {};
|
|
61
71
|
//# sourceMappingURL=getBip32Entropy.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32Entropy.d.mts","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACtB,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;
|
|
1
|
+
{"version":3,"file":"getBip32Entropy.d.mts","sourceRoot":"","sources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B,qBAAqB,CAAC,KACnD,QAAQ,qBAAqB,CAAC,CAqClC"}
|
|
@@ -2,7 +2,7 @@ import { PermissionType, SubjectType } from "@metamask/permission-controller";
|
|
|
2
2
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
3
3
|
import { SnapCaveatType } from "@metamask/snaps-utils";
|
|
4
4
|
import { assert } from "@metamask/utils";
|
|
5
|
-
import {
|
|
5
|
+
import { getNodeFromMnemonic, getNodeFromSeed, getValueFromEntropySource } from "../utils.mjs";
|
|
6
6
|
const targetName = 'snap_getBip32Entropy';
|
|
7
7
|
/**
|
|
8
8
|
* The specification builder for the `snap_getBip32Entropy` permission.
|
|
@@ -32,6 +32,7 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
32
32
|
};
|
|
33
33
|
const methodHooks = {
|
|
34
34
|
getMnemonic: true,
|
|
35
|
+
getMnemonicSeed: true,
|
|
35
36
|
getUnlockPromise: true,
|
|
36
37
|
getClientCryptography: true,
|
|
37
38
|
};
|
|
@@ -45,6 +46,7 @@ export const getBip32EntropyBuilder = Object.freeze({
|
|
|
45
46
|
*
|
|
46
47
|
* @param hooks - The RPC method hooks.
|
|
47
48
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
49
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
48
50
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
49
51
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
50
52
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -52,13 +54,24 @@ export const getBip32EntropyBuilder = Object.freeze({
|
|
|
52
54
|
* @returns The method implementation which returns a `JsonSLIP10Node`.
|
|
53
55
|
* @throws If the params are invalid.
|
|
54
56
|
*/
|
|
55
|
-
export function getBip32EntropyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
|
|
57
|
+
export function getBip32EntropyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
|
|
56
58
|
return async function getBip32Entropy(args) {
|
|
57
59
|
await getUnlockPromise(true);
|
|
58
60
|
const { params } = args;
|
|
59
61
|
assert(params);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
// Using the seed is much faster, but we can only do it for these specific curves.
|
|
63
|
+
if (params.curve === 'secp256k1' || params.curve === 'ed25519') {
|
|
64
|
+
const seed = await getValueFromEntropySource(getMnemonicSeed, params.source);
|
|
65
|
+
const node = await getNodeFromSeed({
|
|
66
|
+
curve: params.curve,
|
|
67
|
+
path: params.path,
|
|
68
|
+
seed,
|
|
69
|
+
cryptographicFunctions: getClientCryptography(),
|
|
70
|
+
});
|
|
71
|
+
return node.toJSON();
|
|
72
|
+
}
|
|
73
|
+
const secretRecoveryPhrase = await getValueFromEntropySource(getMnemonic, params.source);
|
|
74
|
+
const node = await getNodeFromMnemonic({
|
|
62
75
|
curve: params.curve,
|
|
63
76
|
path: params.path,
|
|
64
77
|
secretRecoveryPhrase,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32Entropy.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AAEvD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAGzC,OAAO,
|
|
1
|
+
{"version":3,"file":"getBip32Entropy.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32Entropy.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AAEvD,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAGzC,OAAO,EACL,mBAAmB,EACnB,eAAe,E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CAAC,CAAC;QAEf,kFAAkF;QAClF,IAAI,MAAM,CAAC,KAAK,KAAK,WAAW,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/D,MAAM,IAAI,GAAG,MAAM,yBAAyB,CAC1C,eAAe,EACf,MAAM,CAAC,MAAM,CACd,CAAC;YAEF,MAAM,IAAI,GAAG,MAAM,eAAe,CAAC;gBACjC,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI;gBACJ,sBAAsB,EAAE,qBAAqB,EAAE;aAChD,CAAC,CAAC;YAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,oBAAoB,GAAG,MAAM,yBAAyB,CAC1D,WAAW,EACX,MAAM,CAAC,MAAM,CACd,CAAC;QAEF,MAAM,IAAI,GAAG,MAAM,mBAAmB,CAAC;YACrC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32EntropyParams,\n GetBip32EntropyResult,\n} from '@metamask/snaps-sdk';\nimport { SnapCaveatType } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n getNodeFromMnemonic,\n getNodeFromSeed,\n getValueFromEntropySource,\n} from '../utils';\n\nconst targetName = 'snap_getBip32Entropy';\n\nexport type GetBip32EntropyMethodHooks = {\n /**\n * Get the mnemonic of the provided source. If no source is provided, the\n * mnemonic of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Get the mnemonic seed of the provided source. If no source is provided, the\n * mnemonic seed of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic seed of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32EntropySpecificationBuilderOptions = {\n methodHooks: GetBip32EntropyMethodHooks;\n};\n\ntype GetBip32EntropySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32EntropyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\n/**\n * The specification builder for the `snap_getBip32Entropy` permission.\n * `snap_getBip32Entropy` lets the Snap control private keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32Entropy` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32EntropySpecificationBuilderOptions,\n GetBip32EntropySpecification\n> = ({ methodHooks }: GetBip32EntropySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32EntropyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32EntropyMethodHooks> = {\n getMnemonic: true,\n getMnemonicSeed: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip32EntropyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32Entropy`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a `JsonSLIP10Node`.\n * @throws If the params are invalid.\n */\nexport function getBip32EntropyImplementation({\n getMnemonic,\n getMnemonicSeed,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32EntropyMethodHooks) {\n return async function getBip32Entropy(\n args: RestrictedMethodOptions<GetBip32EntropyParams>,\n ): Promise<GetBip32EntropyResult> {\n await getUnlockPromise(true);\n\n const { params } = args;\n assert(params);\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed,\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic,\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n return node.toJSON();\n };\n}\n"]}
|
|
@@ -42,6 +42,7 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
42
42
|
};
|
|
43
43
|
const methodHooks = {
|
|
44
44
|
getMnemonic: true,
|
|
45
|
+
getMnemonicSeed: true,
|
|
45
46
|
getUnlockPromise: true,
|
|
46
47
|
getClientCryptography: true,
|
|
47
48
|
};
|
|
@@ -55,6 +56,7 @@ exports.getBip32PublicKeyBuilder = Object.freeze({
|
|
|
55
56
|
*
|
|
56
57
|
* @param hooks - The RPC method hooks.
|
|
57
58
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
59
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
58
60
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
59
61
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
60
62
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -62,13 +64,27 @@ exports.getBip32PublicKeyBuilder = Object.freeze({
|
|
|
62
64
|
* @returns The method implementation which returns a public key.
|
|
63
65
|
* @throws If the params are invalid.
|
|
64
66
|
*/
|
|
65
|
-
function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
|
|
67
|
+
function getBip32PublicKeyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
|
|
66
68
|
return async function getBip32PublicKey(args) {
|
|
67
69
|
await getUnlockPromise(true);
|
|
68
70
|
(0, utils_1.assertStruct)(args.params, exports.Bip32PublicKeyArgsStruct, 'Invalid BIP-32 public key params', rpc_errors_1.rpcErrors.invalidParams);
|
|
69
71
|
const { params } = args;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
// Using the seed is much faster, but we can only do it for these specific curves.
|
|
73
|
+
if (params.curve === 'secp256k1' || params.curve === 'ed25519') {
|
|
74
|
+
const seed = await (0, utils_2.getValueFromEntropySource)(getMnemonicSeed, params.source);
|
|
75
|
+
const node = await (0, utils_2.getNodeFromSeed)({
|
|
76
|
+
curve: params.curve,
|
|
77
|
+
path: params.path,
|
|
78
|
+
seed,
|
|
79
|
+
cryptographicFunctions: getClientCryptography(),
|
|
80
|
+
});
|
|
81
|
+
if (params.compressed) {
|
|
82
|
+
return node.compressedPublicKey;
|
|
83
|
+
}
|
|
84
|
+
return node.publicKey;
|
|
85
|
+
}
|
|
86
|
+
const secretRecoveryPhrase = await (0, utils_2.getValueFromEntropySource)(getMnemonic, params.source);
|
|
87
|
+
const node = await (0, utils_2.getNodeFromMnemonic)({
|
|
72
88
|
curve: params.curve,
|
|
73
89
|
path: params.path,
|
|
74
90
|
secretRecoveryPhrase,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32PublicKey.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":";;;AAOA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAK+B;AAC/B,uDAA0E;AAE1E,2CAA+C;AAG/C,
|
|
1
|
+
{"version":3,"file":"getBip32PublicKey.cjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":";;;AAOA,2EAA8E;AAC9E,qDAAiD;AAKjD,uDAK+B;AAC/B,uDAA0E;AAE1E,2CAA+C;AAG/C,wCAIkB;AAElB,MAAM,UAAU,GAAG,wBAAwB,CAAC;AAoD/B,QAAA,wBAAwB,GAAG,IAAA,0BAAY,EAClD,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,6BAAe;IACrB,KAAK,EAAE,yBAAW;IAClB,UAAU,EAAE,IAAA,sBAAQ,EAAC,IAAA,qBAAO,GAAE,CAAC;IAC/B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AAkC,EAClC,sBAAS,CAAC,aAAa,CACxB,CAAC;QAEF,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAExB,kFAAkF;QAClF,IAAI,MAAM,CAAC,KAAK,KAAK,WAAW,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/D,MAAM,IAAI,GAAG,MAAM,IAAA,iCAAyB,EAC1C,eAAe,EACf,MAAM,CAAC,MAAM,CACd,CAAC;YAEF,MAAM,IAAI,GAAG,MAAM,IAAA,uBAAe,EAAC;gBACjC,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI;gBACJ,sBAAsB,EAAE,qBAAqB,EAAE;aAChD,CAAC,CAAC;YAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;gBACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;YAClC,CAAC;YAED,OAAO,IAAI,CAAC,SAAS,CAAC;QACxB,CAAC;QAED,MAAM,oBAAoB,GAAG,MAAM,IAAA,iCAAyB,EAC1D,WAAW,EACX,MAAM,CAAC,MAAM,CACd,CAAC;QAEF,MAAM,IAAI,GAAG,MAAM,IAAA,2BAAmB,EAAC;YACrC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC;AA3DD,0EA2DC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32PublicKeyParams,\n GetBip32PublicKeyResult,\n} from '@metamask/snaps-sdk';\nimport {\n bip32entropy,\n Bip32PathStruct,\n CurveStruct,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport { boolean, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n getValueFromEntropySource,\n getNodeFromMnemonic,\n getNodeFromSeed,\n} from '../utils';\n\nconst targetName = 'snap_getBip32PublicKey';\n\nexport type GetBip32PublicKeyMethodHooks = {\n /**\n * Get the mnemonic of the provided source. If no source is provided, the\n * mnemonic of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Get the mnemonic seed of the provided source. If no source is provided, the\n * mnemonic seed of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic seed of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32PublicKeySpecificationBuilderOptions = {\n methodHooks: GetBip32PublicKeyMethodHooks;\n};\n\ntype GetBip32PublicKeySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32PublicKeyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\nexport const Bip32PublicKeyArgsStruct = bip32entropy(\n object({\n path: Bip32PathStruct,\n curve: CurveStruct,\n compressed: optional(boolean()),\n source: optional(string()),\n }),\n);\n\n/**\n * The specification builder for the `snap_getBip32PublicKey` permission.\n * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32PublicKey` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32PublicKeySpecificationBuilderOptions,\n GetBip32PublicKeySpecification\n> = ({ methodHooks }: GetBip32PublicKeySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32PublicKeyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32PublicKeyMethodHooks> = {\n getMnemonic: true,\n getMnemonicSeed: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip32PublicKeyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32PublicKey`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a public key.\n * @throws If the params are invalid.\n */\nexport function getBip32PublicKeyImplementation({\n getMnemonic,\n getMnemonicSeed,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32PublicKeyMethodHooks) {\n return async function getBip32PublicKey(\n args: RestrictedMethodOptions<GetBip32PublicKeyParams>,\n ): Promise<GetBip32PublicKeyResult> {\n await getUnlockPromise(true);\n\n assertStruct(\n args.params,\n Bip32PublicKeyArgsStruct,\n 'Invalid BIP-32 public key params',\n rpcErrors.invalidParams,\n );\n\n const { params } = args;\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed,\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic,\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n };\n}\n"]}
|
|
@@ -15,6 +15,15 @@ export type GetBip32PublicKeyMethodHooks = {
|
|
|
15
15
|
* source is provided.
|
|
16
16
|
*/
|
|
17
17
|
getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the mnemonic seed of the provided source. If no source is provided, the
|
|
20
|
+
* mnemonic seed of the primary keyring will be returned.
|
|
21
|
+
*
|
|
22
|
+
* @param source - The optional ID of the source to get the mnemonic of.
|
|
23
|
+
* @returns The mnemonic seed of the provided source, or the default source if no
|
|
24
|
+
* source is provided.
|
|
25
|
+
*/
|
|
26
|
+
getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
27
|
/**
|
|
19
28
|
* Waits for the extension to be unlocked.
|
|
20
29
|
*
|
|
@@ -60,6 +69,7 @@ export declare const getBip32PublicKeyBuilder: Readonly<{
|
|
|
60
69
|
*
|
|
61
70
|
* @param hooks - The RPC method hooks.
|
|
62
71
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
72
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
63
73
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
64
74
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
65
75
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -67,6 +77,6 @@ export declare const getBip32PublicKeyBuilder: Readonly<{
|
|
|
67
77
|
* @returns The method implementation which returns a public key.
|
|
68
78
|
* @throws If the params are invalid.
|
|
69
79
|
*/
|
|
70
|
-
export declare function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<GetBip32PublicKeyParams>) => Promise<GetBip32PublicKeyResult>;
|
|
80
|
+
export declare function getBip32PublicKeyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<GetBip32PublicKeyParams>) => Promise<GetBip32PublicKeyResult>;
|
|
71
81
|
export {};
|
|
72
82
|
//# sourceMappingURL=getBip32PublicKey.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32PublicKey.d.cts","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;A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AAM,wBAAwB;;;wBAxDnB,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,sCAAsC,CAAC;wBACxD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;;;EAwD/B,CAAC;AAEZ;;;;;;;;;;;;GAYG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,qBAAqB,GACtB,EAAE,4BAA4B,UAErB,wBAAwB,uBAAuB,CAAC,KACrD,QAAQ,uBAAuB,CAAC,CAmDpC"}
|
|
@@ -15,6 +15,15 @@ export type GetBip32PublicKeyMethodHooks = {
|
|
|
15
15
|
* source is provided.
|
|
16
16
|
*/
|
|
17
17
|
getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the mnemonic seed of the provided source. If no source is provided, the
|
|
20
|
+
* mnemonic seed of the primary keyring will be returned.
|
|
21
|
+
*
|
|
22
|
+
* @param source - The optional ID of the source to get the mnemonic of.
|
|
23
|
+
* @returns The mnemonic seed of the provided source, or the default source if no
|
|
24
|
+
* source is provided.
|
|
25
|
+
*/
|
|
26
|
+
getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;
|
|
18
27
|
/**
|
|
19
28
|
* Waits for the extension to be unlocked.
|
|
20
29
|
*
|
|
@@ -60,6 +69,7 @@ export declare const getBip32PublicKeyBuilder: Readonly<{
|
|
|
60
69
|
*
|
|
61
70
|
* @param hooks - The RPC method hooks.
|
|
62
71
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
72
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
63
73
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
64
74
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
65
75
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -67,6 +77,6 @@ export declare const getBip32PublicKeyBuilder: Readonly<{
|
|
|
67
77
|
* @returns The method implementation which returns a public key.
|
|
68
78
|
* @throws If the params are invalid.
|
|
69
79
|
*/
|
|
70
|
-
export declare function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<GetBip32PublicKeyParams>) => Promise<GetBip32PublicKeyResult>;
|
|
80
|
+
export declare function getBip32PublicKeyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }: GetBip32PublicKeyMethodHooks): (args: RestrictedMethodOptions<GetBip32PublicKeyParams>) => Promise<GetBip32PublicKeyResult>;
|
|
71
81
|
export {};
|
|
72
82
|
//# sourceMappingURL=getBip32PublicKey.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32PublicKey.d.mts","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACxB,4BAA4B;AAQ7B,OAAO,KAAK,EAAE,aAAa,EAAE,wBAAwB;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;
|
|
1
|
+
{"version":3,"file":"getBip32PublicKey.d.mts","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,2BAA2B;AACjE,OAAO,KAAK,EACV,8BAA8B,EAC9B,6BAA6B,EAC7B,uBAAuB,EAExB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACx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object, optional, string } from "@metamask/superstruct";
|
|
5
5
|
import { assertStruct } from "@metamask/utils";
|
|
6
|
-
import {
|
|
6
|
+
import { getValueFromEntropySource, getNodeFromMnemonic, getNodeFromSeed } from "../utils.mjs";
|
|
7
7
|
const targetName = 'snap_getBip32PublicKey';
|
|
8
8
|
export const Bip32PublicKeyArgsStruct = bip32entropy(object({
|
|
9
9
|
path: Bip32PathStruct,
|
|
@@ -39,6 +39,7 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
39
39
|
};
|
|
40
40
|
const methodHooks = {
|
|
41
41
|
getMnemonic: true,
|
|
42
|
+
getMnemonicSeed: true,
|
|
42
43
|
getUnlockPromise: true,
|
|
43
44
|
getClientCryptography: true,
|
|
44
45
|
};
|
|
@@ -52,6 +53,7 @@ export const getBip32PublicKeyBuilder = Object.freeze({
|
|
|
52
53
|
*
|
|
53
54
|
* @param hooks - The RPC method hooks.
|
|
54
55
|
* @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.
|
|
56
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.
|
|
55
57
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked
|
|
56
58
|
* and prompts the user to unlock their MetaMask if it is locked.
|
|
57
59
|
* @param hooks.getClientCryptography - A function to retrieve the cryptographic
|
|
@@ -59,13 +61,27 @@ export const getBip32PublicKeyBuilder = Object.freeze({
|
|
|
59
61
|
* @returns The method implementation which returns a public key.
|
|
60
62
|
* @throws If the params are invalid.
|
|
61
63
|
*/
|
|
62
|
-
export function getBip32PublicKeyImplementation({ getMnemonic, getUnlockPromise, getClientCryptography, }) {
|
|
64
|
+
export function getBip32PublicKeyImplementation({ getMnemonic, getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
|
|
63
65
|
return async function getBip32PublicKey(args) {
|
|
64
66
|
await getUnlockPromise(true);
|
|
65
67
|
assertStruct(args.params, Bip32PublicKeyArgsStruct, 'Invalid BIP-32 public key params', rpcErrors.invalidParams);
|
|
66
68
|
const { params } = args;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
// Using the seed is much faster, but we can only do it for these specific curves.
|
|
70
|
+
if (params.curve === 'secp256k1' || params.curve === 'ed25519') {
|
|
71
|
+
const seed = await getValueFromEntropySource(getMnemonicSeed, params.source);
|
|
72
|
+
const node = await getNodeFromSeed({
|
|
73
|
+
curve: params.curve,
|
|
74
|
+
path: params.path,
|
|
75
|
+
seed,
|
|
76
|
+
cryptographicFunctions: getClientCryptography(),
|
|
77
|
+
});
|
|
78
|
+
if (params.compressed) {
|
|
79
|
+
return node.compressedPublicKey;
|
|
80
|
+
}
|
|
81
|
+
return node.publicKey;
|
|
82
|
+
}
|
|
83
|
+
const secretRecoveryPhrase = await getValueFromEntropySource(getMnemonic, params.source);
|
|
84
|
+
const node = await getNodeFromMnemonic({
|
|
69
85
|
curve: params.curve,
|
|
70
86
|
path: params.path,
|
|
71
87
|
secretRecoveryPhrase,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getBip32PublicKey.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EACL,YAAY,EACZ,eAAe,EACf,WAAW,EACX,cAAc,EACf,8BAA8B;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,8BAA8B;AAE1E,OAAO,EAAE,YAAY,EAAE,wBAAwB;AAG/C,OAAO,
|
|
1
|
+
{"version":3,"file":"getBip32PublicKey.mjs","sourceRoot":"","sources":["../../src/restricted/getBip32PublicKey.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAKjD,OAAO,EACL,YAAY,EACZ,eA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oBAAoB;YACpB,sBAAsB,EAAE,qBAAqB,EAAE;SAChD,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,mBAAmB,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { CryptographicFunctions } from '@metamask/key-tree';\nimport type {\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n GetBip32PublicKeyParams,\n GetBip32PublicKeyResult,\n} from '@metamask/snaps-sdk';\nimport {\n bip32entropy,\n Bip32PathStruct,\n CurveStruct,\n SnapCaveatType,\n} from '@metamask/snaps-utils';\nimport { boolean, object, optional, string } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { assertStruct } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\nimport {\n getValueFromEntropySource,\n getNodeFromMnemonic,\n getNodeFromSeed,\n} from '../utils';\n\nconst targetName = 'snap_getBip32PublicKey';\n\nexport type GetBip32PublicKeyMethodHooks = {\n /**\n * Get the mnemonic of the provided source. If no source is provided, the\n * mnemonic of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Get the mnemonic seed of the provided source. If no source is provided, the\n * mnemonic seed of the primary keyring will be returned.\n *\n * @param source - The optional ID of the source to get the mnemonic of.\n * @returns The mnemonic seed of the provided source, or the default source if no\n * source is provided.\n */\n getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;\n\n /**\n * Waits for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Get the cryptographic functions to use for the client. This may return an\n * empty object or `undefined` to fall back to the default cryptographic\n * functions.\n *\n * @returns The cryptographic functions to use for the client.\n */\n getClientCryptography: () => CryptographicFunctions | undefined;\n};\n\ntype GetBip32PublicKeySpecificationBuilderOptions = {\n methodHooks: GetBip32PublicKeyMethodHooks;\n};\n\ntype GetBip32PublicKeySpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof targetName;\n methodImplementation: ReturnType<typeof getBip32PublicKeyImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n}>;\n\nexport const Bip32PublicKeyArgsStruct = bip32entropy(\n object({\n path: Bip32PathStruct,\n curve: CurveStruct,\n compressed: optional(boolean()),\n source: optional(string()),\n }),\n);\n\n/**\n * The specification builder for the `snap_getBip32PublicKey` permission.\n * `snap_getBip32PublicKey` lets the Snap retrieve public keys for a particular\n * BIP-32 node.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_getBip32PublicKey` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n GetBip32PublicKeySpecificationBuilderOptions,\n GetBip32PublicKeySpecification\n> = ({ methodHooks }: GetBip32PublicKeySpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName,\n allowedCaveats: [SnapCaveatType.PermittedDerivationPaths],\n methodImplementation: getBip32PublicKeyImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (\n caveats?.length !== 1 ||\n caveats[0].type !== SnapCaveatType.PermittedDerivationPaths\n ) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.PermittedDerivationPaths}\" caveat.`,\n });\n }\n },\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<GetBip32PublicKeyMethodHooks> = {\n getMnemonic: true,\n getMnemonicSeed: true,\n getUnlockPromise: true,\n getClientCryptography: true,\n};\n\nexport const getBip32PublicKeyBuilder = Object.freeze({\n targetName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_getBip32PublicKey`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.getMnemonic - A function to retrieve the Secret Recovery Phrase of the user.\n * @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed of the user.\n * @param hooks.getUnlockPromise - A function that resolves once the MetaMask extension is unlocked\n * and prompts the user to unlock their MetaMask if it is locked.\n * @param hooks.getClientCryptography - A function to retrieve the cryptographic\n * functions to use for the client.\n * @returns The method implementation which returns a public key.\n * @throws If the params are invalid.\n */\nexport function getBip32PublicKeyImplementation({\n getMnemonic,\n getMnemonicSeed,\n getUnlockPromise,\n getClientCryptography,\n}: GetBip32PublicKeyMethodHooks) {\n return async function getBip32PublicKey(\n args: RestrictedMethodOptions<GetBip32PublicKeyParams>,\n ): Promise<GetBip32PublicKeyResult> {\n await getUnlockPromise(true);\n\n assertStruct(\n args.params,\n Bip32PublicKeyArgsStruct,\n 'Invalid BIP-32 public key params',\n rpcErrors.invalidParams,\n );\n\n const { params } = args;\n\n // Using the seed is much faster, but we can only do it for these specific curves.\n if (params.curve === 'secp256k1' || params.curve === 'ed25519') {\n const seed = await getValueFromEntropySource(\n getMnemonicSeed,\n params.source,\n );\n\n const node = await getNodeFromSeed({\n curve: params.curve,\n path: params.path,\n seed,\n cryptographicFunctions: getClientCryptography(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n }\n\n const secretRecoveryPhrase = await getValueFromEntropySource(\n getMnemonic,\n params.source,\n );\n\n const node = await getNodeFromMnemonic({\n curve: params.curve,\n path: params.path,\n secretRecoveryPhrase,\n cryptographicFunctions: getClientCryptography(),\n });\n\n if (params.compressed) {\n return node.compressedPublicKey;\n }\n\n return node.publicKey;\n };\n}\n"]}
|
|
@@ -35,7 +35,7 @@ const specificationBuilder = ({ methodHooks }) => {
|
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
37
|
const methodHooks = {
|
|
38
|
-
|
|
38
|
+
getMnemonicSeed: true,
|
|
39
39
|
getUnlockPromise: true,
|
|
40
40
|
getClientCryptography: true,
|
|
41
41
|
};
|
|
@@ -48,7 +48,7 @@ exports.getBip44EntropyBuilder = Object.freeze({
|
|
|
48
48
|
* Builds the method implementation for `snap_getBip44Entropy`.
|
|
49
49
|
*
|
|
50
50
|
* @param hooks - The RPC method hooks.
|
|
51
|
-
* @param hooks.
|
|
51
|
+
* @param hooks.getMnemonicSeed - A function to retrieve the BIP-39 seed
|
|
52
52
|
* of the user.
|
|
53
53
|
* @param hooks.getUnlockPromise - A function that resolves once the MetaMask
|
|
54
54
|
* extension is unlocked and prompts the user to unlock their MetaMask if it is
|
|
@@ -58,13 +58,16 @@ exports.getBip44EntropyBuilder = Object.freeze({
|
|
|
58
58
|
* @returns The method implementation which returns a `BIP44CoinTypeNode`.
|
|
59
59
|
* @throws If the params are invalid.
|
|
60
60
|
*/
|
|
61
|
-
function getBip44EntropyImplementation({
|
|
61
|
+
function getBip44EntropyImplementation({ getMnemonicSeed, getUnlockPromise, getClientCryptography, }) {
|
|
62
62
|
return async function getBip44Entropy(args) {
|
|
63
63
|
await getUnlockPromise(true);
|
|
64
64
|
// `args.params` is validated by the decorator, so it's safe to assert here.
|
|
65
65
|
const params = args.params;
|
|
66
|
-
const
|
|
67
|
-
const node = await key_tree_1.BIP44CoinTypeNode.
|
|
66
|
+
const seed = await (0, utils_1.getValueFromEntropySource)(getMnemonicSeed, params.source);
|
|
67
|
+
const node = await key_tree_1.BIP44CoinTypeNode.fromSeed({
|
|
68
|
+
derivationPath: [seed, `bip32:44'`, `bip32:${params.coinType}'`],
|
|
69
|
+
network: 'mainnet',
|
|
70
|
+
}, getClientCryptography());
|
|
68
71
|
return node.toJSON();
|
|
69
72
|
};
|
|
70
73
|
}
|