add supersha.

This commit is contained in:
Christopher Jeffrey 2016-05-15 04:15:24 -07:00
parent 4b0519a073
commit d91a8b1086
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 40 additions and 2 deletions

View File

@ -211,6 +211,17 @@ MTX.prototype.getSize = function getSize() {
return bcoin.protocol.framer.tx.witnessSize(this);
};
/**
* Calculate the size of the transaction
* with the witness excluded. Note that this
* is _not_ cached.
* @returns {Number} size
*/
MTX.prototype.getBaseSize = function getBaseSize() {
return bcoin.protocol.framer.tx.size(this);
};
/**
* Calculate the virtual size of the transaction.
* Note that this is _not_ cached.

View File

@ -322,6 +322,18 @@ TX.prototype.getSize = function getSize() {
return this.getRaw().length;
};
/**
* Calculate the size of the transaction
* without the witness.
* with the witness included.
* @returns {Number} size
*/
TX.prototype.getBaseSize = function getBaseSize() {
this.getRaw();
return this._size - this._witnessSize;
};
/**
* Test whether the transaction has a non-empty witness.
* @returns {Boolean}
@ -1088,7 +1100,7 @@ TX.prototype.isSane = function isSane(ret) {
return false;
}
if (this.getSize() > constants.block.MAX_SIZE) {
if (this.getBaseSize() > constants.block.MAX_SIZE) {
ret.reason = 'bad-txns-oversize';
ret.score = 100;
return false;

View File

@ -14,7 +14,7 @@ var utils = exports;
var assert = require('assert');
var bn = require('bn.js');
var util = require('util');
var crypto, hash, aes;
var crypto, supersha, hash, aes;
/**
* Whether we're in a browser or not.
@ -27,6 +27,11 @@ utils.isBrowser =
if (!utils.isBrowser) {
crypto = require('cry' + 'pto');
try {
supersha = require('super' + 'sha');
} catch (e) {
;
}
} else {
hash = require('hash.js');
aes = require('./aes');
@ -242,6 +247,11 @@ utils.sha1 = function sha1(data, enc) {
*/
utils.sha256 = function sha256(data, enc) {
if (supersha) {
if (!Buffer.isBuffer(data))
data = new Buffer(data, enc);
return supersha.sha256(data);
}
return utils.hash('sha256', data, enc);
};
@ -264,6 +274,11 @@ utils.ripesha = function ripesha(data, enc) {
*/
utils.dsha256 = function dsha256(data, enc) {
if (supersha) {
if (!Buffer.isBuffer(data))
data = new Buffer(data, enc);
return supersha.dsha256(data);
}
return utils.sha256(utils.sha256(data, enc));
};