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 +++++++++++++++++++++++++++++++++++ routes/assets.js | 3 +++ routes/liquid/receive.js | 7 ++----- 3 files changed, 40 insertions(+), 5 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(); +}; diff --git a/routes/assets.js b/routes/assets.js index 22bf71d0..8140b179 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -7,6 +7,9 @@ import { auth } from "$lib/passport"; import axios from "axios"; import crypto from "crypto"; import fs from "fs"; +import { err, l } from "$lib/logging"; +import lq from "$lib/liquid"; +import { SATS } from "$lib/utils"; let fetchAssets; (fetchAssets = async () => { diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index f3b8397f..480677de 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -1,6 +1,6 @@ import db from "$db"; import config from "$config"; -import { toSats } from "$lib/utils"; +import { getBlockHeight, getUser, toSats } from "$lib/utils"; import { emit } from "$lib/sockets"; import store from "$lib/store"; import { sendLiquid } from "$routes/liquid/send"; @@ -14,7 +14,6 @@ import bitcoin from "bitcoinjs-lib"; import liquidJs from "liquidjs-lib"; import { l, err, warn } from "$lib/logging"; import lq from "$lib/liquid"; -import { getUser } from "$lib/utils"; const { Block, networks, Transaction } = liquidJs; @@ -224,11 +223,9 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { where: { confirmed: 0 } }); - const block = Block.fromHex(message.toString("hex"), true); - let hash, json; - hash = await lq.getBlockHash(block.blockHeight); + hash = await lq.getBlockHash(getBlockHeight(message)); json = await lq.getBlock(hash, 2); json.tx.map(async tx => { -- cgit v1.2.3