Skip to content

Commit

Permalink
feat: wallet_requestpermissions update local provider accounts (#1081)
Browse files Browse the repository at this point in the history
* fix: rpc protocol resetting after refresh

* feat: multiple sdk improvements

* feat: wip

* fix: unit tests

* fix: unit tests

* feat: restore deletion
  • Loading branch information
abretonc7s authored Oct 17, 2024
1 parent a97980d commit cfba775
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/sdk/src/provider/initializeMobileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,42 @@ const initializeMobileProvider = async ({
rpcResponse,
);

// Check for wallet_requestPermissions to update local list of accounts since metamask mobile doesn't update the list automatically
if (method === RPC_METHODS.WALLET_REQUESTPERMISSIONS) {
const permissions = rpcResponse as {
caveats: { type: string; value: string[] }[];
parentCapability: string;
}[];

const accountsToPersist = permissions.reduce(
(acc: string[], permission) => {
if (permission.parentCapability === 'eth_accounts') {
const restrictedAccounts = permission.caveats.find(
(caveat) => caveat.type === 'restrictReturnedAccounts',
)?.value;

if (restrictedAccounts) {
acc.push(...restrictedAccounts);
}
}
return acc;
},
[],
);

logger(
`[initializeMobileProvider: sendRequest()] accountsToPersist:`,
accountsToPersist,
);

if (accountsToPersist.length > 0) {
// Emulate 'accountsChanged' on the provider
provider.handleAccountsChanged(accountsToPersist, false);
// provider.emit('accountsChanged', accountsToPersist);
storageManager?.persistAccounts(accountsToPersist);
}
}

return rpcResponse;
} catch (error) {
console.error(`[initializeMobileProvider: sendRequest()] error:`, error);
Expand Down

0 comments on commit cfba775

Please sign in to comment.