Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/snap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Implement `keyring_setSelectedAccounts` and `keyring_resolveAccountAddress` ([#163](https://github.com/MetaMask/snap-simple-keyring/pull/163))
- Required for compatibility with `@metamask/eth-snap-keyring@^22`, which invokes these methods when MetaMask switches the active account or needs to resolve the signer for a request.
- Allow the `metamask` origin to call `keyring_createAccount`, `keyring_resolveAccountAddress`, and `keyring_setSelectedAccounts` ([#163](https://github.com/MetaMask/snap-simple-keyring/pull/163))

## [2.0.1]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/MetaMask/snap-simple-keyring.git"
},
"source": {
"shasum": "TfeKGNKMSPQqAKQ/IrnOaziMcLqC6tWSjKRWo5YHMLs=",
"shasum": "GOVQr60E+i51GtXbwdaFzHmBrDKxwBVoDKUICf6V8HI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
55 changes: 54 additions & 1 deletion packages/snap/src/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
KeyringAccount,
KeyringEventPayload,
KeyringRequest,
ResolvedAccountAddress,
SubmitRequestResponse,
} from '@metamask/keyring-api';
import {
Expand All @@ -31,7 +32,11 @@ import {
KeyringEvent,
} from '@metamask/keyring-api';
import { emitSnapKeyringEvent } from '@metamask/keyring-snap-sdk';
import { type Json, type JsonRpcRequest } from '@metamask/utils';
import {
type CaipChainId,
type Json,
type JsonRpcRequest,
} from '@metamask/utils';
import { Buffer } from 'buffer';
import { v4 as uuid } from 'uuid';

Expand All @@ -49,6 +54,7 @@ export type KeyringState = {
wallets: Record<string, Wallet>;
pendingRequests: Record<string, KeyringRequest>;
useSyncApprovals: boolean;
selectedAccounts: string[];
};

export type Wallet = {
Expand Down Expand Up @@ -158,6 +164,53 @@ export class SimpleKeyring implements Keyring {
}
}

async setSelectedAccounts(accounts: string[]): Promise<void> {
this.#state.selectedAccounts = accounts;
await this.#saveState();
}

async resolveAccountAddress(
scope: CaipChainId,
request: JsonRpcRequest,
): Promise<ResolvedAccountAddress | null> {
const from = this.#extractFromAddress(request);
if (from === undefined) {
return null;
}

const wallet = Object.values(this.#state.wallets).find(
(entry) => entry.account.address.toLowerCase() === from.toLowerCase(),
);
if (!wallet) {
return null;
}

return { address: `${scope}:${wallet.account.address}` };
}

#extractFromAddress(request: JsonRpcRequest): string | undefined {
const params = (request.params ?? []) as Json[];
switch (request.method) {
case EthMethod.PersonalSign: {
const [, from] = params as [string, string];
return from;
}
case EthMethod.SignTransaction: {
const [tx] = params as [{ from?: string }];
return tx?.from;
}
case EthMethod.SignTypedDataV1:
case EthMethod.SignTypedDataV3:
case EthMethod.SignTypedDataV4:
case EthMethod.Sign: {
const [from] = params as [string];
return from;
}
default:
return undefined;
}
}

async listRequests(): Promise<KeyringRequest[]> {
return Object.values(this.#state.pendingRequests);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/snap/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export const originPermissions = new Map<string, string[]>([
// Keyring methods
KeyringRpcMethod.ListAccounts,
KeyringRpcMethod.GetAccount,
KeyringRpcMethod.CreateAccount,
KeyringRpcMethod.FilterAccountChains,
KeyringRpcMethod.DeleteAccount,
KeyringRpcMethod.ListRequests,
KeyringRpcMethod.GetRequest,
KeyringRpcMethod.SubmitRequest,
KeyringRpcMethod.RejectRequest,
KeyringRpcMethod.ResolveAccountAddress,
KeyringRpcMethod.SetSelectedAccounts,
],
],
[
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/stateManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const defaultState: KeyringState = {
wallets: {},
pendingRequests: {},
useSyncApprovals: true,
selectedAccounts: [],
};

/**
Expand Down
Loading