From a7168a10f45c305a45d2e56db6fbfeef210f1b9b Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 2 Aug 2021 02:05:28 -0700 Subject: silhouettes --- routes/liquid/taxi.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 routes/liquid/taxi.js (limited to 'routes/liquid') diff --git a/routes/liquid/taxi.js b/routes/liquid/taxi.js new file mode 100644 index 00000000..3f450752 --- /dev/null +++ b/routes/liquid/taxi.js @@ -0,0 +1,74 @@ +const { ECPair, Psbt, payments, networks } = require("@asoltys/liquidjs-lib"); +const wretch = require("wretch"); +const fetch = require("node-fetch"); +wretch().polyfills({ fetch }); +const url = prod + ? "https://blockstream.info/liquid/api" + : "http://localhost:3002"; +const electrs = wretch().url(url); +const network = networks.regtest; + +module.exports = ah(async (req, res) => { + let { user } = req; + let { psbt } = req.body; + + let utxos = await electrs + .url(`/address/XLDLqkbbNKvgS9vhj3fQWPv1Y3UNoHEneZ/utxo`) + .get() + .json(); + let tx = utxos.find(tx => tx.value >= 150); + let hex = await electrs + .url(`/tx/${tx.txid}/hex/`) + .get() + .text(); + + let redeemScript = payments.p2wpkh({ + pubkey: Buffer.from( + "03c42e2b536630da346d8f225797406eb86700239d627101a61e26e6d544db072e", + "hex" + ), + network + }).output; + + let p = Psbt.fromBase64(psbt) + .addInput({ + hash: tx.txid, + index: tx.vout, + redeemScript, + nonWitnessUtxo: Buffer.from(hex, "hex") + }) + .addOutput({ + asset: network.assetHash, + nonce: Buffer.alloc(1, 0), + script: Buffer.alloc(0), + value: 150 + }) + .addOutput({ + asset, + nonce: Buffer.alloc(1), + script: Address.toOutputScript( + "XLDLqkbbNKvgS9vhj3fQWPv1Y3UNoHEneZ", + network + ), + value: tx.value - 150 + }) + .signInput( + 1, + ECPair.fromPrivateKey( + Buffer.from( + "6802b8dfed3733e3b147582810c431150444c026066796b808c36f7089d02773", + "hex" + ) + ) + ); + + try { + if ((user.username = "silhouettes")) { + res.send({ psbt: p.toBase64() }); + } else { + throw new Error("taxi service only available to select customers"); + } + } catch (e) { + return res.status(500).send(e); + } +}); -- cgit v1.2.3 From 48e0438048b1c8bca8dcc5a895282ac7ddd4dd84 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 3 Aug 2021 00:18:16 -0700 Subject: taxi endpoint --- routes/liquid/taxi.js | 121 +++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 55 deletions(-) (limited to 'routes/liquid') diff --git a/routes/liquid/taxi.js b/routes/liquid/taxi.js index 3f450752..14da9411 100644 --- a/routes/liquid/taxi.js +++ b/routes/liquid/taxi.js @@ -1,74 +1,85 @@ -const { ECPair, Psbt, payments, networks } = require("@asoltys/liquidjs-lib"); +const { + address: Address, + ECPair, + Psbt, + payments, + networks +} = require("@asoltys/liquidjs-lib"); const wretch = require("wretch"); const fetch = require("node-fetch"); wretch().polyfills({ fetch }); const url = prod ? "https://blockstream.info/liquid/api" - : "http://localhost:3002"; + : "http://electrs-liquid:3002"; const electrs = wretch().url(url); const network = networks.regtest; +const key = ECPair.fromPrivateKey(Buffer.from(config.taxi, "hex")); + module.exports = ah(async (req, res) => { let { user } = req; let { psbt } = req.body; - let utxos = await electrs - .url(`/address/XLDLqkbbNKvgS9vhj3fQWPv1Y3UNoHEneZ/utxo`) - .get() - .json(); - let tx = utxos.find(tx => tx.value >= 150); - let hex = await electrs - .url(`/tx/${tx.txid}/hex/`) - .get() - .text(); - - let redeemScript = payments.p2wpkh({ - pubkey: Buffer.from( - "03c42e2b536630da346d8f225797406eb86700239d627101a61e26e6d544db072e", - "hex" - ), - network - }).output; - - let p = Psbt.fromBase64(psbt) - .addInput({ - hash: tx.txid, - index: tx.vout, - redeemScript, - nonWitnessUtxo: Buffer.from(hex, "hex") - }) - .addOutput({ - asset: network.assetHash, - nonce: Buffer.alloc(1, 0), - script: Buffer.alloc(0), - value: 150 - }) - .addOutput({ - asset, - nonce: Buffer.alloc(1), - script: Address.toOutputScript( - "XLDLqkbbNKvgS9vhj3fQWPv1Y3UNoHEneZ", - network - ), - value: tx.value - 150 - }) - .signInput( - 1, - ECPair.fromPrivateKey( - Buffer.from( - "6802b8dfed3733e3b147582810c431150444c026066796b808c36f7089d02773", - "hex" - ) - ) - ); - try { - if ((user.username = "silhouettes")) { - res.send({ psbt: p.toBase64() }); - } else { + if (user.username !== "silhouettes") { throw new Error("taxi service only available to select customers"); } + + let redeem = payments.p2wpkh({ + pubkey: key.publicKey, + network + }); + + let out = payments.p2sh({ + redeem, + network + }); + + let utxos = await electrs + .url(`/address/${out.address}/utxo`) + .get() + .json(); + + let tx = utxos.find(tx => tx.value >= 150); + + let hex = await electrs + .url(`/tx/${tx.txid}/hex`) + .get() + .text(); + + let raw = Psbt.fromBase64(psbt) + .addInput({ + hash: tx.txid, + index: tx.vout, + redeemScript: redeem.output, + nonWitnessUtxo: Buffer.from(hex, "hex") + }) + .addOutput({ + asset: network.assetHash, + nonce: Buffer.alloc(1, 0), + script: Buffer.alloc(0), + value: 150 + }) + .addOutput({ + asset: network.assetHash, + nonce: Buffer.alloc(1), + script: out.output, + value: tx.value - 150 + }) + .signInput(1, key) + .finalizeInput(1) + .extractTransaction() + .toHex(); + + let txid = await electrs + .url("/tx") + .body(raw) + .post() + .text(); + + res.send({ txid }); } catch (e) { + console.log(e); return res.status(500).send(e); } }); -- cgit v1.2.3