From a9f0ca3d542765da531e103a5e162b2b876f29cf Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Mon, 22 Apr 2024 02:46:44 +0000 Subject: [PATCH] test: add signTransaction case for hooked-wallet-ethtx wallet provider --- test/wallet.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) 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')