forked from SamSamskies/nostrstuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnostr.ts
151 lines (125 loc) · 3.41 KB
/
nostr.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { bech32 } from "bech32";
import "websocket-polyfill";
import { DEFAULT_RELAYS } from "@/constants";
const { nip05, nip19, SimplePool } = require("nostr-tools");
type Filter = {
ids?: string[];
kinds?: number[];
authors?: string[];
since?: number;
until?: number;
limit?: number;
[key: `#${string}`]: string[];
};
export interface Nip05QueryResult {
pubkey: string;
relays?: string[];
}
export const queryNip05 = nip05.queryProfile;
const isHex = (str: string) => str.match(/^[0-9a-fA-F]+$/) !== null;
export const convertToHex = (bech32Value: string) => {
const decoded = bech32.decode(bech32Value);
return Buffer.from(bech32.fromWords(decoded.words)).toString("hex");
};
const normalizeId = (id: string) => (isHex(id) ? id : convertToHex(id));
const findOneFromRelays = async (relays: string[], filter: Filter) => {
let pool;
try {
pool = new SimplePool();
return await pool.get(relays, filter);
} catch (error) {
return error instanceof Error ? error.message : "Something went wrong :(";
} finally {
if (pool) {
try {
pool.close();
} catch {
// fail silently for errors that happen when closing the pool
}
}
}
};
const findFromRelays = async (relays: string[], filters: Filter[]) => {
let pool;
try {
pool = new SimplePool();
return await pool.list(relays, filters);
} catch (error) {
return error instanceof Error ? error.message : "Something went wrong :(";
} finally {
if (pool) {
try {
pool.close();
} catch {
// fail silently for errors that happen when closing the pool
}
}
}
};
export const getUserProfile = (
userId: string,
relays: string[] = DEFAULT_RELAYS
) =>
findOneFromRelays(
Array.from(new Set([...DEFAULT_RELAYS, ...relays, "wss://purplepag.es"])),
{
authors: [normalizeId(userId)],
kinds: [0],
}
);
export const getUserRelays = (
userId: string,
relays: string[] = DEFAULT_RELAYS
) =>
findOneFromRelays(Array.from(new Set(DEFAULT_RELAYS.concat(relays))), {
authors: [normalizeId(userId)],
kinds: [3],
});
export const getUserNoosts = (
userId: string,
relays: string[] = DEFAULT_RELAYS,
limit: number = 10
) =>
findFromRelays(Array.from(new Set(DEFAULT_RELAYS.concat(relays))), [
{
authors: [normalizeId(userId)],
kinds: [1],
limit,
},
]);
export const findEvent = (relays: string[], eventId: string) =>
findOneFromRelays(relays, {
ids: [normalizeId(eventId)],
});
export const encodeNpub = nip19.npubEncode;
export const encodeNip19 = (prefix: "npub" | "note", hexId: string) => {
switch (prefix) {
case "npub":
return encodeNpub(hexId);
case "note":
return nip19.noteEncode(hexId);
default:
return null;
}
};
export const decodeNip19 = nip19.decode;
export const makeNostrUri = (prefix: "npub" | "note", hexId: string) => {
const addNostrPrefix = (nip19Id: string) => `nostr:${nip19Id}`;
switch (prefix) {
case "npub":
return addNostrPrefix(nip19.npubEncode(hexId));
case "note":
return addNostrPrefix(nip19.noteEncode(hexId));
default:
return null;
}
};
export const publishEvent = async (relays: string[], event: Event) => {
let pool;
try {
pool = new SimplePool();
return await pool.publish(relays, event);
} catch (error) {
return error instanceof Error ? error.message : "Something went wrong :(";
}
};