From ff4d63f395a556543e9a642676f92dea5d1c1645 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 21 Nov 2024 16:31:51 -0800 Subject: ecash requests --- index.ts | 2 +- lib/ecash.ts | 52 ++++++++++++++++++---------------------------------- lib/nostr.ts | 5 ++--- routes/ecash.ts | 42 ++++++------------------------------------ routes/nostr.ts | 19 +++++++++++++++---- 5 files changed, 42 insertions(+), 78 deletions(-) diff --git a/index.ts b/index.ts index adb13863..2b6ee92d 100644 --- a/index.ts +++ b/index.ts @@ -47,6 +47,7 @@ app.get("/invoice/:id", invoices.get); app.post("/invoice", optional, invoices.create); app.get("/nostr.json", nostr.identities); +app.get("/profile/:profile", nostr.profile); app.get("/:pubkey/count", nostr.count); app.get("/:pubkey/followers", nostr.followers); app.get("/:pubkey/follows", nostr.follows); @@ -125,7 +126,6 @@ app.post("/unlimit", admin, users.unlimit); app.get("/cash/:id/:version", ecash.get); app.post("/cash", ecash.save); -app.post("/cash/pay", ecash.pay); app.post("/claim", auth, ecash.claim); app.post("/mint", auth, ecash.mint); app.post("/melt", auth, ecash.melt); diff --git a/lib/ecash.ts b/lib/ecash.ts index 05326718..4bcbb6d7 100644 --- a/lib/ecash.ts +++ b/lib/ecash.ts @@ -14,9 +14,10 @@ import { const m = new CashuMint(config.mintUrl); const w = new CashuWallet(m); -const enc = (proofs, v = 4) => - (v === 4 ? getEncodedTokenV4 : getEncodedToken)({ - token: [{ mint: config.mintUrl, proofs }], +const enc = (proofs) => + getEncodedToken({ + mint: config.mintUrl, + proofs, }); const dec = (token) => getDecodedToken(token).token[0]; @@ -28,9 +29,8 @@ const ext = async (mint) => { return issuerPk !== ourPk; }; -export async function get(id, v = 4) { +export async function get(id) { const token = await g(`cash:${id}`); - if (v < 4) return enc(dec(token).proofs, 3); return token; } @@ -46,13 +46,16 @@ export async function claim(token) { return rcvd.reduce((a, b) => a + b.amount, 0); } -export async function mint(amount, v = 4) { - const { proofs } = dec(await g("cash")); - const { send, returnChange } = await w.send(amount, proofs); - const rcvd = await w.receive(enc(send, v)); - const change = enc(returnChange, v); +export async function mint(amount) { + const { keysets } = await m.getKeySets(); + const w = new CashuWallet(m, { keysets }); + const { proofs } = getDecodedToken(await g("cash")); + const { send, keep } = await w.send(amount, proofs); + console.log("SEND", send); + const rcvd = await w.receive(enc(send)); + const change = enc(keep); await s("cash", change); - return enc(rcvd, v); + return enc(rcvd); } export async function check(token) { @@ -79,10 +82,11 @@ export async function init(amount = 10000) { return state === MintQuoteState.PAID; }); - const { proofs } = await w.mintTokens(amount, quote); + const proofs = await w.mintProofs(amount, quote); const cash = getEncodedTokenV4({ - token: [{ mint: config.mintUrl, proofs }], + mint: config.mintUrl, + proofs, }); await s("cash", cash); @@ -90,24 +94,4 @@ export async function init(amount = 10000) { console.log(e); } } - -export async function payNostr(token) { - const decodedToken = getDecodedToken(token); - if (!decodedToken) { - console.error("could not decode token"); - return; - } - const proofs = token.getProofs(decodedToken); - const mint = token.getMint(decodedToken); - const paymentPayload: PaymentRequestPayload = { - id: request.id, - mint: mint, - unit: request.unit || "", - proofs: proofs, - }; - const paymentPayloadString = JSON.stringify(paymentPayload); - await nostrStore.sendNip17DirectMessageToNprofile( - transport.target, - paymentPayloadString, - ); -} +init(); diff --git a/lib/nostr.ts b/lib/nostr.ts index 82251baa..bf37779b 100644 --- a/lib/nostr.ts +++ b/lib/nostr.ts @@ -1,5 +1,5 @@ import config from "$config"; -import { db, g, s } from "$lib/db"; +import { g, s } from "$lib/db"; import { l } from "$lib/logging"; import { fail } from "$lib/utils"; import { finalizeEvent, getPublicKey, nip19 } from "nostr-tools"; @@ -119,12 +119,11 @@ export const getRelays = async (pubkey): Promise => { return { read, write }; }; -export const getProfile = async (pubkey) => { +export const getProfile = async (pubkey, relays = [config.nostr]) => { let profile = await g(`${pubkey}:profile`); if (!profile) { const pubkeys = [pubkey]; - let relays = [config.nostr]; let filter: any = { authors: pubkeys, kinds: [0] }; let ev = await pool.get(relays, filter, opts); diff --git a/routes/ecash.ts b/routes/ecash.ts index ff092374..101d1aca 100644 --- a/routes/ecash.ts +++ b/routes/ecash.ts @@ -1,26 +1,21 @@ import { db, g, s } from "$lib/db"; -import { check, claim, get, mint, request } from "$lib/ecash"; +import { check, claim, get, mint } from "$lib/ecash"; import { err, l } from "$lib/logging"; import { credit, debit, types } from "$lib/payments"; import { emit } from "$lib/sockets"; import { bail, fail } from "$lib/utils"; -import { - PaymentRequest, - PaymentRequestPayload, - PaymentRequestTransport, - PaymentRequestTransportType, - decodePaymentRequest, - getDecodedToken, -} from "@cashu/cashu-ts"; import { v4 } from "uuid"; const { ecash: type } = types; const sendCash = async ({ amount, user }) => { + console.log("HELLO", amount); const id = v4(); const hash = v4(); const p = await debit({ hash, amount, user, type }); + console.log("P", p); const token = await mint(amount); + console.log("TOKEN", token); p.memo = id; await s(`payment:${p.id}`, p); s(`cash:${id}`, token); @@ -93,8 +88,10 @@ export default { user, } = req; try { + console.log("OY"); res.send(await sendCash({ amount, user })); } catch (e) { + console.log(e); err(e.message); bail(res, e.message); } @@ -152,31 +149,4 @@ export default { bail(res, e.message); } }, - - async pay(req, res) { - const { user } = req; - const { request } = req.body; - const { amount } = decodePaymentRequest(request); - const { id, token } = await sendCash({ amount, user }); - - - const decodedToken = getDecodedToken(token); - if (!decodedToken) { - console.error("could not decode token"); - return; - } - const proofs = token.getProofs(decodedToken); - const mint = token.getMint(decodedToken); - const paymentPayload: PaymentRequestPayload = { - id: request.id, - mint: mint, - unit: request.unit || "", - proofs: proofs, - }; - const paymentPayloadString = JSON.stringify(paymentPayload); - try { - await nostrStore.sendNip17DirectMessageToNprofile(transport.target, paymentPayloadString); - - res.send({ id, token }); - }, }; diff --git a/routes/nostr.ts b/routes/nostr.ts index 1ad86baf..360ae22c 100644 --- a/routes/nostr.ts +++ b/routes/nostr.ts @@ -1,14 +1,17 @@ import config from "$config"; -import { bail, fail, getUser, time } from "$lib/utils"; -import { g, s, db } from "$lib/db"; +import { db, g, s } from "$lib/db"; import { anon, - send, getCount, + getProfile, getRelays, - serverPubkey, pool, + send, + serverPubkey, } from "$lib/nostr"; +import { bail, fail, getUser } from "$lib/utils"; +import { decode } from "nostr-tools/nip19"; +import type { ProfilePointer } from "nostr-tools/nip19"; const opts = { maxWait: 2000 }; @@ -198,4 +201,12 @@ export default { async info(_, res) { res.send({ pubkey: serverPubkey }); }, + + async profile(req, res) { + const { profile } = req.params; + const { data } = decode(profile); + const { pubkey, relays } = data as ProfilePointer; + const user = await getProfile(pubkey, relays); + res.send(user); + }, }; -- cgit v1.2.3