Skip to content

Commit 3dfa946

Browse files
authored
Merge branch 'main' into mikesposito/keyring-typed-message-type
2 parents 0952d0a + 9e42021 commit 3dfa946

File tree

4 files changed

+167
-161
lines changed

4 files changed

+167
-161
lines changed

.github/workflows/security-code-scanner.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
name: 'MetaMask Security Code Scanner'
1+
name: MetaMask Security Code Scanner
22

33
on:
44
push:
5-
branches: ['main']
5+
branches:
6+
- main
67
pull_request:
7-
branches: ['main']
8+
branches:
9+
- main
10+
workflow_dispatch:
811

912
jobs:
1013
run-security-scan:

packages/keyring-eth-hd/src/hd-keyring.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import {
1919
type MessageTypes,
2020
type EIP7702Authorization,
2121
} from '@metamask/eth-sig-util';
22+
import { mnemonicPhraseToBytes } from '@metamask/key-tree';
2223
import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';
2324
import { assert, bytesToHex, hexToBytes, type Hex } from '@metamask/utils';
2425
import { webcrypto } from 'crypto';
26+
// eslint-disable-next-line n/no-sync
27+
import { mnemonicToSeedSync } from 'ethereum-cryptography/bip39';
2528
import { keccak256 } from 'ethereum-cryptography/keccak';
2629
// eslint-disable-next-line @typescript-eslint/naming-convention
2730
import OldHdKeyring from 'old-hd-keyring';
@@ -37,6 +40,10 @@ const sampleMnemonic =
3740
const firstAcct = '0x1c96099350f13d558464ec79b9be4445aa0ef579';
3841
const secondAcct = '0x1b00aed43a693f3a957f9feb5cc08afa031e37a0';
3942

43+
const sampleMnemonicBytes = mnemonicPhraseToBytes(sampleMnemonic);
44+
// eslint-disable-next-line n/no-sync
45+
const sampleMnemonicSeed = mnemonicToSeedSync(sampleMnemonic);
46+
4047
const notKeyringAddress = '0xbD20F6F5F1616947a39E11926E78ec94817B3931';
4148

4249
const getAddressAtIndex = (keyring: HdKeyring, index: number): Hex => {
@@ -175,6 +182,8 @@ describe('hd-keyring', () => {
175182
const accounts = keyring.getAccounts();
176183
expect(accounts[0]).toStrictEqual(firstAcct);
177184
expect(accounts[1]).toStrictEqual(secondAcct);
185+
expect(keyring.mnemonic).toStrictEqual(sampleMnemonicBytes);
186+
expect(keyring.seed).toStrictEqual(sampleMnemonicSeed);
178187
});
179188

180189
it('deserializes with a typeof buffer mnemonic', async () => {
@@ -188,6 +197,8 @@ describe('hd-keyring', () => {
188197
const accounts = keyring.getAccounts();
189198
expect(accounts[0]).toStrictEqual(firstAcct);
190199
expect(accounts[1]).toStrictEqual(secondAcct);
200+
expect(keyring.mnemonic).toStrictEqual(sampleMnemonicBytes);
201+
expect(keyring.seed).toStrictEqual(sampleMnemonicSeed);
191202
});
192203

193204
it('deserializes with a typeof Uint8Array mnemonic', async () => {
@@ -207,6 +218,8 @@ describe('hd-keyring', () => {
207218
const accounts = keyring.getAccounts();
208219
expect(accounts[0]).toStrictEqual(firstAcct);
209220
expect(accounts[1]).toStrictEqual(secondAcct);
221+
expect(keyring.mnemonic).toStrictEqual(sampleMnemonicBytes);
222+
expect(keyring.seed).toStrictEqual(sampleMnemonicSeed);
210223
});
211224

212225
it('deserializes using custom cryptography', async () => {
@@ -251,6 +264,8 @@ describe('hd-keyring', () => {
251264
const accounts = keyring.getAccounts();
252265
expect(accounts[0]).toStrictEqual(firstAcct);
253266
expect(accounts[1]).toStrictEqual(secondAcct);
267+
expect(keyring.mnemonic).toStrictEqual(sampleMnemonicBytes);
268+
expect(keyring.seed).toStrictEqual(sampleMnemonicSeed);
254269
expect(cryptographicFunctions.pbkdf2Sha512).toHaveBeenCalledTimes(1);
255270
});
256271

packages/keyring-eth-hd/src/hd-keyring.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export class HdKeyring {
9090

9191
mnemonic?: Uint8Array | null;
9292

93+
seed?: Uint8Array | null;
94+
9395
root?: HDKey | null;
9496

9597
hdWallet?: HDKey;
@@ -154,6 +156,7 @@ export class HdKeyring {
154156
}
155157
this.#wallets = [];
156158
this.mnemonic = null;
159+
this.seed = null;
157160
this.root = null;
158161
this.hdPath = opts.hdPath ?? hdPathString;
159162

@@ -592,12 +595,12 @@ export class HdKeyring {
592595

593596
this.mnemonic = this.#mnemonicToUint8Array(mnemonic);
594597

595-
const seed = await mnemonicToSeed(
598+
this.seed = await mnemonicToSeed(
596599
this.mnemonic,
597600
'', // No passphrase
598601
this.#cryptographicFunctions,
599602
);
600-
this.hdWallet = HDKey.fromMasterSeed(seed);
603+
this.hdWallet = HDKey.fromMasterSeed(this.seed);
601604
this.root = this.hdWallet.derive(this.hdPath);
602605
}
603606

0 commit comments

Comments
 (0)