diff options
| author | Adam Soltys <[email protected]> | 2024-12-23 07:38:00 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-12-23 07:38:01 +0000 |
| commit | 47236bc1ac522aa14e280e1a9664b3bd90e761db (patch) | |
| tree | bed7cd95f52ec45f2f6746afe296588208bd7872 /lib | |
| parent | 053db6c4d155b42bfdd5bb27530ec27dd4421e26 (diff) | |
| download | coinos-server-47236bc1ac522aa14e280e1a9664b3bd90e761db.tar.gz coinos-server-47236bc1ac522aa14e280e1a9664b3bd90e761db.zip | |
replay function
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lightning.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/lightning.ts b/lib/lightning.ts index 5c365805..974a91c3 100644 --- a/lib/lightning.ts +++ b/lib/lightning.ts @@ -53,3 +53,56 @@ export async function listenForLightning() { err("problem receiving lightning payment", e.message); } } + + +export async function replay() { + const inv = await ln.waitanyinvoice(440516 - 1); + const { + local_offer_id, + bolt11, + description, + pay_index, + amount_received_msat, + payment_preimage: preimage, + } = inv; + + const received = Math.round(amount_received_msat / 1000); + console.log("replaying", pay_index); + + try { + if (!preimage) return; + + const id = + (await g(`invoice:${bolt11}`)) || (await g(`invoice:${local_offer_id}`)); + const invoice = await g(`invoice:${id}`); + if (!invoice) return warn("received lightning with no invoice", bolt11); + + if (invoice?.memo) { + try { + if (JSON.parse(description).kind === 9734) await handleZap(inv); + } catch (e) { + if (!e.message.startsWith("Unexpected")) + warn("failed to handle zap", e.message); + } + } + + const p = await getPayment(bolt11 || local_offer_id); + if (p) { + console.log(invoice); + return warn("already processed", bolt11, local_offer_id); + } + + await credit({ + hash: bolt11 || local_offer_id, + amount: received, + memo: invoice.memo, + ref: preimage, + type: types.lightning, + }); + } catch (e) { + console.log(e); + err("problem receiving lightning payment", e.message); + } +} + +// replay(); |
