From 044c1737289f89d111bcc8c3c709479286dcf29d Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 4 Sep 2025 08:45:01 -0700 Subject: support readonly tokens --- routes/users.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'routes') diff --git a/routes/users.ts b/routes/users.ts index 36f2aab4..750494b6 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -311,7 +311,7 @@ export default { emit(user.id, "user", user); res.send({ user }); } catch (e) { - console.log(e) + console.log(e); warn("failed to update", user.username, e.message); bail(res, e.message); } @@ -1044,4 +1044,11 @@ export default { const liquid = await g(`credit:liquid:${id}`); res.send({ bitcoin, lightning, liquid }); }, + + async ro(req, res) { + const { user } = req; + const payload = { id: `${user.id}-ro` }; + const token = jwt.sign(payload, config.jwt); + res.send(token); + }, }; -- cgit v1.2.3 From fdfbb781f40775f39dbaa10269f559695f670b37 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 4 Sep 2025 08:20:11 -0700 Subject: create external payment --- index.ts | 1 + lib/square.ts | 1 - routes/square.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) (limited to 'routes') diff --git a/index.ts b/index.ts index 02a6e0b5..d4748059 100644 --- a/index.ts +++ b/index.ts @@ -86,6 +86,7 @@ app.post("/replace", auth, payments.replace); app.get("/decode/:bolt11", payments.decode); app.post("/fetchinvoice", payments.fetchinvoice); +app.get("/square/sync", auth, square.sync); app.get("/square/connect", auth, square.connect); app.get("/square/auth", auth, square.auth); app.post("/square/payment", square.payment); diff --git a/lib/square.ts b/lib/square.ts index e00f0a44..11710774 100644 --- a/lib/square.ts +++ b/lib/square.ts @@ -153,7 +153,6 @@ async function getOrCacheLocationId(uid: string, client: SquareClient): Promise< return firstActiveId; } -// ----- Public entry: syncSquare ----- export async function syncSquare(inv: CoinosInv, p: CoinosPayment, user: CoinosUser) { // 1) Load/refresh per-user OAuth creds const auth = await ensureFreshAccessToken(user.id); diff --git a/routes/square.ts b/routes/square.ts index fd8c3abc..eaffff1e 100644 --- a/routes/square.ts +++ b/routes/square.ts @@ -7,8 +7,9 @@ import { sha256 } from "@noble/hashes/sha256"; import { utf8ToBytes } from "@noble/hashes/utils"; import { base64urlnopad as base64 } from "@scure/base"; import { SquareClient } from "square"; +import { v4 } from "uuid"; -const { scopes, url, appId, environment } = config.square; +const { scopes, url, appId, clientSecret, environment } = config.square; export default { async connect(req, res) { @@ -31,6 +32,62 @@ export default { ); }, + async sync(req, res) { + try { + const { user } = req; + let square = await g(`${user.id}:square`); + + let client = new SquareClient({ + environment, + }); + + const { refreshToken } = square; + + square = await client.oAuth.obtainToken({ + clientId: appId, + grantType: "refresh_token", + refreshToken, + }); + + await s(`${user.id}:square`, square); + + const { accessToken } = square; + + client = new SquareClient({ + token: accessToken, + environment, + }); + + // const payments = await authedClient.payments.list({}); + // console.log(payments); + const locs = await client.locations.list({}); + + const pay = await client.payments.create({ + sourceId: "EXTERNAL", + idempotencyKey: v4(), + amountMoney: { + amount: BigInt(1000), + currency: "CAD", + }, + // appFeeMoney: { + // amount: BigInt(10), + // currency: "CAD", + // }, + autocomplete: true, + externalDetails: { type: "OTHER", source: `Coinos ${v4()}` }, + locationId: locs.locations[0].id, + referenceId: v4(), + note: `Brief description ${v4()}`, + }); + + console.log(pay); + + res.send({}); + } catch (e) { + console.log(e); + } + }, + async auth(req, res) { const { user } = req; -- cgit v1.2.3 From 780a7cca58f3e34140fd9430e2ae02145e4149e0 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 6 Sep 2025 16:46:30 -0700 Subject: convert pubkey from npub --- routes/users.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'routes') diff --git a/routes/users.ts b/routes/users.ts index 750494b6..82bff805 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -207,6 +207,7 @@ export default { let { pubkey } = body; if (pubkey) { pubkey = pubkey.trim(); + if (pubkey.startsWith("npub")) pubkey = nip19.decode(pubkey).data; exists = await getUser(pubkey); const existingUsername = exists?.username ?.toLowerCase() @@ -226,7 +227,6 @@ export default { fail("Invalid signature or challenge mismatch."); pubkey = pubkey.replace(/\s*/g, ""); - if (pubkey.startsWith("npub")) pubkey = nip19.decode(pubkey).data; if (pubkey.length !== 64) fail(`Invalid pubkey ${pubkey}`); await db.del(`user:${user.pubkey}`); user.pubkey = pubkey; -- cgit v1.2.3