Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nip-05 #15

Merged
merged 9 commits into from
Dec 24, 2024
2 changes: 2 additions & 0 deletions drizzle/0002_crazy_sauron.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "users" ADD COLUMN "nostr_pubkey" text;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_nostr_pubkey_unique" UNIQUE("nostr_pubkey");
209 changes: 209 additions & 0 deletions drizzle/meta/0002_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"id": "0158a5e2-5076-4051-8e8d-2d382552344d",
"prevId": "52607bd8-7a1a-4a34-ad27-91b78733e85c",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.invoices": {
"name": "invoices",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"amount": {
"name": "amount",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false
},
"payment_request": {
"name": "payment_request",
"type": "text",
"primaryKey": false,
"notNull": true
},
"payment_hash": {
"name": "payment_hash",
"type": "text",
"primaryKey": false,
"notNull": true
},
"preimage": {
"name": "preimage",
"type": "text",
"primaryKey": false,
"notNull": false
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": false
},
"settled_at": {
"name": "settled_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"user_id_idx": {
"name": "user_id_idx",
"columns": [
{
"expression": "user_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"user_payment_hash_idx": {
"name": "user_payment_hash_idx",
"columns": [
{
"expression": "user_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "payment_hash",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"invoices_user_id_users_id_fk": {
"name": "invoices_user_id_users_id_fk",
"tableFrom": "invoices",
"tableTo": "users",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"invoices_payment_request_unique": {
"name": "invoices_payment_request_unique",
"nullsNotDistinct": false,
"columns": [
"payment_request"
]
},
"invoices_payment_hash_unique": {
"name": "invoices_payment_hash_unique",
"nullsNotDistinct": false,
"columns": [
"payment_hash"
]
}
}
},
"public.users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"connection_secret": {
"name": "connection_secret",
"type": "text",
"primaryKey": false,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
},
"nostr_pubkey": {
"name": "nostr_pubkey",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"users_username_unique": {
"name": "users_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
},
"users_nostr_pubkey_unique": {
"name": "users_nostr_pubkey_unique",
"nullsNotDistinct": false,
"columns": [
"nostr_pubkey"
]
}
}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
7 changes: 7 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"when": 1733813329314,
"tag": "0001_white_prism",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1734078845730,
"tag": "0002_crazy_sauron",
"breakpoints": true
}
]
}
7 changes: 5 additions & 2 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class DB {

async createUser(
connectionSecret: string,
username?: string
username?: string,
nostrPubkey?: string
) {
const parsed = nwc.NWCClient.parseWalletConnectUrl(connectionSecret);
if (!parsed.secret) {
Expand All @@ -43,7 +44,8 @@ export class DB {
const [newUser] = await this._db.insert(users).values({
encryptedConnectionSecret,
username,
}).returning({ id: users.id, username: users.username });
nostrPubkey
}).returning({ id: users.id, username: users.username, nostrPubkey: users.nostrPubkey });

return newUser;
}
Expand All @@ -62,6 +64,7 @@ export class DB {
const connectionSecret = await decrypt(result.encryptedConnectionSecret);
return {
id: result.id,
nostrPubkey: result.nostrPubkey,
connectionSecret
};
}
Expand Down
1 change: 1 addition & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const users = pgTable("users", {
id: serial("id").primaryKey(),
encryptedConnectionSecret: text("connection_secret").notNull(),
username: text("username").unique().notNull(),
nostrPubkey: text("nostr_pubkey").unique(),
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
createdAt: timestamp("created_at").notNull().defaultNow(),
});

Expand Down
55 changes: 1 addition & 54 deletions src/lnurlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,8 @@ import { validateZapRequest } from "@nostr/tools/nip57";
import { Hono } from "hono";
import { nwc } from "npm:@getalby/sdk";
import { logger } from "../src/logger.ts";
import { BASE_URL, DOMAIN, NOSTR_NIP57_PUBLIC_KEY } from "./constants.ts";
import { BASE_URL } from "./constants.ts";
import { DB } from "./db/db.ts";
import "./nwc/nwcPool.ts";

function getLnurlMetadata(username: string): string {
return JSON.stringify([
["text/identifier", `${username}@${DOMAIN}`],
["text/plain", `Sats for ${username}`],
])
}

export function createLnurlWellKnownApp(db: DB) {
const hono = new Hono();

hono.get("/:username", async (c) => {
try {
const username = c.req.param("username");

logger.debug("LNURLp request", { username });

// check the user exists
await db.findUser(username);

// TODO: zapper support

return c.json({
tag: "payRequest",
commentAllowed: 255,
callback: `${BASE_URL}/lnurlp/${username}/callback`,
minSendable: 1000,
maxSendable: 10000000000,
metadata: getLnurlMetadata(username),
payerData: {
name: {
mandatory: false
},
email: {
mandatory: false
},
pubkey: {
mandatory: false
}
},
...(NOSTR_NIP57_PUBLIC_KEY ? {
nostrPubkey: NOSTR_NIP57_PUBLIC_KEY,
allowsNostr: true,
} : {})
});
} catch (error) {
return c.json({ status: "ERROR", reason: "" + error });
}
});

return hono;
}

export function createLnurlApp(db: DB) {
const hono = new Hono();
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { secureHeaders } from "hono/secure-headers";
//import { sentry } from "npm:@hono/sentry";
import { PORT } from "./constants.ts";
import { DB, runMigration } from "./db/db.ts";
import { createLnurlApp, createLnurlWellKnownApp } from "./lnurlp.ts";
import { createLnurlApp } from "./lnurlp.ts";
import { LOG_LEVEL, logger, loggerMiddleware } from "./logger.ts";
import { NWCPool } from "./nwc/nwcPool.ts";
import { createUsersApp } from "./users.ts";
import { createLnurlWellKnownApp, createNostrWellKnownApp } from "./well-known/index.ts";

await runMigration();

Expand All @@ -29,6 +30,7 @@ hono.use(cors());
}*/

hono.route("/.well-known/lnurlp", createLnurlWellKnownApp(db));
hono.route("/.well-known/nostr.json", createNostrWellKnownApp(db));
hono.route("/lnurlp", createLnurlApp(db));
hono.route("/users", createUsersApp(db, nwcPool));

Expand Down
Loading
Loading