Skip to content

Commit

Permalink
feat(multi-srp): add keyringId to getMnemonic fn
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykLucka committed Jan 13, 2025
1 parent 6861362 commit 436f4e8
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 20 deletions.
7 changes: 4 additions & 3 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* A hook to get dynamic feature flags at runtime.
Expand Down Expand Up @@ -826,7 +827,7 @@ export class SnapController extends BaseController<

#encryptor: ExportableKeyEncryptor;

#getMnemonic: () => Promise<Uint8Array>;
#getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

#getFeatureFlags: () => DynamicFeatureFlags;

Expand Down
7 changes: 4 additions & 3 deletions packages/snaps-rpc-methods/src/restricted/getBip32Entropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* Waits for the extension to be unlocked.
Expand Down Expand Up @@ -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(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* Waits for the extension to be unlocked.
Expand Down Expand Up @@ -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(),
});

Expand Down
11 changes: 8 additions & 3 deletions packages/snaps-rpc-methods/src/restricted/getBip44Entropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* Waits for the extension to be unlocked.
Expand Down Expand Up @@ -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(),
);
Expand Down
7 changes: 4 additions & 3 deletions packages/snaps-rpc-methods/src/restricted/getEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* Waits for the extension to be unlocked.
Expand Down Expand Up @@ -130,7 +131,7 @@ function getEntropyImplementation({
);

await getUnlockPromise(true);
const mnemonicPhrase = await getMnemonic();
const mnemonicPhrase = await getMnemonic(params.keyringId);

return deriveEntropy({
input: origin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Bip32Entropy } from '../permissions';
* to `false`.
*/
export type GetBip32PublicKeyParams = Bip32Entropy & {
keyringId?: string;
compressed?: boolean;
};

Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-sdk/src/types/methods/get-entropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-sdk/src/types/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-simulation/src/methods/specifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;
};

export type GetPermissionSpecificationsOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from '@metamask/utils';

export type GetAccountsHandlerHooks = {
getMnemonic: () => Promise<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;
};

const methodHandlers = {
Expand Down
3 changes: 2 additions & 1 deletion packages/snaps-simulation/src/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ export type RestrictedMiddlewareHooks = {
/**
* 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<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;

/**
* A hook that returns whether the client is locked or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const methodHandlers = {
/* eslint-enable @typescript-eslint/naming-convention */

export type MiscMiddlewareHooks = {
getMnemonic: () => Promise<Uint8Array>;
getMnemonic: (keyringId?: string) => Promise<Uint8Array>;
};

/**
Expand Down

0 comments on commit 436f4e8

Please sign in to comment.