-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendNote.js
More file actions
43 lines (38 loc) · 1.48 KB
/
sendNote.js
File metadata and controls
43 lines (38 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk";
import { nip19 } from "nostr-tools";
import readline from "readline";
import { mineEventPow } from "./pow.js";
async function prompt(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, answer => {
rl.close();
resolve(answer);
}));
}
export async function sendNote({ ndk, signer, npub, powBits, timeoutMs }) {
const content = await prompt("Enter note content: ");
const noteEvent = new NDKEvent(ndk, { kind: NDKKind.Text, content });
await noteEvent.sign();
const minedRaw = await mineEventPow(noteEvent, powBits);
const minedEv = new NDKEvent(ndk, minedRaw);
await minedEv.sign();
try {
const okRelays = await minedEv.publish(undefined, timeoutMs);
console.log("✅ stored on:", [...okRelays].map(r => r.url));
} catch (e) {
console.error("Publish error:", e);
}
const { data: pubHex } = nip19.decode(npub);
const filter = {
authors: [pubHex],
kinds: [0, 1],
limit: 10
};
const events = await ndk.fetchEvents(filter, { timeoutSec: 5 });
console.log(`\n📝 Latest 10 events by ${npub} (kinds 0 & 1):\n`);
[...events]
.sort((a, b) => b.created_at - a.created_at)
.forEach((e, i) => {
console.log(`${i + 1}. [${e.kind}] ${e.content} (id: ${e.id}, created_at: ${e.created_at})\n`);
});
}