Skip to content

Commit

Permalink
refactor: sort methods in packages/mask/entry-sdk/bridge/eth.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed May 13, 2024
1 parent f2ca61e commit 30ddc83
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
98 changes: 50 additions & 48 deletions packages/mask/entry-sdk/bridge/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PassthroughMethods = [
EthereumMethodType.eth_newBlockFilter,
EthereumMethodType.eth_newFilter,
EthereumMethodType.eth_uninstallFilter,
]
] as const
type PassthroughMethods = (typeof PassthroughMethods)[number]
const passthroughMethods: Record<PassthroughMethods, (...params: any[]) => Promise<any>> = {} as any
for (const method of PassthroughMethods) {
Expand Down Expand Up @@ -92,13 +92,21 @@ function getInteractiveClient(): Promise<InteractiveClient> {
const methods: Methods = {
...passthroughMethods,

async eth_accounts() {
return Services.Wallet.sdk_eth_accounts(location.origin)
},
async eth_call(...params) {
if (params[0].chainId) {
const chainId = await Services.Wallet.sdk_eth_chainId()
if (params[0].chainId !== '0x' + chainId.toString(16))
return err.the_provider_is_disconnected_from_the_specified_chain()
}
return passthroughMethods.eth_call(params) as Promise<string>
},
async eth_chainId() {
const chainId = await Services.Wallet.sdk_eth_chainId()
return '0x' + chainId.toString(16)
},
async eth_accounts() {
return Services.Wallet.sdk_eth_accounts(location.origin)
},
async eth_requestAccounts() {
await Services.Wallet.requestUnlockWallet()
let wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
Expand All @@ -109,40 +117,17 @@ const methods: Methods = {
if (wallets.length) return wallets
return err.user_rejected_the_request()
},
async eth_signTypedData_v4(requestedAddress, typedData) {
await Services.Wallet.requestUnlockWallet()
const wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
if (!wallets.some((addr) => isSameAddress(addr, requestedAddress)))
return err.the_requested_account_and_or_method_has_not_been_authorized_by_the_user()
return providers.EVMWeb3.getWeb3Provider({
async eth_sendRawTransaction(transaction) {
const p = providers.EVMWeb3.getWeb3Provider({
providerType: ProviderType.MaskWallet,
account: requestedAddress,
silent: false,
readonly: false,
}).request({
method: EthereumMethodType.eth_signTypedData_v4,
params: [requestedAddress, typedData],
})
},
async personal_sign(challenge, requestedAddress) {
// check challenge is 0x hex
await Services.Wallet.requestUnlockWallet()
const wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
if (!wallets.some((addr) => isSameAddress(addr, requestedAddress)))
return err.the_requested_account_and_or_method_has_not_been_authorized_by_the_user()
return providers.EVMWeb3.getWeb3Provider({
providerType: ProviderType.MaskWallet,
account: requestedAddress,
silent: false,
readonly: false,
}).request({
method: EthereumMethodType.personal_sign,
params: [challenge, requestedAddress],
return p.request({
method: EthereumMethodType.eth_sendRawTransaction,
params: [transaction] as any,
})
},
async personal_ecRecover(message, signature) {
return providers.EVMWeb3.getWeb3().eth.accounts.recover(message, signature)
},
async eth_sendTransaction(options) {
const wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
if (!wallets.some((addr) => isSameAddress(addr, options.from)))
Expand All @@ -158,15 +143,19 @@ const methods: Methods = {
params: [options],
})
},
async eth_sendRawTransaction(transaction) {
const p = providers.EVMWeb3.getWeb3Provider({
async eth_signTypedData_v4(requestedAddress, typedData) {
await Services.Wallet.requestUnlockWallet()
const wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
if (!wallets.some((addr) => isSameAddress(addr, requestedAddress)))
return err.the_requested_account_and_or_method_has_not_been_authorized_by_the_user()
return providers.EVMWeb3.getWeb3Provider({
providerType: ProviderType.MaskWallet,
account: requestedAddress,
silent: false,
readonly: false,
})
return p.request({
method: EthereumMethodType.eth_sendRawTransaction,
params: [transaction] as any,
}).request({
method: EthereumMethodType.eth_signTypedData_v4,
params: [requestedAddress, typedData],
})
},
async eth_subscribe(...params) {
Expand All @@ -178,8 +167,8 @@ const methods: Methods = {
async eth_unsubscribe(...params) {
return (await getInteractiveClient()).eth_unsubscribe!(...params)
},
// https://eips.ethereum.org/EIPS/eip-2255
wallet_getPermissions() {
wallet_addEthereumChain: null!,
async wallet_getPermissions() {
return Services.Wallet.sdk_EIP2255_wallet_getPermissions(location.origin)
},
async wallet_requestPermissions(request) {
Expand All @@ -196,14 +185,8 @@ const methods: Methods = {
request as EIP2255PermissionRequest,
)
},
async eth_call(...params) {
if (params[0].chainId) {
const chainId = await Services.Wallet.sdk_eth_chainId()
if (params[0].chainId !== '0x' + chainId.toString(16))
return err.the_provider_is_disconnected_from_the_specified_chain()
}
return passthroughMethods.eth_call(params) as Promise<string>
},
wallet_revokePermissions: null!,
wallet_switchEthereumChain: null!,
async wallet_watchAsset({ type, options: { address, decimals, image, symbol, tokenId } }) {
// TODO: throw error if chainId is unknown (https://eips.ethereum.org/EIPS/eip-747#erc1046-type)
if (!isValidChecksumAddress(address)) return err.invalid_address()
Expand Down Expand Up @@ -292,6 +275,25 @@ const methods: Methods = {
}).catch(() => {})
return true
},
async personal_sign(challenge, requestedAddress) {
// check challenge is 0x hex
await Services.Wallet.requestUnlockWallet()
const wallets = await Services.Wallet.sdk_getGrantedWallets(location.origin)
if (!wallets.some((addr) => isSameAddress(addr, requestedAddress)))
return err.the_requested_account_and_or_method_has_not_been_authorized_by_the_user()
return providers.EVMWeb3.getWeb3Provider({
providerType: ProviderType.MaskWallet,
account: requestedAddress,
silent: false,
readonly: false,
}).request({
method: EthereumMethodType.personal_sign,
params: [challenge, requestedAddress],
})
},
async personal_ecRecover(message, signature) {
return providers.EVMWeb3.getWeb3().eth.accounts.recover(message, signature)
},
}

export async function eth_request(request: unknown): Promise<{ e?: MaskEthereumProviderRpcError | null; d?: unknown }> {
Expand Down
10 changes: 5 additions & 5 deletions packages/mask/entry-sdk/bridge/eth/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ export const methodValidate = {
return: z.boolean(),
},
// deprecated
eth_decrypt: { args: z.tuple([z.string().describe('EncryptedMessage'), _.address]), return: z.never() },
// eth_decrypt: { args: z.tuple([z.string().describe('EncryptedMessage'), _.address]), return: z.never() },
// deprecated
eth_getEncryptionPublicKey: { args: z.tuple([_.address]), return: z.never() },
// eth_getEncryptionPublicKey: { args: z.tuple([_.address]), return: z.never() },
eth_requestAccounts: { args: z.tuple([]), return: _.address.array() },
eth_accounts: { args: z.tuple([]), return: _.address.array() },
eth_blobBaseFee: { args: z.tuple([]), return: _.unpadded_hex },
Expand Down Expand Up @@ -259,11 +259,11 @@ export const methodValidate = {
]),
return: _.transaction_hash,
},
web3_clientVersion: { args: z.tuple([]), return: z.string() },
// web3_clientVersion: { args: z.tuple([]), return: z.string() },
eth_blockNumber: { args: z.tuple([]), return: _.unpadded_hex },
eth_call: { args: z.tuple([_.transaction, _.block.nullish()]), return: _.hex },
eth_chainId: { args: z.tuple([]), return: _.chainId },
eth_coinbase: { args: z.tuple([]), return: _.address },
// eth_coinbase: { args: z.tuple([]), return: _.address },
eth_estimateGas: { args: z.tuple([_.transaction, _.block_number_or_tag.nullish()]), return: _.unpadded_hex },
eth_feeHistory: {
args: z.tuple([
Expand Down Expand Up @@ -326,7 +326,7 @@ type returns = {
}

export type Methods = {
[K in keyof typeof methodValidate]?: (
[K in keyof typeof methodValidate]: (
...params: z.infer<(typeof methodValidate)[K]['args']>
) => Promise<returns[K] | Result<returns[K], any> | MaskEthereumProviderRpcError>
}

0 comments on commit 30ddc83

Please sign in to comment.