Skip to content

Commit

Permalink
feat: add endpoints for invoice ops on node server
Browse files Browse the repository at this point in the history
fix: invoice generation from node server
  • Loading branch information
DhananjayPurohit committed Aug 22, 2024
1 parent ec6be60 commit 2a25642
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 26 deletions.
102 changes: 102 additions & 0 deletions clients/tests/web/server-regtest.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}`)
})
72 changes: 46 additions & 26 deletions clients/tests/web/test-utils.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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 };

0 comments on commit 2a25642

Please sign in to comment.