Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async getUserInvoices() {
let range = await this._redis.lrange('userinvoices_for_' + this._userid, 0, -1);
let result = [];
for (let invoice of range) {
invoice = JSON.parse(invoice);
let decoded = lightningPayReq.decode(invoice.payment_request);
invoice.description = '';
for (let tag of decoded.tags) {
if (tag.tagName === 'description') {
invoice.description += decodeURIComponent(tag.data);
}
if (tag.tagName === 'payment_hash') {
invoice.payment_hash = tag.data;
}
}
invoice.ispaid = _invoice_ispaid_cache[invoice.payment_hash] || !!(await this.getPaymentHashPaid(invoice.payment_hash));
if (!invoice.ispaid) {
if (decoded && decoded.timestamp > ((+new Date()) / 1000 - 3600 * 24 * 5)) {
// if invoice is not too old we query lnd to find out if its paid
let lookup_info = await this.lookupInvoice(invoice.payment_hash);
invoice.ispaid = lookup_info.settled; // TODO: start using `state` instead as its future proof, and this one might get deprecated
module.exports = ({invoice}, cbk) => {
if (!invoice) {
return cbk([0, 'Expected invoice']);
}
try {
const {tags} = decode(invoice);
const [paymentHashTag] = tags.filter(t => t.tagName === 'payment_hash');
return cbk(null, {id: paymentHashTag.data});
} catch (e) {
return cbk([0, 'Error parsing invoice', e]);
}
};
export const isLn = (input, chain = 'bitcoin', network = 'mainnet') => {
if (!input || typeof input !== 'string') {
return false
}
try {
const decoded = lightningRequestReq.decode(input)
if (decoded.coinType !== get(coinTypes, `${chain}.${network}`)) {
throw new Error('Invalid coin type')
}
return true
} catch (e) {
return false
}
}
async saveUserInvoice(doc) {
let decoded = lightningPayReq.decode(doc.payment_request);
let payment_hash;
for (let tag of decoded.tags) {
if (tag.tagName === 'payment_hash') {
payment_hash = tag.data;
}
}
await this._redis.set('payment_hash_' + payment_hash, this._userid);
return await this._redis.rpush('userinvoices_for_' + this._userid, JSON.stringify(doc));
}
export const decodePayReq = (payReq, addDefaults = true) => {
const data = lightningRequestReq.decode(payReq)
const expiry = getTag(data, 'expire_time')
if (addDefaults && !expiry) {
data.tags.push({
tagName: 'expire_time',
data: 3600,
})
data.timeExpireDate = data.timestamp + 3600
data.timeExpireDateString = new Date(data.timeExpireDate * 1000).toISOString()
}
return data
}
export const decodePayReq = (payReq, addDefaults = true) => {
const data = lightningRequestReq.decode(payReq)
const expiry = data.tags.find(t => t.tagName === 'expire_time')
if (addDefaults && !expiry) {
data.tags.push({
tagName: 'expire_time',
data: 3600,
})
data.timeExpireDate = data.timestamp + 3600
data.timeExpireDateString = new Date(data.timeExpireDate * 1000).toISOString()
}
return data
}
export const isLn = (input, chain = 'bitcoin', network = 'mainnet') => {
if (!input || typeof input !== 'string') {
return false
}
try {
const decoded = lightningRequestReq.decode(input)
if (decoded.coinType !== get(coinTypes, `${chain}.${network}`)) {
throw new Error('Invalid coin type')
}
return true
} catch (e) {
return false
}
}
decodePayReqLocally(payReq) {
this._decoded_locally = lightningPayReq.decode(payReq);
}
tagName: 'payment_hash',
data: '0001020304050607080900010203040506070809000102030405060708090102',
},
{
tagName: 'expire_time',
data: 30,
},
{
tagName: 'description',
data: memo,
},
],
}
data[unit] = amount
var encoded = lightningPayReq.encode(data)
var privateKeyHex = 'e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734'
var signed = lightningPayReq.sign(encoded, privateKeyHex)
return signed.paymentRequest
}
const mockCreateInvoice = async (amount, currency, memo = '') => {
const satoshis = convert(currency, 'sats', amount)
const encoded = lightningPayReq.encode({
coinType: 'bitcoin',
satoshis,
tags: [
{
tagName: 'purpose_commit_hash',
data: '3925b6f67e2c340036ed12093dd44e0368df1b6ea26c53dbe4811f58fd5db8c1',
},
{
tagName: 'payment_hash',
data: '0001020304050607080900010203040506070809000102030405060708090102',
},
{
tagName: 'expire_time',
data: 30,
},
{