add getHash for txs and blocks.
This commit is contained in:
parent
9fd6229221
commit
63f506b590
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
var net = require('net');
|
||||
var crypto = require('crypto');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var bitcoindjs = require('../build/Release/bitcoindjs.node');
|
||||
var util = require('util');
|
||||
|
@ -334,6 +335,29 @@ Block.isBlock = function(block) {
|
|||
return block._isBlock === Block._blockFlag;
|
||||
};
|
||||
|
||||
Block.prototype.hash =
|
||||
Block.prototype.getHash = function(enc) {
|
||||
if (!this._hash) {
|
||||
this._hash = utils.dsha256(this.rawHeader(), 'hex');
|
||||
}
|
||||
return enc === 'hex'
|
||||
? this._hash
|
||||
: utils.dsha256(this.rawHeader());
|
||||
};
|
||||
|
||||
Block.prototype.rawHeader = function() {
|
||||
var res = new Array(80);
|
||||
|
||||
utils.writeU32(res, this.version, 0);
|
||||
utils.copy(utils.toArray(this.previousblockhash, 'hex'), res, 4);
|
||||
utils.copy(utils.toArray(this.merkleroot, 'hex'), res, 36);
|
||||
utils.writeU32(res, this.time, 68);
|
||||
utils.writeU32(res, this.bits, 72);
|
||||
utils.writeU32(res, this.nonce, 76);
|
||||
|
||||
return new Buffer(res);
|
||||
};
|
||||
|
||||
Block.prototype.verify = function() {
|
||||
return this.verified = this.verified || bitcoindjs.verifyBlock(this);
|
||||
};
|
||||
|
@ -512,8 +536,14 @@ Transaction.prototype.isNull = function() {
|
|||
;
|
||||
};
|
||||
|
||||
Transaction.prototype.getHash = function() {
|
||||
;
|
||||
Transaction.prototype.hash =
|
||||
Transaction.prototype.getHash = function(enc) {
|
||||
if (!this._hash) {
|
||||
this._hash = utils.dsha256(this.toBinary(), 'hex');
|
||||
}
|
||||
return enc === 'hex'
|
||||
? this._hash
|
||||
: utils.dsha256(this.toBinary());
|
||||
};
|
||||
|
||||
Transaction.prototype.getValueOut = function() {
|
||||
|
@ -859,6 +889,33 @@ utils.varint = function(arr, value, off) {
|
|||
}
|
||||
};
|
||||
|
||||
utils.ripesha = function(data, enc) {
|
||||
return utils.ripemd160(utils.sha256(data, enc));
|
||||
};
|
||||
|
||||
utils.checksum = function(data, enc) {
|
||||
var b = new Buffer(utils.toArray(utils.dsha256(data, enc)).slice(0, 4));
|
||||
return enc ? b.toString(enc) : b;
|
||||
};
|
||||
|
||||
utils.dsha256 = function(data, enc) {
|
||||
return utils.sha256(utils.sha256(data, enc));
|
||||
};
|
||||
|
||||
utils._hash = function(algo, data, enc) {
|
||||
var hash = crypto.createHash(algo);
|
||||
hash.update(data);
|
||||
return hash.digest(enc);
|
||||
};
|
||||
|
||||
utils.sha256 = function(data, enc) {
|
||||
return utils._hash('sha256', data, enc);
|
||||
};
|
||||
|
||||
utils.ripemd160 = function(data, enc) {
|
||||
return utils._hash('ripemd160', data, enc);
|
||||
};
|
||||
|
||||
utils.copy = function copy(src, dst, off, force) {
|
||||
var len = src.length;
|
||||
if (!force)
|
||||
|
@ -869,6 +926,8 @@ utils.copy = function copy(src, dst, off, force) {
|
|||
};
|
||||
|
||||
function toArray(msg, enc) {
|
||||
if (Buffer.isBuffer(msg))
|
||||
return Array.prototype.slice.call(msg);
|
||||
if (Array.isArray(msg))
|
||||
return msg.slice();
|
||||
if (!msg)
|
||||
|
|
Loading…
Reference in New Issue