Skip to content

Commit 55bcad3

Browse files
committed
Merge remote-tracking branch 'origin/main' into hm/improve-ci
2 parents 1442b21 + 7ecc424 commit 55bcad3

16 files changed

Lines changed: 189 additions & 41 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/accounts-monorepo",
3-
"version": "117.0.0",
3+
"version": "118.0.0",
44
"private": true,
55
"description": "Monorepo for MetaMask accounts related packages",
66
"repository": {

packages/account-api/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
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+
## [1.1.0]
11+
12+
### Added
13+
14+
- Add `PrivateKeyAccount<Account>` type helper ([#590](https://github.com/MetaMask/accounts/pull/590))
15+
1016
### Changed
1117

1218
- Bump `@metamask/keyring-utils` from `^3.2.0` to `^3.3.1` ([#544](https://github.com/MetaMask/accounts/pull/544), [#546](https://github.com/MetaMask/accounts/pull/546))
@@ -168,7 +174,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
168174

169175
- Add `AccountGroup` and `AccountWallet` ([#307](https://github.com/MetaMask/accounts/pull/307))
170176

171-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.0.4...HEAD
177+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.1.0...HEAD
178+
[1.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.0.4...@metamask/account-api@1.1.0
172179
[1.0.4]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.0.3...@metamask/account-api@1.0.4
173180
[1.0.3]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.0.2...@metamask/account-api@1.0.3
174181
[1.0.2]: https://github.com/MetaMask/accounts/compare/@metamask/account-api@1.0.1...@metamask/account-api@1.0.2

packages/account-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/account-api",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "MetaMask Account API",
55
"keywords": [
66
"account",

packages/account-api/src/api/bip44.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
KeyringAccount,
33
KeyringAccountEntropyMnemonicOptions,
44
} from '@metamask/keyring-api';
5+
import { KeyringAccountEntropyTypeOption } from '@metamask/keyring-api';
56

67
/**
78
* BIP-44 compatible account type.
@@ -25,7 +26,10 @@ export function isBip44Account<Account extends KeyringAccount>(
2526
): account is Bip44Account<Account> {
2627
// To be BIP-44 compatible, we just check for the entropy type (the
2728
// the `entropy` shape will be inferred automatically).
28-
return account.options.entropy?.type === 'mnemonic';
29+
return (
30+
account.options.entropy?.type ===
31+
`${KeyringAccountEntropyTypeOption.Mnemonic}`
32+
);
2933
}
3034

3135
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import type {
2+
KeyringAccount,
3+
KeyringAccountEntropyPrivateKeyOptions,
4+
} from '@metamask/keyring-api';
5+
import {
6+
EthAccountType,
7+
EthMethod,
8+
EthScope,
9+
KeyringAccountEntropyTypeOption,
10+
} from '@metamask/keyring-api';
11+
12+
import { MOCK_HD_ACCOUNT_1 } from '../mocks';
13+
import type { PrivateKeyAccount } from './private-key';
14+
import { isPrivateKeyAccount, assertIsPrivateKeyAccount } from './private-key';
15+
16+
const MOCK_PRIVATE_KEY_ACCOUNT: PrivateKeyAccount<KeyringAccount> = {
17+
id: 'mock-pk-id-1',
18+
address: '0x123',
19+
options: {
20+
entropy: {
21+
type: KeyringAccountEntropyTypeOption.PrivateKey,
22+
},
23+
},
24+
methods: [
25+
EthMethod.PersonalSign,
26+
EthMethod.Sign,
27+
EthMethod.SignTransaction,
28+
EthMethod.SignTypedDataV1,
29+
EthMethod.SignTypedDataV3,
30+
EthMethod.SignTypedDataV4,
31+
],
32+
type: EthAccountType.Eoa,
33+
scopes: [EthScope.Eoa],
34+
};
35+
36+
describe('private-key', () => {
37+
describe('isPrivateKeyAccount', () => {
38+
it('returns true if the account is a private key account', () => {
39+
expect(isPrivateKeyAccount(MOCK_PRIVATE_KEY_ACCOUNT)).toBe(true);
40+
expect(() =>
41+
assertIsPrivateKeyAccount(MOCK_PRIVATE_KEY_ACCOUNT),
42+
).not.toThrow();
43+
});
44+
45+
it.each([
46+
{
47+
tc: 'missing type',
48+
options: {
49+
entropy: {
50+
// Missing type.
51+
},
52+
},
53+
},
54+
{
55+
tc: 'wrong type',
56+
options: {
57+
entropy: {
58+
type: KeyringAccountEntropyTypeOption.Mnemonic,
59+
},
60+
},
61+
},
62+
{
63+
tc: 'missing entropy',
64+
options: {},
65+
},
66+
])(
67+
'returns false if the account is not a private key account with: $tc',
68+
({ options }) => {
69+
const account = {
70+
...MOCK_HD_ACCOUNT_1,
71+
options: {
72+
entropy:
73+
// Force the error case here.
74+
options as unknown as KeyringAccountEntropyPrivateKeyOptions,
75+
},
76+
};
77+
78+
expect(isPrivateKeyAccount(account)).toBe(false);
79+
expect(() => assertIsPrivateKeyAccount(account)).toThrow(
80+
'Account is not private key account',
81+
);
82+
},
83+
);
84+
});
85+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { KeyringAccountEntropyTypeOption } from '@metamask/keyring-api';
2+
import type {
3+
KeyringAccount,
4+
KeyringAccountEntropyPrivateKeyOptions,
5+
} from '@metamask/keyring-api';
6+
7+
/**
8+
* Private key account type.
9+
*/
10+
export type PrivateKeyAccount<Account extends KeyringAccount> = Account & {
11+
// We force the option type for those accounts. (That's how we identify
12+
// if an account is private key compatible).
13+
options: {
14+
entropy: KeyringAccountEntropyPrivateKeyOptions;
15+
};
16+
};
17+
18+
/**
19+
* Checks if an account is a private key account.
20+
*
21+
* @param account - The account to be tested.
22+
* @returns True if the account is a private key account.
23+
*/
24+
export function isPrivateKeyAccount<Account extends KeyringAccount>(
25+
account: Account,
26+
): account is PrivateKeyAccount<Account> {
27+
// To be private key compatible, we just check for the entropy type (the
28+
// the `entropy` shape will be inferred automatically).
29+
return (
30+
account.options.entropy?.type ===
31+
`${KeyringAccountEntropyTypeOption.PrivateKey}`
32+
);
33+
}
34+
35+
/**
36+
* Asserts a keyring account is a private key account.
37+
*
38+
* @param account - Keyring account to check.
39+
* @throws If the keyring account is not a private key account.
40+
*/
41+
export function assertIsPrivateKeyAccount<Account extends KeyringAccount>(
42+
account: Account,
43+
): asserts account is PrivateKeyAccount<Account> {
44+
if (!isPrivateKeyAccount(account)) {
45+
throw new Error('Account is not private key account');
46+
}
47+
}

packages/keyring-eth-hd/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"devDependencies": {
8282
"@lavamoat/allow-scripts": "^3.2.1",
8383
"@lavamoat/preinstall-always-fail": "^2.1.0",
84-
"@metamask/account-api": "^1.0.4",
84+
"@metamask/account-api": "^1.1.0",
8585
"@metamask/auto-changelog": "^6.1.0",
8686
"@metamask/bip39": "^4.0.0",
8787
"@metamask/old-hd-keyring": "npm:@metamask/eth-hd-keyring@^4.0.1",

packages/keyring-eth-ledger-bridge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"@ledgerhq/types-cryptoassets": "^7.15.1",
9191
"@ledgerhq/types-devices": "^6.25.3",
9292
"@ledgerhq/types-live": "^6.52.0",
93-
"@metamask/account-api": "^1.0.4",
93+
"@metamask/account-api": "^1.1.0",
9494
"@metamask/auto-changelog": "^6.1.0",
9595
"@metamask/keyring-utils": "^3.3.1",
9696
"@metamask/utils": "^11.11.0",

packages/keyring-eth-qr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"@keystonehq/metamask-airgapped-keyring": "^0.15.2",
8989
"@lavamoat/allow-scripts": "^3.2.1",
9090
"@lavamoat/preinstall-always-fail": "^2.1.0",
91-
"@metamask/account-api": "^1.0.4",
91+
"@metamask/account-api": "^1.1.0",
9292
"@metamask/auto-changelog": "^6.1.0",
9393
"@types/hdkey": "^2.0.1",
9494
"@types/jest": "^29.5.12",

packages/keyring-eth-trezor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"@ethereumjs/common": "^4.4.0",
8686
"@lavamoat/allow-scripts": "^3.2.1",
8787
"@lavamoat/preinstall-always-fail": "^2.1.0",
88-
"@metamask/account-api": "^1.0.4",
88+
"@metamask/account-api": "^1.1.0",
8989
"@metamask/auto-changelog": "^6.1.0",
9090
"@ts-bridge/cli": "^0.6.3",
9191
"@types/ethereumjs-tx": "^1.0.1",

0 commit comments

Comments
 (0)