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:
parent
40d17c5180
commit
aff3992ffb
|
@ -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;
|
||||
|
|
|
@ -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…
Reference in New Issue