-
Notifications
You must be signed in to change notification settings - Fork 13
/
nostr.js
168 lines (127 loc) · 4.64 KB
/
nostr.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const fastify = require('fastify')({ logger: true })
const { RelayPool, Relay, signId, calculateId, verifyEvent, getPublicKey } = require('nostr')
const { bech32 } = require('bech32')
const buffer = require('buffer')
const _nostrPrivKey = process.env.LIGESS_NOSTR_ZAPPER_PRIVATE_KEY
const _nostrPubKey = _nostrPrivKey ? getPublicKey(_nostrPrivKey) : null
const pendingZapRequests = {}
if (_nostrPubKey) {
fastify.log.info({msg: 'Nostr NIP-57 enabled', npub: pubkeytonpub(_nostrPubKey)})
}
const getNostrPubKey = () => _nostrPubKey
const verifyZapRequest = async (zapRequest, queryAmount) => {
if (!zapRequest) return
try {
zapRequest = JSON.parse(zapRequest)
} catch(error) {
throw new Error(`Invalid JSON on zap request`)
}
if (zapRequest.kind !== 9734) {
throw new Error(`Invalid zap request kind ${zapRequest.kind}`)
}
if (await calculateId(zapRequest) !== zapRequest.id) {
throw new Error(`Invalid id on zap request`)
}
if (!await verifyEvent(zapRequest)) {
throw new Error(`Invalid signature in zap request`)
}
if (!zapRequest.tags || zapRequest.tags.length === 0) {
throw new Error(`No tags on zap request`)
}
const ptags = getTags(zapRequest.tags, 'p')
if (ptags.length === 0) {
throw new Error(`No p tag on zap request`)
}
if (ptags.length >= 2) {
throw new Error(`Multiple p tags on zap request`)
}
const etags = getTags(zapRequest.tags, 'e')
if (etags.length >= 2) {
throw new Error(`Multiple e tags on zap request`)
}
const relaytags = getTags(zapRequest.tags, 'relays')
if (relaytags.length === 0) {
throw new Error(`No relay tag on zap request`)
}
if (relaytags.length >= 2) {
throw new Error(`Multiple relay tags on zap request`)
}
const amounttags = getTags(zapRequest.tags, 'amount')
if (amounttags.length === 1 && queryAmount && amounttags[0][1] !== queryAmount) {
throw new Error(`Amount tag in the zap request does not equal amount on query`)
}
if (amounttags.length >= 2) {
throw new Error(`Multiple amount tags on zap request`)
}
return zapRequest
}
const storePendingZapRequest = (paymentHash, zapRequest, comment, logger) => {
pendingZapRequests[paymentHash] = {zapRequest: zapRequest, comment: comment, logger: logger}
}
const handleInvoiceUpdate = async (invoice) => {
if (invoice.status == 'Cancelled') {
delete pendingZapRequests[invoice.paymentHash]
return
}
if (!invoice.settled) return
if (!pendingZapRequests[invoice.paymentHash]) return
const {zapRequest, comment, logger} = pendingZapRequests[invoice.paymentHash]
let content = ''
if (comment) {
content = comment
} else if (zapRequest.content) {
content = zapRequest.content
}
const zapNote = {
kind: 9735,
pubkey: _nostrPubKey,
created_at: Date.parse(invoice.settleDate) / 1000,
tags: [],
content: content
}
const ptags = getTags(zapRequest.tags, 'p')
zapNote.tags.push(ptags[0])
const etags = getTags(zapRequest.tags, 'e')
if (etags.length === 1) zapNote.tags.push(etags[[0]])
zapNote.tags.push(['bolt11', invoice.bolt11])
zapNote.tags.push(['description', JSON.stringify(zapRequest)])
zapNote.tags.push(['preimage', invoice.preImage])
zapNote.id = await calculateId(zapNote)
zapNote.sig = await signId(_nostrPrivKey, zapNote.id)
logger.info({msg: 'Invoice settled', note: zapNote.id, amount: invoice.amount, npub: pubkeytonpub(zapRequest.pubkey), comment: content})
const relaytags = getTags(zapRequest.tags, 'relays')
relaytags[0].slice(1).forEach(relay => sendNote(relay, zapNote, logger))
delete pendingZapRequests[invoice.paymentHash]
}
function getTags(tags, tag) {
return tags.filter(t => t && t.length && t.length >= 2 && t[0] === tag)
}
function sendNote(url, note, logger) {
const relay = Relay(url, {reconnect: false})
relay.on('open', async () => {
await relay.send(["EVENT", note])
setTimeout(() => relay.close(), 5_000)
});
relay.on('notice', (notice) => {
logger.info({msg: notice, relay: relay.url})
});
relay.on('close', (e) => {
if (e.code !== 1000 && e.code !== 1005) {
logger.info({msg: 'Close', relay: relay.url, code: e.code, reason: e.reason})
}
});
relay.on('error', (e) => {
logger.warn({msg: e.message, relay: relay.url})
});
relay.on('ok', (id) => {
if (id === note.id) {
logger.info({msg: 'Zap note sent', relay: relay.url})
setImmediate(() => relay.close())
}
});
}
function pubkeytonpub(pubkey) {
let words = bech32.toWords( buffer.Buffer.from( pubkey, 'hex' ) );
return bech32.encode( "npub", words );
}
module.exports = { getNostrPubKey, verifyZapRequest, storePendingZapRequest, handleInvoiceUpdate }