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

Commit

Permalink
test: add signTransaction case for hooked-wallet-ethtx wallet provider
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Apr 22, 2024
1 parent e49d736 commit a9f0ca3
Showing 1 changed file with 84 additions and 1 deletion.
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

0 comments on commit a9f0ca3

Please sign in to comment.