Skip to content

Commit d9ae4a6

Browse files
committed
address requested changes
1 parent 27ae3ac commit d9ae4a6

File tree

8 files changed

+161
-222
lines changed

8 files changed

+161
-222
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 93.9,
3-
"functions": 98.2,
4-
"lines": 98.55,
5-
"statements": 98.39
2+
"branches": 93.88,
3+
"functions": 98.21,
4+
"lines": 98.54,
5+
"statements": 98.38
66
}

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,6 @@ describe('SnapInterfaceController', () => {
311311
}),
312312
);
313313

314-
rootMessenger.registerActionHandler(
315-
'AccountsController:setSelectedAccount',
316-
() => {
317-
// no-op
318-
},
319-
);
320-
321314
// eslint-disable-next-line no-new
322315
new SnapInterfaceController({
323316
messenger: controllerMessenger,
@@ -351,12 +344,6 @@ describe('SnapInterfaceController', () => {
351344
'7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
352345
);
353346

354-
expect(rootMessenger.call).toHaveBeenNthCalledWith(
355-
3,
356-
'AccountsController:setSelectedAccount',
357-
MOCK_ACCOUNT_ID,
358-
);
359-
360347
expect(content).toStrictEqual(element);
361348
expect(state).toStrictEqual({
362349
foo: {
@@ -407,13 +394,6 @@ describe('SnapInterfaceController', () => {
407394
],
408395
);
409396

410-
rootMessenger.registerActionHandler(
411-
'AccountsController:setSelectedAccount',
412-
() => {
413-
// no-op
414-
},
415-
);
416-
417397
// eslint-disable-next-line no-new
418398
new SnapInterfaceController({
419399
messenger: controllerMessenger,
@@ -487,13 +467,6 @@ describe('SnapInterfaceController', () => {
487467
],
488468
);
489469

490-
rootMessenger.registerActionHandler(
491-
'AccountsController:setSelectedAccount',
492-
() => {
493-
// no-op
494-
},
495-
);
496-
497470
// eslint-disable-next-line no-new
498471
new SnapInterfaceController({
499472
messenger: controllerMessenger,
@@ -532,12 +505,6 @@ describe('SnapInterfaceController', () => {
532505
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
533506
);
534507

535-
expect(rootMessenger.call).toHaveBeenNthCalledWith(
536-
4,
537-
'AccountsController:setSelectedAccount',
538-
MOCK_ACCOUNT_ID,
539-
);
540-
541508
expect(content).toStrictEqual(element);
542509
expect(state).toStrictEqual({
543510
foo: {
@@ -1467,6 +1434,94 @@ describe('SnapInterfaceController', () => {
14671434
),
14681435
).rejects.toThrow('Interface not created by foo.');
14691436
});
1437+
1438+
it('can select an account owned by the snap', async () => {
1439+
const rootMessenger = getRootSnapInterfaceControllerMessenger();
1440+
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(
1441+
rootMessenger,
1442+
false,
1443+
);
1444+
1445+
rootMessenger.registerActionHandler(
1446+
'AccountsController:getSelectedMultichainAccount',
1447+
() => ({
1448+
id: MOCK_ACCOUNT_ID,
1449+
address: '0x1234567890123456789012345678901234567890',
1450+
scopes: ['eip155:0'],
1451+
metadata: {
1452+
// @ts-expect-error partial mock
1453+
snap: {
1454+
id: 'npm:[email protected]' as SnapId,
1455+
},
1456+
},
1457+
}),
1458+
);
1459+
1460+
rootMessenger.registerActionHandler(
1461+
'AccountsController:getAccountByAddress',
1462+
() => ({
1463+
id: MOCK_ACCOUNT_ID,
1464+
address: '7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
1465+
scopes: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'],
1466+
metadata: {
1467+
// @ts-expect-error partial mock
1468+
snap: {
1469+
id: MOCK_SNAP_ID,
1470+
},
1471+
},
1472+
}),
1473+
);
1474+
1475+
// eslint-disable-next-line no-new
1476+
new SnapInterfaceController({
1477+
messenger: controllerMessenger,
1478+
});
1479+
1480+
const element = (
1481+
<Box>
1482+
<AccountSelector name="foo" />
1483+
</Box>
1484+
);
1485+
1486+
const newElement = (
1487+
<Box>
1488+
<AccountSelector
1489+
name="foo"
1490+
hideExternalAccounts
1491+
value="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv"
1492+
/>
1493+
</Box>
1494+
);
1495+
1496+
const id = await rootMessenger.call(
1497+
'SnapInterfaceController:createInterface',
1498+
MOCK_SNAP_ID,
1499+
element,
1500+
);
1501+
1502+
await rootMessenger.call(
1503+
'SnapInterfaceController:updateInterface',
1504+
MOCK_SNAP_ID,
1505+
id,
1506+
newElement,
1507+
);
1508+
1509+
const { content, state } = rootMessenger.call(
1510+
'SnapInterfaceController:getInterface',
1511+
MOCK_SNAP_ID,
1512+
id,
1513+
);
1514+
1515+
expect(content).toStrictEqual(newElement);
1516+
expect(state).toStrictEqual({
1517+
foo: {
1518+
accountId: MOCK_ACCOUNT_ID,
1519+
addresses: [
1520+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
1521+
],
1522+
},
1523+
});
1524+
});
14701525
});
14711526

14721527
describe('updateInterfaceState', () => {

packages/snaps-controllers/src/interface/SnapInterfaceController.ts

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import type {
1919
import { ContentType } from '@metamask/snaps-sdk';
2020
import type { JSXElement } from '@metamask/snaps-sdk/jsx';
2121
import type { InternalAccount } from '@metamask/snaps-utils';
22-
import { getJsonSizeUnsafe, validateJsxElements } from '@metamask/snaps-utils';
22+
import {
23+
getJsonSizeUnsafe,
24+
snapOwnsAccount,
25+
validateJsxElements,
26+
} from '@metamask/snaps-utils';
2327
import type {
2428
CaipAccountId,
2529
CaipAssetType,
@@ -81,11 +85,6 @@ type AccountsControllerGetSelectedMultichainAccountAction = {
8185
handler: () => InternalAccount;
8286
};
8387

84-
type AccountsControllerSetSelectedAccountAction = {
85-
type: `AccountsController:setSelectedAccount`;
86-
handler: (accountId: string) => void;
87-
};
88-
8988
type AccountsControllerListMultichainAccountsAction = {
9089
type: `AccountsController:listMultichainAccounts`;
9190
handler: (chainId?: CaipChainId) => InternalAccount[];
@@ -114,7 +113,6 @@ export type SnapInterfaceControllerAllowedActions =
114113
| MultichainAssetsControllerGetStateAction
115114
| AccountsControllerGetSelectedMultichainAccountAction
116115
| AccountsControllerGetAccountByAddressAction
117-
| AccountsControllerSetSelectedAccountAction
118116
| AccountsControllerListMultichainAccountsAction;
119117

120118
export type SnapInterfaceControllerActions =
@@ -291,9 +289,9 @@ export class SnapInterfaceController extends BaseController<
291289
getAssetsState: this.#getAssetsState.bind(this),
292290
getAccountByAddress: this.#getAccountByAddress.bind(this),
293291
getSelectedAccount: this.#getSelectedAccount.bind(this),
294-
setSelectedAccount: this.#setSelectedAccount.bind(this),
295292
listAccounts: this.#listAccounts.bind(this),
296-
snapOwnsAccount: this.#snapOwnsAccount.bind(this, snapId),
293+
snapOwnsAccount: (account: InternalAccount) =>
294+
snapOwnsAccount(snapId, account),
297295
});
298296

299297
this.update((draftState) => {
@@ -348,9 +346,9 @@ export class SnapInterfaceController extends BaseController<
348346
getAssetsState: this.#getAssetsState.bind(this),
349347
getAccountByAddress: this.#getAccountByAddress.bind(this),
350348
getSelectedAccount: this.#getSelectedAccount.bind(this),
351-
setSelectedAccount: this.#setSelectedAccount.bind(this),
352349
listAccounts: this.#listAccounts.bind(this),
353-
snapOwnsAccount: this.#snapOwnsAccount.bind(this, snapId),
350+
snapOwnsAccount: (account: InternalAccount) =>
351+
snapOwnsAccount(snapId, account),
354352
});
355353

356354
this.update((draftState) => {
@@ -482,18 +480,6 @@ export class SnapInterfaceController extends BaseController<
482480
);
483481
}
484482

485-
/**
486-
* Set the selected account in the client.
487-
*
488-
* @param accountId - The account id.
489-
*/
490-
#setSelectedAccount(accountId: string) {
491-
this.messagingSystem.call(
492-
'AccountsController:setSelectedAccount',
493-
accountId,
494-
);
495-
}
496-
497483
/**
498484
* Get a list of accounts for the given chain IDs.
499485
*
@@ -554,17 +540,6 @@ export class SnapInterfaceController extends BaseController<
554540
return this.messagingSystem.call('SnapController:get', id);
555541
}
556542

557-
/**
558-
* Whether if the snap owns the account.
559-
*
560-
* @param snapId - The snap id.
561-
* @param account - The account.
562-
* @returns True if the snap owns the account, otherwise false.
563-
*/
564-
#snapOwnsAccount(snapId: SnapId, account: InternalAccount) {
565-
return account.metadata.snap?.id === snapId;
566-
}
567-
568543
/**
569544
* Utility function to validate the components of an interface.
570545
* Throws if something is invalid.

0 commit comments

Comments
 (0)