From 302bbe81ba17670b556f0d52c6bfb28112a840ab Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 5 Jan 2016 02:38:04 +1100 Subject: [PATCH] script_number: refactor to mimic bitcoin/bitcoin --- src/script_number.js | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/script_number.js b/src/script_number.js index f2db26b..5177a8c 100644 --- a/src/script_number.js +++ b/src/script_number.js @@ -20,23 +20,11 @@ function decode (buffer, maxLength, minimal) { if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a) return b * 0x100000000 + a + } - // 32-bit - } else if (length === 4) { - result = buffer.readUInt32LE(0) - - // 24-bit - } else if (length === 3) { - result = buffer.readUInt16LE(0) - result |= buffer.readUInt8(2) << 16 - - // 16-bit - } else if (length === 2) { - result = buffer.readUInt16LE(0) - - // 8-bit - } else { - result = buffer.readUInt8(0) + // 32-bit / 24-bit / 16-bit / 8-bit + for (var i = 0; i < length; ++i) { + result |= buffer[i] << (8 * i) } if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1))))