aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-09-06 16:25:30 -0700
committerAdam Soltys <[email protected]>2022-09-06 16:25:30 -0700
commit86cca84229ff86b7a2e7c3b525c497f8f58099ad (patch)
tree1a22323c28e386d0046b87bbbd7b1640216fdb8f /lib/utils.js
parent864304bb422d00f0eb404fa875514fcc72e8bc0a (diff)
downloadcoinos-server-86cca84229ff86b7a2e7c3b525c497f8f58099ad.tar.gz
coinos-server-86cca84229ff86b7a2e7c3b525c497f8f58099ad.zip
get block height helper
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js35
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();
+};