Skip to content

Commit

Permalink
Merge pull request #15 from getAlby/task-nip05
Browse files Browse the repository at this point in the history
feat: add nip-05
  • Loading branch information
im-adithya authored Dec 24, 2024
2 parents 7ecffde + d24748c commit 84d004d
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 78 deletions.
8 changes: 7 additions & 1 deletion drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { defineConfig } from "drizzle-kit";
import { DATABASE_URL } from "./src/constants.ts";
const DATABASE_URL = Deno.env.get("DATABASE_URL");

if (!DATABASE_URL) {
console.log("no DATABASE_URL provided, exiting");
Deno.exit(1);
}

export default defineConfig({
dialect: "postgresql",
schema: "./src/db/schema.ts",
Expand Down
1 change: 1 addition & 0 deletions drizzle/0002_green_frightful_four.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "users" ADD COLUMN "nostr_pubkey" text NOT NULL;
202 changes: 202 additions & 0 deletions drizzle/meta/0002_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"id": "cc14ef05-cccd-4838-94d5-31521a8d5625",
"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": true
},
"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"
]
}
}
}
},
"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": 1735020701926,
"tag": "0002_green_frightful_four",
"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").notNull(),
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

0 comments on commit 84d004d

Please sign in to comment.