From cda25863b9e4a82a02ff65bc6d4497897eee7379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20=C5=81ucka?= <5708018+PatrykLucka@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:19:30 +0100 Subject: [PATCH] feat(multi-srp): add keyringId to getMnemonic fn --- .../snaps-controllers/src/snaps/SnapController.ts | 7 ++++--- .../src/restricted/getBip32Entropy.ts | 7 ++++--- .../src/restricted/getBip32PublicKey.ts | 7 ++++--- .../src/restricted/getBip44Entropy.ts | 11 ++++++++--- .../snaps-rpc-methods/src/restricted/getEntropy.ts | 7 ++++--- .../src/types/methods/get-bip32-public-key.ts | 1 + packages/snaps-sdk/src/types/methods/get-entropy.ts | 2 ++ packages/snaps-sdk/src/types/permissions.ts | 2 ++ .../snaps-simulation/src/methods/specifications.ts | 3 ++- .../src/middleware/internal-methods/accounts.ts | 2 +- .../src/middleware/internal-methods/middleware.ts | 3 ++- packages/snaps-simulation/src/simulation.ts | 3 ++- .../src/features/simulation/middleware.ts | 2 +- 13 files changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/snaps-controllers/src/snaps/SnapController.ts b/packages/snaps-controllers/src/snaps/SnapController.ts index 7b655839d0..564a6b043d 100644 --- a/packages/snaps-controllers/src/snaps/SnapController.ts +++ b/packages/snaps-controllers/src/snaps/SnapController.ts @@ -700,11 +700,12 @@ type SnapControllerArgs = { encryptor: ExportableKeyEncryptor; /** - * A hook to access the mnemonic of the user's primary keyring. + * A hook to access the mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring. * + * @param keyringId - The ID of the keyring to get the mnemonic for. * @returns The mnemonic as bytes. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * A hook to get dynamic feature flags at runtime. @@ -803,7 +804,7 @@ export class SnapController extends BaseController< #encryptor: ExportableKeyEncryptor; - #getMnemonic: () => Promise; + #getMnemonic: (keyringId?: string) => Promise; #getFeatureFlags: () => DynamicFeatureFlags; diff --git a/packages/snaps-rpc-methods/src/restricted/getBip32Entropy.ts b/packages/snaps-rpc-methods/src/restricted/getBip32Entropy.ts index 4734d71e9c..3b622109ac 100644 --- a/packages/snaps-rpc-methods/src/restricted/getBip32Entropy.ts +++ b/packages/snaps-rpc-methods/src/restricted/getBip32Entropy.ts @@ -22,9 +22,10 @@ const targetName = 'snap_getBip32Entropy'; export type GetBip32EntropyMethodHooks = { /** - * @returns The mnemonic of the user's primary keyring. + * @param keyringId - The ID of the keyring to get the mnemonic for. + * @returns The mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * Waits for the extension to be unlocked. @@ -128,7 +129,7 @@ export function getBip32EntropyImplementation({ const node = await getNode({ curve: params.curve, path: params.path, - secretRecoveryPhrase: await getMnemonic(), + secretRecoveryPhrase: await getMnemonic(params.keyringId), cryptographicFunctions: getClientCryptography(), }); diff --git a/packages/snaps-rpc-methods/src/restricted/getBip32PublicKey.ts b/packages/snaps-rpc-methods/src/restricted/getBip32PublicKey.ts index ba9cfc81a0..e435ae113d 100644 --- a/packages/snaps-rpc-methods/src/restricted/getBip32PublicKey.ts +++ b/packages/snaps-rpc-methods/src/restricted/getBip32PublicKey.ts @@ -28,9 +28,10 @@ const targetName = 'snap_getBip32PublicKey'; export type GetBip32PublicKeyMethodHooks = { /** - * @returns The mnemonic of the user's primary keyring. + * @param keyringId - The ID of the keyring to get the mnemonic for. + * @returns The mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * Waits for the extension to be unlocked. @@ -147,7 +148,7 @@ export function getBip32PublicKeyImplementation({ const node = await getNode({ curve: params.curve, path: params.path, - secretRecoveryPhrase: await getMnemonic(), + secretRecoveryPhrase: await getMnemonic(params.keyringId), cryptographicFunctions: getClientCryptography(), }); diff --git a/packages/snaps-rpc-methods/src/restricted/getBip44Entropy.ts b/packages/snaps-rpc-methods/src/restricted/getBip44Entropy.ts index 8fff798b8d..5124cff40c 100644 --- a/packages/snaps-rpc-methods/src/restricted/getBip44Entropy.ts +++ b/packages/snaps-rpc-methods/src/restricted/getBip44Entropy.ts @@ -21,9 +21,10 @@ const targetName = 'snap_getBip44Entropy'; export type GetBip44EntropyMethodHooks = { /** - * @returns The mnemonic of the user's primary keyring. + * @param keyringId - The ID of the keyring to get the mnemonic for. + * @returns The mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * Waits for the extension to be unlocked. @@ -128,7 +129,11 @@ export function getBip44EntropyImplementation({ const params = args.params as GetBip44EntropyParams; const node = await BIP44CoinTypeNode.fromDerivationPath( - [await getMnemonic(), `bip32:44'`, `bip32:${params.coinType}'`], + [ + await getMnemonic(params.keyringId), + `bip32:44'`, + `bip32:${params.coinType}'`, + ], 'mainnet', getClientCryptography(), ); diff --git a/packages/snaps-rpc-methods/src/restricted/getEntropy.ts b/packages/snaps-rpc-methods/src/restricted/getEntropy.ts index b5f685cc2c..c6a71165fc 100644 --- a/packages/snaps-rpc-methods/src/restricted/getEntropy.ts +++ b/packages/snaps-rpc-methods/src/restricted/getEntropy.ts @@ -74,9 +74,10 @@ export const getEntropyBuilder = Object.freeze({ export type GetEntropyHooks = { /** - * @returns The mnemonic of the user's primary keyring. + * @param keyringId - The ID of the keyring to get the mnemonic for. + * @returns The mnemonic of the user's keyring, if the keyringId is not provided, it will return the mnemonic of the primary keyring. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * Waits for the extension to be unlocked. @@ -130,7 +131,7 @@ function getEntropyImplementation({ ); await getUnlockPromise(true); - const mnemonicPhrase = await getMnemonic(); + const mnemonicPhrase = await getMnemonic(params.keyringId); return deriveEntropy({ input: origin, diff --git a/packages/snaps-sdk/src/types/methods/get-bip32-public-key.ts b/packages/snaps-sdk/src/types/methods/get-bip32-public-key.ts index 76af535fa1..be95a431b2 100644 --- a/packages/snaps-sdk/src/types/methods/get-bip32-public-key.ts +++ b/packages/snaps-sdk/src/types/methods/get-bip32-public-key.ts @@ -9,6 +9,7 @@ import type { Bip32Entropy } from '../permissions'; * to `false`. */ export type GetBip32PublicKeyParams = Bip32Entropy & { + keyringId?: string; compressed?: boolean; }; diff --git a/packages/snaps-sdk/src/types/methods/get-entropy.ts b/packages/snaps-sdk/src/types/methods/get-entropy.ts index cfb3ffb44f..020dc13232 100644 --- a/packages/snaps-sdk/src/types/methods/get-entropy.ts +++ b/packages/snaps-sdk/src/types/methods/get-entropy.ts @@ -6,10 +6,12 @@ import type { Hex } from '@metamask/utils'; * @property version - The version of the entropy to retrieve. This is used for * backwards compatibility. As of now, only version 1 is supported. * @property salt - The optional salt to use when deriving the entropy. + * @property keyringId - The ID of the keyring to get the mnemonic for. */ export type GetEntropyParams = { version: 1; salt?: string; + keyringId?: string; }; /** diff --git a/packages/snaps-sdk/src/types/permissions.ts b/packages/snaps-sdk/src/types/permissions.ts index 7caed09815..390bab7d76 100644 --- a/packages/snaps-sdk/src/types/permissions.ts +++ b/packages/snaps-sdk/src/types/permissions.ts @@ -25,10 +25,12 @@ export type NameLookupMatchers = export type Bip32Entropy = { curve: SupportedCurve; path: string[]; + keyringId?: string; }; export type Bip44Entropy = { coinType: number; + keyringId?: string; }; export type RequestedSnap = { diff --git a/packages/snaps-simulation/src/methods/specifications.ts b/packages/snaps-simulation/src/methods/specifications.ts index 45c5ab4cf5..ed255dea93 100644 --- a/packages/snaps-simulation/src/methods/specifications.ts +++ b/packages/snaps-simulation/src/methods/specifications.ts @@ -30,9 +30,10 @@ export type PermissionSpecificationsHooks = { /** * A hook that returns the user's secret recovery phrase. * + * @param keyringId - The ID of the keyring to get the mnemonic for. * @returns The user's secret recovery phrase. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; }; export type GetPermissionSpecificationsOptions = { diff --git a/packages/snaps-simulation/src/middleware/internal-methods/accounts.ts b/packages/snaps-simulation/src/middleware/internal-methods/accounts.ts index 44e1328ead..00672f0a7d 100644 --- a/packages/snaps-simulation/src/middleware/internal-methods/accounts.ts +++ b/packages/snaps-simulation/src/middleware/internal-methods/accounts.ts @@ -10,7 +10,7 @@ import type { } from '@metamask/utils'; export type GetAccountsHandlerHooks = { - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; }; /** diff --git a/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts b/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts index 72859120de..2baf2f1714 100644 --- a/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts +++ b/packages/snaps-simulation/src/middleware/internal-methods/middleware.ts @@ -9,9 +9,10 @@ export type InternalMethodsMiddlewareHooks = { /** * A hook that returns the user's secret recovery phrase. * + * @param keyringId - The ID of the keyring to get the mnemonic for. * @returns The user's secret recovery phrase. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; }; const methodHandlers = { diff --git a/packages/snaps-simulation/src/simulation.ts b/packages/snaps-simulation/src/simulation.ts index cdffb66251..a7faa679c0 100644 --- a/packages/snaps-simulation/src/simulation.ts +++ b/packages/snaps-simulation/src/simulation.ts @@ -97,9 +97,10 @@ export type MiddlewareHooks = { /** * A hook that returns the user's secret recovery phrase. * + * @param keyringId - The ID of the keyring to get the mnemonic for. * @returns The user's secret recovery phrase. */ - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; /** * A hook that returns the Snap's auxiliary file for the given path. diff --git a/packages/snaps-simulator/src/features/simulation/middleware.ts b/packages/snaps-simulator/src/features/simulation/middleware.ts index 6834363a1b..60f101d547 100644 --- a/packages/snaps-simulator/src/features/simulation/middleware.ts +++ b/packages/snaps-simulator/src/features/simulation/middleware.ts @@ -21,7 +21,7 @@ export const methodHandlers = { /* eslint-enable @typescript-eslint/naming-convention */ export type MiscMiddlewareHooks = { - getMnemonic: () => Promise; + getMnemonic: (keyringId?: string) => Promise; }; /**