Last active
August 11, 2022 12:31
-
-
Save timothyylim/d0fb6f1a33881093aa0d0d3989f81ff1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
let axios = require('axios') | |
const https = require('https') | |
const macaroon = fs.readFileSync('./admin.macaroon').toString('hex') | |
const myNode = axios.create({ | |
baseURL: '', // Your endpoint here | |
httpsAgent: new https.Agent({ | |
rejectUnauthorized: false | |
}), | |
headers: { 'Grpc-Metadata-macaroon': macaroon } | |
}) | |
const getInfo = async () => { | |
try { | |
let res = await myNode.post('/v1/getinfo') | |
return res | |
} catch(err) { | |
console.error(err.repsonse) | |
} | |
} | |
const payInvoice = async (invoice) => { | |
try { | |
let res = await myNode.post('/v2/router/send', { | |
payment_request: invoice, | |
timeout_seconds: 60, | |
fee_limit_sat: 100 | |
}) | |
return res | |
} catch (err) { | |
console.error(err.response) | |
} | |
} | |
const createInvoice = async (amountSatoshis) => { | |
try { | |
let res = await myNode.post('/v1/invoices', { | |
value: amountSatoshis | |
}) | |
return res | |
} catch (err) { | |
console.error(err.response) | |
} | |
} | |
const isSettled = async (r_hash) => { | |
try { | |
const buffer = Buffer.from(r_hash, 'base64') | |
let res = await myNode.get(`/v1/invoice/${buffer.toString('hex')}`) | |
return res.data.settled | |
} catch (err) { | |
console.log(err.response) | |
} | |
} | |
const run = async () => { | |
/* Get information of the lightning node */ | |
const res = getInfo() | |
/* Pay an invoice generated elsewhere */ | |
// const invoice = process.argv[2] | |
// const res = await payInvoice(invoice) | |
/* Create an invoice */ | |
// const res = await createInvoice(5000) | |
/* Check that an invoice has been paid */ | |
// const invoice = process.argv[2] | |
// const res = await isSettled(r_hash) | |
console.log(res) | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe you have 2 misspellings here, line 18 '/v1/getisfo' and line 21, "error.repsonse". Also, should this be "err.response" at line 21 as it is at lines 34 and 45?