From 86cca84229ff86b7a2e7c3b525c497f8f58099ad Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 6 Sep 2022 16:25:30 -0700 Subject: get block height helper --- lib/utils.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/utils.js') 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(); +}; -- cgit v1.2.3