diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 92841158..319b00f8 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -180,19 +180,9 @@ utils.writeU64 = function writeU64(dst, num, off) { if (!off) off = 0; - var num = new bn(num); - var left = +num.shrn(32).toString(10); - var right = +num.maskn(32).toString(10); - - dst[off] = right & 0xff; - dst[off + 1] = (right >>> 8) & 0xff; - dst[off + 2] = (right >>> 16) & 0xff; - dst[off + 3] = (right >>> 24) & 0xff; - - dst[off + 4] = left & 0xff; - dst[off + 5] = (left >>> 8) & 0xff; - dst[off + 6] = (left >>> 16) & 0xff; - dst[off + 7] = (left >>> 24) & 0xff; + new bn(num).toArray().reverse().forEach(function(ch, i) { + dst[off + i] = ch; + }); return 8; }; @@ -219,19 +209,9 @@ utils.writeU64BE = function writeU64BE(dst, num, off) { if (!off) off = 0; - var num = new bn(num); - var left = +num.shrn(32).toString(10); - var right = +num.maskn(32).toString(10); - - dst[off] = (left >>> 24) & 0xff; - dst[off + 1] = (left >>> 16) & 0xff; - dst[off + 2] = (left >>> 8) & 0xff; - dst[off + 3] = left & 0xff; - - dst[off + 4] = (right >>> 24) & 0xff; - dst[off + 5] = (right >>> 16) & 0xff; - dst[off + 6] = (right >>> 8) & 0xff; - dst[off + 7] = right & 0xff; + new bn(num).toArray().forEach(function(ch, i) { + dst[off + i] = ch; + }); return 8; };