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

Commit

Permalink
feat: make filterAccountChains optional
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed May 24, 2024
1 parent 65a534d commit ba28735
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,31 @@ export const KeyringAccountStruct = object({
]),

/**
* Account main address.
* Account addresses for different chains. Can be used to support Smart
* Contract accounts with different addresses on different chains.
*
* @example
* ```ts
* address: {
* // Different addresses per chain.
* 'eip155:1': ['0x1234...'],
* 'eip155:137': ['0x5678...'],
* }
* ```
* @example
* ```ts
* address: {
* // The address is the same across all 'eip155' chains.
* 'eip155': ['0x1234...'],
* }
* ```
* @example
* ```ts
* // Assumed to be under the 'eip155' namespace.
* address: '0x1234...',
* ```
*/
address: string(),
address: union([string(), record(string(), array(string()))]),

/**
* Account options.
Expand All @@ -68,6 +90,26 @@ export const KeyringAccountStruct = object({
*/
export type KeyringAccount = Infer<typeof KeyringAccountStruct>;

/**
* A struct which represents a blockchain account object.
*/
export type BlockchainAccount = {
/**
* Account address.
*/
address: string;

/**
* CAIP-2 chain ID or namespace.
*/
chainId: string;

/**
* Account ID of the parent (keyring) account.
*/
parentAccount: string;
};

export const KeyringRequestStruct = object({
/**
* Keyring request ID (UUIDv4).
Expand Down Expand Up @@ -209,12 +251,17 @@ export type Keyring = {
/**
* Filter supported chains for a given account.
*
* See {@link KeyringAccount}.
*
* @deprecated Use the keys of the `address` map of the account object to
* indicate the supported chains. This method will be removed in a future
* version of the Keyring API.
* @param id - ID of the account to be checked.
* @param chains - List of chains (CAIP-2) to be checked.
* @returns A Promise that resolves to a filtered list of CAIP-2 IDs
* representing the supported chains.
*/
filterAccountChains(id: string, chains: string[]): Promise<string[]>;
filterAccountChains?(id: string, chains: string[]): Promise<string[]>;

/**
* Update an account.
Expand Down
21 changes: 21 additions & 0 deletions src/rpc-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ describe('keyringRpcDispatcher', () => {
expect(result).toBe('FilterSupportedChains result');
});

it('fails if the `keyring_filterAccountChains` method is not implemented', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: '7c507ff0-365f-4de0-8cd5-eb83c30ebda4',
method: 'keyring_filterAccountChains',
params: {
id: '4f983fa2-4f53-4c63-a7c2-f9a5ed750041',
chains: ['chain1', 'chain2'],
},
};

const partialKeyring: Keyring = {
...keyring,
};
delete partialKeyring.filterAccountChains;

await expect(handleKeyringRequest(partialKeyring, request)).rejects.toThrow(
'Method not supported: keyring_filterAccountChains',
);
});

it('should call keyring_updateAccount', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
Expand Down
3 changes: 3 additions & 0 deletions src/rpc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export async function handleKeyringRequest(
}

case KeyringRpcMethod.FilterAccountChains: {
if (keyring.filterAccountChains === undefined) {
throw new MethodNotSupportedError(request.method);
}
assert(request, FilterAccountChainsStruct);
return keyring.filterAccountChains(
request.params.id,
Expand Down

0 comments on commit ba28735

Please sign in to comment.