aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nostr.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nostr.ts')
-rw-r--r--lib/nostr.ts43
1 files changed, 33 insertions, 10 deletions
diff --git a/lib/nostr.ts b/lib/nostr.ts
index 4c28ebc9..ed1b2f42 100644
--- a/lib/nostr.ts
+++ b/lib/nostr.ts
@@ -3,20 +3,31 @@ import { db, g, s } from "$lib/db";
import { l, warn } from "$lib/logging";
import { scan } from "$lib/strfry";
import { fail, fields, getUser, pick } from "$lib/utils";
-import { bytesToHex } from "@noble/hashes/utils";
-import { finalizeEvent, getPublicKey, nip19, verifyEvent } from "nostr-tools";
+import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
+import {
+ finalizeEvent,
+ getPublicKey,
+ nip04,
+ nip19,
+ nip44,
+ verifyEvent,
+} from "nostr-tools";
import type { Event } from "nostr-tools";
import { AbstractSimplePool } from "nostr-tools/abstract-pool";
import { Relay } from "nostr-tools/relay";
export const EX = 60 * 60 * 24;
-const coinos = await Relay.connect(config.nostr);
export const serverSecret = bytesToHex(
nip19.decode(config.nostrKey).data as Uint8Array,
);
-export const serverPubkey = getPublicKey(serverSecret);
+export const serverSecret2 = bytesToHex(
+ nip19.decode(config.nostrKey2).data as Uint8Array,
+);
+
+export const serverPubkey = getPublicKey(hexToBytes(serverSecret));
+export const serverPubkey2 = getPublicKey(hexToBytes(serverSecret2));
const alwaysTrue: any = (t: Event) => {
t[Symbol("verified")] = true;
@@ -33,6 +44,21 @@ export const anon = (pubkey) => ({
followers: [],
});
+// NIP-47 encryption schemes: clients pick one from the 13194 info event's
+// `encryption` tag and mark their requests with the same tag; replies use
+// whatever the request used. No tag means legacy nip04.
+export const encryptionSchemes = "nip44_v2 nip04";
+
+export const decryptPayload = (scheme, sk, pk, content) =>
+ scheme === "nip44_v2"
+ ? nip44.decrypt(content, nip44.getConversationKey(hexToBytes(sk), pk))
+ : nip04.decrypt(sk, pk, content);
+
+export const encryptPayload = (scheme, sk, pk, plaintext) =>
+ scheme === "nip44_v2"
+ ? nip44.encrypt(plaintext, nip44.getConversationKey(hexToBytes(sk), pk))
+ : nip04.encrypt(sk, pk, plaintext);
+
export async function publish(ev, url = config.nostr) {
if (!verifyEvent(ev)) fail("Invalid event");
const r = await Relay.connect(url);
@@ -42,7 +68,7 @@ export async function publish(ev, url = config.nostr) {
export async function handleZap(invoice, sender = undefined) {
try {
- const pubkey = serverPubkey;
+ const pubkey = serverPubkey2;
const zapreq = JSON.parse(invoice.description);
if (!zapreq.tags || zapreq.tags.length === 0) {
@@ -95,7 +121,7 @@ export async function handleZap(invoice, sender = undefined) {
tags.push(["preimage", invoice.payment_preimage]);
const ev = { pubkey, kind, created_at, content, tags };
- const signed = await finalizeEvent(ev, serverSecret);
+ const signed = await finalizeEvent(ev, hexToBytes(serverSecret2));
l("sending receipt");
@@ -180,9 +206,7 @@ export const getCount = async (pubkey) => {
}
return { follows, followers };
- } catch (e) {
- console.log(e);
- }
+ } catch (e) {}
};
export const getNostrUser = async (key) => {
@@ -231,7 +255,6 @@ export const q = async (f) => {
pool.subscribeMany(["wss://relay.primal.net"], [f], {
onevent(e) {
r.push(e);
- coinos.publish(e);
},
oneose() {
resolve(r);