handle compact mantissa numbers.
This commit is contained in:
parent
43b0533a18
commit
67d1405c15
@ -200,7 +200,7 @@ Output.getData = function getData(output) {
|
||||
}
|
||||
|
||||
if (bcoin.script.isScripthash(s)) {
|
||||
hash = utils.toHex(sub[1]);
|
||||
hash = sub[1];
|
||||
addr = bcoin.wallet.hash2addr(hash, 'scripthash');
|
||||
return utils.merge(def, {
|
||||
type: 'scripthash',
|
||||
|
||||
@ -1187,7 +1187,7 @@ script.coinbaseBits = function coinbaseBits(s, block) {
|
||||
if (s[0].length > 6)
|
||||
return { type: 'value', value: s[0] };
|
||||
|
||||
value = new bn(s[0].reverse()).toNumber();
|
||||
value = new bn(s[0].slice().reverse()).toNumber();
|
||||
|
||||
// Test for bits and ts
|
||||
if (block) {
|
||||
|
||||
@ -420,57 +420,6 @@ function binaryInsert(list, item, compare, search) {
|
||||
|
||||
utils.binaryInsert = binaryInsert;
|
||||
|
||||
function bitsToTarget(bits) {
|
||||
var len = (bits >>> 24) & 0xff;
|
||||
var hi = (bits >>> 16) & 0xff;
|
||||
var mid = (bits >>> 8) & 0xff;
|
||||
var lo = bits & 0xff;
|
||||
var res = new Array(len);
|
||||
var i = 0;
|
||||
|
||||
for (; i < len - 3; i++)
|
||||
res[i] = 0;
|
||||
|
||||
res[i++] = lo;
|
||||
res[i++] = mid;
|
||||
res[i++] = hi;
|
||||
|
||||
if (hi === 0)
|
||||
res.pop();
|
||||
|
||||
if (hi === 0 && mid === 0)
|
||||
res.pop();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
utils.bitsToTarget = bitsToTarget;
|
||||
|
||||
function testTarget(target, hash) {
|
||||
var i;
|
||||
|
||||
if (typeof target === 'number')
|
||||
target = bitsToTarget(target);
|
||||
|
||||
hash = utils.toArray(hash, 'hex');
|
||||
|
||||
for (i = hash.length - 1; i >= target.length; i--)
|
||||
if (hash[i] !== 0)
|
||||
return false;
|
||||
|
||||
for (; i >= 0; i--) {
|
||||
if (hash[i] === target[i])
|
||||
continue;
|
||||
if (hash[i] > target[i])
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
utils.testTarget = testTarget;
|
||||
|
||||
utils.isEqual = function isEqual(a, b) {
|
||||
var i = 0;
|
||||
|
||||
@ -811,3 +760,63 @@ utils.uniq = function(obj) {
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
utils.fromCompact = function fromCompact(compact) {
|
||||
var exponent = compact >> 24;
|
||||
var negative = (compact >> 23) & 0x01;
|
||||
var mantissa = compact & 0x007fffff;
|
||||
var num;
|
||||
|
||||
if (compact === 0)
|
||||
return new bn(0);
|
||||
|
||||
if (exponent <= 3) {
|
||||
mantissa >>= 8 * (3 - exponent);
|
||||
num = new bn(mantissa);
|
||||
} else {
|
||||
num = new bn(mantissa);
|
||||
num.iushln(8 * (exponent - 3));
|
||||
}
|
||||
|
||||
if (negative)
|
||||
num.ineg();
|
||||
|
||||
return num;
|
||||
};
|
||||
|
||||
utils.toCompact = function toCompact(num) {
|
||||
var mantissa, exponent, compact;
|
||||
|
||||
if (num.cmpn(0) === 0)
|
||||
return 0;
|
||||
|
||||
exponent = num.byteLength();
|
||||
if (exponent <= 3) {
|
||||
mantissa = num.toNumber();
|
||||
mantissa <<= 8 * (3 - exponent);
|
||||
} else {
|
||||
mantissa = num.ushrn(8 * (exponent - 3)).toNumber();
|
||||
}
|
||||
|
||||
if (mantissa & 0x00800000) {
|
||||
mantissa >>= 8;
|
||||
exponent++;
|
||||
}
|
||||
|
||||
compact = (exponent << 24) | mantissa;
|
||||
|
||||
if (num.isNeg())
|
||||
compact |= 0x00800000;
|
||||
|
||||
return compact;
|
||||
};
|
||||
|
||||
utils.testTarget = function testTarget(target, hash) {
|
||||
if (typeof target === 'number')
|
||||
target = utils.fromCompact(target);
|
||||
|
||||
if (typeof hash === 'string')
|
||||
hash = utils.toArray(hash, 'hex');
|
||||
|
||||
return new bn(hash.slice().reverse()).cmp(target) < 0;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user