Skip to content

Commit

Permalink
run migration 66/67 for old scope values
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Feb 13, 2025
1 parent 2955e78 commit 5cc4e95
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/store/migrations/066.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,24 @@ describe('migration #66', () => {
// @ts-expect-error Testing invalid scope type
scopes: undefined,
},
'invalid-4': {
id: 'evm-1',
type: 'eip155:eoa',
address: '0x123',
options: {},
metadata: {
name: 'Account 1',
keyring: { type: 'HD Key Tree' },
importTime: Date.now(),
},
methods: [
EthMethod.PersonalSign,
EthMethod.SignTransaction,
EthMethod.SignTypedDataV4,
],
// Invalid scope value that's not in any enum
scopes: ['some-random-scope' as `${string}:${string}`],
},
},
},
},
Expand All @@ -349,6 +367,7 @@ describe('migration #66', () => {
expect(accounts['invalid-1']?.scopes).toEqual([EthScope.Eoa]);
expect(accounts['invalid-2']?.scopes).toEqual([EthScope.Eoa]);
expect(accounts['invalid-3']?.scopes).toEqual([EthScope.Eoa]);
expect(accounts['invalid-4']?.scopes).toEqual([EthScope.Eoa]);
});

it('logs unknown account types to Sentry', () => {
Expand Down
13 changes: 12 additions & 1 deletion app/store/migrations/066.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import {
} from '@metamask/keyring-api';
import { captureException } from '@sentry/react-native';

// Helper to check if a scope is a valid enum value
function isValidScope(scope: string): boolean {
return (
Object.values(EthScope).includes(scope as EthScope) ||
Object.values(BtcScope).includes(scope as BtcScope) ||
Object.values(SolScope).includes(scope as SolScope)
);
}

function getScopesForAccountType(
accountType: string,
migrationNumber: number,
Expand Down Expand Up @@ -90,7 +99,9 @@ export function migration66(state: unknown, migrationNumber: number) {
hasProperty(account, 'scopes') &&
Array.isArray(account.scopes) &&
account.scopes.length > 0 &&
account.scopes.every((scope) => typeof scope === 'string')
account.scopes.every(
(scope) => typeof scope === 'string' && isValidScope(scope),
)
) {
continue;
}
Expand Down
101 changes: 101 additions & 0 deletions app/store/migrations/067.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,105 @@ describe('migration #67', () => {
),
);
});

it('updates accounts that were previously migrated with old scope values', () => {
// Old scope values for testing migration from previous state
const OLD_ETH_NAMESPACE_SCOPE = 'eip155' as `${string}:${string}`;
const OLD_SOL_NAMESPACE_SCOPE = 'solana' as `${string}:${string}`;
const OLD_BTC_NAMESPACE_SCOPE = 'bip122' as `${string}:${string}`;

const stateWithOldScopes: StateType = {
engine: {
backgroundState: {
AccountsController: {
internalAccounts: {
selectedAccount: 'evm-1',
accounts: {
'evm-1': {
id: 'evm-1',
type: 'eip155:eoa',
address: '0x123',
options: {},
metadata: {
name: 'Account 1',
keyring: { type: 'HD Key Tree' },
importTime: Date.now(),
},
methods: [
EthMethod.PersonalSign,
EthMethod.SignTransaction,
EthMethod.SignTypedDataV4,
],
// This represents the old scope value from migration 66
scopes: [OLD_ETH_NAMESPACE_SCOPE],
},
'evm-2': {
id: 'evm-2',
type: 'eip155:erc4337',
address: '0x456',
options: {},
metadata: {
name: 'Account 2',
keyring: { type: 'HD Key Tree' },
importTime: Date.now(),
},
methods: [
EthMethod.PersonalSign,
EthMethod.SignTransaction,
EthMethod.SignTypedDataV4,
],
// This represents the old scope value from migration 66
scopes: [OLD_ETH_NAMESPACE_SCOPE],
},
'sol-1': {
id: 'sol-1',
type: 'solana:data-account',
address: 'solana123',
options: {},
metadata: {
name: 'Solana Account',
keyring: { type: 'HD Key Tree' },
importTime: Date.now(),
},
methods: [],
// Old Solana namespace scope
scopes: [OLD_SOL_NAMESPACE_SCOPE],
},
'btc-1': {
id: 'btc-1',
type: 'bip122:p2wpkh',
address: 'bc1abc',
options: {},
metadata: {
name: 'BTC Account',
keyring: { type: 'HD Key Tree' },
importTime: Date.now(),
},
methods: [],
// Old BTC namespace scope
scopes: [OLD_BTC_NAMESPACE_SCOPE],
},
},
},
},
},
},
};

const stateCopy = JSON.parse(JSON.stringify(stateWithOldScopes));
const result = migration(stateCopy) as StateType;
const accounts =
result.engine.backgroundState.AccountsController.internalAccounts
.accounts;

// Check that old scope values were updated to new ones
expect(accounts['evm-1']?.scopes).toEqual([EthScope.Eoa]);
expect(accounts['evm-2']?.scopes).toEqual([EthScope.Eoa]);
expect(accounts['sol-1']?.scopes).toEqual([
SolScope.Mainnet,
SolScope.Testnet,
SolScope.Devnet,
]);
expect(accounts['btc-1']?.scopes).toEqual([BtcScope.Mainnet]);
});
});

0 comments on commit 5cc4e95

Please sign in to comment.