do not allow negative compact numbers.
This commit is contained in:
parent
4802f409ca
commit
019617162c
@ -1091,16 +1091,18 @@ utils.uniq = function uniq(obj) {
|
||||
*/
|
||||
|
||||
utils.fromCompact = function fromCompact(compact) {
|
||||
var exponent = compact >> 24;
|
||||
var negative = (compact >> 23) & 0x01;
|
||||
var mantissa = compact & 0x007fffff;
|
||||
var exponent = compact >>> 24;
|
||||
var negative = (compact >>> 23) & 1;
|
||||
var mantissa = compact & 0x7fffff;
|
||||
var num;
|
||||
|
||||
if (compact === 0)
|
||||
return new bn(0);
|
||||
|
||||
// Logic ported from btcd since
|
||||
// the bitcoind code is a nightmare.
|
||||
if (exponent <= 3) {
|
||||
mantissa >>= 8 * (3 - exponent);
|
||||
mantissa >>>= 8 * (3 - exponent);
|
||||
num = new bn(mantissa);
|
||||
} else {
|
||||
num = new bn(mantissa);
|
||||
@ -1127,6 +1129,9 @@ utils.toCompact = function toCompact(num) {
|
||||
return 0;
|
||||
|
||||
exponent = num.byteLength();
|
||||
|
||||
// Logic ported from btcd since
|
||||
// the bitcoind code is a nightmare.
|
||||
if (exponent <= 3) {
|
||||
mantissa = num.toNumber();
|
||||
mantissa <<= 8 * (3 - exponent);
|
||||
@ -1134,7 +1139,7 @@ utils.toCompact = function toCompact(num) {
|
||||
mantissa = num.ushrn(8 * (exponent - 3)).toNumber();
|
||||
}
|
||||
|
||||
if (mantissa & 0x00800000) {
|
||||
if (mantissa & 0x800000) {
|
||||
mantissa >>= 8;
|
||||
exponent++;
|
||||
}
|
||||
@ -1142,7 +1147,10 @@ utils.toCompact = function toCompact(num) {
|
||||
compact = (exponent << 24) | mantissa;
|
||||
|
||||
if (num.isNeg())
|
||||
compact |= 0x00800000;
|
||||
compact |= 0x800000;
|
||||
|
||||
if (compact < 0)
|
||||
compact += 0x100000000;
|
||||
|
||||
return compact;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user