forked from nbd-wtf/nostr-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nip69.ts
44 lines (40 loc) · 1.91 KB
/
nip69.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
import { decrypt, encrypt, getConversationKey } from "./nip44.ts"
import { finalizeEvent, getPublicKey } from "./pure.ts"
import { AbstractSimplePool, SubCloser } from "./abstract-pool.ts"
export type NofferData = { offer: string, amount?: number, zap?: string, payer_data?: any }
export type Nip69Success = { bolt11: string }
export type Nip69Error = { code: number, error: string, range: { min: number, max: number } }
export type Nip69Response = Nip69Success | Nip69Error
export const SendNofferRequest = async (pool: AbstractSimplePool, privateKey: Uint8Array, relays: string[], toPubKey: string, data: NofferData, timeoutSeconds = 30): Promise<Nip69Response> => {
const publicKey = getPublicKey(privateKey)
const content = encrypt(JSON.stringify(data), getConversationKey(privateKey, toPubKey))
const event = newNip69Event(content, publicKey, toPubKey)
const signed = finalizeEvent(event, privateKey)
await Promise.all(pool.publish(relays, signed))
return new Promise<Nip69Response>((res, rej) => {
let closer: SubCloser = { close: () => { } }
const timeout = setTimeout(() => {
closer.close(); rej("failed to get nip69 response in time")
}, timeoutSeconds * 1000)
closer = pool.subscribeMany(relays, [newNip69Filter(publicKey, signed.id)], {
onevent: async (e) => {
clearTimeout(timeout)
const content = decrypt(e.content, getConversationKey(privateKey, toPubKey))
res(JSON.parse(content))
}
})
})
}
export const newNip69Event = (content: string, fromPub: string, toPub: string) => ({
content,
created_at: Math.floor(Date.now() / 1000),
kind: 21001,
pubkey: fromPub,
tags: [['p', toPub]]
})
export const newNip69Filter = (publicKey: string, eventId: string) => ({
since: Math.floor(Date.now() / 1000) - 1,
kinds: [21001],
'#p': [publicKey],
'#e': [eventId]
})