diff --git a/clients/tests/web/server-regtest.cjs b/clients/tests/web/server-regtest.cjs index b68d64e6..2e4caa1d 100644 --- a/clients/tests/web/server-regtest.cjs +++ b/clients/tests/web/server-regtest.cjs @@ -30,6 +30,32 @@ async function depositCoin(amount, address) { await exec(sendBitcoinCommand); } +const generateInvoice = async (paymentHash, amountInSats) => { + const generateInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-alice-1") lncli -n regtest addholdinvoice ${paymentHash} --amt ${amountInSats}`; + + const { stdout, stderr } = await exec(generateInvoiceCommand); + if (stderr) { + console.error('Error:', stderr); + return null; + } + return stdout.trim(); +} + +const payInvoice = async (paymentRequest) => { + const payInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-bob-1") lncli -n regtest payinvoice --force ${paymentRequest}`; + await exec(payInvoiceCommand); +} + +const payHoldInvoice = (paymentRequest) => { + const payInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-bob-1") lncli -n regtest payinvoice --force ${paymentRequest}`; + exec(payInvoiceCommand); +} + +const settleInvoice = async (preimage) => { + const settleInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-alice-1") lncli -n regtest settleinvoice ${preimage}`; + await exec(settleInvoiceCommand); +} + app.post('/deposit_amount', async (req, res) => { const { address, amount } = req.body @@ -63,6 +89,82 @@ app.post('/generate_blocks', async (req, res) => { } }) +app.post('/generate_invoice', async (req, res) => { + const { paymentHash, amountInSats } = req.body + + if (typeof paymentHash === 'string' && Number.isInteger(amountInSats)) { + console.log(`Generating invoice ...`) + + try { + const invoice = await generateInvoice(paymentHash, amountInSats); + res.status(200).send({ message: 'Invoice generated successfully', invoice }) + } catch (error) { + console.log(error.message); + res.status(500).send({ message: error.message }) + } + + } else { + res.status(400).send({ message: 'Invalid input' }) + } +}) + +app.post('/pay_invoice', async (req, res) => { + const { paymentRequest } = req.body + + if (typeof paymentRequest === 'string') { + console.log(`Paying invoice ...`) + + try { + await payInvoice(paymentRequest); + } catch (error) { + console.log(error.message); + res.status(500).send({ message: error.message }) + } + + res.status(200).send({ message: 'Invoice paid successfully' }) + } else { + res.status(400).send({ message: 'Invalid input' }) + } +}) + +app.post('/pay_holdinvoice', async (req, res) => { + const { paymentRequest } = req.body + + if (typeof paymentRequest === 'string') { + console.log(`Paying invoice ...`) + + try { + payHoldInvoice(paymentRequest); + } catch (error) { + console.log(error.message); + res.status(500).send({ message: error.message }) + } + + res.status(200).send({ message: 'Invoice paid successfully' }) + } else { + res.status(400).send({ message: 'Invalid input' }) + } +}) + +app.post('/settle_invoice', async (req, res) => { + const { preimage } = req.body + + if (typeof preimage === 'string') { + console.log(`Settling invoice ...`) + + try { + await settleInvoice(preimage); + } catch (error) { + console.log(error.message); + res.status(500).send({ message: error.message }) + } + + res.status(200).send({ message: 'Invoice settled successfully' }) + } else { + res.status(400).send({ message: 'Invalid input' }) + } +}) + app.listen(port, () => { console.log(`Docker server listening on port ${port}`) }) \ No newline at end of file diff --git a/clients/tests/web/test-utils.js b/clients/tests/web/test-utils.js index 02f3f3de..ca66f42b 100644 --- a/clients/tests/web/test-utils.js +++ b/clients/tests/web/test-utils.js @@ -1,6 +1,4 @@ import axios from 'axios'; -const util = require('node:util'); -const exec = util.promisify(require('node:child_process').exec); const generateBlocks = async (blocks) => { const body = { @@ -45,44 +43,66 @@ const sleep = (ms) => { const generateInvoice = async (paymentHash, amountInSats) => { - const generateInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-alice-1") lncli -n regtest addholdinvoice ${paymentHash} --amt ${amountInSats}`; - const { stdout, stderr } = await exec(generateInvoiceCommand); - if (stderr) { - console.error('Error:', stderr); - return null; - } + const body = { + paymentHash, + amountInSats + }; + + const url = `http://0.0.0.0:3000/generate_invoice`; - try { - const response = JSON.parse(stdout.trim()); - return response; - } catch (error) { - console.error('Error parsing JSON:', error); - return null; + let response = await axios.post(url, body); + + if (response.status == 200) { + const invoice = JSON.parse(response.data.invoice); + return invoice; + } else { + throw new Error(`Failed to generate invoice`); } } const payInvoice = async (paymentRequest) => { + + const body = { + paymentRequest + }; + + const url = `http://0.0.0.0:3000/pay_invoice`; - const payInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-bob-1") lncli -n regtest payinvoice --force ${paymentRequest}`; - const { stdout, stderr } = await exec(payInvoiceCommand); - if (stderr) { - console.error('Error:', stderr); - return null; + let response = await axios.post(url, body); + + if (response.status != 200) { + throw new Error(`Failed to pay invoice`); } - console.log('stdout:', stdout.trim()); - return stdout.trim(); } -const payHoldInvoice = (paymentRequest) => { +const payHoldInvoice = async (paymentRequest) => { + + const body = { + paymentRequest + }; + + const url = `http://0.0.0.0:3000/pay_holdinvoice`; - const payInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-bob-1") lncli -n regtest payinvoice --force ${paymentRequest}`; - exec(payInvoiceCommand); + let response = await axios.post(url, body); + + if (response.status != 200) { + throw new Error(`Failed to pay hold invoice`); + } } const settleInvoice = async (preimage) => { - const settleInvoiceCommand = `docker exec $(docker ps -qf "name=mercurylayer-alice-1") lncli -n regtest settleinvoice ${preimage}`; - await exec(settleInvoiceCommand); + const body = { + preimage + }; + + const url = `http://0.0.0.0:3000/settle_invoice`; + + let response = await axios.post(url, body); + + if (response.status != 200) { + throw new Error(`Failed to settle invoice`); + } } export { generateBlocks, depositCoin, sleep, generateInvoice, payInvoice, payHoldInvoice, settleInvoice };