@@ -39,6 +39,11 @@ import {
3939 type InternalAccount ,
4040} from '@metamask/keyring-internal-api' ;
4141import { KeyringInternalSnapClient } from '@metamask/keyring-internal-snap-client' ;
42+ import {
43+ type GetSelectedAccountsResponse ,
44+ GetSelectedAccountsRequestStruct ,
45+ SnapManageAccountsMethod ,
46+ } from '@metamask/keyring-snap-sdk' ;
4247import type { AccountId , JsonRpcRequest } from '@metamask/keyring-utils' ;
4348import { strictMask } from '@metamask/keyring-utils' ;
4449import type { SnapId } from '@metamask/snaps-sdk' ;
@@ -183,6 +188,11 @@ export class SnapKeyring {
183188 snapId : SnapId ;
184189 } > ;
185190
191+ /**
192+ * Mapping between Snap IDs and the selected accounts.
193+ */
194+ readonly #selectedAccounts: Map < SnapId , AccountId [ ] > ;
195+
186196 /**
187197 * Mapping between request IDs and their deferred promises.
188198 */
@@ -238,6 +248,7 @@ export class SnapKeyring {
238248 this . #options = new SnapIdMap ( ) ;
239249 this . #callbacks = callbacks ;
240250 this . #isAnyAccountTypeAllowed = isAnyAccountTypeAllowed ;
251+ this . #selectedAccounts = new Map ( ) ;
241252 }
242253
243254 /**
@@ -497,6 +508,21 @@ export class SnapKeyring {
497508 return null ;
498509 }
499510
511+ /**
512+ * Handle a Get Selected Accounts method call from a Snap.
513+ *
514+ * @param snapId - Snap ID.
515+ * @param message - Method call message.
516+ * @returns The selected accounts.
517+ */
518+ async #handleGetSelectedAccounts(
519+ snapId : SnapId ,
520+ message : SnapMessage ,
521+ ) : Promise < GetSelectedAccountsResponse > {
522+ assert ( message , GetSelectedAccountsRequestStruct ) ;
523+ return this . #selectedAccounts. get ( snapId ) ?? [ ] ;
524+ }
525+
500526 /**
501527 * Handle an Request Approved event from a Snap.
502528 *
@@ -701,6 +727,10 @@ export class SnapKeyring {
701727 return this . #handleAccountTransactionsUpdated( snapId , message ) ;
702728 }
703729
730+ case `${ SnapManageAccountsMethod . GetSelectedAccounts } ` : {
731+ return this . #handleGetSelectedAccounts( snapId , message ) ;
732+ }
733+
704734 default :
705735 throw new Error ( `Method not supported: ${ message . method } ` ) ;
706736 }
@@ -1497,6 +1527,48 @@ export class SnapKeyring {
14971527 ) ;
14981528 }
14991529
1530+ /**
1531+ * Update the in-memory selected accounts map.
1532+ *
1533+ * @param accounts - The accounts to update the map with.
1534+ */
1535+ #updateSelectedAccountsMap( accounts : AccountId [ ] ) : void {
1536+ const selectedAccounts = this . #selectedAccounts;
1537+ selectedAccounts . clear ( ) ;
1538+ for ( const account of accounts ) {
1539+ const snapId = this . #accounts. getSnapId ( account ) ;
1540+ if ( ! snapId ) {
1541+ continue ;
1542+ }
1543+ const snapAccounts = selectedAccounts . get ( snapId ) ?? [ ] ;
1544+ snapAccounts . push ( account ) ;
1545+ selectedAccounts . set ( snapId , snapAccounts ) ;
1546+ }
1547+ }
1548+
1549+ /**
1550+ * Set the selected accounts.
1551+ *
1552+ * @param accounts - The accounts to set as selected.
1553+ */
1554+ async setSelectedAccounts ( accounts : AccountId [ ] ) : Promise < void > {
1555+ this . #updateSelectedAccountsMap( accounts ) ;
1556+ const entries = [ ...this . #selectedAccounts. entries ( ) ] ;
1557+ await Promise . all (
1558+ entries . map ( async ( [ snapId , accountIds ] ) => {
1559+ try {
1560+ await this . #snapClient
1561+ . withSnapId ( snapId )
1562+ . setSelectedAccounts ( accountIds ) ;
1563+ } catch ( error : any ) {
1564+ console . error (
1565+ `Failed to set selected accounts for ${ snapId } snap: '${ error . message } '` ,
1566+ ) ;
1567+ }
1568+ } ) ,
1569+ ) ;
1570+ }
1571+
15001572 /**
15011573 * Get the Snap associated with the given Snap ID.
15021574 *
0 commit comments