script_number: refactor to mimic bitcoin/bitcoin

This commit is contained in:
Daniel Cousens 2016-01-05 02:38:04 +11:00
parent 945bdfa747
commit 302bbe81ba

View file

@ -20,23 +20,11 @@ function decode (buffer, maxLength, minimal) {
if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a) if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a)
return b * 0x100000000 + a return b * 0x100000000 + a
}
// 32-bit // 32-bit / 24-bit / 16-bit / 8-bit
} else if (length === 4) { for (var i = 0; i < length; ++i) {
result = buffer.readUInt32LE(0) result |= buffer[i] << (8 * i)
// 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)
} }
if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1)))) if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1))))