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 #81 from benjamincburns/issue80-hash-collisions
Browse files Browse the repository at this point in the history
fixes #80
  • Loading branch information
jwasinger authored Dec 21, 2017
2 parents 5ad3f05 + 7f24cf9 commit 1caa1bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ module.exports = class FakeTransaction extends Transaction {
this.from = data.from
}
}

/**
* Computes a sha3-256 hash of the serialized tx, using the sender address to generate a fake signature.
* @param {Boolean} [includeSignature=true] whether or not to inculde the signature
* @return {Buffer}
*/
hash (includeSignature) {
if (includeSignature && this._from) {
// 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)
}

return super.hash(includeSignature)
}
}

23 changes: 23 additions & 0 deletions test/fake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const tape = require('tape')
const utils = require('ethereumjs-util')
const FakeTransaction = require('../fake.js')
tape('[FakeTransaction]: Basic functions', function (t) {
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(true))
var modifiedFromFieldTxHash = utils.bufferToHex(modifiedFromFieldTx.hash(true))
st.notEqual(baseTxHash, modifiedFromFieldTxHash, 'FakeTransactions with different `from` addresses but otherwise identical data should have different hashes')
})
})
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require('./fake.js')
require('./api.js')
require('./transactionRunner.js')

0 comments on commit 1caa1bb

Please sign in to comment.