diff --git a/fake.js b/fake.js index d3173b8..2929c76 100644 --- a/fake.js +++ b/fake.js @@ -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) + } } + diff --git a/test/fake.js b/test/fake.js new file mode 100644 index 0000000..196a6d2 --- /dev/null +++ b/test/fake.js @@ -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') + }) +}) diff --git a/test/index.js b/test/index.js index b608c28..0903a5c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,2 +1,3 @@ +require('./fake.js') require('./api.js') require('./transactionRunner.js')