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.
This commit is contained in:
Ryan X. Charles 2014-09-19 17:25:35 -07:00
parent 40d17c5180
commit aff3992ffb
2 changed files with 28 additions and 0 deletions

View File

@ -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;

View File

@ -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);
});
});
});