Skip to content

Commit 70ecb88

Browse files
authored
feat!: add scopes field to KeyringAccount (#5066)
## Explanation Testing the new `scopes` field for `KeyringAccount` ## References - MetaMask/accounts#101 - MetaMask/metamask-extension#29195 ## Changelog ### `@metamask/accounts-controller` - **CHANGED**: Bump `keyring-api` to `^13.0.0` - **CHANGED**: Bump `keyring-internal-api` to `^1.1.0` - **CHANGED**: **BREAKING:** Add new `scopes` field to `InternalAccount` - This new field wil automatically defaults to `eip155` (CAIP-2 chain ID namespace for EVM) for newly created EOA accounts (both Snap and "normal" accounts). ### `@metamask/assets-controllers` - **CHANGED**: Bump `keyring-internal-api` to `^1.1.0` ### `@metamask/chain-controller` - **CHANGED**: Bump `keyring-internal-api` to `^1.1.0` ### `@metamask/keyring-controller` - **CHANGED**: Bump `keyring-api` to `^13.0.0` - **CHANGED**: Bump `keyring-internal-api` to `^1.1.0` - **CHANGED**: Await for `generateRandomMnemonic` keyring calls - Not all keyrings uses an `async` implementation for this method, but `await`ing should cover both (synchronous and asynchronous) cases. - This change is required since the bump of `@metamask/utils@^11.0.1` that changed the `generateRandomMnemonic` function signature for the `EthKeyring` interface (coming from `@metamask/keyring-internal-api`. ### `@metamask/profile-sync-controller` - **CHANGED**: Bump `keyring-api` to `^13.0.0` - **CHANGED**: Bump `keyring-internal-api` to `^1.1.0` ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent dd040e5 commit 70ecb88

File tree

12 files changed

+108
-70
lines changed

12 files changed

+108
-70
lines changed

packages/accounts-controller/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"@ethereumjs/util": "^8.1.0",
5151
"@metamask/base-controller": "^7.1.0",
5252
"@metamask/eth-snap-keyring": "^7.0.0",
53-
"@metamask/keyring-api": "^12.0.0",
54-
"@metamask/keyring-internal-api": "^1.0.0",
53+
"@metamask/keyring-api": "^13.0.0",
54+
"@metamask/keyring-internal-api": "^1.1.0",
5555
"@metamask/snaps-sdk": "^6.7.0",
5656
"@metamask/snaps-utils": "^8.3.0",
5757
"@metamask/utils": "^11.0.1",

packages/accounts-controller/src/AccountsController.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
EthAccountType,
55
BtcMethod,
66
EthMethod,
7+
EthScopes,
8+
BtcScopes,
79
} from '@metamask/keyring-api';
810
import { KeyringTypes } from '@metamask/keyring-controller';
911
import type {
@@ -66,6 +68,7 @@ const mockAccount: InternalAccount = {
6668
options: {},
6769
methods: [...ETH_EOA_METHODS],
6870
type: EthAccountType.Eoa,
71+
scopes: [EthScopes.Namespace],
6972
metadata: {
7073
name: 'Account 1',
7174
keyring: { type: KeyringTypes.hd },
@@ -81,6 +84,7 @@ const mockAccount2: InternalAccount = {
8184
options: {},
8285
methods: [...ETH_EOA_METHODS],
8386
type: EthAccountType.Eoa,
87+
scopes: [EthScopes.Namespace],
8488
metadata: {
8589
name: 'Account 2',
8690
keyring: { type: KeyringTypes.hd },
@@ -95,6 +99,7 @@ const mockAccount3: InternalAccount = {
9599
options: {},
96100
methods: [...ETH_EOA_METHODS],
97101
type: EthAccountType.Eoa,
102+
scopes: [EthScopes.Namespace],
98103
metadata: {
99104
name: '',
100105
keyring: { type: KeyringTypes.snap },
@@ -114,6 +119,7 @@ const mockAccount4: InternalAccount = {
114119
options: {},
115120
methods: [...ETH_EOA_METHODS],
116121
type: EthAccountType.Eoa,
122+
scopes: [EthScopes.Namespace],
117123
metadata: {
118124
name: 'Custom Name',
119125
keyring: { type: KeyringTypes.snap },
@@ -191,24 +197,32 @@ function createExpectedInternalAccount({
191197
lastSelected?: number;
192198
nameLastUpdatedAt?: number;
193199
}): InternalAccount {
194-
const accountTypeToMethods = {
195-
[`${EthAccountType.Eoa}`]: [...Object.values(ETH_EOA_METHODS)],
196-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
197-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
198-
[`${EthAccountType.Erc4337}`]: [...Object.values(ETH_ERC_4337_METHODS)],
199-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
200-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
201-
[`${BtcAccountType.P2wpkh}`]: [...Object.values(BtcMethod)],
200+
const accountTypeToInfo: Record<
201+
string,
202+
{ methods: string[]; scopes: string[] }
203+
> = {
204+
[`${EthAccountType.Eoa}`]: {
205+
methods: [...Object.values(ETH_EOA_METHODS)],
206+
scopes: [EthScopes.Namespace],
207+
},
208+
[`${EthAccountType.Erc4337}`]: {
209+
methods: [...Object.values(ETH_ERC_4337_METHODS)],
210+
scopes: [EthScopes.Mainnet], // Assuming we are using mainnet for those Smart Accounts
211+
},
212+
[`${BtcAccountType.P2wpkh}`]: {
213+
methods: [...Object.values(BtcMethod)],
214+
scopes: [BtcScopes.Mainnet],
215+
},
202216
};
203217

204-
const methods =
205-
accountTypeToMethods[type as keyof typeof accountTypeToMethods];
218+
const { methods, scopes } = accountTypeToInfo[type];
206219

207-
const account = {
220+
const account: InternalAccount = {
208221
id,
209222
address,
210223
options: {},
211224
methods,
225+
scopes,
212226
type,
213227
metadata: {
214228
name,
@@ -217,7 +231,7 @@ function createExpectedInternalAccount({
217231
lastSelected: lastSelected || expect.any(Number),
218232
...(nameLastUpdatedAt && { nameLastUpdatedAt }),
219233
},
220-
} as InternalAccount;
234+
};
221235

222236
if (snapId) {
223237
account.metadata.snap = {

packages/accounts-controller/src/AccountsController.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SnapKeyring } from '@metamask/eth-snap-keyring';
88
import {
99
EthAccountType,
1010
EthMethod,
11+
EthScopes,
1112
isEvmAccountType,
1213
} from '@metamask/keyring-api';
1314
import { KeyringTypes } from '@metamask/keyring-controller';
@@ -203,6 +204,7 @@ export const EMPTY_ACCOUNT = {
203204
options: {},
204205
methods: [],
205206
type: EthAccountType.Eoa,
207+
scopes: [EthScopes.Namespace],
206208
metadata: {
207209
name: '',
208210
keyring: {
@@ -456,7 +458,7 @@ export class AccountsController extends BaseController<
456458
};
457459
// Do not remove this comment - This error is flaky: Comment out or restore the `ts-expect-error` directive below as needed.
458460
// See: https://github.com/MetaMask/utils/issues/168
459-
// @ts-expect-error Known issue - `Json` causes recursive error in immer `Draft`/`WritableDraft` types
461+
// // @ts-expect-error Known issue - `Json` causes recursive error in immer `Draft`/`WritableDraft` types
460462
currentState.internalAccounts.accounts[accountId] = internalAccount;
461463

462464
if (metadata.name) {
@@ -582,6 +584,7 @@ export class AccountsController extends BaseController<
582584
EthMethod.SignTypedDataV3,
583585
EthMethod.SignTypedDataV4,
584586
],
587+
scopes: [EthScopes.Namespace],
585588
type: EthAccountType.Eoa,
586589
metadata: {
587590
name: '',
@@ -656,6 +659,7 @@ export class AccountsController extends BaseController<
656659
EthMethod.SignTypedDataV3,
657660
EthMethod.SignTypedDataV4,
658661
],
662+
scopes: [EthScopes.Namespace],
659663
type: EthAccountType.Eoa,
660664
metadata: {
661665
name: this.#populateExistingMetadata(id, 'name') ?? '',

packages/assets-controllers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"@metamask/auto-changelog": "^3.4.4",
8181
"@metamask/ethjs-provider-http": "^0.3.0",
8282
"@metamask/keyring-controller": "^19.0.2",
83-
"@metamask/keyring-internal-api": "^1.0.0",
83+
"@metamask/keyring-internal-api": "^1.1.0",
8484
"@metamask/network-controller": "^22.1.1",
8585
"@metamask/preferences-controller": "^15.0.1",
8686
"@metamask/providers": "^18.1.1",

packages/chain-controller/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"dependencies": {
5050
"@metamask/base-controller": "^7.1.0",
5151
"@metamask/chain-api": "^0.1.0",
52-
"@metamask/keyring-internal-api": "^1.0.0",
52+
"@metamask/keyring-internal-api": "^1.1.0",
5353
"@metamask/keyring-utils": "^1.0.0",
5454
"@metamask/snaps-controllers": "^9.10.0",
5555
"@metamask/snaps-sdk": "^6.7.0",

packages/keyring-controller/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"@metamask/eth-hd-keyring": "^7.0.4",
5555
"@metamask/eth-sig-util": "^8.0.0",
5656
"@metamask/eth-simple-keyring": "^6.0.5",
57-
"@metamask/keyring-api": "^12.0.0",
58-
"@metamask/keyring-internal-api": "^1.0.0",
57+
"@metamask/keyring-api": "^13.0.0",
58+
"@metamask/keyring-internal-api": "^1.1.0",
5959
"@metamask/message-manager": "^11.0.3",
6060
"@metamask/utils": "^11.0.1",
6161
"async-mutex": "^0.5.0",

packages/keyring-controller/src/KeyringController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,13 @@ export class KeyringController extends BaseController<
21992199
);
22002200
}
22012201

2202-
keyring.generateRandomMnemonic();
2202+
// NOTE: Not all keyrings implement this method in a asynchronous-way. Using `await` for
2203+
// non-thenable will still be valid (despite not being really useful). It allows us to cover both
2204+
// cases and allow retro-compatibility too.
2205+
// FIXME: For some reason, it seems that eslint is complaining about this call being non-thenable
2206+
// even though it is... For now, we just disable it:
2207+
// eslint-disable-next-line @typescript-eslint/await-thenable
2208+
await keyring.generateRandomMnemonic();
22032209
await keyring.addAccounts(1);
22042210
}
22052211

packages/profile-sync-controller/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
},
102102
"dependencies": {
103103
"@metamask/base-controller": "^7.1.0",
104-
"@metamask/keyring-api": "^12.0.0",
104+
"@metamask/keyring-api": "^13.0.0",
105105
"@metamask/keyring-controller": "^19.0.2",
106106
"@metamask/network-controller": "^22.1.1",
107107
"@metamask/snaps-sdk": "^6.7.0",
@@ -117,7 +117,7 @@
117117
"@lavamoat/preinstall-always-fail": "^2.1.0",
118118
"@metamask/accounts-controller": "^20.0.2",
119119
"@metamask/auto-changelog": "^3.4.4",
120-
"@metamask/keyring-internal-api": "^1.0.0",
120+
"@metamask/keyring-internal-api": "^1.1.0",
121121
"@metamask/providers": "^18.1.1",
122122
"@metamask/snaps-controllers": "^9.10.0",
123123
"@types/jest": "^27.4.1",

packages/transaction-controller/src/TransactionController.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ const INTERNAL_ACCOUNT_MOCK: InternalAccount = {
388388
id: '58def058-d35f-49a1-a7ab-e2580565f6f5',
389389
address: ACCOUNT_MOCK,
390390
type: 'eip155:eoa',
391+
scopes: ['eip155'],
391392
options: {},
392393
methods: [],
393394
metadata: {

packages/transaction-controller/src/TransactionControllerIntegration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const createMockInternalAccount = ({
9595
options: {},
9696
methods: [],
9797
type: 'eip155:eoa',
98+
scopes: ['eip155'],
9899
metadata: {
99100
name,
100101
keyring: { type: 'HD Key Tree' },

packages/transaction-controller/src/helpers/IncomingTransactionHelper.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const CONTROLLER_ARGS_MOCK: ConstructorParameters<
2828
type: 'eip155:eoa' as const,
2929
options: {},
3030
methods: [],
31+
scopes: ['eip155'],
3132
metadata: {
3233
name: 'Account 1',
3334
keyring: { type: 'HD Key Tree' },

0 commit comments

Comments
 (0)