From 2ec0a1a559390c1d2d4cf9990e164159b6bcf764 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Mon, 15 Apr 2024 16:16:32 +0200 Subject: [PATCH] Use crypto.randomBytes for unique CLN invoice label (#1074) --- lib/cln.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/cln.js b/lib/cln.js index 034457f8..b11b2f1d 100644 --- a/lib/cln.js +++ b/lib/cln.js @@ -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