Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

fix: hooked-wallet-ethtx tx.sign #465

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions subproviders/hooked-wallet-ethtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
}

Expand Down
85 changes: 84 additions & 1 deletion test/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading