diff options
| author | Adam Soltys <[email protected]> | 2021-06-01 21:40:40 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-06-09 16:07:17 -0700 |
| commit | 386a3e099a1d8169a722549e1db0b71054ea06e8 (patch) | |
| tree | 376e67e8150488e27d973675d8e522e1724fba0b /routes | |
| parent | ef2e67a7dce5c49a74ffca9440d0a256f69f9853 (diff) | |
| download | coinos-server-386a3e099a1d8169a722549e1db0b71054ea06e8.tar.gz coinos-server-386a3e099a1d8169a722549e1db0b71054ea06e8.zip | |
update sample config
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/bitcoin/receive.js | 146 |
1 files changed, 76 insertions, 70 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index c89e2275..6bf38a9e 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -21,96 +21,102 @@ const network = zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); let tx = bitcoin.Transaction.fromHex(message); + let hash = reverse(tx.getHash()).toString("hex"); if (payments.includes(hash)) return; Promise.all( tx.outs.map(async o => { - const { value } = o; - - let address; try { - address = bitcoin.address.fromOutputScript(o.script, network); - } catch (e) { - return; - } + const { value } = o; - if ( - Object.keys(addresses).includes(address) && - !change.includes(address) - ) { - payments.push(hash); + let address; + try { + address = bitcoin.address.fromOutputScript(o.script, network); + } catch (e) { + return; + } - let user = await db.User.findOne({ - where: { - username: addresses[address] - } - }); + if ( + Object.keys(addresses).includes(address) && + !change.includes(address) + ) { + payments.push(hash); - const invoice = await db.Invoice.findOne({ - where: { - user_id: user.id, - network: "bitcoin" - }, - order: [["id", "DESC"]], - include: { - model: db.Account, - as: "account" - } - }); + let user = await db.User.findOne({ + where: { + username: addresses[address] + } + }); - if (!invoice) return; + const invoice = await db.Invoice.findOne({ + where: { + user_id: user.id, + network: "bitcoin" + }, + order: [["id", "DESC"]], + include: { + model: db.Account, + as: "account" + } + }); - const currency = invoice ? invoice.currency : user.currency; - const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; - const tip = invoice ? invoice.tip : 0; - const memo = invoice ? invoice.memo : ""; + if (!invoice) return; - let confirmed = false; + const currency = invoice ? invoice.currency : user.currency; + const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; + const tip = invoice ? invoice.tip : 0; + const memo = invoice ? invoice.memo : ""; - let { account } = invoice; + let confirmed = false; - account.pending += value; - await account.save(); + let { account } = invoice; - if (config.bitcoin.walletpass) - await bc.walletPassphrase(config.bitcoin.walletpass, 300); + account.pending += value; + await account.save(); - let totalOutputs = tx.outs.reduce((a, b) => a + b.value, 0); - let totalInputs = 0; - for (let i = 0; i < tx.ins.length; i++) { - let { hash, index } = tx.ins[i]; - hash = reverse(hash).toString("hex"); - let hex = await bc.getRawTransaction(hash.toString("hex")); - let inputTx = bitcoin.Transaction.fromHex(hex); - totalInputs += inputTx.outs[index].value; - } - let fee = totalInputs - totalOutputs; + if (config.bitcoin.walletpass) + await bc.walletPassphrase(config.bitcoin.walletpass, 300); - let payment = await db.Payment.create({ - account_id: account.id, - user_id: user.id, - hash, - fee, - memo, - amount: value - tip, - currency, - rate, - received: true, - tip, - confirmed, - address, - network: "bitcoin" - }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); + let totalOutputs = tx.outs.reduce((a, b) => a + b.value, 0); + let totalInputs = 0; + for (let i = 0; i < tx.ins.length; i++) { + let { hash, index } = tx.ins[i]; + hash = reverse(hash).toString("hex"); + console.log(hash); + let hex = await bc.getRawTransaction(hash); + let inputTx = bitcoin.Transaction.fromHex(hex); + totalInputs += inputTx.outs[index].value; + } + let fee = totalInputs - totalOutputs; - emit(user.username, "account", account); + let payment = await db.Payment.create({ + account_id: account.id, + user_id: user.id, + hash, + fee, + memo, + amount: value - tip, + currency, + rate, + received: true, + tip, + confirmed, + address, + network: "bitcoin" + }); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + + emit(user.username, "account", account); - emit(user.username, "payment", payment); - l.info("bitcoin detected", user.username, value); - notify(user, `${value} SAT payment detected`); + emit(user.username, "payment", payment); + l.info("bitcoin detected", user.username, value); + notify(user, `${value} SAT payment detected`); + } + } catch (e) { + console.log(e); } }) ); |
