From 67d1405c157236aafab5f6f15a9be6481f4868c9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 29 Dec 2015 13:13:51 -0800 Subject: [PATCH] handle compact mantissa numbers. --- lib/bcoin/output.js | 2 +- lib/bcoin/script.js | 2 +- lib/bcoin/utils.js | 111 ++++++++++++++++++++++++-------------------- 3 files changed, 62 insertions(+), 53 deletions(-) diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index ad2ee879..5c415a81 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -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', diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 181561f7..ffb0ddc0 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -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) { diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index d94010a0..ed40319d 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -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; +};