From 141ec52eb1dda15ef1b643c2620ac0515ac93761 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 11 Jul 2022 14:47:23 +0800 Subject: [PATCH 1/6] fix: failed to sign tx --- packages/mask/src/plugins/Wallet/services/send.ts | 2 +- packages/mask/src/plugins/Wallet/services/wallet/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mask/src/plugins/Wallet/services/send.ts b/packages/mask/src/plugins/Wallet/services/send.ts index 30c627ddb7bd..4320a297779c 100644 --- a/packages/mask/src/plugins/Wallet/services/send.ts +++ b/packages/mask/src/plugins/Wallet/services/send.ts @@ -1,6 +1,7 @@ import Web3 from 'web3' import type { HttpProvider } from 'web3-core' import type { JsonRpcPayload, JsonRpcResponse } from 'web3-core-helpers' +import { isNil } from 'lodash-unified' import { defer } from '@dimensiondev/kit' import { ChainId, @@ -13,7 +14,6 @@ import { import { openPopupWindow, removePopupWindow } from '../../../../background/services/helper' import { nativeAPI } from '../../../../shared/native-rpc' import { WalletRPC } from '../messages' -import { isNil } from 'lodash-unified' enum JSON_RPC_ERROR_CODE { INVALID_REQUEST = -32600, diff --git a/packages/mask/src/plugins/Wallet/services/wallet/index.ts b/packages/mask/src/plugins/Wallet/services/wallet/index.ts index dcfa9df55250..8769fc167998 100644 --- a/packages/mask/src/plugins/Wallet/services/wallet/index.ts +++ b/packages/mask/src/plugins/Wallet/services/wallet/index.ts @@ -123,7 +123,7 @@ export async function signTransaction(address: string, config: Transaction) { amount: (config.value as string) ?? null, gas_limit: config.gas ? toHex(config.gas) : null, gas_price: config.gasPrice?.toString() ?? null, - chain_id: config.chainId, + chain_id: config.chainId ? config.chainId.toString() : null, max_fee_per_gas: (config.maxFeePerGas as string | undefined) ?? null, max_inclusion_fee_per_gas: (config.maxFeePerGas as string | undefined) ?? null, nonce: config.nonce ? toHex(config.nonce) : null, From 042d42916ffd8492ba09af77f7f1aae04f085089 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 11 Jul 2022 15:00:20 +0800 Subject: [PATCH 2/6] fix: tx gas --- packages/web3-shared/evm/utils/contract.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/web3-shared/evm/utils/contract.ts b/packages/web3-shared/evm/utils/contract.ts index 19d935af9259..42a7995ce1f6 100644 --- a/packages/web3-shared/evm/utils/contract.ts +++ b/packages/web3-shared/evm/utils/contract.ts @@ -57,6 +57,8 @@ export async function encodeContractTransaction( if (!tx.gas) { tx.gas = await transaction.estimateGas({ from: tx.from as string | undefined, + to: tx.to as string | undefined, + data: tx.data as string | undefined, value: tx.value, data: tx.data as string | undefined, to: tx.to as string | undefined, From 9cd2a2b37e5094c29016f4383f5e995c01ea8831 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Mon, 11 Jul 2022 17:21:16 +0800 Subject: [PATCH 3/6] fix: error message --- packages/mask/src/plugins/Wallet/services/send.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mask/src/plugins/Wallet/services/send.ts b/packages/mask/src/plugins/Wallet/services/send.ts index 4320a297779c..2c797a1ac480 100644 --- a/packages/mask/src/plugins/Wallet/services/send.ts +++ b/packages/mask/src/plugins/Wallet/services/send.ts @@ -219,7 +219,7 @@ export async function confirmRequest(payload: JsonRpcPayload, options?: Options) return } if (response?.error) { - reject(new Error(`Failed to send transaction: ${response.error}`)) + reject(new Error(`Failed to send transaction: ${response.error?.message ?? response.error}`)) return } WalletRPC.deleteUnconfirmedRequest(payload) From d6b4715dc9ac8020f6918768e5dfe7c85fedd795 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Tue, 12 Jul 2022 11:13:14 +0800 Subject: [PATCH 4/6] chore: wip --- .../mask/src/plugins/Wallet/services/send.ts | 20 +++++++++---------- packages/web3-shared/evm/utils/provider.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/mask/src/plugins/Wallet/services/send.ts b/packages/mask/src/plugins/Wallet/services/send.ts index 2c797a1ac480..d501ff14cb2a 100644 --- a/packages/mask/src/plugins/Wallet/services/send.ts +++ b/packages/mask/src/plugins/Wallet/services/send.ts @@ -5,6 +5,7 @@ import { isNil } from 'lodash-unified' import { defer } from '@dimensiondev/kit' import { ChainId, + createWeb3, EthereumMethodType, getPayloadConfig, getPayloadId, @@ -125,28 +126,27 @@ export async function send( case EthereumMethodType.MASK_REPLACE_TRANSACTION: if (!computedPayload?.from || !computedPayload.to || !options?.chainId) return - const rawTransaction = await WalletRPC.signTransaction(computedPayload.from as string, { - ...computedPayload, - chainId: options.chainId, - }) - if (!rawTransaction) break + const privateKey = await WalletRPC.exportPrivateKey(computedPayload.from as string) + const web3 = createWeb3(provider) + const transactionSigned = await web3.eth.accounts.signTransaction(computedPayload, privateKey) + if (!transactionSigned.rawTransaction) break return provider.send( { ...payload, method: EthereumMethodType.ETH_SEND_RAW_TRANSACTION, - params: [rawTransaction], + params: [transactionSigned.rawTransaction], }, callback, ) case EthereumMethodType.ETH_SIGN_TYPED_DATA: const [address, dataToSign] = payload.params as [string, string] - const signed = await WalletRPC.signTypedData(address, dataToSign) + const dataSigned = await WalletRPC.signTypedData(address, dataToSign) try { callback(null, { jsonrpc: '2.0', id: payload.id as number, - result: signed, + result: dataSigned, }) } catch (error) { callback(getError(error, null, 'Failed to sign message.')) @@ -154,12 +154,12 @@ export async function send( break case EthereumMethodType.PERSONAL_SIGN: const [data, account] = payload.params as [string, string] - const personalSigned = await WalletRPC.signPersonalMessage(data, account) + const messageSigned = await WalletRPC.signPersonalMessage(data, account) try { callback(null, { jsonrpc: '2.0', id: payload.id as number, - result: personalSigned, + result: messageSigned, }) } catch (error) { callback(getError(error, null, 'Failed to sign message.')) diff --git a/packages/web3-shared/evm/utils/provider.ts b/packages/web3-shared/evm/utils/provider.ts index 4ec56364cdc1..2367e25a3568 100644 --- a/packages/web3-shared/evm/utils/provider.ts +++ b/packages/web3-shared/evm/utils/provider.ts @@ -12,6 +12,15 @@ export function createWeb3(provider: Provider) { return web3 } +export function createSignableWeb3(provider: Provider, keys: string[]) { + const web3 = createWeb3(provider) + if (keys.length) { + web3.eth.accounts.wallet.clear() + keys.forEach((k) => k && ['0x', '0x0'].includes(k) && web3.eth.accounts.wallet.add(k)) + } + return web3 +} + export function createWeb3Provider(request: (requestArguments: RequestArguments) => Promise): Web3Provider { const provider: Web3Provider = { on() { From 4ef976ef3c4a91516617deb7e89edcda03fa3132 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Tue, 12 Jul 2022 13:44:08 +0800 Subject: [PATCH 5/6] refactor: revoke changes --- packages/mask/src/plugins/Wallet/services/wallet/index.ts | 2 +- packages/web3-shared/evm/utils/contract.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/mask/src/plugins/Wallet/services/wallet/index.ts b/packages/mask/src/plugins/Wallet/services/wallet/index.ts index 8769fc167998..dcfa9df55250 100644 --- a/packages/mask/src/plugins/Wallet/services/wallet/index.ts +++ b/packages/mask/src/plugins/Wallet/services/wallet/index.ts @@ -123,7 +123,7 @@ export async function signTransaction(address: string, config: Transaction) { amount: (config.value as string) ?? null, gas_limit: config.gas ? toHex(config.gas) : null, gas_price: config.gasPrice?.toString() ?? null, - chain_id: config.chainId ? config.chainId.toString() : null, + chain_id: config.chainId, max_fee_per_gas: (config.maxFeePerGas as string | undefined) ?? null, max_inclusion_fee_per_gas: (config.maxFeePerGas as string | undefined) ?? null, nonce: config.nonce ? toHex(config.nonce) : null, diff --git a/packages/web3-shared/evm/utils/contract.ts b/packages/web3-shared/evm/utils/contract.ts index 42a7995ce1f6..46d2c0e53caf 100644 --- a/packages/web3-shared/evm/utils/contract.ts +++ b/packages/web3-shared/evm/utils/contract.ts @@ -60,8 +60,6 @@ export async function encodeContractTransaction( to: tx.to as string | undefined, data: tx.data as string | undefined, value: tx.value, - data: tx.data as string | undefined, - to: tx.to as string | undefined, }) } From 391c2379423f1d94db63842115cca045ac99560d Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Tue, 12 Jul 2022 15:00:59 +0800 Subject: [PATCH 6/6] fix: sign payload --- .../mask/src/plugins/Wallet/services/send.ts | 8 +++--- packages/web3-shared/evm/utils/payload.ts | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/mask/src/plugins/Wallet/services/send.ts b/packages/mask/src/plugins/Wallet/services/send.ts index d501ff14cb2a..1f79b681d5c0 100644 --- a/packages/mask/src/plugins/Wallet/services/send.ts +++ b/packages/mask/src/plugins/Wallet/services/send.ts @@ -1,5 +1,6 @@ import Web3 from 'web3' import type { HttpProvider } from 'web3-core' +import { isHex, hexToNumber } from 'web3-utils' import type { JsonRpcPayload, JsonRpcResponse } from 'web3-core-helpers' import { isNil } from 'lodash-unified' import { defer } from '@dimensiondev/kit' @@ -7,7 +8,7 @@ import { ChainId, createWeb3, EthereumMethodType, - getPayloadConfig, + getSignablePayloadConfig, getPayloadId, getRPCConstants, isRiskMethod, @@ -120,15 +121,16 @@ export async function send( options?: Options, ) { const provider = await createProvider(options?.chainId) - const computedPayload = getPayloadConfig(payload) + switch (payload.method) { case EthereumMethodType.ETH_SEND_TRANSACTION: case EthereumMethodType.MASK_REPLACE_TRANSACTION: + const computedPayload = getSignablePayloadConfig(payload) if (!computedPayload?.from || !computedPayload.to || !options?.chainId) return const privateKey = await WalletRPC.exportPrivateKey(computedPayload.from as string) const web3 = createWeb3(provider) - const transactionSigned = await web3.eth.accounts.signTransaction(computedPayload, privateKey) + const transactionSigned = await web3.eth.accounts.signTransaction(computedPayload, `0x${privateKey}`) if (!transactionSigned.rawTransaction) break return provider.send( diff --git a/packages/web3-shared/evm/utils/payload.ts b/packages/web3-shared/evm/utils/payload.ts index 58c2e282ac65..b4073caa5b22 100644 --- a/packages/web3-shared/evm/utils/payload.ts +++ b/packages/web3-shared/evm/utils/payload.ts @@ -1,6 +1,7 @@ import BigNumber from 'bignumber.js' -import { first } from 'lodash-unified' +import { first, isUndefined, omitBy } from 'lodash-unified' import type { JsonRpcPayload } from 'web3-core-helpers' +import { hexToNumber } from 'web3-utils' import { EthereumMethodType, Transaction } from '../types' export function addGasMargin(value: BigNumber.Value, scale = 3000) { @@ -56,3 +57,25 @@ export function getPayloadConfig(payload: JsonRpcPayload) { return } } + +export function getSignablePayloadConfig(payload: JsonRpcPayload) { + const raw = getPayloadConfig(payload) + if (!raw) return + + const parseHexNumber = (hex: string | number | undefined) => + typeof hex !== 'undefined' ? hexToNumber(hex ?? '0x0') : undefined + + return omitBy( + { + ...raw, + value: parseHexNumber(raw.value as string | undefined), + gas: parseHexNumber(raw.gas), + gasPrice: parseHexNumber(raw.gasPrice as string | undefined), + maxFeePerGas: parseHexNumber(raw.maxFeePerGas as string | undefined), + maxPriorityFeePerGas: parseHexNumber(raw.maxPriorityFeePerGas as string | undefined), + chainId: parseHexNumber(raw.chainId), + nonce: parseHexNumber(raw.nonce), + }, + isUndefined, + ) as Transaction +}