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

Commit d9ab11d

Browse files
authored
Merge pull request #110 from ethereumjs/revert-pr-94
Fix Fake transaction hash creation (revert PR #94)
2 parents 2aa7e54 + 0bd25a1 commit d9ab11d

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

fake.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ module.exports = class FakeTransaction extends Transaction {
4949
}
5050
})
5151

52-
// set from address or default to null address
53-
this.from = (data && data.from) ? data.from : '0x0000000000000000000000000000000000000000'
52+
this.from = data.from
5453
}
5554

5655
/**
@@ -59,7 +58,7 @@ module.exports = class FakeTransaction extends Transaction {
5958
* @return {Buffer}
6059
*/
6160
hash (includeSignature = true) {
62-
if (includeSignature) {
61+
if (includeSignature && this._from && this._from.toString('hex') !== '') {
6362
// include a fake signature using the from address as a private key
6463
let fakeKey = Buffer.concat([this._from, this._from.slice(0, 12)])
6564
this.sign(fakeKey)

test/fake.js

+43-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
const tape = require('tape')
22
const utils = require('ethereumjs-util')
33
const FakeTransaction = require('../fake.js')
4+
5+
var txData = {
6+
data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
7+
gasLimit: '0x15f90',
8+
gasPrice: '0x1',
9+
nonce: '0x01',
10+
to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc',
11+
value: '0x0',
12+
from: '0x1111111111111111111111111111111111111111'
13+
}
14+
415
tape('[FakeTransaction]: Basic functions', function (t) {
16+
t.test('instantiate with from / create a hash', function (st) {
17+
st.plan(3)
18+
var tx = new FakeTransaction(txData)
19+
var hash = tx.hash()
20+
var cmpHash = Buffer.from('f0327c058946be12609d2afc0c45e8e1fffe57acbbff0e9c252e8fab61c3b2b9', 'hex')
21+
st.deepEqual(hash, cmpHash, 'should create hash with includeSignature=true (default)')
22+
var hash2 = tx.hash(false)
23+
var cmpHash2 = Buffer.from('0401bf740d698674be321d0064f92cd6ebba5d73d1e5e5189c0bebbda33a85fe', 'hex')
24+
st.deepEqual(hash2, cmpHash2, 'should create hash with includeSignature=false')
25+
st.notDeepEqual(hash, hash2, 'previous hashes should be different')
26+
})
27+
28+
t.test('instantiate without from / create a hash', function (st) {
29+
var txDataNoFrom = Object.assign({}, txData)
30+
delete txDataNoFrom['from']
31+
st.plan(3)
32+
var tx = new FakeTransaction(txDataNoFrom)
33+
var hash = tx.hash()
34+
var cmpHash = Buffer.from('7521eb94880840a93e2105f064cec3fe605f0159778a420b9b529c2f3d3b4e37', 'hex')
35+
st.deepEqual(hash, cmpHash, 'should create hash with includeSignature=true (default)')
36+
var hash2 = tx.hash(false)
37+
var cmpHash2 = Buffer.from('0401bf740d698674be321d0064f92cd6ebba5d73d1e5e5189c0bebbda33a85fe', 'hex')
38+
st.deepEqual(hash2, cmpHash2, 'should create hash with includeSignature=false')
39+
st.notDeepEqual(hash, hash2, 'previous hashes should be different')
40+
})
41+
542
t.test('should not produce hash collsions for different senders', function (st) {
643
st.plan(1)
7-
var baseTxData = {
8-
data: '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',
9-
gasLimit: '0x15f90',
10-
gasPrice: '0x1',
11-
nonce: '0x01',
12-
to: '0xd9024df085d09398ec76fbed18cac0e1149f50dc',
13-
value: '0x0',
14-
from: '0x1111111111111111111111111111111111111111'
15-
}
16-
var modifiedFromFieldTxData = Object.assign({}, baseTxData, { from: '0x2222222222222222222222222222222222222222' })
17-
var baseTx = new FakeTransaction(baseTxData)
18-
var modifiedFromFieldTx = new FakeTransaction(modifiedFromFieldTxData)
19-
var baseTxHash = utils.bufferToHex(baseTx.hash())
20-
var modifiedFromFieldTxHash = utils.bufferToHex(modifiedFromFieldTx.hash())
21-
st.notEqual(baseTxHash, modifiedFromFieldTxHash, 'FakeTransactions with different `from` addresses but otherwise identical data should have different hashes')
44+
var txDataModFrom = Object.assign({}, txData, { from: '0x2222222222222222222222222222222222222222' })
45+
var tx = new FakeTransaction(txData)
46+
var txModFrom = new FakeTransaction(txDataModFrom)
47+
var hash = utils.bufferToHex(tx.hash())
48+
var hashModFrom = utils.bufferToHex(txModFrom.hash())
49+
st.notEqual(hash, hashModFrom, 'FakeTransactions with different `from` addresses but otherwise identical data should have different hashes')
2250
})
2351
})

0 commit comments

Comments
 (0)