-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from getAlby/task-zapper
feat: add zapper support
- Loading branch information
Showing
10 changed files
with
390 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { getPublicKey } from "@nostr/tools"; | ||
import { hexToBytes } from "npm:@noble/[email protected]/utils"; | ||
|
||
export const PORT = parseInt(Deno.env.get("PORT") || "8080"); | ||
export const BASE_URL = Deno.env.get("BASE_URL"); | ||
if (!BASE_URL) { | ||
|
@@ -11,3 +14,6 @@ if (!databaseUrl) { | |
Deno.exit(1); | ||
} | ||
export const DATABASE_URL = databaseUrl; | ||
|
||
export const NOSTR_NIP57_PRIVATE_KEY = Deno.env.get("NOSTR_NIP57_PRIVATE_KEY") || ""; | ||
export const NOSTR_NIP57_PUBLIC_KEY = NOSTR_NIP57_PRIVATE_KEY ? getPublicKey(hexToBytes(NOSTR_NIP57_PRIVATE_KEY)) : ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js"; | ||
import { migrate } from "drizzle-orm/postgres-js/migrator"; | ||
import postgres from "https://deno.land/x/[email protected]/mod.js"; | ||
import { nwc } from "npm:@getalby/sdk"; | ||
import postgres from "postgres"; | ||
|
||
import { and, eq } from "drizzle-orm"; | ||
import { DATABASE_URL } from "../constants.ts"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import { Event, finalizeEvent, SimplePool } from "@nostr/tools"; | ||
import { makeZapReceipt } from "@nostr/tools/nip57"; | ||
import { nwc } from "npm:@getalby/sdk"; | ||
import { hexToBytes } from "npm:@noble/[email protected]/utils"; | ||
import { NOSTR_NIP57_PRIVATE_KEY } from "../constants.ts"; | ||
import { decrypt } from "../db/aesgcm.ts"; | ||
import { DB } from "../db/db.ts"; | ||
import { logger } from "../logger.ts"; | ||
|
||
export class NWCPool { | ||
private readonly _db: DB; | ||
private readonly pool: SimplePool; | ||
private readonly zapperPrivateKey: string; | ||
|
||
constructor(db: DB) { | ||
this._db = db; | ||
this.pool = new SimplePool() | ||
this.zapperPrivateKey = NOSTR_NIP57_PRIVATE_KEY; | ||
} | ||
|
||
async init() { | ||
|
@@ -24,13 +33,74 @@ export class NWCPool { | |
}); | ||
|
||
nwcClient.subscribeNotifications( | ||
(notification) => { | ||
async (notification) => { | ||
logger.debug("received notification", { userId, notification }); | ||
if (notification.notification_type === "payment_received") { | ||
this._db.markInvoiceSettled(userId, notification.notification) | ||
const transaction = notification.notification | ||
try { | ||
this._db.markInvoiceSettled(userId, transaction) | ||
await this.publishZap(userId, transaction) | ||
} catch (error) { | ||
logger.error("error processing payment_received notification", { userId, transaction, error }); | ||
} | ||
} | ||
}, | ||
["payment_received"] | ||
); | ||
} | ||
|
||
async publishZap(userId: number, transaction: nwc.Nip47Transaction) { | ||
const metadata = transaction.metadata | ||
const requestEvent = metadata?.nostr as Event | ||
|
||
if (!requestEvent) { | ||
return; | ||
} | ||
|
||
const zapReceipt = makeZapReceipt({ | ||
zapRequest: JSON.stringify(requestEvent), | ||
preimage: transaction.preimage, | ||
bolt11: transaction.invoice, | ||
paidAt: new Date(transaction.settled_at * 1000) | ||
}) | ||
const relays = requestEvent.tags.find(tag => tag[0] === 'relays')?.slice(1); | ||
if (!relays || !relays.length) { | ||
logger.error("no relays specified in zap request", { user_id: userId, transaction }); | ||
return; | ||
} | ||
|
||
const signedEvent = finalizeEvent(zapReceipt, hexToBytes(this.zapperPrivateKey)) | ||
|
||
const results = await Promise.allSettled(this.pool.publish(relays, signedEvent)) | ||
|
||
const successfulRelays: string[] = []; | ||
const failedRelays: string[] = []; | ||
|
||
results.forEach((result, index) => { | ||
const relay = relays[index]; | ||
if (result.status === 'fulfilled') { | ||
successfulRelays.push(relay); | ||
} else { | ||
failedRelays.push(relay); | ||
} | ||
}); | ||
|
||
if (failedRelays.length === relays.length) { | ||
logger.error("failed to publish zap", { | ||
user_id: userId, | ||
event_id: signedEvent.id, | ||
payment_hash: transaction.payment_hash, | ||
failed_relays: relays, | ||
}); | ||
return; | ||
} | ||
|
||
logger.debug("published zap", { | ||
user_id: userId, | ||
event_id: signedEvent.id, | ||
payment_hash: transaction.payment_hash, | ||
successful_relays: successfulRelays, | ||
failed_relays: failedRelays, | ||
}); | ||
} | ||
} |