From 756017c5bf6b1858c231d39670b217a606f4e454 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 7 Oct 2022 21:10:48 -0700 Subject: fix liquid asset receiving --- docker-compose.yml | 14 +++++++ index.js | 4 +- lib/notifications.js | 38 +++++++++-------- lib/webhooks.js | 10 +++-- routes/assets.js | 104 ++++++++++++++++++++++++++++++++++++----------- routes/liquid/receive.js | 50 ++++++++++++----------- 6 files changed, 154 insertions(+), 66 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9e0b8c64..70e2d725 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -164,6 +164,20 @@ services: networks: default: ipv4_address: 172.18.0.30 + spark: + image: shesek/spark-wallet:standalone + container_name: spark + volumes: + - ./config/spark:/data + - ./config/lightning/regtest:/etc/lightning + ports: + - '9737:9737' + environment: + LOGIN: "${LOGIN}" + NO_TLS: "${NO_TLS}" + networks: + default: + ipv4_address: 172.18.0.32 networks: default: name: net diff --git a/index.js b/index.js index c96abe5f..06338fbc 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ import persist from "./lib/persist"; import config from "./config/index"; import store from "./lib/store"; import { l, err } from "./lib/logging"; +import { optionalAuth } from "$lib/passport"; import "./db"; import "./lib/sockets"; @@ -28,7 +29,8 @@ if (config.lna) store.networks.push("lightning"); let host = process.env.HOST || "0.0.0.0"; let port = process.env.PORT || 3119; -app.post('/echo', async (req, res) => { +app.post('/echo', optionalAuth, async (req, res) => { + console.log(req.user.username) console.log(req.body) res.send(req.body); }); diff --git a/lib/notifications.js b/lib/notifications.js index 9c9c95dc..8f092c5c 100644 --- a/lib/notifications.js +++ b/lib/notifications.js @@ -9,24 +9,30 @@ if (config.vapid) { ); } -export const notify = (user, payload) => { - let { subscriptions } = user; +export const notify = async (user, payload) => { + try { + let { subscriptions } = user; - if (subscriptions) { - subscriptions.map(async (subscription, i) => { - try { - await webpush.sendNotification(subscription, payload); - } catch (e) { - if ( - e.body && - (e.body.includes("expired") || e.body.includes("No such")) - ) { - user.subscriptions.splice(i, 1); - await user.save(); - } else { - err("problem sending notification", e); + if (subscriptions) { + let i = 0; + for (let subscription of subscriptions) { + i++; + try { + await webpush.sendNotification(subscription, payload); + } catch (e) { + if ( + e.body && + (e.body.includes("expired") || e.body.includes("No such")) + ) { + user.subscriptions.splice(i, 1); + await user.save(); + } else { + err("problem sending notification", e); + } } } - }); + } + } catch (e) { + console.log("problem notifying", e); } }; diff --git a/lib/webhooks.js b/lib/webhooks.js index 901bd264..51a9a99e 100644 --- a/lib/webhooks.js +++ b/lib/webhooks.js @@ -2,7 +2,8 @@ import config from "$config"; import wretch from "./wretch"; import { l } from "$lib/logging"; -export const callWebhook = (invoice, payment) => { +export const callWebhook = async (invoice, payment) => { + try { if (!invoice || !payment) return; let { address, received, text, webhook } = invoice; @@ -13,7 +14,7 @@ export const callWebhook = (invoice, payment) => { let secret = config.webhooks[webhook]; l("calling webhook", webhook, amount, hash, address, text); - return wretch() + let res = await wretch() .headers({ "x-webhook": secret }) .url(webhook) .post({ @@ -28,6 +29,9 @@ export const callWebhook = (invoice, payment) => { webhook }) .res() - .catch(console.log); + return res; } + } catch(e) { + console.log("problem calling webhook", e) + } }; diff --git a/routes/assets.js b/routes/assets.js index 53e89602..ecc44d84 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -225,30 +225,60 @@ app.post("/assets", auth, async (req, res) => { account.id ); - if (!address) { - const asset_payment = await db.Payment.create( + const { id: invoice_id } = await db.Invoice.create( + { + account_id: account.id, + user_id, + amount: Math.round(params.asset_amount * SATS), + address: asset_address, + currency: user.currency, + rate: store.rates[user.currency], + network: "liquid" + }, + { transaction } + ); + + const asset_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + invoice_id, + hash: txid, + amount: Math.round(params.asset_amount * SATS), + received: true, + confirmed: false, + address: asset_address, + network: "liquid" + }, + { transaction } + ); + emit(user.username, "payment", asset_payment); + + if (address) { + const asset_send = await db.Payment.create( { account_id: account.id, user_id, + invoice_id, hash: txid, - amount: Math.round(params.asset_amount * SATS), - received: true, + amount: -Math.round(params.asset_amount * SATS), + received: false, confirmed: false, - address: asset_address, + address, network: "liquid" }, { transaction } ); - emit(user.username, "payment", asset_payment); - - store.issuances[txid] = { - user_id, - asset, - asset_amount: params.asset_amount, - asset_payment_id: asset_payment.id - }; + emit(user.username, "payment", asset_send); } + store.issuances[txid] = { + user_id, + asset, + asset_amount: params.asset_amount, + asset_payment_id: asset_payment.id + }; + if (token_amount) { account = await db.Account.create( { @@ -265,26 +295,54 @@ app.post("/assets", auth, async (req, res) => { ); emit(user.username, "account", account); - if (!address) { - const token_payment = await db.Payment.create( + const { id: invoice_id } = await db.Invoice.create( + { + account_id: account.id, + user_id, + amount: Math.round(params.asset_amount * SATS), + address: asset_address, + currency: user.currency, + rate: store.rates[user.currency], + network: "liquid" + }, + { transaction } + ); + + const token_payment = await db.Payment.create( + { + account_id: account.id, + user_id, + hash: txid, + amount: Math.round(token_amount * SATS), + received: true, + confirmed: false, + address: token_address, + network: "liquid" + }, + { transaction } + ); + emit(user.username, "payment", token_payment); + + if (address) { + const token_send = await db.Payment.create( { account_id: account.id, user_id, hash: txid, - amount: Math.round(token_amount * SATS), - received: true, + amount: -Math.round(token_amount * SATS), + received: false, confirmed: false, - address: token_address, + address, network: "liquid" }, { transaction } ); - emit(user.username, "payment", token_payment); - - store.issuances[txid].token = token; - store.issuances[txid].token_amount = token_amount; - store.issuances[txid].token_payment_id = token_payment.id; + emit(user.username, "payment", token_send); } + + store.issuances[txid].token = token; + store.issuances[txid].token_amount = token_amount; + store.issuances[txid].token_payment_id = token_payment.id; } }); diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index a8d73718..8f482d39 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -1,6 +1,6 @@ import db from "$db"; import config from "$config"; -import { getBlockHeight, getUser, toSats } from "$lib/utils"; +import { getBlockHeight, getUser, getUserById, toSats, SATS } from "$lib/utils"; import { emit } from "$lib/sockets"; import store from "$lib/store"; import { sendLiquid } from "$routes/liquid/send"; @@ -98,30 +98,31 @@ const queue = {}; const seen = []; zmqRawTx.on("message", async (topic, message, sequence) => { - const hex = message.toString("hex"); - - let unblinded, tx, blinded, txid; try { - txid = Transaction.fromHex(hex).getId(); - if (seen.includes(txid)) return; - seen.push(txid); - if (seen.length > 5000) seen.shift(); - - unblinded = await lq.unblindRawTransaction(hex); - tx = await lq.decodeRawTransaction(unblinded.hex); - blinded = await lq.decodeRawTransaction(hex); - } catch (e) { - return err("problem decoding liquid tx", e.message); - } + const hex = message.toString("hex"); + + let unblinded, tx, blinded, txid; + try { + txid = Transaction.fromHex(hex).getId(); + if (seen.includes(txid)) return; + seen.push(txid); + if (seen.length > 5000) seen.shift(); + + unblinded = await lq.unblindRawTransaction(hex); + tx = await lq.decodeRawTransaction(unblinded.hex); + blinded = await lq.decodeRawTransaction(hex); + } catch (e) { + return err("problem decoding liquid tx", e.message); + } - Promise.all( - tx.vout.map(async o => { + for (let o of tx.vout) { try { - if (!(o.scriptPubKey && o.scriptPubKey.address)) return; + if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return; const { asset } = o; const value = toSats(o.value); - const { address } = o.scriptPubKey; + const { addresses } = o.scriptPubKey; + const address = addresses[0]; if ( Object.keys(store.addresses).includes(address) && @@ -212,10 +213,13 @@ zmqRawTx.on("message", async (topic, message, sequence) => { }); } } catch (e) { + console.log(e); err("Problem processing transaction", e.message, e.stack); } - }) - ); + } + } catch (e) { + console.log(e); + } }); zmqRawBlock.on("message", async (topic, message, sequence) => { @@ -229,7 +233,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { hash = await lq.getBlockHash(getBlockHeight(message)); json = await lq.getBlock(hash, 2); - json.tx.map(async tx => { + for (let tx of json.tx) { if (store.issuances[tx.txid]) { await db.transaction(async transaction => { const { @@ -302,7 +306,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { } }); } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1; - }); + } } catch (e) { return console.log(e); } -- cgit v1.2.3