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

Commit

Permalink
Merge pull request #110 from ethereumjs/revert-pr-94
Browse files Browse the repository at this point in the history
Fix Fake transaction hash creation (revert PR #94)
  • Loading branch information
holgerd77 authored Jul 2, 2018
2 parents 2aa7e54 + 0bd25a1 commit d9ab11d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
5 changes: 2 additions & 3 deletions fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ module.exports = class FakeTransaction extends Transaction {
}
})

// set from address or default to null address
this.from = (data && data.from) ? data.from : '0x0000000000000000000000000000000000000000'
this.from = data.from
}

/**
Expand All @@ -59,7 +58,7 @@ module.exports = class FakeTransaction extends Transaction {
* @return {Buffer}
*/
hash (includeSignature = true) {
if (includeSignature) {
if (includeSignature && this._from && this._from.toString('hex') !== '') {
// include a fake signature using the from address as a private key
let fakeKey = Buffer.concat([this._from, this._from.slice(0, 12)])
this.sign(fakeKey)
Expand Down
58 changes: 43 additions & 15 deletions test/fake.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
const tape = require('tape')
const utils = require('ethereumjs-util')
const FakeTransaction = require('../fake.js')

var txData = {
data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
gasLimit: '0x15f90',
gasPrice: '0x1',
nonce: '0x01',
to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc',
value: '0x0',
from: '0x1111111111111111111111111111111111111111'
}

tape('[FakeTransaction]: Basic functions', function (t) {
t.test('instantiate with from / create a hash', function (st) {
st.plan(3)
var tx = new FakeTransaction(txData)
var hash = tx.hash()
var cmpHash = Buffer.from('f0327c058946be12609d2afc0c45e8e1fffe57acbbff0e9c252e8fab61c3b2b9', 'hex')
st.deepEqual(hash, cmpHash, 'should create hash with includeSignature=true (default)')
var hash2 = tx.hash(false)
var cmpHash2 = Buffer.from('0401bf740d698674be321d0064f92cd6ebba5d73d1e5e5189c0bebbda33a85fe', 'hex')
st.deepEqual(hash2, cmpHash2, 'should create hash with includeSignature=false')
st.notDeepEqual(hash, hash2, 'previous hashes should be different')
})

t.test('instantiate without from / create a hash', function (st) {
var txDataNoFrom = Object.assign({}, txData)
delete txDataNoFrom['from']
st.plan(3)
var tx = new FakeTransaction(txDataNoFrom)
var hash = tx.hash()
var cmpHash = Buffer.from('7521eb94880840a93e2105f064cec3fe605f0159778a420b9b529c2f3d3b4e37', 'hex')
st.deepEqual(hash, cmpHash, 'should create hash with includeSignature=true (default)')
var hash2 = tx.hash(false)
var cmpHash2 = Buffer.from('0401bf740d698674be321d0064f92cd6ebba5d73d1e5e5189c0bebbda33a85fe', 'hex')
st.deepEqual(hash2, cmpHash2, 'should create hash with includeSignature=false')
st.notDeepEqual(hash, hash2, 'previous hashes should be different')
})

t.test('should not produce hash collsions for different senders', function (st) {
st.plan(1)
var baseTxData = {
data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
gasLimit: '0x15f90',
gasPrice: '0x1',
nonce: '0x01',
to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc',
value: '0x0',
from: '0x1111111111111111111111111111111111111111'
}
var modifiedFromFieldTxData = Object.assign({}, baseTxData, { from: '0x2222222222222222222222222222222222222222' })
var baseTx = new FakeTransaction(baseTxData)
var modifiedFromFieldTx = new FakeTransaction(modifiedFromFieldTxData)
var baseTxHash = utils.bufferToHex(baseTx.hash())
var modifiedFromFieldTxHash = utils.bufferToHex(modifiedFromFieldTx.hash())
st.notEqual(baseTxHash, modifiedFromFieldTxHash, 'FakeTransactions with different `from` addresses but otherwise identical data should have different hashes')
var txDataModFrom = Object.assign({}, txData, { from: '0x2222222222222222222222222222222222222222' })
var tx = new FakeTransaction(txData)
var txModFrom = new FakeTransaction(txDataModFrom)
var hash = utils.bufferToHex(tx.hash())
var hashModFrom = utils.bufferToHex(txModFrom.hash())
st.notEqual(hash, hashModFrom, 'FakeTransactions with different `from` addresses but otherwise identical data should have different hashes')
})
})

0 comments on commit d9ab11d

Please sign in to comment.