Browse Source

transaction hash and id

...the hash is the usual hash, and the id is the reverse of that, which is what
is usually graphically displayed by bitcoind.
patch-2
Ryan X. Charles 10 years ago
parent
commit
aff3992ffb
  1. 9
      lib/transaction.js
  2. 19
      test/transaction.js

9
lib/transaction.js

@ -3,6 +3,7 @@ var Txout = require('./txout');
var BufferWriter = require('./bufferwriter');
var BufferReader = require('./bufferreader');
var Varint = require('./varint');
var Hash = require('./hash');
var Transaction = function Transaction(version, txinsvi, txins, txoutsvi, txouts, nlocktime) {
if (!(this instanceof Transaction))
@ -113,4 +114,12 @@ Transaction.prototype.toBufferWriter = function(bw) {
return bw;
};
Transaction.prototype.hash = function() {
return Hash.sha256sha256(this.toBuffer());
};
Transaction.prototype.id = function() {
return BufferReader(this.hash()).reverse().read();
};
module.exports = Transaction;

19
test/transaction.js

@ -124,4 +124,23 @@ describe('Transaction', function() {
});
describe('#hash', function() {
it('should correctly calculate the hash of this known transaction', function() {
var tx = Transaction().fromBuffer(tx2buf);
var txhashbuf = new Buffer(Array.apply([], new Buffer(tx2idhex, 'hex')).reverse());
tx.hash().toString('hex').should.equal(txhashbuf.toString('hex'));
});
});
describe('#id', function() {
it('should correctly calculate the id of this known transaction', function() {
var tx = Transaction().fromBuffer(tx2buf);
tx.id().toString('hex').should.equal(tx2idhex);
});
});
});

Loading…
Cancel
Save