diff options
| author | Adam Soltys <[email protected]> | 2024-09-19 13:04:01 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-09-19 13:04:01 -0700 |
| commit | a8dce552da00fcdd144dbc2a39d660c342d4c7b5 (patch) | |
| tree | e6d8782dd3a1d650ea36eb69cfa85536e758f2d3 | |
| parent | 0b2f9a963ed2503ac611eab3d8bd5b640ebe08f2 (diff) | |
| download | coinos-server-a8dce552da00fcdd144dbc2a39d660c342d4c7b5.tar.gz coinos-server-a8dce552da00fcdd144dbc2a39d660c342d4c7b5.zip | |
nwc make_invoice
| -rw-r--r-- | lib/invoices.ts | 3 | ||||
| -rw-r--r-- | lib/nwc.ts | 43 | ||||
| -rw-r--r-- | routes/invoices.ts | 9 |
3 files changed, 38 insertions, 17 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts index 45c97fe2..0094c6d7 100644 --- a/lib/invoices.ts +++ b/lib/invoices.ts @@ -10,7 +10,7 @@ import ln from "$lib/ln"; let bc = rpc(config.bitcoin); let lq = rpc(config.liquid); -export let generate = async ({ invoice, user, sender = undefined }) => { +export let generate = async ({ invoice, user }) => { let { bolt11, account, @@ -34,7 +34,6 @@ export let generate = async ({ invoice, user, sender = undefined }) => { tip = parseInt(tip) || null; if (user) user = await getUser(user.username); - else if (sender) user = await getUser(sender.username); if (!user) fail("user not provided"); if (account === user.id) account = undefined; @@ -7,6 +7,7 @@ import { sendInternal, sendLightning } from "$lib/payments"; import { serverPubkey } from "$lib/nostr"; import { nip04, nip19, finalizeEvent } from "nostr-tools"; import type { UnsignedEvent } from "nostr-tools"; +import { generate } from "$lib/invoices"; import ln from "$lib/ln"; let result = (result) => ({ result }); @@ -98,20 +99,42 @@ let handle = (method, params, user) => async get_info() { let { alias, blockheight, color } = await ln.getinfo(); - return { - result: { - alias, - color, - pubkey: serverPubkey, - network: "mainnet", - block_height: blockheight, - methods: ["pay_invoice", "get_balance", "get_info"], - }, - }; + return result({ + alias, + color, + pubkey: serverPubkey, + network: "mainnet", + block_height: blockheight, + methods: ["pay_invoice", "get_balance", "get_info"], + }); }, async get_balance() { let balance = await g(`balance:${user.id}`); return result({ balance }); }, + + async make_invoice() { + let { amount, description, description_hash, expiry } = params; + + let invoice = { + amount: Math.round(amount / 1000), + type: "lightning", + memo: description, + expiry, + }; + + let { hash, created: created_at } = await generate({ invoice, user }); + + return result({ + type: "incoming", + invoice: hash, + description, + description_hash, + amount, + created_at, + expires_at: created_at + expiry, + metadata: {}, + }); + }, })[method](params); diff --git a/routes/invoices.ts b/routes/invoices.ts index 3dd60de0..4cbd9660 100644 --- a/routes/invoices.ts +++ b/routes/invoices.ts @@ -29,13 +29,12 @@ export default { }, async create(req, res) { - let { - body: { invoice, user }, - user: sender, - } = req; + let { body, user } = req; + if (body.user) user = body.user; + let { invoice } = body; try { - res.send(await generate({ invoice, user, sender })); + res.send(await generate({ invoice, user })); } catch (e) { err("problem generating invoice", e.message); bail(res, e.message); |
