Use crypto.randomBytes for unique CLN invoice label (#1074)

This commit is contained in:
ekzyis 2024-04-15 16:16:32 +02:00 committed by GitHub
parent 40131da8bd
commit 2ec0a1a559
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 3 deletions

View File

@ -1,10 +1,10 @@
import fetch from 'node-fetch'
import https from 'https'
import crypto from 'crypto'
export const createInvoice = async ({ socket, rune, cert, label, description, msats, expiry }) => {
const agent = cert ? new https.Agent({ ca: Buffer.from(cert, 'base64') }) : undefined
const url = 'https://' + socket + '/v1/invoice'
const randomId = Math.floor(Math.random() * 1000)
const res = await fetch(url, {
method: 'POST',
headers: {
@ -16,8 +16,9 @@ export const createInvoice = async ({ socket, rune, cert, label, description, ms
},
agent,
body: JSON.stringify({
// why does CLN require a unique label?
label: description ? `${description} ${randomId}` : randomId,
// CLN requires a unique label for every invoice
// see https://docs.corelightning.org/reference/lightning-invoice
label: crypto.randomBytes(16).toString('hex'),
description,
amount_msat: msats,
expiry