Skip to content

Commit 9cc2446

Browse files
Add simulation hooks for getMnemonicSeed
1 parent 59ee984 commit 9cc2446

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { mnemonicPhraseToBytes } from '@metamask/key-tree';
2+
3+
import { getGetMnemonicSeedImplementation } from './get-mnemonic-seed';
4+
import { DEFAULT_ALTERNATIVE_SRP, DEFAULT_SRP } from '../../constants';
5+
6+
describe('getGetMnemonicSeedImplementation', () => {
7+
it('returns the default mnemonic seed', async () => {
8+
const getMnemonicSeed = getGetMnemonicSeedImplementation();
9+
expect(await getMnemonicSeed()).toStrictEqual(
10+
mnemonicPhraseToBytes(DEFAULT_SRP),
11+
);
12+
13+
expect(await getMnemonicSeed('default')).toStrictEqual(
14+
mnemonicPhraseToBytes(DEFAULT_SRP),
15+
);
16+
});
17+
18+
it('returns the seed of the provided default mnemonic phrase', async () => {
19+
const getMnemonicSeed = getGetMnemonicSeedImplementation(
20+
DEFAULT_ALTERNATIVE_SRP,
21+
);
22+
expect(await getMnemonicSeed()).toStrictEqual(
23+
mnemonicPhraseToBytes(DEFAULT_ALTERNATIVE_SRP),
24+
);
25+
26+
expect(await getMnemonicSeed('default')).toStrictEqual(
27+
mnemonicPhraseToBytes(DEFAULT_ALTERNATIVE_SRP),
28+
);
29+
});
30+
31+
it('returns the alternative mnemonic seed', async () => {
32+
const getMnemonicSeed = getGetMnemonicSeedImplementation();
33+
expect(await getMnemonicSeed('alternative')).toStrictEqual(
34+
mnemonicPhraseToBytes(DEFAULT_ALTERNATIVE_SRP),
35+
);
36+
});
37+
38+
it('throws an error for an unknown entropy source', async () => {
39+
const getMnemonicSeed = getGetMnemonicSeedImplementation();
40+
await expect(getMnemonicSeed('unknown')).rejects.toThrow(
41+
'Entropy source with ID "unknown" not found.',
42+
);
43+
});
44+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { mnemonicToSeed } from '@metamask/key-tree';
2+
3+
import { DEFAULT_ALTERNATIVE_SRP, DEFAULT_SRP } from '../../constants';
4+
5+
/**
6+
* Get the implementation of the `getMnemonicSeed` method.
7+
*
8+
* @param defaultSecretRecoveryPhrase - The default secret recovery phrase to
9+
* use.
10+
* @returns The implementation of the `getMnemonicSeed` method.
11+
*/
12+
export function getGetMnemonicSeedImplementation(
13+
defaultSecretRecoveryPhrase: string = DEFAULT_SRP,
14+
) {
15+
return async (source?: string | undefined): Promise<Uint8Array> => {
16+
if (!source) {
17+
return mnemonicToSeed(defaultSecretRecoveryPhrase);
18+
}
19+
20+
switch (source) {
21+
case 'default':
22+
return mnemonicToSeed(defaultSecretRecoveryPhrase);
23+
case 'alternative':
24+
return mnemonicToSeed(DEFAULT_ALTERNATIVE_SRP);
25+
default:
26+
throw new Error(`Entropy source with ID "${source}" not found.`);
27+
}
28+
};
29+
}

packages/snaps-simulation/src/simulation.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
getGetEntropySourcesImplementation,
4545
getGetMnemonicImplementation,
4646
} from './methods/hooks';
47+
import { getGetMnemonicSeedImplementation } from './methods/hooks/get-mnemonic-seed';
4748
import { createJsonRpcEngine } from './middleware';
4849
import type { SimulationOptions, SimulationUserOptions } from './options';
4950
import { getOptions } from './options';
@@ -108,6 +109,14 @@ export type RestrictedMiddlewareHooks = {
108109
*/
109110
getMnemonic: (source?: string | undefined) => Promise<Uint8Array>;
110111

112+
/**
113+
* A hook that returns the seed derived from the user's secret recovery phrase.
114+
*
115+
* @param source - The entropy source to get the seed from.
116+
* @returns The seed.
117+
*/
118+
getMnemonicSeed: (source?: string | undefined) => Promise<Uint8Array>;
119+
111120
/**
112121
* A hook that returns whether the client is locked or not.
113122
*
@@ -382,6 +391,9 @@ export function getRestrictedHooks(
382391
): RestrictedMiddlewareHooks {
383392
return {
384393
getMnemonic: getGetMnemonicImplementation(options.secretRecoveryPhrase),
394+
getMnemonicSeed: getGetMnemonicSeedImplementation(
395+
options.secretRecoveryPhrase,
396+
),
385397
getIsLocked: () => false,
386398
getClientCryptography: () => ({}),
387399
};

packages/snaps-simulator/src/features/simulation/sagas.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Messenger } from '@metamask/base-controller';
22
import { createFetchMiddleware } from '@metamask/eth-json-rpc-middleware';
33
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
44
import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
5-
import { mnemonicPhraseToBytes } from '@metamask/key-tree';
5+
import { mnemonicPhraseToBytes, mnemonicToSeed } from '@metamask/key-tree';
66
import type { GenericPermissionController } from '@metamask/permission-controller';
77
import {
88
PermissionController,
@@ -122,6 +122,7 @@ export function* initSaga({ payload }: PayloadAction<string>) {
122122

123123
const sharedHooks = {
124124
getMnemonic: async () => mnemonicPhraseToBytes(srp),
125+
getMnemonicSeed: async () => mnemonicToSeed(srp),
125126
};
126127

127128
const permissionSpecifications = {

0 commit comments

Comments
 (0)