Skip to content

Commit 5cc4e95

Browse files
committed
run migration 66/67 for old scope values
1 parent 2955e78 commit 5cc4e95

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

app/store/migrations/066.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,24 @@ describe('migration #66', () => {
333333
// @ts-expect-error Testing invalid scope type
334334
scopes: undefined,
335335
},
336+
'invalid-4': {
337+
id: 'evm-1',
338+
type: 'eip155:eoa',
339+
address: '0x123',
340+
options: {},
341+
metadata: {
342+
name: 'Account 1',
343+
keyring: { type: 'HD Key Tree' },
344+
importTime: Date.now(),
345+
},
346+
methods: [
347+
EthMethod.PersonalSign,
348+
EthMethod.SignTransaction,
349+
EthMethod.SignTypedDataV4,
350+
],
351+
// Invalid scope value that's not in any enum
352+
scopes: ['some-random-scope' as `${string}:${string}`],
353+
},
336354
},
337355
},
338356
},
@@ -349,6 +367,7 @@ describe('migration #66', () => {
349367
expect(accounts['invalid-1']?.scopes).toEqual([EthScope.Eoa]);
350368
expect(accounts['invalid-2']?.scopes).toEqual([EthScope.Eoa]);
351369
expect(accounts['invalid-3']?.scopes).toEqual([EthScope.Eoa]);
370+
expect(accounts['invalid-4']?.scopes).toEqual([EthScope.Eoa]);
352371
});
353372

354373
it('logs unknown account types to Sentry', () => {

app/store/migrations/066.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import {
1111
} from '@metamask/keyring-api';
1212
import { captureException } from '@sentry/react-native';
1313

14+
// Helper to check if a scope is a valid enum value
15+
function isValidScope(scope: string): boolean {
16+
return (
17+
Object.values(EthScope).includes(scope as EthScope) ||
18+
Object.values(BtcScope).includes(scope as BtcScope) ||
19+
Object.values(SolScope).includes(scope as SolScope)
20+
);
21+
}
22+
1423
function getScopesForAccountType(
1524
accountType: string,
1625
migrationNumber: number,
@@ -90,7 +99,9 @@ export function migration66(state: unknown, migrationNumber: number) {
9099
hasProperty(account, 'scopes') &&
91100
Array.isArray(account.scopes) &&
92101
account.scopes.length > 0 &&
93-
account.scopes.every((scope) => typeof scope === 'string')
102+
account.scopes.every(
103+
(scope) => typeof scope === 'string' && isValidScope(scope),
104+
)
94105
) {
95106
continue;
96107
}

app/store/migrations/067.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,105 @@ describe('migration #67', () => {
396396
),
397397
);
398398
});
399+
400+
it('updates accounts that were previously migrated with old scope values', () => {
401+
// Old scope values for testing migration from previous state
402+
const OLD_ETH_NAMESPACE_SCOPE = 'eip155' as `${string}:${string}`;
403+
const OLD_SOL_NAMESPACE_SCOPE = 'solana' as `${string}:${string}`;
404+
const OLD_BTC_NAMESPACE_SCOPE = 'bip122' as `${string}:${string}`;
405+
406+
const stateWithOldScopes: StateType = {
407+
engine: {
408+
backgroundState: {
409+
AccountsController: {
410+
internalAccounts: {
411+
selectedAccount: 'evm-1',
412+
accounts: {
413+
'evm-1': {
414+
id: 'evm-1',
415+
type: 'eip155:eoa',
416+
address: '0x123',
417+
options: {},
418+
metadata: {
419+
name: 'Account 1',
420+
keyring: { type: 'HD Key Tree' },
421+
importTime: Date.now(),
422+
},
423+
methods: [
424+
EthMethod.PersonalSign,
425+
EthMethod.SignTransaction,
426+
EthMethod.SignTypedDataV4,
427+
],
428+
// This represents the old scope value from migration 66
429+
scopes: [OLD_ETH_NAMESPACE_SCOPE],
430+
},
431+
'evm-2': {
432+
id: 'evm-2',
433+
type: 'eip155:erc4337',
434+
address: '0x456',
435+
options: {},
436+
metadata: {
437+
name: 'Account 2',
438+
keyring: { type: 'HD Key Tree' },
439+
importTime: Date.now(),
440+
},
441+
methods: [
442+
EthMethod.PersonalSign,
443+
EthMethod.SignTransaction,
444+
EthMethod.SignTypedDataV4,
445+
],
446+
// This represents the old scope value from migration 66
447+
scopes: [OLD_ETH_NAMESPACE_SCOPE],
448+
},
449+
'sol-1': {
450+
id: 'sol-1',
451+
type: 'solana:data-account',
452+
address: 'solana123',
453+
options: {},
454+
metadata: {
455+
name: 'Solana Account',
456+
keyring: { type: 'HD Key Tree' },
457+
importTime: Date.now(),
458+
},
459+
methods: [],
460+
// Old Solana namespace scope
461+
scopes: [OLD_SOL_NAMESPACE_SCOPE],
462+
},
463+
'btc-1': {
464+
id: 'btc-1',
465+
type: 'bip122:p2wpkh',
466+
address: 'bc1abc',
467+
options: {},
468+
metadata: {
469+
name: 'BTC Account',
470+
keyring: { type: 'HD Key Tree' },
471+
importTime: Date.now(),
472+
},
473+
methods: [],
474+
// Old BTC namespace scope
475+
scopes: [OLD_BTC_NAMESPACE_SCOPE],
476+
},
477+
},
478+
},
479+
},
480+
},
481+
},
482+
};
483+
484+
const stateCopy = JSON.parse(JSON.stringify(stateWithOldScopes));
485+
const result = migration(stateCopy) as StateType;
486+
const accounts =
487+
result.engine.backgroundState.AccountsController.internalAccounts
488+
.accounts;
489+
490+
// Check that old scope values were updated to new ones
491+
expect(accounts['evm-1']?.scopes).toEqual([EthScope.Eoa]);
492+
expect(accounts['evm-2']?.scopes).toEqual([EthScope.Eoa]);
493+
expect(accounts['sol-1']?.scopes).toEqual([
494+
SolScope.Mainnet,
495+
SolScope.Testnet,
496+
SolScope.Devnet,
497+
]);
498+
expect(accounts['btc-1']?.scopes).toEqual([BtcScope.Mainnet]);
499+
});
399500
});

0 commit comments

Comments
 (0)