diff options
| author | Adam Soltys <[email protected]> | 2021-08-03 02:11:23 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-08-03 02:11:23 -0700 |
| commit | 02d8ef89dadfaee37aa058f85082985004acbc0a (patch) | |
| tree | 57bd1a4079a7b49af753aa81ce5127f533f37b74 | |
| parent | 803998539992b2f0ca07f7ab869750e7b1061dd3 (diff) | |
| parent | 6abfe155c5bb3845f59775b95eeb9d4034aa92b8 (diff) | |
| download | coinos-server-02d8ef89dadfaee37aa058f85082985004acbc0a.tar.gz coinos-server-02d8ef89dadfaee37aa058f85082985004acbc0a.zip | |
Merge branch 'taxi'
| -rwxr-xr-x | mine.sh | 8 | ||||
| -rw-r--r-- | routes/assets.js | 9 | ||||
| -rw-r--r-- | routes/liquid/taxi.js | 85 | ||||
| -rw-r--r-- | routes/payments.js | 1 | ||||
| -rw-r--r-- | sampleconfig/liquid/elements.conf | 2 |
5 files changed, 100 insertions, 5 deletions
diff --git a/mine.sh b/mine.sh new file mode 100755 index 00000000..0df5b6ba --- /dev/null +++ b/mine.sh @@ -0,0 +1,8 @@ +#!/bin/bash + + +while true +do + docker exec -it liquid elements-cli -datadir=/config generatetoaddress 1 $(docker exec -it liquid elements-cli -datadir=/config getnewaddress) + sleep 8 +done diff --git a/routes/assets.js b/routes/assets.js index f99301ae..3f483a28 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -111,7 +111,7 @@ app.post( if (filename) contract.filename = filename; - l.info("attempting issuance", req.user.username, contract); + l.info("attempting issuance", req.user.username, contract, "address", address, "pubkey", pubkey); sha256.update(JSON.stringify(contract)); const hash = sha256.digest("hex"); @@ -149,7 +149,7 @@ app.post( const srt = await lq.signRawTransactionWithWallet(brt); const allowed = (await lq.testMempoolAccept([srt.hex]))[0].allowed; fs.writeFileSync('tx', srt.hex); - if (!allowed) throw new Error(); + if (!allowed) throw new Error("issuance rejected by mempool"); const txid = await lq.sendRawTransaction(srt.hex); await db.transaction(async transaction => { @@ -185,6 +185,7 @@ app.post( await account.reload({ transaction }); emit(user.username, "account", account); + if (address) return; account = await db.Account.create( { asset, @@ -269,9 +270,9 @@ app.post( } }); - res.send(issuances[txid]); + res.send(issuances[txid] ? issuances[txid] : { asset }); } catch (e) { - l.error("asset issuance failed", e.message); + l.error("asset issuance failed", e.message, e.stack); res.status(500).send(e.message); } }) diff --git a/routes/liquid/taxi.js b/routes/liquid/taxi.js new file mode 100644 index 00000000..14da9411 --- /dev/null +++ b/routes/liquid/taxi.js @@ -0,0 +1,85 @@ +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://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; + + try { + 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); + } +}); diff --git a/routes/payments.js b/routes/payments.js index a4bae562..a749e333 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -142,6 +142,7 @@ ah(async () => { app.get("/liquid/generate", auth, require("./liquid/generate")); app.post("/liquid/fee", auth, require("./liquid/fee")); app.post("/liquid/send", auth, require("./liquid/send")); + app.post("/taxi", auth, require("./liquid/taxi")); require("./liquid/receive"); setTimeout(async () => { diff --git a/sampleconfig/liquid/elements.conf b/sampleconfig/liquid/elements.conf index bfd01638..ce93b307 100644 --- a/sampleconfig/liquid/elements.conf +++ b/sampleconfig/liquid/elements.conf @@ -7,7 +7,7 @@ rpcuser=admin1 rpcpassword=123 rpcbind=0.0.0.0 rpcallowip=0.0.0.0/0 -rcpwallet=a +rpcwallet=a validatepegin=0 initialfreecoins=2100000000000000 |
