From 6a57b56aa01d3de3cc382b83ab6b8723b5bdabe0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 22 Apr 2024 11:59:32 +0900 Subject: [PATCH] fix: hooked-wallet-ethtx tx.sign (#465) * fix: hooked-wallet-ethtx tx.sign * test: add signTransaction case for hooked-wallet-ethtx wallet provider --------- Co-authored-by: legobt <6wbvkn0j@anonaddy.me> --- subproviders/hooked-wallet-ethtx.js | 6 +- test/wallet.js | 85 ++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/subproviders/hooked-wallet-ethtx.js b/subproviders/hooked-wallet-ethtx.js index f96bccee..1a07bd8f 100644 --- a/subproviders/hooked-wallet-ethtx.js +++ b/subproviders/hooked-wallet-ethtx.js @@ -32,9 +32,9 @@ function HookedWalletEthTxSubprovider(opts) { opts.getPrivateKey(txData.from, function(err, privateKey) { if (err) return cb(err) - var tx = TransactionFactory.fromTxData(txData) - tx.sign(privateKey) - cb(null, '0x' + tx.serialize().toString('hex')) + const rawTx = TransactionFactory.fromTxData(txData) + const signedTx = rawTx.sign(privateKey) + cb(null, '0x' + signedTx.serialize().toString('hex')) }) } diff --git a/test/wallet.js b/test/wallet.js index 0caf1fd3..ca1f5aab 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -11,7 +11,90 @@ const createPayload = require('../util/create-payload.js') const injectMetrics = require('./util/inject-metrics') -test('tx sig', function(t){ +test('tx sig ethtx provider', function(t){ + t.plan(12) + + var privateKey = Buffer.from('cccd8f4d88de61f92f3747e4a9604a0395e6ad5138add4bec4a2ddf231ee24f9', 'hex') + var address = Buffer.from('1234362ef32bcd26d3dd18ca749378213625ba0b', 'hex') + var addressHex = '0x'+address.toString('hex') + + // sign all tx's + var providerA = injectMetrics(new HookedWalletTxProvider({ + getAccounts: function(cb){ + cb(null, [addressHex]) + }, + getPrivateKey: function(address, cb){ + cb(null, privateKey) + }, + signTransaction: function(txParams, cb){ + const tx = TransactionFactory.fromTxData(txParams) + const signedTransaction = tx.sign(privateKey) + var rawTx = '0x'+signedTransaction.serialize().toString('hex') + cb(null, rawTx) + }, + })) + + // handle nonce requests + var providerB = injectMetrics(new NonceTracker()) + // handle all bottom requests + var providerC = injectMetrics(new FixtureProvider({ + eth_gasPrice: '0x1234', + eth_getTransactionCount: '0x00', + eth_sendRawTransaction: function(payload, next, done){ + var rawTx = ethUtil.toBuffer(payload.params[0]) + var tx = TransactionFactory.fromSerializedData(rawTx) + var hash = '0x'+tx.hash().toString('hex') + done(null, hash) + }, + })) + // handle block requests + var providerD = injectMetrics(new TestBlockProvider()) + + var engine = new ProviderEngine() + engine.addProvider(providerA) + engine.addProvider(providerB) + engine.addProvider(providerC) + engine.addProvider(providerD) + + var txPayload = { + method: 'eth_sendTransaction', + params: [{ + from: addressHex, + to: addressHex, + value: '0x01', + gas: '0x1234567890', + }] + } + + engine.start() + engine.sendAsync(createPayload(txPayload), function(err, response){ + t.ifError(err, 'did not error') + t.ok(response, 'has response') + + // intial tx request + t.equal(providerA.getWitnessed('eth_sendTransaction').length, 1, 'providerA did see "signTransaction"') + t.equal(providerA.getHandled('eth_sendTransaction').length, 1, 'providerA did handle "signTransaction"') + + // tx nonce + t.equal(providerB.getWitnessed('eth_getTransactionCount').length, 1, 'providerB did see "eth_getTransactionCount"') + t.equal(providerB.getHandled('eth_getTransactionCount').length, 0, 'providerB did NOT handle "eth_getTransactionCount"') + t.equal(providerC.getWitnessed('eth_getTransactionCount').length, 1, 'providerC did see "eth_getTransactionCount"') + t.equal(providerC.getHandled('eth_getTransactionCount').length, 1, 'providerC did handle "eth_getTransactionCount"') + + // gas price + t.equal(providerC.getWitnessed('eth_gasPrice').length, 1, 'providerB did see "eth_gasPrice"') + t.equal(providerC.getHandled('eth_gasPrice').length, 1, 'providerB did handle "eth_gasPrice"') + + // send raw tx + t.equal(providerC.getWitnessed('eth_sendRawTransaction').length, 1, 'providerC did see "eth_sendRawTransaction"') + t.equal(providerC.getHandled('eth_sendRawTransaction').length, 1, 'providerC did handle "eth_sendRawTransaction"') + + engine.stop() + t.end() + }) +}) + +test('tx sig wallet provider', function(t){ t.plan(12) var privateKey = Buffer.from('cccd8f4d88de61f92f3747e4a9604a0395e6ad5138add4bec4a2ddf231ee24f9', 'hex')