Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/keyring-api/src/api/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('api', () => {
scopes: [scope],
};
expect(() => assert(account, KeyringAccountStruct)).toThrow(
`At path: scopes.0 -- Expected the value to satisfy a union of \`string | string\`, but received: "${scope}"`,
`At path: scopes.0 -- Expected a string matching \`/^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/\` but received "${scope}"`,
);
});
});
Expand Down
19 changes: 4 additions & 15 deletions packages/keyring-api/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { AccountIdStruct, object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import {
nonempty,
array,
enums,
record,
string,
union,
} from '@metamask/superstruct';
import {
CaipChainIdStruct,
CaipNamespaceStruct,
JsonStruct,
} from '@metamask/utils';
import { nonempty, array, enums, record, string } from '@metamask/superstruct';
import { CaipChainIdStruct, JsonStruct } from '@metamask/utils';

/**
* Supported Ethereum account types.
Expand Down Expand Up @@ -74,9 +63,9 @@ export const KeyringAccountStruct = object({
address: string(),

/**
* Account supported scopes (CAIP-2 chain IDs or CAIP-2 namespaces).
* Account supported scopes (CAIP-2 chain IDs).
*/
scopes: nonempty(array(union([CaipNamespaceStruct, CaipChainIdStruct]))),
scopes: nonempty(array(CaipChainIdStruct)),

/**
* Account options.
Expand Down
1 change: 0 additions & 1 deletion packages/keyring-api/src/btc/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Scopes for Bitcoin account type. See {@link KeyringAccount.scopes}.
*/
export enum BtcScope {
Namespace = 'bip122',
Mainnet = 'bip122:000000000019d6689c085ae165831e93',
Testnet = 'bip122:000000000933ea01ad0ee984209779ba',
Testnet4 = 'bip122:00000000da84f2bafbbc53dee25a72ae',
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/eth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Scopes for EVM account type. See {@link KeyringAccount.scopes}.
*/
export enum EthScope {
Namespace = 'eip155',
Eoa = 'eip155:0',
Mainnet = 'eip155:1',
Testnet = 'eip155:11155111',
}
4 changes: 2 additions & 2 deletions packages/keyring-api/src/eth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export const EthEoaAccountStruct = object({
type: literal(`${EthAccountType.Eoa}`),

/**
* Account scopes (must be ['eip155']).
* Account scopes (must be ['eip155:0']).
*/
scopes: nonempty(array(literal(EthScope.Namespace))),
scopes: nonempty(array(literal(EthScope.Eoa))),

/**
* Account supported methods.
Expand Down
10 changes: 5 additions & 5 deletions packages/keyring-api/src/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
},
},
Expand All @@ -40,7 +40,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
},
},
Expand All @@ -58,7 +58,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
},
displayConfirmation: true,
Expand All @@ -79,7 +79,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
},
},
Expand All @@ -97,7 +97,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
},
},
Expand Down
1 change: 0 additions & 1 deletion packages/keyring-api/src/sol/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Scopes for Solana account type. See {@link KeyringAccount.scopes}.
*/
export enum SolScope {
Namespace = 'solana',
Devnet = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
Mainnet = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
Testnet = 'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',
Expand Down
14 changes: 7 additions & 7 deletions packages/keyring-internal-api/src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InternalAccountStruct } from '.';

describe('InternalAccount', () => {
it.each([
{ type: 'eip155:eoa', address: '0x000', scopes: ['eip155'] },
{ type: 'eip155:eoa', address: '0x000', scopes: ['eip155:0'] },
{
type: 'bip122:p2wpkh',
address: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('InternalAccount', () => {
address: '0x000',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
metadata: {
keyring: {},
Expand All @@ -57,7 +57,7 @@ describe('InternalAccount', () => {
address: '0x000',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
metadata: {
name: 'Account 1',
Expand All @@ -76,7 +76,7 @@ describe('InternalAccount', () => {
address: '0x000',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
};

Expand Down Expand Up @@ -105,7 +105,7 @@ describe('InternalAccount', () => {
address: '0x000',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
metadata: {
keyring: {
Expand All @@ -129,7 +129,7 @@ describe('InternalAccount', () => {
options: {},
methods: [],
type: 'eip155:eoa',
scopes: ['eip155'],
scopes: ['eip155:0'],
metadata: {
keyring: {
type: 'Test Keyring',
Expand All @@ -155,7 +155,7 @@ describe('InternalAccount', () => {
address: '0x000',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
metadata: {
keyring: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('KeyringInternalSnapClient', () => {
address: '0xE9A74AACd7df8112911ca93260fC5a046f8a64Ae',
options: {},
methods: [],
scopes: ['eip155'],
scopes: ['eip155:0'],
type: 'eip155:eoa',
},
];
Expand Down
43 changes: 29 additions & 14 deletions packages/keyring-snap-bridge/src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
SolScope,
} from '@metamask/keyring-api';
import type { SnapId } from '@metamask/snaps-sdk';
import { toCaipChainId } from '@metamask/utils';
import { KnownCaipNamespace, toCaipChainId } from '@metamask/utils';

import type { KeyringState } from '.';
import { SnapKeyring } from '.';
Expand Down Expand Up @@ -104,23 +104,23 @@ describe('SnapKeyring', () => {
address: '0xC728514Df8A7F9271f4B7a4dd2Aa6d2D723d3eE3'.toLowerCase(),
options: {},
methods: ETH_EOA_METHODS,
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
};
const ethEoaAccount2 = {
id: '33c96b60-2237-488e-a7bb-233576f3d22f',
address: '0x34b13912eAc00152bE0Cb409A301Ab8E55739e63'.toLowerCase(),
options: {},
methods: ETH_EOA_METHODS,
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
};
const ethEoaAccount3 = {
id: 'c6697bcf-5710-4751-a1cb-340e4b50617a',
address: '0xf7bDe8609231033c69E502C08f85153f8A1548F2'.toLowerCase(),
options: {},
methods: ETH_EOA_METHODS,
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
};
const ethErc4337Account = {
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('SnapKeyring', () => {
methods: [],
// For unknown accounts, we consider them as EVM EOA for now, so just re-use the
// same scopes.
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
// This should not be really possible to create such account, but since we potentially
// migrate data upon the Snap keyring initialization, we want to cover edge-cases
// like this one to avoid crashing and blocking everything...
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('SnapKeyring', () => {
id: 'b05d918a-b37c-497a-bb28-3d15c0d56b7a',
options: {},
methods: ETH_EOA_METHODS,
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
type: EthAccountType.Eoa,
// Even checksummed address will be lower-cased by the bridge.
address: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('SnapKeyring', () => {
metadata: expect.any(Object),
// By default, new EVM accounts will have this scopes if it not provided
// during the account creation flow.
scopes: [EthScope.Namespace],
scopes: [EthScope.Eoa],
});
});

Expand Down Expand Up @@ -707,7 +707,7 @@ describe('SnapKeyring', () => {

const keyringAccounts = keyring.listAccounts();
expect(keyringAccounts.length).toBeGreaterThan(0);
expect(keyringAccounts[0]?.scopes).toStrictEqual([EthScope.Namespace]);
expect(keyringAccounts[0]?.scopes).toStrictEqual([EthScope.Eoa]);
});

it('updates a ERC4337 account with the no scope will throw an error', async () => {
Expand Down Expand Up @@ -1044,7 +1044,7 @@ describe('SnapKeyring', () => {

it('unknown v1 accounts scopes defaults to EOA scopes', () => {
expect(getScopesForAccountV1(unknownAccount)).toStrictEqual([
EthScope.Namespace,
EthScope.Eoa,
]);
});
});
Expand Down Expand Up @@ -1472,7 +1472,10 @@ describe('SnapKeyring', () => {
method: 'keyring_submitRequest',
params: {
id: expect.any(String),
scope: toCaipChainId(EthScope.Namespace, executionContext.chainId),
scope: toCaipChainId(
KnownCaipNamespace.Eip155,
executionContext.chainId,
),
account: ethErc4337Account.id,
request: {
method: 'eth_prepareUserOperation',
Expand Down Expand Up @@ -1525,7 +1528,10 @@ describe('SnapKeyring', () => {
method: 'keyring_submitRequest',
params: {
id: expect.any(String),
scope: toCaipChainId(EthScope.Namespace, executionContext.chainId),
scope: toCaipChainId(
KnownCaipNamespace.Eip155,
executionContext.chainId,
),
account: ethErc4337Account.id,
request: {
method: 'eth_patchUserOperation',
Expand Down Expand Up @@ -1574,7 +1580,10 @@ describe('SnapKeyring', () => {
method: 'keyring_submitRequest',
params: {
id: expect.any(String),
scope: toCaipChainId(EthScope.Namespace, executionContext.chainId),
scope: toCaipChainId(
KnownCaipNamespace.Eip155,
executionContext.chainId,
),
account: ethErc4337Account.id,
request: {
method: 'eth_signUserOperation',
Expand Down Expand Up @@ -1799,7 +1808,10 @@ describe('SnapKeyring', () => {
method: 'keyring_submitRequest',
params: {
id: expect.any(String),
scope: toCaipChainId(EthScope.Namespace, executionContext.chainId),
scope: toCaipChainId(
KnownCaipNamespace.Eip155,
executionContext.chainId,
),
account: ethErc4337Account.id,
request: {
method: 'eth_prepareUserOperation',
Expand Down Expand Up @@ -1866,7 +1878,10 @@ describe('SnapKeyring', () => {
method: 'keyring_submitRequest',
params: {
id: expect.any(String),
scope: toCaipChainId(EthScope.Namespace, executionContext.chainId),
scope: toCaipChainId(
KnownCaipNamespace.Eip155,
executionContext.chainId,
),
account: ethErc4337Account.id,
request: {
method: 'eth_patchUserOperation',
Expand Down
15 changes: 10 additions & 5 deletions packages/keyring-snap-bridge/src/migrations/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@metamask/keyring-api';
import { isBtcMainnetAddress } from '@metamask/keyring-utils';
import { is } from '@metamask/superstruct';
import type { CaipChainId } from '@metamask/utils';

import {
assertKeyringAccount,
Expand All @@ -35,12 +36,16 @@ export function isAccountV1(
* @param accountV1 - A v1 account.
* @returns The list of scopes for that accounts.
*/
export function getScopesForAccountV1(accountV1: KeyringAccountV1): string[] {
export function getScopesForAccountV1(
accountV1: KeyringAccountV1,
): CaipChainId[] {
switch (accountV1.type) {
case EthAccountType.Eoa: {
// EVM EOA account are compatible with any EVM networks, and we use CAIP-2
// namespaces when the scope relates to ALL chains (from that namespace).
return [EthScope.Namespace];
// EVM EOA account are compatible with any EVM networks, we use the
// 'eip155:0' scope as defined in the EVM CAIP-10 namespaces.
//
// See: https://namespaces.chainagnostic.org/eip155/caip10
return [EthScope.Eoa];
}
case EthAccountType.Erc4337: {
// EVM Erc4337 account
Expand All @@ -64,7 +69,7 @@ export function getScopesForAccountV1(accountV1: KeyringAccountV1): string[] {
}
default:
// We re-use EOA scopes if we don't know what to do for now.
return [EthScope.Namespace];
return [EthScope.Eoa];
}
}

Expand Down
Loading
Loading