diff --git a/packages/wallet-sdk/src/provider/JSONRPC.ts b/packages/wallet-sdk/src/provider/JSONRPC.ts new file mode 100644 index 0000000000..c861265480 --- /dev/null +++ b/packages/wallet-sdk/src/provider/JSONRPC.ts @@ -0,0 +1,179 @@ +// Copyright (c) 2018-2022 Coinbase, Inc. +// Licensed under the Apache License, version 2.0 + +import { AddressString, HexString } from '../core/type'; + +export type JSONRPCMethodName = keyof typeof JSONRPCMethods; + +export function isValidJSONRPCMethod(method: string): method is JSONRPCMethodName { + return method in JSONRPCMethods; +} + +export type JSONRPCRequest = { + jsonrpc: '2.0'; + id: number; + method: M; + params: (typeof JSONRPCMethods)[M]['params']; +}; + +export type JSONRPCResponse = { + jsonrpc: '2.0'; + id: number; +} & ( + | { + error: { + code: number; + message: string; + data?: unknown; + }; + } + | { + result: (typeof JSONRPCMethods)[M]['result']; + } +); + +const JSONRPCMethods = { + eth_accounts: { + params: {} as never, + result: {} as AddressString[], + }, + eth_coinbase: { + params: {} as never, + result: {} as AddressString, + }, + net_version: { + params: {} as never, + result: {} as string, + }, + eth_chainId: { + params: {} as never, + result: {} as string, + }, + eth_uninstallFilter: { + params: {} as [HexString], + result: {} as boolean, + }, + eth_requestAccounts: { + params: {} as never, + result: {} as AddressString[], + }, + eth_sign: { + params: {} as [AddressString, Buffer], + result: {} as HexString, + }, + eth_ecRecover: { + params: {} as [Buffer, Buffer], + result: {} as AddressString, + }, + personal_sign: { + params: {} as [Buffer, AddressString], + result: {} as HexString, + }, + personal_ecRecover: { + params: {} as [Buffer, Buffer], + result: {} as AddressString, + }, + eth_signTransaction: { + params: {} as EthereumTransactionParams, + result: {} as HexString, + }, + eth_sendRawTransaction: { + params: {} as [Buffer], + result: {} as HexString, + }, + eth_sendTransaction: { + params: {} as EthereumTransactionParams, + result: {} as HexString, + }, + eth_signTypedData_v1: { + params: {} as [object, AddressString], + result: {} as HexString, + }, + eth_signTypedData_v2: { + params: {} as never, + result: {} as never, + }, + eth_signTypedData_v3: { + params: {} as [AddressString, object], + result: {} as HexString, + }, + eth_signTypedData_v4: { + params: {} as [AddressString, object], + result: {} as HexString, + }, + eth_signTypedData: { + params: {} as [AddressString, object], + result: {} as HexString, + }, + wallet_addEthereumChain: { + params: {} as [ + { + chainId: string; + blockExplorerUrls?: string[]; + chainName?: string; + iconUrls?: string[]; + rpcUrls?: string[]; + nativeCurrency?: { + name: string; + symbol: string; + decimals: number; + }; + }, + ], + result: null, + }, + wallet_switchEthereumChain: { + params: {} as [ + { + chainId: string; + }, + ], + result: null, + }, + wallet_watchAsset: { + params: {} as { + type: string; + options: { + address: string; + symbol: string; + decimals: number; + image?: string; + }; + }, + result: {} as boolean, + }, + eth_subscribe: { + params: {} as [ + 'newHeads' | 'logs' | 'newPendingTransactions' | 'syncing', + { + address?: AddressString; + topics?: string[]; + }, + ], + result: {} as unknown, + }, + eth_unsubscribe: { + params: {} as string[], + result: {} as unknown, + }, + eth_newFilter: { + params: {} as unknown, + result: {} as HexString, + }, + eth_newBlockFilter: { + params: {} as never, + result: {} as HexString, + }, + eth_newPendingTransactionFilter: { + params: {} as never, + result: {} as HexString, + }, + eth_getFilterChanges: { + params: {} as [HexString], + result: {} as unknown, + }, + eth_getFilterLogs: { + params: {} as [HexString], + result: {} as unknown, + }, +} as const; diff --git a/packages/wallet-sdk/src/provider/JSONRPCMethod.ts b/packages/wallet-sdk/src/provider/JSONRPCMethod.ts deleted file mode 100644 index b5328bd689..0000000000 --- a/packages/wallet-sdk/src/provider/JSONRPCMethod.ts +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2018-2023 Coinbase, Inc. -// Licensed under the Apache License, version 2.0 - -export type JSONRPCMethod = - // synchronous or asynchronous - | 'eth_accounts' - | 'eth_coinbase' - | 'net_version' - | 'eth_chainId' - | 'eth_uninstallFilter' // synchronous - - // asynchronous only - | 'eth_requestAccounts' - | 'eth_sign' - | 'eth_ecRecover' - | 'personal_sign' - | 'personal_ecRecover' - | 'eth_signTransaction' - | 'eth_sendRawTransaction' - | 'eth_sendTransaction' - | 'eth_signTypedData_v1' - | 'eth_signTypedData_v2' - | 'eth_signTypedData_v3' - | 'eth_signTypedData_v4' - | 'eth_signTypedData' - | 'wallet_addEthereumChain' - | 'wallet_switchEthereumChain' - | 'wallet_watchAsset' - - // asynchronous pub/sub - | 'eth_subscribe' - | 'eth_unsubscribe' - - // asynchronous filter methods - | 'eth_newFilter' - | 'eth_newBlockFilter' - | 'eth_newPendingTransactionFilter' - | 'eth_getFilterChanges' - | 'eth_getFilterLogs'; diff --git a/packages/wallet-sdk/src/provider/JSONRPCRequest.ts b/packages/wallet-sdk/src/provider/JSONRPCRequest.ts deleted file mode 100644 index 070ac261e4..0000000000 --- a/packages/wallet-sdk/src/provider/JSONRPCRequest.ts +++ /dev/null @@ -1,172 +0,0 @@ -import BN from 'bn.js'; - -import { AddressString, HexString, IntNumber } from '../core/type'; -import { JSONRPCMethodName } from './JSONRPC'; - -type EthereumTransactionParams = { - fromAddress: AddressString; - toAddress?: AddressString; - weiValue: BN; - data: Buffer; - nonce?: IntNumber; - gasPriceInWei?: BN; - maxFeePerGas?: BN; // in wei - maxPriorityFeePerGas?: BN; // in wei - gasLimit?: BN; - chainId: IntNumber; -}; - -type AddEthereumChainParams = { - chainId: string; - blockExplorerUrls?: string[]; - chainName?: string; - iconUrls?: string[]; - rpcUrls?: string[]; - nativeCurrency?: { - name: string; - symbol: string; - decimals: number; - }; -}; - -type WatchAssetParams = { - type: string; - options: { - address: string; - symbol?: string; - decimals?: number; - image?: string; - }; -}; - -type SubscriptionType = 'newHeads' | 'logs' | 'newPendingTransactions' | 'syncing'; - -export type JSONRPCRequest = { - jsonrpc: '2.0'; - id: number; - method: JSONRPCMethodName; -} & ( - | { - method: 'eth_accounts'; - params: never; - } - | { - method: 'eth_coinbase'; - params: never; - } - | { - method: 'net_version'; - params: never; - } - | { - method: 'eth_chainId'; - params: never; - } - | { - method: 'eth_uninstallFilter'; - params: [HexString]; - } - | { - method: 'eth_requestAccounts'; - params: never; - } - | { - method: 'eth_sign'; - params: [AddressString, Buffer]; - } - | { - method: 'eth_ecRecover'; - params: [Buffer, Buffer]; - } - | { - method: 'personal_sign'; - params: [Buffer, AddressString]; - } - | { - method: 'personal_ecRecover'; - params: [Buffer, Buffer]; - } - | { - method: 'eth_signTransaction'; - params: EthereumTransactionParams; - } - | { - method: 'eth_sendRawTransaction'; - params: [Buffer]; - } - | { - method: 'eth_sendTransaction'; - params: EthereumTransactionParams; - } - | { - method: 'eth_signTypedData_v1'; - params: [object, AddressString]; - } - | { - // This is not supported - method: 'eth_signTypedData_v2'; - params: never; - } - | { - method: 'eth_signTypedData_v3'; - params: [AddressString, object]; - } - | { - method: 'eth_signTypedData_v4'; - params: [AddressString, object]; - } - | { - method: 'eth_signTypedData'; - params: [AddressString, object]; - } - | { - method: 'wallet_addEthereumChain'; - params: [AddEthereumChainParams]; - } - | { - method: 'wallet_switchEthereumChain'; - params: [ - { - chainId: string; - }, - ]; - } - | { - method: 'wallet_watchAsset'; - params: WatchAssetParams | [WatchAssetParams]; - } - | { - method: 'eth_subscribe'; - params: [ - SubscriptionType, - { - address?: AddressString; - topics?: string[]; - }, - ]; - } - | { - method: 'eth_unsubscribe'; - params: string[]; - } - | { - method: 'eth_newFilter'; - params: unknown; - } - | { - method: 'eth_newBlockFilter'; - params: never; - } - | { - method: 'eth_newPendingTransactionFilter'; - params: never; - } - | { - method: 'eth_getFilterChanges'; - params: [HexString]; - } - | { - method: 'eth_getFilterLogs'; - params: [HexString]; - } -); diff --git a/packages/wallet-sdk/src/provider/JSONRPCResponse.ts b/packages/wallet-sdk/src/provider/JSONRPCResponse.ts deleted file mode 100644 index d47fc0fff5..0000000000 --- a/packages/wallet-sdk/src/provider/JSONRPCResponse.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { AddressString, HexString } from '../core/type'; -import { JSONRPCMethodName } from './JSONRPC'; - -export type JSONRPCResponse = { - jsonrpc: '2.0'; - id: number; - method?: JSONRPCMethodName; -} & ( - | { - error: { - code: number; - message: string; - data?: unknown; - }; - } - | { - method: 'eth_accounts'; - result: string[]; - } - | { - method: 'eth_coinbase'; - result: string; - } - | { - method: 'net_version'; - result: string; - } - | { - method: 'eth_chainId'; - result: string; - } - | { - method: 'eth_uninstallFilter'; - result: boolean; - } - | { - method: 'eth_requestAccounts'; - result: AddressString[]; - } - | { - method: 'eth_sign'; - result: HexString; - } - | { - method: 'eth_ecRecover'; - result: AddressString; - } - | { - method: 'personal_sign'; - result: HexString; - } - | { - method: 'personal_ecRecover'; - result: AddressString; - } - | { - method: 'eth_signTransaction'; - result: HexString; - } - | { - method: 'eth_sendRawTransaction'; - result: HexString; - } - | { - method: 'eth_sendTransaction'; - result: HexString; - } - | { - method: 'eth_signTypedData_v1'; - result: HexString; - } - | { - method: 'eth_signTypedData_v2'; - result: never; - } - | { - method: 'eth_signTypedData_v3'; - result: HexString; - } - | { - method: 'eth_signTypedData_v4'; - result: HexString; - } - | { - method: 'eth_signTypedData'; - result: HexString; - } - | { - method: 'wallet_addEthereumChain'; - result: null; - } - | { - method: 'wallet_switchEthereumChain'; - result: null; - } - | { - method: 'wallet_watchAsset'; - result: boolean; - } - | { - method: 'eth_subscribe'; - result: unknown; - } - | { - method: 'eth_unsubscribe'; - result: unknown; - } - | { - method: 'eth_newFilter'; - result: HexString; - } - | { - method: 'eth_newBlockFilter'; - result: HexString; - } - | { - method: 'eth_newPendingTransactionFilter'; - result: HexString; - } - | { - method: 'eth_getFilterChanges'; - result: unknown; - } - | { - method: 'eth_getFilterLogs'; - result: unknown; - } -);