|
| 1 | +import type { AccountId } from '@metamask/keyring-utils'; |
| 2 | +import { UuidStruct } from '@metamask/keyring-utils'; |
| 3 | +import type { Infer } from '@metamask/superstruct'; |
| 4 | +import { |
| 5 | + array, |
| 6 | + literal, |
| 7 | + number, |
| 8 | + object, |
| 9 | + record, |
| 10 | + string, |
| 11 | + union, |
| 12 | +} from '@metamask/superstruct'; |
| 13 | + |
| 14 | +import { BalanceStruct } from '../../api/balance'; |
| 15 | +import type { Balance } from '../../api/balance'; |
| 16 | +import { CaipAssetTypeOrIdStruct, CaipAssetTypeStruct } from '../../api/caip'; |
| 17 | +import type { CaipAssetType, CaipAssetTypeOrId } from '../../api/caip'; |
| 18 | +import { PaginationStruct } from '../../api/pagination'; |
| 19 | +import type { Pagination } from '../../api/pagination'; |
| 20 | +import { TransactionsPageStruct } from '../../api/transaction'; |
| 21 | +import type { TransactionsPage } from '../../api/transaction'; |
| 22 | +import { KeyringRpcMethod } from './keyring-rpc'; |
| 23 | +import type { KeyringRpc, KeyringRpcRequests } from './keyring-rpc'; |
| 24 | + |
| 25 | +/** |
| 26 | + * All keyring RPC methods available to a Snap - includes the base |
| 27 | + * {@link KeyringRpcMethod} set plus snap-specific extensions. |
| 28 | + */ |
| 29 | +export const KeyringSnapRpcMethod = { |
| 30 | + ...KeyringRpcMethod, |
| 31 | + SetSelectedAccounts: 'keyring_setSelectedAccounts', |
| 32 | + GetAccountTransactions: 'keyring_getAccountTransactions', |
| 33 | + GetAccountAssets: 'keyring_getAccountAssets', |
| 34 | + GetAccountBalances: 'keyring_getAccountBalances', |
| 35 | +} as const; |
| 36 | + |
| 37 | +/** |
| 38 | + * All keyring RPC methods available to a Snap. |
| 39 | + */ |
| 40 | +export type KeyringSnapRpcMethod = |
| 41 | + (typeof KeyringSnapRpcMethod)[keyof typeof KeyringSnapRpcMethod]; |
| 42 | + |
| 43 | +/** |
| 44 | + * Check if a method is a Snap keyring RPC method (v2). |
| 45 | + * |
| 46 | + * @param method - Method to check. |
| 47 | + * @returns Whether the method is a Snap keyring RPC method (v2). |
| 48 | + */ |
| 49 | +export function isKeyringSnapRpcMethod( |
| 50 | + method: string, |
| 51 | +): method is KeyringSnapRpcMethod { |
| 52 | + return Object.values(KeyringSnapRpcMethod).includes( |
| 53 | + method as KeyringSnapRpcMethod, |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +// ---------------------------------------------------------------------------- |
| 58 | + |
| 59 | +const CommonHeader = { |
| 60 | + jsonrpc: literal('2.0'), |
| 61 | + id: union([string(), number(), literal(null)]), |
| 62 | +}; |
| 63 | + |
| 64 | +// ---------------------------------------------------------------------------- |
| 65 | +// Set selected accounts |
| 66 | + |
| 67 | +export const SetSelectedAccountsRequestStruct = object({ |
| 68 | + ...CommonHeader, |
| 69 | + method: literal(`${KeyringSnapRpcMethod.SetSelectedAccounts}`), |
| 70 | + params: object({ |
| 71 | + accounts: array(string()), |
| 72 | + }), |
| 73 | +}); |
| 74 | + |
| 75 | +export type SetSelectedAccountsRequest = Infer< |
| 76 | + typeof SetSelectedAccountsRequestStruct |
| 77 | +>; |
| 78 | + |
| 79 | +export const SetSelectedAccountsResponseStruct = literal(null); |
| 80 | + |
| 81 | +export type SetSelectedAccountsResponse = Infer< |
| 82 | + typeof SetSelectedAccountsResponseStruct |
| 83 | +>; |
| 84 | + |
| 85 | +// ---------------------------------------------------------------------------- |
| 86 | +// Get account transactions |
| 87 | + |
| 88 | +export const GetAccountTransactionsRequestStruct = object({ |
| 89 | + ...CommonHeader, |
| 90 | + method: literal(`${KeyringSnapRpcMethod.GetAccountTransactions}`), |
| 91 | + params: object({ |
| 92 | + id: UuidStruct, |
| 93 | + pagination: PaginationStruct, |
| 94 | + }), |
| 95 | +}); |
| 96 | + |
| 97 | +export type GetAccountTransactionsRequest = Infer< |
| 98 | + typeof GetAccountTransactionsRequestStruct |
| 99 | +>; |
| 100 | + |
| 101 | +export const GetAccountTransactionsResponseStruct = TransactionsPageStruct; |
| 102 | + |
| 103 | +export type GetAccountTransactionsResponse = Infer< |
| 104 | + typeof GetAccountTransactionsResponseStruct |
| 105 | +>; |
| 106 | + |
| 107 | +// ---------------------------------------------------------------------------- |
| 108 | +// Get account assets |
| 109 | + |
| 110 | +export const GetAccountAssetsRequestStruct = object({ |
| 111 | + ...CommonHeader, |
| 112 | + method: literal(`${KeyringSnapRpcMethod.GetAccountAssets}`), |
| 113 | + params: object({ |
| 114 | + id: UuidStruct, |
| 115 | + }), |
| 116 | +}); |
| 117 | + |
| 118 | +export type GetAccountAssetsRequest = Infer< |
| 119 | + typeof GetAccountAssetsRequestStruct |
| 120 | +>; |
| 121 | + |
| 122 | +export const GetAccountAssetsResponseStruct = array(CaipAssetTypeOrIdStruct); |
| 123 | + |
| 124 | +export type GetAccountAssetsResponse = Infer< |
| 125 | + typeof GetAccountAssetsResponseStruct |
| 126 | +>; |
| 127 | + |
| 128 | +// ---------------------------------------------------------------------------- |
| 129 | +// Get account balances |
| 130 | + |
| 131 | +export const GetAccountBalancesRequestStruct = object({ |
| 132 | + ...CommonHeader, |
| 133 | + method: literal(`${KeyringSnapRpcMethod.GetAccountBalances}`), |
| 134 | + params: object({ |
| 135 | + id: UuidStruct, |
| 136 | + assets: array(CaipAssetTypeStruct), |
| 137 | + }), |
| 138 | +}); |
| 139 | + |
| 140 | +export type GetAccountBalancesRequest = Infer< |
| 141 | + typeof GetAccountBalancesRequestStruct |
| 142 | +>; |
| 143 | + |
| 144 | +export const GetAccountBalancesResponseStruct = record( |
| 145 | + CaipAssetTypeStruct, |
| 146 | + BalanceStruct, |
| 147 | +); |
| 148 | + |
| 149 | +export type GetAccountBalancesResponse = Infer< |
| 150 | + typeof GetAccountBalancesResponseStruct |
| 151 | +>; |
| 152 | + |
| 153 | +// ---------------------------------------------------------------------------- |
| 154 | + |
| 155 | +/** |
| 156 | + * All keyring RPC requests available to a Snap - includes base |
| 157 | + * {@link KeyringRpcRequests} plus snap-specific request types. |
| 158 | + */ |
| 159 | +export type KeyringSnapRpcRequests = |
| 160 | + | KeyringRpcRequests |
| 161 | + | SetSelectedAccountsRequest |
| 162 | + | GetAccountTransactionsRequest |
| 163 | + | GetAccountAssetsRequest |
| 164 | + | GetAccountBalancesRequest; |
| 165 | + |
| 166 | +/** |
| 167 | + * Extract the proper request type for a given {@link KeyringSnapRpcMethod}. |
| 168 | + */ |
| 169 | +export type KeyringSnapRpcRequest<RpcMethod extends KeyringSnapRpcMethod> = |
| 170 | + Extract<KeyringSnapRpcRequests, { method: `${RpcMethod}` }>; |
| 171 | + |
| 172 | +// ---------------------------------------------------------------------------- |
| 173 | + |
| 174 | +/** |
| 175 | + * Snap keyring RPC interface - extends the base {@link KeyringRpc} with |
| 176 | + * optional snap-specific methods that a Snap may expose. |
| 177 | + */ |
| 178 | +export type KeyringSnapRpc = KeyringRpc & { |
| 179 | + /** |
| 180 | + * Notify the Snap of the currently selected accounts. |
| 181 | + * Maps to `keyring_setSelectedAccounts`. |
| 182 | + */ |
| 183 | + setSelectedAccounts?: (accounts: AccountId[]) => Promise<void>; |
| 184 | + |
| 185 | + /** |
| 186 | + * Get transactions for an account with pagination. |
| 187 | + * Maps to `keyring_getAccountTransactions`. |
| 188 | + */ |
| 189 | + getAccountTransactions?: ( |
| 190 | + id: AccountId, |
| 191 | + pagination: Pagination, |
| 192 | + ) => Promise<TransactionsPage>; |
| 193 | + |
| 194 | + /** |
| 195 | + * Get the asset types supported by an account. |
| 196 | + * Maps to `keyring_getAccountAssets`. |
| 197 | + */ |
| 198 | + getAccountAssets?: (id: AccountId) => Promise<CaipAssetTypeOrId[]>; |
| 199 | + |
| 200 | + /** |
| 201 | + * Get balances for an account for the requested asset types. |
| 202 | + * Maps to `keyring_getAccountBalances`. |
| 203 | + */ |
| 204 | + getAccountBalances?: ( |
| 205 | + id: AccountId, |
| 206 | + assets: CaipAssetType[], |
| 207 | + ) => Promise<Record<CaipAssetType, Balance>>; |
| 208 | +}; |
0 commit comments