Skip to content

Commit 94360d0

Browse files
committed
refactor: align options type
1 parent e2435c6 commit 94360d0

File tree

11 files changed

+215
-192
lines changed

11 files changed

+215
-192
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/CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING:** The method signature for `signTypedData` has been changed ([#224](https://github.com/MetaMask/accounts/pull/224))
13+
- The method now accepts a `TypedDataV1` object when `SignTypedDataVersion.V1` is passed in the options, and `TypedMessage<Types>` when other versions are requested.
14+
1015
## [12.0.0]
1116

1217
### Changed
@@ -22,8 +27,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2227
### Changed
2328

2429
- **BREAKING:** The `HdKeyring` is not exported as default anymore ([#161](https://github.com/MetaMask/accounts/pull/161))
25-
- **BREAKING:** The method signature for `signTypedData` has been changed ([#224](https://github.com/MetaMask/accounts/pull/224))
26-
- The method now accepts a `TypedDataV1` object when `SignTypedDataVersion.V1` is passed in the options, and `TypedMessage<Types>` when other versions are requested.
2730

2831
## [10.0.1]
2932

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

+17-13
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

@@ -338,30 +341,31 @@ export class HdKeyring {
338341
* Sign a typed message using the private key of the specified account.
339342
* This method is compatible with the `eth_signTypedData` RPC method.
340343
*
341-
* @param withAccount - The address of the account.
342-
* @param typedData - The typed data to sign.
343-
* @param opts - The options for signing the message.
344+
* @param address - The address of the account.
345+
* @param data - The typed data to sign.
346+
* @param options - The options for signing the message.
344347
* @returns The signature of the message.
345348
*/
346349
async signTypedData<
347350
Version extends SignTypedDataVersion,
348351
Types extends MessageTypes,
349352
Options extends { version: Version },
350353
>(
351-
withAccount: Hex,
352-
typedData: Version extends 'V1' ? TypedDataV1 : TypedMessage<Types>,
353-
opts?: HDKeyringAccountSelectionOptions & Options,
354+
address: Hex,
355+
data: Version extends 'V1' ? TypedDataV1 : TypedMessage<Types>,
356+
options?: HDKeyringAccountSelectionOptions & Options,
354357
): Promise<string> {
355-
const options = opts ?? { version: SignTypedDataVersion.V1 };
358+
let { version } = options ?? { version: SignTypedDataVersion.V1 };
359+
356360
// Treat invalid versions as "V1"
357-
const version = Object.keys(SignTypedDataVersion).includes(options.version)
358-
? options.version
361+
version = Object.keys(SignTypedDataVersion).includes(version)
362+
? version
359363
: SignTypedDataVersion.V1;
360364

361-
const privateKey = this.#getPrivateKeyFor(withAccount, opts);
365+
const privateKey = this.#getPrivateKeyFor(address, options);
362366
return signTypedData({
363367
privateKey: Buffer.from(privateKey),
364-
data: typedData,
368+
data,
365369
version,
366370
});
367371
}
@@ -591,12 +595,12 @@ export class HdKeyring {
591595

592596
this.mnemonic = this.#mnemonicToUint8Array(mnemonic);
593597

594-
const seed = await mnemonicToSeed(
598+
this.seed = await mnemonicToSeed(
595599
this.mnemonic,
596600
'', // No passphrase
597601
this.#cryptographicFunctions,
598602
);
599-
this.hdWallet = HDKey.fromMasterSeed(seed);
603+
this.hdWallet = HDKey.fromMasterSeed(this.seed);
600604
this.root = this.hdWallet.derive(this.hdPath);
601605
}
602606

packages/keyring-eth-ledger-bridge/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING:** The `signTypedData` method now requires `SignTypedDataVersion.V4` as version for the `options` argument ([#224](https://github.com/MetaMask/accounts/pull/224)).
13+
1014
## [10.0.0]
1115

1216
### Changed
@@ -35,7 +39,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3539
- The `signPersonalMessage` method now accepts an `Hex` typed value as the `withAccount` parameter.
3640
- The `signTypedData` method now accepts an `Hex` typed value as the `withAccount` parameter.
3741
- The `unlockAccountByAddress` method now accepts an `Hex` typed value as the `address` parameter.
38-
- **BREAKING:** The `signTypedData` method now requires `SignTypedDataVersion` as version for the `options` argument ([#224](https://github.com/MetaMask/accounts/pull/224)).
3942

4043
### Removed
4144

packages/keyring-eth-ledger-bridge/src/ledger-keyring.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,9 @@ export class LedgerKeyring implements Keyring {
494494
}
495495

496496
async signTypedData<
497-
Version extends SignTypedDataVersion,
497+
Version extends SignTypedDataVersion.V4,
498498
Types extends MessageTypes,
499-
Options extends { version?: Version },
499+
Options extends { version: Version },
500500
>(
501501
withAccount: Hex,
502502
data: TypedMessage<Types>,

packages/keyring-eth-simple/CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING:** The method signature for `signTypedData` has been changed ([#224](https://github.com/MetaMask/accounts/pull/224))
13+
- The method now accepts a `TypedDataV1` object when `SignTypedDataVersion.V1` is passed in the options, and `TypedMessage<Types>` when other versions are requested.
14+
- The `options` argument type has been changed to `{ version: SignTypedDataVersion } | undefined`.
15+
1016
## [10.0.0]
1117

1218
### Changed
@@ -23,8 +29,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2329

2430
- **BREAKING:** The `SimpleKeyring` class now implements `Keyring` from `@metamask/keyring-utils` ([#217](https://github.com/MetaMask/accounts/pull/217))
2531
- The `deserialize` method now requires a `string[]` argument.
26-
- **BREAKING:** The method signature for `signTypedData` has been changed ([#224](https://github.com/MetaMask/accounts/pull/224))
27-
- The method now accepts a `TypedDataV1` object when `SignTypedDataVersion.V1` is passed in the options, and `TypedMessage<Types>` when other versions are requested.
2832

2933
## [8.1.1]
3034

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,21 @@ export default class SimpleKeyring implements Keyring {
160160
async signTypedData<
161161
Version extends SignTypedDataVersion,
162162
Types extends MessageTypes,
163-
Options extends { version?: Version } & KeyringOpt,
163+
Options extends { version: Version } & KeyringOpt,
164164
>(
165165
address: Hex,
166-
typedData: Version extends 'V1' ? TypedDataV1 : TypedMessage<Types>,
167-
opts?: Options,
166+
data: Version extends 'V1' ? TypedDataV1 : TypedMessage<Types>,
167+
options?: Options,
168168
): Promise<string> {
169-
const options = opts ?? { version: SignTypedDataVersion.V1 };
170-
// Treat invalid versions as "V1"
171-
let version = SignTypedDataVersion.V1;
169+
let { version } = options ?? { version: SignTypedDataVersion.V1 };
172170

173-
if (options.version && isSignTypedDataVersion(options.version)) {
174-
version = SignTypedDataVersion[options.version];
171+
// Treat invalid versions as "V1"
172+
if (!isSignTypedDataVersion(version)) {
173+
version = SignTypedDataVersion.V1;
175174
}
176175

177-
const privateKey = this.#getPrivateKeyFor(address, opts);
178-
return signTypedData({ privateKey, data: typedData, version });
176+
const privateKey = this.#getPrivateKeyFor(address, options);
177+
return signTypedData({ privateKey, data, version });
179178
}
180179

181180
// get public key for nacl

packages/keyring-eth-trezor/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING:** The method signature for `signTypedData` has been changed ([#224](https://github.com/MetaMask/accounts/pull/224))
13+
- The `options` argument type has been changed to `{ version: SignTypedDataVersion.V3 | SignTypedDataVersion.V4 } | undefined`.
14+
- The `options.version` argument type has been restricted to accept `SignTypedDataVersion.V3 | SignTypedDataVersion.V4` ([#224](https://github.com/MetaMask/accounts/pull/224))
15+
1016
## [8.0.0]
1117

1218
### Changed
@@ -35,7 +41,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3541
- The `signPersonalMessage` method now accepts an `Hex` typed value as the `withAccount` parameter.
3642
- The `signTypedData` method now accepts an `Hex` typed value as the `withAccount` parameter.
3743
- The `unlockAccountByAddress` method now accepts an `Hex` typed value as the `address` parameter.
38-
- **BREAKING:** The `signTypedData` `options.version` argument type has been restricted to accept `SignTypedDataVersion.V3 | SignTypedDataVersion.V4` ([#224](https://github.com/MetaMask/accounts/pull/224))
3944

4045
### Removed
4146

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,14 @@ export class TrezorKeyring implements Keyring {
462462
async signTypedData<
463463
Version extends SignTypedDataVersion.V3 | SignTypedDataVersion.V4,
464464
Types extends MessageTypes,
465-
Options extends { version?: Version },
465+
Options extends { version: Version },
466466
>(
467467
address: Hex,
468468
data: TypedMessage<Types>,
469-
{ version }: Options,
469+
options?: Options,
470470
): Promise<string> {
471+
const { version } = options ?? { version: SignTypedDataVersion.V4 };
472+
471473
const dataWithHashes = transformTypedData(
472474
data,
473475
version === SignTypedDataVersion.V4,

0 commit comments

Comments
 (0)