diff options
Diffstat (limited to 'lib/utils.js')
| -rw-r--r-- | lib/utils.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js index 004ca7ac..1ff24c67 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -55,3 +55,38 @@ export const fail = msg => { export const SATS = 100000000; export const toSats = n => Math.round(n * SATS); + +export const getBlockHeight = message => { + let buffer = Buffer.from(message, "hex"); + if (buffer.length < 80) throw new Error("Buffer too small (< 80 bytes)"); + + let offset = 0; + const readSlice = n => { + offset += n; + return buffer.slice(offset - n, offset); + }; + + const readUInt32 = () => { + const i = buffer.readUInt32LE(offset); + offset += 4; + return i; + }; + + const readUInt8 = () => { + const i = buffer.readUInt8(offset); + offset += 1; + return i; + }; + + const readVarInt = () => { + const vi = varuint.decode(buffer, offset); + offset += varuint.decode.bytes; + return vi; + }; + + readUInt32(); + readSlice(32); + readSlice(32); + readUInt32(); + return readUInt32(); +}; |
