From d9c370e7ac51da6c0a054bfd42f4aae561b20c47 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 3 Aug 2023 11:31:42 +0200 Subject: [PATCH 1/6] feat: add `InternalAccount` type and create submodule `internal` --- src/KeyringClient.ts | 2 +- src/KeyringSnapControllerClient.ts | 6 ++- src/KeyringSnapRpcClient.ts | 6 ++- src/api.ts | 4 +- src/{internal-api.ts => internal/api.ts} | 4 +- src/internal/index.ts | 2 + src/internal/types.test.ts | 62 ++++++++++++++++++++++++ src/internal/types.ts | 27 +++++++++++ src/rpc-handler.ts | 2 +- 9 files changed, 107 insertions(+), 8 deletions(-) rename src/{internal-api.ts => internal/api.ts} (98%) create mode 100644 src/internal/index.ts create mode 100644 src/internal/types.test.ts create mode 100644 src/internal/types.ts diff --git a/src/KeyringClient.ts b/src/KeyringClient.ts index 69850ceac..874f0e5aa 100644 --- a/src/KeyringClient.ts +++ b/src/KeyringClient.ts @@ -21,7 +21,7 @@ import { type SubmitRequestResponse, UpdateAccountResponseStruct, InternalResponseStruct, -} from './internal-api'; +} from './internal/api'; import { type OmitUnion, strictMask } from './utils'; export type Sender = { diff --git a/src/KeyringSnapControllerClient.ts b/src/KeyringSnapControllerClient.ts index b19b63870..3e574f8db 100644 --- a/src/KeyringSnapControllerClient.ts +++ b/src/KeyringSnapControllerClient.ts @@ -1,7 +1,11 @@ import type { SnapController } from '@metamask/snaps-controllers'; import type { HandlerType, ValidatedSnapId } from '@metamask/snaps-utils'; -import type { InternalRequest, InternalResponse } from './internal-api'; +import { + type InternalRequest, + type InternalResponse, + InternalResponseStruct, +} from './internal/api'; import { KeyringClient, type Sender } from './KeyringClient'; /** diff --git a/src/KeyringSnapRpcClient.ts b/src/KeyringSnapRpcClient.ts index 18e4f6370..f1de9aff0 100644 --- a/src/KeyringSnapRpcClient.ts +++ b/src/KeyringSnapRpcClient.ts @@ -1,6 +1,10 @@ import type { MetaMaskInpageProvider } from '@metamask/providers'; -import type { InternalRequest, InternalResponse } from './internal-api'; +import { + InternalResponseStruct, + type InternalRequest, + type InternalResponse, +} from './internal/api'; import { KeyringClient, type Sender } from './KeyringClient'; /** diff --git a/src/api.ts b/src/api.ts index bd0908634..070464666 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,14 +4,14 @@ import { object, string, enums, record, array, type Infer } from 'superstruct'; import type { ExportAccountResponse, SubmitRequestResponse, -} from './internal-api'; +} from './internal/api'; import { JsonRpcRequestStruct } from './JsonRpcRequest'; import { UuidStruct } from './utils'; export type { ExportAccountResponse, SubmitRequestResponse, -} from './internal-api'; +} from './internal/api'; /** * Supported Ethereum methods. diff --git a/src/internal-api.ts b/src/internal/api.ts similarity index 98% rename from src/internal-api.ts rename to src/internal/api.ts index f73c3f83a..e1c559ad7 100644 --- a/src/internal-api.ts +++ b/src/internal/api.ts @@ -9,8 +9,8 @@ import { type Infer, } from 'superstruct'; -import { KeyringAccountStruct, KeyringRequestStruct } from './api'; -import { UuidStruct } from './utils'; +import { KeyringAccountStruct, KeyringRequestStruct } from '../api'; +import { UuidStruct } from '../utils'; const CommonHeader = { jsonrpc: literal('2.0'), diff --git a/src/internal/index.ts b/src/internal/index.ts new file mode 100644 index 000000000..4d4b4e299 --- /dev/null +++ b/src/internal/index.ts @@ -0,0 +1,2 @@ +export * from './api'; +export * from './types'; diff --git a/src/internal/types.test.ts b/src/internal/types.test.ts new file mode 100644 index 000000000..19f4b1023 --- /dev/null +++ b/src/internal/types.test.ts @@ -0,0 +1,62 @@ +import { assert } from 'superstruct'; + +import { InternalAccountStruct } from '.'; + +describe('InternalAccount', () => { + it('should have the correct structure', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: { + keyring: { + type: 'Test Keyring', + }, + }, + }; + + expect(() => assert(account, InternalAccountStruct)).not.toThrow(); + }); + + it('should throw if snap ID is missing', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: { + snap: {}, + keyring: { + type: 'Test Keyring', + }, + }, + }; + + expect(() => assert(account, InternalAccountStruct)).toThrow( + 'At path: metadata.snap.id -- Expected a string, but received: undefined', + ); + }); + + it('should throw if there are extra fields', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: { + keyring: { + type: 'Test Keyring', + }, + extra: 'field', + }, + }; + + expect(() => assert(account, InternalAccountStruct)).toThrow( + 'At path: metadata.extra -- Expected a value of type `never`', + ); + }); +}); diff --git a/src/internal/types.ts b/src/internal/types.ts new file mode 100644 index 000000000..9016224e6 --- /dev/null +++ b/src/internal/types.ts @@ -0,0 +1,27 @@ +import { boolean, object, optional, string, type Infer } from 'superstruct'; + +import { KeyringAccountStruct } from '../api'; + +export const InternalAccountStruct = object({ + ...KeyringAccountStruct.schema, + metadata: object({ + snap: optional( + object({ + id: string(), + name: optional(string()), + enabled: optional(boolean()), + }), + ), + keyring: object({ + type: string(), + }), + }), +}); + +/** + * Internal account representation. + * + * This type is used internally by MetaMask to add additional metadata to the + * account object. It's should not be used by external applications. + */ +export type InternalAccount = Infer; diff --git a/src/rpc-handler.ts b/src/rpc-handler.ts index c31a6b90c..a1e5431b5 100644 --- a/src/rpc-handler.ts +++ b/src/rpc-handler.ts @@ -15,7 +15,7 @@ import { FilterAccountChainsStruct, ListAccountsRequestStruct, ListRequestsRequestStruct, -} from './internal-api'; +} from './internal/api'; import { type JsonRpcRequest, JsonRpcRequestStruct } from './JsonRpcRequest'; /** From 21cd222dbf0853f8e105650dce7b7217362aa22c Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Fri, 4 Aug 2023 00:26:40 +0200 Subject: [PATCH 2/6] feat: export internal types --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 8b4719024..75fe0edc2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,4 @@ export * from './KeyringClient'; export * from './rpc-handler'; export * from './KeyringSnapControllerClient'; export * from './KeyringSnapRpcClient'; +export * from './internal'; From f4c7756c5dc60a443b68f74247c165c2bcc7d2b3 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Fri, 4 Aug 2023 00:32:26 +0200 Subject: [PATCH 3/6] chore: make snap ID optional --- src/internal/types.test.ts | 38 ++++++++++++++++++++++++++++++++------ src/internal/types.ts | 2 +- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/internal/types.test.ts b/src/internal/types.test.ts index 19f4b1023..c2eba773b 100644 --- a/src/internal/types.test.ts +++ b/src/internal/types.test.ts @@ -20,7 +20,7 @@ describe('InternalAccount', () => { expect(() => assert(account, InternalAccountStruct)).not.toThrow(); }); - it('should throw if snap ID is missing', () => { + it('should throw if metadata.keyring.type is not set', () => { const account = { id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', address: '0x000', @@ -28,15 +28,41 @@ describe('InternalAccount', () => { methods: [], type: 'eip155:eoa', metadata: { - snap: {}, - keyring: { - type: 'Test Keyring', - }, + keyring: {}, }, }; expect(() => assert(account, InternalAccountStruct)).toThrow( - 'At path: metadata.snap.id -- Expected a string, but received: undefined', + 'At path: metadata.keyring.type -- Expected a string, but received: undefined', + ); + }); + + it('should throw if metadata.keyring is not set', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: {}, + }; + + expect(() => assert(account, InternalAccountStruct)).toThrow( + 'At path: metadata.keyring -- Expected an object, but received: undefined', + ); + }); + + it('should throw if metadata is not set', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + }; + + expect(() => assert(account, InternalAccountStruct)).toThrow( + 'At path: metadata -- Expected an object, but received: undefined', ); }); diff --git a/src/internal/types.ts b/src/internal/types.ts index 9016224e6..387ce0a96 100644 --- a/src/internal/types.ts +++ b/src/internal/types.ts @@ -7,7 +7,7 @@ export const InternalAccountStruct = object({ metadata: object({ snap: optional( object({ - id: string(), + id: optional(string()), name: optional(string()), enabled: optional(boolean()), }), From 6ccbb597828b0ec9a7123794ebae7eef27498235 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:27:06 +0200 Subject: [PATCH 4/6] fix: fix linting errors --- src/KeyringSnapControllerClient.ts | 12 +++--------- src/KeyringSnapRpcClient.ts | 12 +++--------- src/api.ts | 7 +------ 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/KeyringSnapControllerClient.ts b/src/KeyringSnapControllerClient.ts index 3e574f8db..3dd88702c 100644 --- a/src/KeyringSnapControllerClient.ts +++ b/src/KeyringSnapControllerClient.ts @@ -1,11 +1,7 @@ import type { SnapController } from '@metamask/snaps-controllers'; import type { HandlerType, ValidatedSnapId } from '@metamask/snaps-utils'; -import { - type InternalRequest, - type InternalResponse, - InternalResponseStruct, -} from './internal/api'; +import type { InternalRequest, InternalResponse } from './internal/api'; import { KeyringClient, type Sender } from './KeyringClient'; /** @@ -48,14 +44,12 @@ class SnapControllerSender implements Sender { * @returns A promise that resolves to the response of the request. */ async send(request: InternalRequest): Promise { - const response = await this.#controller.handleRequest({ + return this.#controller.handleRequest({ snapId: this.#snapId as ValidatedSnapId, origin: this.#origin, handler: this.#handler, request, - }); - - return response as InternalResponse; + }) as Promise; } } diff --git a/src/KeyringSnapRpcClient.ts b/src/KeyringSnapRpcClient.ts index f1de9aff0..e0259d33a 100644 --- a/src/KeyringSnapRpcClient.ts +++ b/src/KeyringSnapRpcClient.ts @@ -1,10 +1,6 @@ import type { MetaMaskInpageProvider } from '@metamask/providers'; -import { - InternalResponseStruct, - type InternalRequest, - type InternalResponse, -} from './internal/api'; +import type { InternalRequest, InternalResponse } from './internal/api'; import { KeyringClient, type Sender } from './KeyringClient'; /** @@ -34,15 +30,13 @@ export class SnapRpcSender implements Sender { * @returns A promise that resolves to the response of the request. */ async send(request: InternalRequest): Promise { - const response = await this.#provider.request({ + return this.#provider.request({ method: 'wallet_invokeSnap', params: { snapId: this.#origin, request, }, - }); - - return response as InternalResponse; + }) as Promise; } } diff --git a/src/api.ts b/src/api.ts index 070464666..bed204b55 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,5 @@ import { type Json, JsonStruct } from '@metamask/utils'; -import { object, string, enums, record, array, type Infer } from 'superstruct'; +import { type Infer, array, enums, object, record, string } from 'superstruct'; import type { ExportAccountResponse, @@ -8,11 +8,6 @@ import type { import { JsonRpcRequestStruct } from './JsonRpcRequest'; import { UuidStruct } from './utils'; -export type { - ExportAccountResponse, - SubmitRequestResponse, -} from './internal/api'; - /** * Supported Ethereum methods. */ From 7c61dfe16ea7f85edcdce31d25cb6827c4b39449 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:35:02 +0200 Subject: [PATCH 5/6] refactor: move some common types to public API --- src/KeyringClient.ts | 16 +++++++++++----- src/api.ts | 31 ++++++++++++++++++++++++++----- src/internal/api.ts | 23 +++++++---------------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/KeyringClient.ts b/src/KeyringClient.ts index 874f0e5aa..b3c82e64b 100644 --- a/src/KeyringClient.ts +++ b/src/KeyringClient.ts @@ -2,23 +2,29 @@ import type { Json } from '@metamask/utils'; import { assert } from 'superstruct'; import { v4 as uuid } from 'uuid'; -import type { Keyring, KeyringAccount, KeyringRequest } from './api'; +import { + ExportAccountResponseStruct, + SubmitRequestResponseStruct, +} from './api'; +import type { + Keyring, + KeyringAccount, + KeyringRequest, + ExportAccountResponse, + SubmitRequestResponse, +} from './api'; import { ApproveRequestResponseStruct, CreateAccountResponseStruct, DeleteAccountResponseStruct, - ExportAccountResponseStruct, FilterAccountChainsResponseStruct, GetAccountResponseStruct, GetRequestResponseStruct, ListAccountsResponseStruct, ListRequestsResponseStruct, RejectRequestResponseStruct, - SubmitRequestResponseStruct, - type ExportAccountResponse, type InternalRequest, type InternalResponse, - type SubmitRequestResponse, UpdateAccountResponseStruct, InternalResponseStruct, } from './internal/api'; diff --git a/src/api.ts b/src/api.ts index bed204b55..888e7fe5a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,10 +1,15 @@ import { type Json, JsonStruct } from '@metamask/utils'; -import { type Infer, array, enums, object, record, string } from 'superstruct'; +import { + type Infer, + array, + enums, + literal, + object, + record, + string, + union, +} from 'superstruct'; -import type { - ExportAccountResponse, - SubmitRequestResponse, -} from './internal/api'; import { JsonRpcRequestStruct } from './JsonRpcRequest'; import { UuidStruct } from './utils'; @@ -99,6 +104,22 @@ export const KeyringRequestStruct = object({ */ export type KeyringRequest = Infer; +export const ExportAccountResponseStruct = record(string(), JsonStruct); + +export type ExportAccountResponse = Infer; + +export const SubmitRequestResponseStruct = union([ + object({ + pending: literal(true), + }), + object({ + pending: literal(false), + result: JsonStruct, + }), +]); + +export type SubmitRequestResponse = Infer; + /** * Keyring interface. * diff --git a/src/internal/api.ts b/src/internal/api.ts index e1c559ad7..4d8303fae 100644 --- a/src/internal/api.ts +++ b/src/internal/api.ts @@ -9,7 +9,12 @@ import { type Infer, } from 'superstruct'; -import { KeyringAccountStruct, KeyringRequestStruct } from '../api'; +import { + ExportAccountResponseStruct, + KeyringAccountStruct, + KeyringRequestStruct, + SubmitRequestResponseStruct, +} from '../api'; import { UuidStruct } from '../utils'; const CommonHeader = { @@ -134,10 +139,6 @@ export const ExportAccountRequestStruct = object({ export type ExportAccountRequest = Infer; -export const ExportAccountResponseStruct = record(string(), JsonStruct); - -export type ExportAccountResponse = Infer; - // ---------------------------------------------------------------------------- // List requests @@ -180,17 +181,7 @@ export const SubmitRequestRequestStruct = object({ export type SubmitRequestRequest = Infer; -export const SubmitRequestResponseStruct = union([ - object({ - pending: literal(true), - }), - object({ - pending: literal(false), - result: JsonStruct, - }), -]); - -export type SubmitRequestResponse = Infer; +// Response type is defined in the public API. // ---------------------------------------------------------------------------- // Approve request From 66e61a03ea8165892ab5f39aae5e329d7d2952d5 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:28:48 +0200 Subject: [PATCH 6/6] refactor: remove circular dependency between APIs Removes the circular dependency between the `./api` and `./internal/api` modules. Now, the internal API depends on the public API, and redefines types when needed. --- src/KeyringClient.test.ts | 4 +-- src/KeyringClient.ts | 39 +++++++++------------- src/KeyringSnapControllerClient.ts | 7 ++-- src/KeyringSnapRpcClient.ts | 7 ++-- src/api.ts | 24 ++++++++++---- src/internal/api.ts | 53 +++++------------------------- 6 files changed, 53 insertions(+), 81 deletions(-) diff --git a/src/KeyringClient.test.ts b/src/KeyringClient.test.ts index f5a394ab3..2d6bc684f 100644 --- a/src/KeyringClient.test.ts +++ b/src/KeyringClient.test.ts @@ -1,7 +1,7 @@ import { type KeyringAccount, type KeyringRequest, - type SubmitRequestResponse, + type KeyringResponse, KeyringClient, } from '.'; // Import from `index.ts` to test the public API @@ -225,7 +225,7 @@ describe('KeyringClient', () => { params: ['0xe9a74aacd7df8112911ca93260fc5a046f8a64ae', '0x0'], }, }; - const expectedResponse: SubmitRequestResponse = { + const expectedResponse: KeyringResponse = { pending: true, }; diff --git a/src/KeyringClient.ts b/src/KeyringClient.ts index b3c82e64b..597f42900 100644 --- a/src/KeyringClient.ts +++ b/src/KeyringClient.ts @@ -2,36 +2,32 @@ import type { Json } from '@metamask/utils'; import { assert } from 'superstruct'; import { v4 as uuid } from 'uuid'; -import { - ExportAccountResponseStruct, - SubmitRequestResponseStruct, -} from './api'; import type { Keyring, KeyringAccount, KeyringRequest, - ExportAccountResponse, - SubmitRequestResponse, + KeyringAccountData, + KeyringResponse, } from './api'; import { ApproveRequestResponseStruct, CreateAccountResponseStruct, DeleteAccountResponseStruct, + ExportAccountResponseStruct, FilterAccountChainsResponseStruct, GetAccountResponseStruct, GetRequestResponseStruct, ListAccountsResponseStruct, ListRequestsResponseStruct, RejectRequestResponseStruct, - type InternalRequest, - type InternalResponse, + SubmitRequestResponseStruct, UpdateAccountResponseStruct, - InternalResponseStruct, } from './internal/api'; +import type { JsonRpcRequest } from './JsonRpcRequest'; import { type OmitUnion, strictMask } from './utils'; export type Sender = { - send(request: InternalRequest): Promise; + send(request: JsonRpcRequest): Promise; }; export class KeyringClient implements Keyring { @@ -49,20 +45,17 @@ export class KeyringClient implements Keyring { /** * Send a request to the snap and return the response. * - * @param partial - Partial internal request (method and params). + * @param partial - A partial JSON-RPC request (method and params). * @returns A promise that resolves to the response to the request. */ async #send( - partial: OmitUnion, - ): Promise { - return strictMask( - await this.#sender.send({ - jsonrpc: '2.0', - id: uuid(), - ...partial, - }), - InternalResponseStruct, - ); + partial: OmitUnion, + ): Promise { + return this.#sender.send({ + jsonrpc: '2.0', + id: uuid(), + ...partial, + }); } async listAccounts(): Promise { @@ -126,7 +119,7 @@ export class KeyringClient implements Keyring { ); } - async exportAccount(id: string): Promise { + async exportAccount(id: string): Promise { return strictMask( await this.#send({ method: 'keyring_exportAccount', @@ -155,7 +148,7 @@ export class KeyringClient implements Keyring { ); } - async submitRequest(request: KeyringRequest): Promise { + async submitRequest(request: KeyringRequest): Promise { return strictMask( await this.#send({ method: 'keyring_submitRequest', diff --git a/src/KeyringSnapControllerClient.ts b/src/KeyringSnapControllerClient.ts index 3dd88702c..536e474c2 100644 --- a/src/KeyringSnapControllerClient.ts +++ b/src/KeyringSnapControllerClient.ts @@ -1,7 +1,8 @@ import type { SnapController } from '@metamask/snaps-controllers'; import type { HandlerType, ValidatedSnapId } from '@metamask/snaps-utils'; +import type { Json } from '@metamask/utils'; -import type { InternalRequest, InternalResponse } from './internal/api'; +import type { JsonRpcRequest } from './JsonRpcRequest'; import { KeyringClient, type Sender } from './KeyringClient'; /** @@ -43,13 +44,13 @@ class SnapControllerSender implements Sender { * @param request - JSON-RPC request to send to the snap. * @returns A promise that resolves to the response of the request. */ - async send(request: InternalRequest): Promise { + async send(request: JsonRpcRequest): Promise { return this.#controller.handleRequest({ snapId: this.#snapId as ValidatedSnapId, origin: this.#origin, handler: this.#handler, request, - }) as Promise; + }) as Promise; } } diff --git a/src/KeyringSnapRpcClient.ts b/src/KeyringSnapRpcClient.ts index e0259d33a..e03528371 100644 --- a/src/KeyringSnapRpcClient.ts +++ b/src/KeyringSnapRpcClient.ts @@ -1,6 +1,7 @@ import type { MetaMaskInpageProvider } from '@metamask/providers'; +import type { Json } from '@metamask/utils'; -import type { InternalRequest, InternalResponse } from './internal/api'; +import type { JsonRpcRequest } from './JsonRpcRequest'; import { KeyringClient, type Sender } from './KeyringClient'; /** @@ -29,14 +30,14 @@ export class SnapRpcSender implements Sender { * @param request - The JSON-RPC request to send to the snap. * @returns A promise that resolves to the response of the request. */ - async send(request: InternalRequest): Promise { + async send(request: JsonRpcRequest): Promise { return this.#provider.request({ method: 'wallet_invokeSnap', params: { snapId: this.#origin, request, }, - }) as Promise; + }) as Promise; } } diff --git a/src/api.ts b/src/api.ts index 888e7fe5a..a6f0e5424 100644 --- a/src/api.ts +++ b/src/api.ts @@ -104,11 +104,16 @@ export const KeyringRequestStruct = object({ */ export type KeyringRequest = Infer; -export const ExportAccountResponseStruct = record(string(), JsonStruct); +export const KeyringAccountDataStruct = record(string(), JsonStruct); -export type ExportAccountResponse = Infer; +/** + * Response to a call to `exportAccount`. + * + * The exact response depends on the keyring implementation. + */ +export type KeyringAccountData = Infer; -export const SubmitRequestResponseStruct = union([ +export const KeyringResponseStruct = union([ object({ pending: literal(true), }), @@ -118,7 +123,14 @@ export const SubmitRequestResponseStruct = union([ }), ]); -export type SubmitRequestResponse = Infer; +/** + * Response to a call to `submitRequest`. + * + * Keyring implementations must return a response with `pending: true` if the + * request will be handled asynchronously. Otherwise, the response must contain + * the result of the request and `pending: false`. + */ +export type KeyringResponse = Infer; /** * Keyring interface. @@ -199,7 +211,7 @@ export type Keyring = { * @param id - The ID of the account to export. * @returns A promise that resolves to the exported account. */ - exportAccount(id: string): Promise; + exportAccount(id: string): Promise; /** * List all submitted requests. @@ -230,7 +242,7 @@ export type Keyring = { * @param request - The KeyringRequest object to submit. * @returns A promise that resolves to the request response. */ - submitRequest(request: KeyringRequest): Promise; + submitRequest(request: KeyringRequest): Promise; /** * Approve a request. diff --git a/src/internal/api.ts b/src/internal/api.ts index 4d8303fae..3d61ac25c 100644 --- a/src/internal/api.ts +++ b/src/internal/api.ts @@ -5,15 +5,14 @@ import { object, record, string, - union, type Infer, } from 'superstruct'; import { - ExportAccountResponseStruct, + KeyringAccountDataStruct, KeyringAccountStruct, KeyringRequestStruct, - SubmitRequestResponseStruct, + KeyringResponseStruct, } from '../api'; import { UuidStruct } from '../utils'; @@ -139,6 +138,10 @@ export const ExportAccountRequestStruct = object({ export type ExportAccountRequest = Infer; +export const ExportAccountResponseStruct = KeyringAccountDataStruct; + +export type ExportAccountResponse = Infer; + // ---------------------------------------------------------------------------- // List requests @@ -181,7 +184,9 @@ export const SubmitRequestRequestStruct = object({ export type SubmitRequestRequest = Infer; -// Response type is defined in the public API. +export const SubmitRequestResponseStruct = KeyringResponseStruct; + +export type SubmitRequestResponse = Infer; // ---------------------------------------------------------------------------- // Approve request @@ -217,43 +222,3 @@ export type RejectRequestRequest = Infer; export const RejectRequestResponseStruct = literal(null); export type RejectRequestResponse = Infer; - -// ---------------------------------------------------------------------------- -// Internal request - -export const InternalRequestStruct = union([ - ListAccountsRequestStruct, - GetAccountRequestStruct, - CreateAccountRequestStruct, - FilterAccountChainsStruct, - UpdateAccountRequestStruct, - DeleteAccountRequestStruct, - ExportAccountRequestStruct, - ListRequestsRequestStruct, - GetRequestRequestStruct, - SubmitRequestRequestStruct, - ApproveRequestRequestStruct, - RejectRequestRequestStruct, -]); - -export type InternalRequest = Infer; - -// ---------------------------------------------------------------------------- -// Internal response - -export const InternalResponseStruct = union([ - ListAccountsResponseStruct, - GetAccountResponseStruct, - CreateAccountResponseStruct, - FilterAccountChainsResponseStruct, - UpdateAccountResponseStruct, - DeleteAccountResponseStruct, - ExportAccountResponseStruct, - ListRequestsResponseStruct, - GetRequestResponseStruct, - SubmitRequestResponseStruct, - ApproveRequestResponseStruct, - RejectRequestResponseStruct, -]); - -export type InternalResponse = Infer;