Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wallet_requestpermissions update local provider accounts #1081

Merged
merged 9 commits into from
Oct 17, 2024
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
Loading