diff --git a/packages/mask-sdk/public-api/mask-wallet.ts b/packages/mask-sdk/public-api/mask-wallet.ts index e2391596530..daf3d255ab4 100644 --- a/packages/mask-sdk/public-api/mask-wallet.ts +++ b/packages/mask-sdk/public-api/mask-wallet.ts @@ -219,24 +219,57 @@ export declare namespace Ethereum.RPC { } } interface ImplementedMethods { - // Readonly methods - eth_getCode(address: string, block?: string): string - eth_gasPrice(): string - eth_blockNumber(): string - eth_getBalance(address: string, block?: string): string - eth_getBlockByNumber(block: string, hydrated_transactions: boolean): Block | null - eth_getBlockByHash(hash: string, hydrated_transactions: boolean): Block | null - eth_getTransactionByHash(hash: string): Transaction | null - eth_getTransactionReceipt(transaction_hash: string): Receipt | null - eth_getTransactionCount(address: string, block?: string): string - eth_getFilterChanges(id: string): string[] | Log[] - eth_newPendingTransactionFilter(): string - eth_estimateGas(transaction: Transaction, block?: string): string - eth_call(transaction: Transaction, block?: string): string - eth_getLogs(filter: Filter): string[] | Log[] + net_version: () => string + eth_accounts: () => string[] + eth_blockNumber: () => string + eth_call: (transaction: Transaction, block?: string) => string + eth_chainId: () => string + eth_estimateGas: (transaction: Transaction, block?: string) => string + eth_feeHistory: (args_0: string | number, args_1: string, args_2: number[]) => any + eth_gasPrice: () => string + eth_getBalance: (address: string, block?: string | null | undefined) => string + eth_getBlockByHash: (hash: string, hydrated_transactions?: boolean | null | undefined) => Block | null + eth_getBlockByNumber: (block: string, hydrated_transactions?: boolean | null | undefined) => Block | null + eth_getBlockReceipts: (args_0: string) => any + eth_getBlockTransactionCountByHash: (args_0: string) => string | null | undefined + eth_getBlockTransactionCountByNumber: (args_0: string) => string | null | undefined + eth_getCode: (address: string, block?: string | null | undefined) => string + eth_getLogs: (filter: Filter) => string[] | Log[] + eth_getProof: (args_0: string, args_1: string[], args_2: string) => any + eth_getStorageAt: (args_0: string, args_1: string, args_2: string | null | undefined) => string + eth_getTransactionByBlockHashAndIndex: (args_0: string, args_1: string) => any + eth_getTransactionByBlockNumberAndIndex: (args_0: string, args_1: string) => any + eth_getTransactionByHash: (hash: string) => Transaction | null + eth_getTransactionCount: (address: string, block?: string | null | undefined) => string + eth_getTransactionReceipt: (transaction_hash: string) => Receipt | null + eth_getUncleCountByBlockHash: (args_0: string) => string | null | undefined + eth_getUncleCountByBlockNumber: (args_0: string) => string | null | undefined + eth_syncing: () => any - // https://eips.ethereum.org/EIPS/eip-2255 - wallet_getPermissions(): EIP2255Permission[] + personal_sign: (args_0: string, args_1: string) => string + eth_sendTransaction: (transaction: any) => any + eth_sendRawTransaction: (args_0: string) => string + eth_subscribe: ( + args_0: 'newHeads' | 'logs' | 'newPendingTransactions' | 'syncing', + args_1: + | { + topics: string[] + address?: string | string[] | null | undefined + } + | null + | undefined, + ) => string + eth_unsubscribe: (args_0: string) => boolean + + eth_getFilterChanges: (id: string) => string[] | Log[] + eth_getFilterLogs: (args_0: string) => any + eth_newBlockFilter: () => string + eth_newFilter: (args_0: any) => string + eth_uninstallFilter: (args_0: string) => boolean + + eth_requestAccounts: () => string[] + + wallet_getPermissions: () => EIP2255Permission[] wallet_requestPermissions(request: EIP2255PermissionRequest): EIP2255RequestedPermission[] } } diff --git a/packages/mask/entry-sdk/README.md b/packages/mask/entry-sdk/README.md index e126964b3f8..8bd11e0853c 100644 --- a/packages/mask/entry-sdk/README.md +++ b/packages/mask/entry-sdk/README.md @@ -5,12 +5,11 @@ The list is built from what [MetaMask supported](https://docs.metamask.io/wallet - -- [ ] Do params check before sending them to the background. - ## Need revisit ## Read ETH methods +- [x] net_version - [x] eth_accounts - [x] eth_blockNumber - [x] eth_call diff --git a/packages/mask/entry-sdk/bridge/eth.ts b/packages/mask/entry-sdk/bridge/eth.ts index c88d5a0b35c..0b7a967d281 100644 --- a/packages/mask/entry-sdk/bridge/eth.ts +++ b/packages/mask/entry-sdk/bridge/eth.ts @@ -22,7 +22,10 @@ type PassthroughMethods = (typeof PassthroughMethods)[number] const passthroughMethods: Record Promise> = {} as any for (const method of PassthroughMethods) { passthroughMethods[method] = async (...params: unknown[]) => { - return (await Services.Wallet.send({ jsonrpc: '2.0', method, params })).result + return providers.EVMWeb3.getWeb3Provider({ + providerType: ProviderType.MaskWallet, + readonly: true, + }).request({ method, params }) } } diff --git a/packages/mask/entry-sdk/bridge/eth/validator.ts b/packages/mask/entry-sdk/bridge/eth/validator.ts index 04bb5cf5128..7f39c7367e2 100644 --- a/packages/mask/entry-sdk/bridge/eth/validator.ts +++ b/packages/mask/entry-sdk/bridge/eth/validator.ts @@ -37,16 +37,16 @@ namespace _ { export const decimal = z.number().min(0).max(36) export const filter = z .object({ - fromBlock: unpadded_hex.nullish().nullable(), - toBlock: unpadded_hex.nullish().nullable(), - address: address.or(address.array()).nullable(), - topics: z.any().nullable(), + fromBlock: unpadded_hex.nullish(), + toBlock: unpadded_hex.nullish(), + address: address.or(address.array()).nullish(), + topics: z.any().nullish(), }) .partial() .strict() .describe('Filter') export const filter_identifier = unpadded_hex.describe('FilterIdentifier') - export const hydrated_transactions = z.boolean().nullable().describe('HydratedTransactions') + export const hydrated_transactions = z.boolean().nullish().describe('HydratedTransactions') export const subscription_id = z.string() export const symbol = z.string().min(2).max(11) export const transaction = z @@ -54,18 +54,18 @@ namespace _ { type: z .string() .regex(/^0x([\d,A-Fa-f]?){1,2}$/g) - .nullable(), - nonce: unpadded_hex.nullable(), - to: address.nullable(), - from: address.nullable(), - gas: unpadded_hex.nullable(), - value: unpadded_hex.nullable(), - data: hexAllowCap.nullable(), - input: hex.nullable(), - gasPrice: unpadded_hex.nullable(), - maxPriorityFeePerGas: unpadded_hex.nullable(), - maxFeePerGas: unpadded_hex.nullable(), - maxFeePerBlobGas: unpadded_hex.nullable(), + .nullish(), + nonce: unpadded_hex.nullish(), + to: address.nullish(), + from: address.nullish(), + gas: unpadded_hex.nullish(), + value: unpadded_hex.nullish(), + data: hexAllowCap.nullish(), + input: hex.nullish(), + gasPrice: unpadded_hex.nullish(), + maxPriorityFeePerGas: unpadded_hex.nullish(), + maxFeePerGas: unpadded_hex.nullish(), + maxFeePerBlobGas: unpadded_hex.nullish(), accessList: z .object({ address, @@ -73,9 +73,9 @@ namespace _ { }) .strict() .array() - .nullable(), - blobVersionedHashes: z.string().array().nullable(), - blobs: z.string().array().nullable(), + .nullish(), + blobVersionedHashes: z.string().array().nullish(), + blobs: z.string().array().nullish(), chainId, }) .partial() @@ -112,6 +112,7 @@ namespace _ { // No error message interpolation support until https://github.com/colinhacks/zod/issues/3048 export const ParamsValidate = { + net_version: z.tuple([]), eth_subscribe: z.tuple([ z.enum(['newHeads', 'logs', 'newPendingTransactions', 'syncing']).describe('subscriptionType'), z @@ -187,14 +188,15 @@ export const ParamsValidate = { // ! not same as _.transaction z .object({ - to: _.address.nullish(), + to: _.address.nullish().optional(), from: _.address, - gas: _.unpadded_hex.nullish(), - value: _.unpadded_hex.nullish(), - data: _.hex.nullish(), - gasPrice: _.unpadded_hex.nullish(), - maxPriorityFeePerGas: _.unpadded_hex.nullish(), - maxFeePerGas: _.unpadded_hex.nullish(), + gas: _.unpadded_hex.nullish().optional(), + value: _.unpadded_hex.nullish().optional(), + data: _.hex.nullish().optional(), + gasLimit: _.unpadded_hex.nullish().optional(), + gasPrice: _.unpadded_hex.nullish().optional(), + maxPriorityFeePerGas: _.unpadded_hex.nullish().optional(), + maxFeePerGas: _.unpadded_hex.nullish().optional(), }) .strict() .describe('Transaction'), @@ -246,6 +248,7 @@ export const ParamsValidate = { eth_uninstallFilter: z.tuple([_.filter_identifier]), } export const ReturnValidate = { + net_version: z.string(), eth_subscribe: _.subscription_id, eth_unsubscribe: z.boolean(), wallet_addEthereumChain: z.null(), diff --git a/packages/web3-providers/src/types/Wallet.ts b/packages/web3-providers/src/types/Wallet.ts index 229d0a8c13e..9a5684fe495 100644 --- a/packages/web3-providers/src/types/Wallet.ts +++ b/packages/web3-providers/src/types/Wallet.ts @@ -76,15 +76,13 @@ export namespace WalletAPI { ) => Promise export interface MessageIOContext { /** Send request to native API, for a risky request will be added into the waiting queue. */ - send(payload: JsonRpcPayload, options?: TransactionOptions): Promise + send(payload: JsonRpcPayload, options: TransactionOptions): Promise /** Open popup window */ openPopupWindow( route: T, params: T extends keyof PopupRoutesParamsMap ? PopupRoutesParamsMap[T] : undefined, ): Promise hasPaymentPassword(): Promise - /** Send request to native API, for a risky request will be added into the waiting queue. */ - send(payload: JsonRpcPayload, options?: TransactionOptions): Promise } export interface IOContext { MaskWalletContext: MaskWalletIOContext diff --git a/packages/web3-shared/evm/src/helpers/isReadonlyMethodType.ts b/packages/web3-shared/evm/src/helpers/isReadonlyMethodType.ts index 74674c03a90..2ecd17ceaa1 100644 --- a/packages/web3-shared/evm/src/helpers/isReadonlyMethodType.ts +++ b/packages/web3-shared/evm/src/helpers/isReadonlyMethodType.ts @@ -1,6 +1,7 @@ import { EthereumMethodType } from '../types/index.js' export const readonlyMethodType = [ + EthereumMethodType.NET_VERSION, EthereumMethodType.ETH_BLOCK_NUMBER, EthereumMethodType.ETH_CALL, EthereumMethodType.ETH_ESTIMATE_GAS, diff --git a/packages/web3-shared/evm/src/types/index.ts b/packages/web3-shared/evm/src/types/index.ts index 124183c19e0..aacb541e3cb 100644 --- a/packages/web3-shared/evm/src/types/index.ts +++ b/packages/web3-shared/evm/src/types/index.ts @@ -259,6 +259,8 @@ export enum EthereumMethodType { MASK_REMOVE_WALLETS = 'MASK_REMOVE_WALLETS', MASK_RESET_ALL_WALLETS = 'MASK_RESET_ALL_WALLETS', MASK_REPLACE_TRANSACTION = 'mask_replaceTransaction', + + NET_VERSION = 'net_version', } export enum TransactionEventType {