From f4b9b0f0c95dd20de5e3c91f8c9bd356bb001b09 Mon Sep 17 00:00:00 2001 From: iosh Date: Wed, 27 Nov 2024 18:02:36 +0800 Subject: [PATCH] fix: add type to core space tx and fix type format --- .../middlewares/validate-rpc-params.js | 7 ++++- packages/rpcs/cfx_signTransaction/index.js | 27 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/rpc-engine/middlewares/validate-rpc-params.js b/packages/rpc-engine/middlewares/validate-rpc-params.js index d07b6257..80f184c6 100644 --- a/packages/rpc-engine/middlewares/validate-rpc-params.js +++ b/packages/rpc-engine/middlewares/validate-rpc-params.js @@ -34,8 +34,13 @@ export default defMiddleware(({tx: {map, comp}}) => ({ const {params, method} = req const {schemas, Err} = rpcStore[method] // TODO: add a preprocess middleware to transform req params for more compatibilities - if (method === 'eth_sendTransaction' && isArray(params)) + if ( + (method === 'eth_sendTransaction' || + method === 'cfx_sendTransaction') && + isArray(params) + ) { params[0] = preprocessTx(params[0]) + } if (schemas.input) { if (!validate(schemas.input, params, {netId: req.network.netId})) { throw Err.InvalidParams( diff --git a/packages/rpcs/cfx_signTransaction/index.js b/packages/rpcs/cfx_signTransaction/index.js index 9b07d2e7..ab1c8a53 100644 --- a/packages/rpcs/cfx_signTransaction/index.js +++ b/packages/rpcs/cfx_signTransaction/index.js @@ -88,6 +88,16 @@ export const permissions = { db: ['findAddress'], } +// conflux js sdk +// hex type must be integer +function formatTx(tx) { + const newTx = {...tx} + + if (newTx.type && typeof newTx.type === 'string') { + newTx.type = parseInt(tx.type, 16) + } + return newTx +} export const main = async args => { const { app, @@ -153,6 +163,11 @@ export const main = async args => { } else { newTx.type = ETH_TX_TYPES.LEGACY } + } else { + // if there has type, we need check is v1.x ledger app + if (isV1LedgerAPP) { + newTx.type = ETH_TX_TYPES.LEGACY + } } // tx without to must have data (deploy contract) @@ -228,17 +243,11 @@ export const main = async args => { ).toHexString() } - // change the type to number - - if (newTx.type && typeof newTx.type === 'string') { - newTx.type = Number.parseInt(newTx.type, 16) - } - let raw if (fromAddr.account.accountGroup.vault.type === 'hw') { if (dryRun) { raw = cfxSignTransaction( - newTx, + formatTx(newTx), '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', network.netId, ) @@ -246,7 +255,7 @@ export const main = async args => { raw = await signWithHardwareWallet({ args, accountId: fromAddr.account.eid, - tx: newTx, + tx: formatTx(newTx), addressId: fromAddr.eid, device: fromAddr.account.accountGroup.vault.device, }) @@ -259,7 +268,7 @@ export const main = async args => { if (dryRun) pk = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' - raw = cfxSignTransaction(newTx, pk, network.netId) + raw = cfxSignTransaction(formatTx(newTx), pk, network.netId) } if (returnTxMeta) {