bitcoinjs-lib/src/bufferutils.ts

31 lines
953 B
TypeScript
Raw Normal View History

2016-10-06 12:53:48 +02:00
// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) {
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
if (value > max) throw new Error('RangeError: value out of range')
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
}
2015-02-23 00:36:57 +01:00
function readUInt64LE (buffer, offset) {
const a = buffer.readUInt32LE(offset)
let b = buffer.readUInt32LE(offset + 4)
2014-05-01 11:38:18 +02:00
b *= 0x100000000
verifuint(b + a, 0x001fffffffffffff)
2014-05-01 11:38:18 +02:00
return b + a
}
2015-02-23 00:36:57 +01:00
function writeUInt64LE (buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)
2014-05-01 11:38:18 +02:00
buffer.writeInt32LE(value & -1, offset)
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
return offset + 8
2014-05-01 11:38:18 +02:00
}
module.exports = {
readUInt64LE: readUInt64LE,
2018-04-13 17:07:10 +02:00
writeUInt64LE: writeUInt64LE
2014-05-01 11:38:18 +02:00
}
export {}