Skip to content

Commit

Permalink
fix: chainId problem, add net_version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jan 13, 2024
1 parent 6329823 commit 76cc047
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 50 deletions.
67 changes: 50 additions & 17 deletions packages/mask-sdk/public-api/mask-wallet.ts
Expand Up @@ -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[]
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/mask/entry-sdk/README.md
Expand Up @@ -5,12 +5,11 @@ The list is built from what [MetaMask supported](https://docs.metamask.io/wallet

- <https://ethereum.github.io/execution-apis/api-documentation/>

- [ ] 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
Expand Down
5 changes: 4 additions & 1 deletion packages/mask/entry-sdk/bridge/eth.ts
Expand Up @@ -22,7 +22,10 @@ type PassthroughMethods = (typeof PassthroughMethods)[number]
const passthroughMethods: Record<PassthroughMethods, (params: unknown[] | undefined) => Promise<unknown>> = {} 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 })
}
}

Expand Down
57 changes: 30 additions & 27 deletions packages/mask/entry-sdk/bridge/eth/validator.ts
Expand Up @@ -37,45 +37,45 @@ 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
.object({
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,
storageKeys: z.string().array(),
})
.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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions packages/web3-providers/src/types/Wallet.ts
Expand Up @@ -76,15 +76,13 @@ export namespace WalletAPI {
) => Promise<string>
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<JsonRpcResponse>
send(payload: JsonRpcPayload, options: TransactionOptions): Promise<JsonRpcResponse>
/** Open popup window */
openPopupWindow<T extends PopupRoutes>(
route: T,
params: T extends keyof PopupRoutesParamsMap ? PopupRoutesParamsMap[T] : undefined,
): Promise<void>
hasPaymentPassword(): Promise<boolean>
/** Send request to native API, for a risky request will be added into the waiting queue. */
send(payload: JsonRpcPayload, options?: TransactionOptions): Promise<JsonRpcResponse>
}
export interface IOContext {
MaskWalletContext: MaskWalletIOContext
Expand Down
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-shared/evm/src/types/index.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit 76cc047

Please sign in to comment.