Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 3f3289c

Browse files
committed
feat: make filterAccountChains optional
1 parent 37409d7 commit 3f3289c

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/api/account.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JsonStruct } from '@metamask/utils';
22
import type { Infer } from 'superstruct';
3-
import { array, enums, record, string } from 'superstruct';
3+
import { array, enums, record, string, union } from 'superstruct';
44

55
import { object } from '../superstruct';
66
import { UuidStruct } from '../utils';
@@ -51,9 +51,35 @@ export const KeyringAccountStruct = object({
5151
]),
5252

5353
/**
54-
* Account main address.
54+
* Account addresses. It can be a single address or a map of addresses per
55+
* chain.
56+
*
57+
* If the address is a string, it's assumed to be under the 'eip155'
58+
* namespace. Otherwise, it must be a map of addresses per chain, where the
59+
* key is the chain ID (CAIP-2) and the value is an array of addresses.
60+
*
61+
* @example
62+
* ```ts
63+
* address: {
64+
* // Different addresses per chain.
65+
* 'eip155:1': ['0x1234...'],
66+
* 'eip155:137': ['0x5678...'],
67+
* }
68+
* ```
69+
* @example
70+
* ```ts
71+
* address: {
72+
* // The address is the same across all 'eip155' chains.
73+
* 'eip155': ['0x1234...'],
74+
* }
75+
* ```
76+
* @example
77+
* ```ts
78+
* // Assumed to be under the 'eip155' namespace.
79+
* address: '0x1234...',
80+
* ```
5581
*/
56-
address: string(),
82+
address: union([string(), record(string(), array(string()))]),
5783

5884
/**
5985
* Account options.

src/api/keyring.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ export type Keyring = {
8181
/**
8282
* Filter supported chains for a given account.
8383
*
84+
* See {@link KeyringAccount}.
85+
*
86+
* @deprecated Use the keys of the `address` map of the account object to
87+
* indicate the supported chains. This method will be removed in a future
88+
* version of the Keyring API.
8489
* @param id - ID of the account to be checked.
8590
* @param chains - List of chains (CAIP-2) to be checked.
8691
* @returns A Promise that resolves to a filtered list of CAIP-2 IDs
8792
* representing the supported chains.
8893
*/
89-
filterAccountChains(id: string, chains: string[]): Promise<string[]>;
94+
filterAccountChains?(id: string, chains: string[]): Promise<string[]>;
9095

9196
/**
9297
* Update an account.

src/rpc-handler.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,25 @@ describe('handleKeyringRequest', () => {
133133
expect(result).toBe('FilterSupportedChains result');
134134
});
135135

136-
it('calls `keyring_updateAccount`', async () => {
136+
it('fails because `keyring_filterAccountChains` is not implemented', async () => {
137+
const request: JsonRpcRequest = {
138+
jsonrpc: '2.0',
139+
id: '7c507ff0-365f-4de0-8cd5-eb83c30ebda4',
140+
method: 'keyring_filterAccountChains',
141+
params: {
142+
id: '4f983fa2-4f53-4c63-a7c2-f9a5ed750041',
143+
chains: ['chain1', 'chain2'],
144+
},
145+
};
146+
147+
const { filterAccountChains, ...partialKeyring } = keyring;
148+
149+
await expect(handleKeyringRequest(partialKeyring, request)).rejects.toThrow(
150+
'Method not supported: keyring_filterAccountChains',
151+
);
152+
});
153+
154+
it('calls keyring_updateAccount', async () => {
137155
const request: JsonRpcRequest = {
138156
jsonrpc: '2.0',
139157
id: '7c507ff0-365f-4de0-8cd5-eb83c30ebda4',

src/rpc-handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ async function dispatchRequest(
7474
}
7575

7676
case KeyringRpcMethod.FilterAccountChains: {
77+
if (keyring.filterAccountChains === undefined) {
78+
throw new MethodNotSupportedError(request.method);
79+
}
7780
assert(request, FilterAccountChainsStruct);
7881
return keyring.filterAccountChains(
7982
request.params.id,

0 commit comments

Comments
 (0)