forked from Dolu89/ligess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nostrWalletConnect.js
319 lines (243 loc) · 10.1 KB
/
nostrWalletConnect.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const fastify = require('fastify')({ logger: true })
const { signId, calculateId, verifyEvent, getPublicKey, decryptDm, encryptDm } = require('nostr')
const { bech32 } = require('bech32')
const buffer = require('buffer')
const { getLnClient } = require('./lnClient')
const crypto = require('crypto')
const bolt11 = require('bolt11')
const fs = require('fs')
const unaWrapper = getLnClient()
const _nostrWalletConnectEncryptPrivKey = process.env.LIGESS_NOSTR_WALLET_CONNECT_PRIVATE_KEY
const _nostrWalletConnectEncryptPubKey = _nostrWalletConnectEncryptPrivKey ? getPublicKey(_nostrWalletConnectEncryptPrivKey) : null
const _nostrWalletConnectAuthPubKey = process.env.LIGESS_NOSTR_WALLET_CONNECT_PUBLIC_KEY
const _nostrWalletConnectRelayHost = new URL(process.env.LIGESS_NOSTR_WALLET_CONNECT_RELAY).hostname
const _nostrWalletConnectBudgetZap = process.env.LIGESS_NOSTR_WALLET_CONNECT_BUDGET_ZAP
const _nostrWalletConnectBudgetHour = process.env.LIGESS_NOSTR_WALLET_CONNECT_BUDGET_HOUR
const _nostrWalletConnectBudgetDay = process.env.LIGESS_NOSTR_WALLET_CONNECT_BUDGET_DAY
const ZAPS_FILE = 'zaps.json'
const TIME_WINDOW_HOUR = 60 * 60 * 1000
const TIME_WINDOW_DAY = TIME_WINDOW_HOUR * 24
let zaps = []
if (fs.existsSync(ZAPS_FILE)) {
zaps = JSON.parse(fs.readFileSync(ZAPS_FILE))
zaps = filterZaps(TIME_WINDOW_DAY)
}
const isWalletConnectEnabled = () => _nostrWalletConnectEncryptPrivKey !== undefined
const getNostrRelayInformation = (file) => {
if (file) {
if (!fs.existsSync(file)) {
throw new Error(`Relay information file ${file} not found`)
}
let content = JSON.parse(fs.readFileSync(file))
fastify.log.info({msg: 'Nostr Wallet Connect Relay Information (NIP-11) enabled', content: content})
return content
}
}
const _nostrRelayInformation = getNostrRelayInformation(process.env.LIGESS_NOSTR_RELAY_INFORMATION)
const getWalletConnectHandler = () => (request, reply) => {
if (_nostrRelayInformation) {
reply.send(_nostrRelayInformation)
} else {
reply.code(404).send()
}
}
const getWalletConnectWsHandler = () => {
fastify.log.info({msg: 'Nostr Wallet Connect (NIP-47) enabled', npub: encode('npub', _nostrWalletConnectEncryptPubKey)})
if (_nostrWalletConnectAuthPubKey) {
fastify.log.info({msg: 'Nostr Wallet Connect Authentication (NIP-42) enabled', npub: encode('npub', _nostrWalletConnectAuthPubKey)})
}
fastify.log.info({msg: `Nostr Wallet Connect budget: max zap ${_nostrWalletConnectBudgetZap}, hourly: ${_nostrWalletConnectBudgetDay}, daily: ${_nostrWalletConnectBudgetHour}`})
return handleRelayConnection
}
const handleRelayConnection = (connection, request) => {
const logger = request.log
const challenge = crypto.randomBytes(20).toString('hex')
let isAuthenticated = false
let zapRequest = null
let subscriptionId = null
connection.socket.on('message', async (data) => {
try {
const message = JSON.parse(data);
logger.info({msg: 'Message received', message: message})
switch(message[0])
{
case 'REQ':
subscriptionId = message[1]
const payload = message[2]
if (payload.kinds && payload.kinds.includes(13194)) {
let response = {
pubkey: _nostrWalletConnectEncryptPubKey,
kind: 13194,
content: 'pay_invoice'
}
response.id = await calculateId(response)
response.sig = await signId(_nostrWalletConnectEncryptPrivKey, response.id)
send('EVENT', subscriptionId, response)
}
send('EOSE', subscriptionId)
break
case 'EVENT':
await verifyZapRequest(message[1])
zapRequest = message[1]
checkProgress()
break
case 'AUTH':
const authPubkey = await verifyAuthResponse(message[1], challenge)
if (!isAuthenticated) logger.info(`Connection is authenticated: ${encode('npub', authPubkey)}`)
isAuthenticated = true
checkProgress()
break
}
}
catch (error) {
logger.warn({msg: error.message})
connection.socket.close()
}
})
async function checkProgress() {
if (!isAuthenticated) return
if (!zapRequest) return
let zapResponse = await processZapRequest(zapRequest, logger)
send('EVENT', subscriptionId, zapResponse)
zapRequest = null
}
connection.socket.on('close', async (code, reason) => {
if (code != 1000) {
logger.info({msg: 'Connection closed', code: code, reason: reason})
}
})
connection.socket.on('error', async (message) => {
logger.info({msg: message})
})
send('AUTH',challenge)
setTimeout(() => {
if (!isAuthenticated) {
logger.info({msg: 'Closing idle connection'})
connection.socket.close()
}
}, 10000)
function send(...message) {
logger.info({msg: 'Message sent', message: message})
connection.socket.send(JSON.stringify(message))
}
}
async function verifyZapRequest(zapRequest) {
if (zapRequest.kind != 23194)
throw new Error('Event is not a zap request')
if (Math.abs(zapRequest.created_at - Math.floor(Date.now() / 1000)) > 10)
throw new Error('Timestamp out of bounds')
if (zapRequest.pubkey != _nostrWalletConnectEncryptPubKey)
throw new Error('Event has unknown pubkey')
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')
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')
}
async function verifyAuthResponse(authResponse, challenge) {
if (authResponse.kind != 22242)
throw new Error('Auth event is not an auth response')
if (Math.abs(authResponse.created_at - Math.floor(Date.now() / 1000)) > 10)
throw new Error('Timestamp out of bounds')
const challengeTags = getTags(authResponse.tags, 'challenge')
if (challengeTags.length != 1)
throw new Error('Challange tags invalid length')
if (challengeTags[0][1] != challenge)
throw new Error('Challenge does not match')
const relayTags = getTags(authResponse.tags, 'relay')
if (relayTags.length != 1)
throw new Error('Relay tags invalid length')
let relay = new URL(relayTags[0][1])
if (relay.protocol !== 'ws:' && relay.protocol !== 'wss:')
throw new Error('Invalid relay protocol')
if (process.env.HOST != '0.0.0.0' && relay.host !== _nostrWalletConnectRelayHost)
throw new Error('Relay host mismatch')
if (await calculateId(authResponse) !== authResponse.id)
throw new Error('Invalid id on auth response')
if (!await verifyEvent(authResponse))
throw new Error('Invalid signature in auth response')
// AUTH response could be on either pubkey
if (authResponse.pubkey !== _nostrWalletConnectAuthPubKey && authResponse.pubkey !== _nostrWalletConnectEncryptPubKey)
throw new Error(`Authentication of unknown pubkey: ${encode('npub', authResponse.pubkey)}`)
return authResponse.pubkey
}
async function processZapRequest(zapRequest, logger) {
let response = {
kind: 23195,
pubkey: _nostrWalletConnectEncryptPubKey,
created_at: Math.round(Date.now() / 1000),
content: {
result_type: 'pay_invoice',
},
tags: [['e', zapRequest.id]]
}
try {
const invoice = decryptInvoice(zapRequest, logger)
verifyZapAmount(invoice.satoshis, logger)
let invoicePaid = await unaWrapper.payInvoice({bolt11: invoice.paymentRequest})
logger.info({msg: 'Invoice paid', result: invoicePaid})
zaps.push({ timestamp: Date.now(), amount: invoice.satoshis })
fs.writeFileSync(ZAPS_FILE, JSON.stringify(zaps))
response.content.result = {
preimage: invoicePaid.paymentPreimage
}
}
catch (error) {
logger.warn({msg: 'Error processing zap request', error: error.message})
response.kind = 23196
response.content.error = {
code: error.name ?? 'OTHER',
message: error.message
}
}
response.content = encryptDm(_nostrWalletConnectEncryptPrivKey, _nostrWalletConnectEncryptPubKey, JSON.stringify(response.content))
response.id = await calculateId(response)
response.sig = await signId(_nostrWalletConnectEncryptPrivKey, response.id)
return response
}
function decryptInvoice(zapRequest, logger) {
const decrypted = decryptDm(_nostrWalletConnectEncryptPrivKey, zapRequest)
const payRequest = JSON.parse(decrypted)
logger.info({ msg: 'Pay request', content: payRequest })
if (payRequest.method !== 'pay_invoice')
throw new Error('NOT_IMPLEMENTED', 'Unknown method on zap request')
const invoice = bolt11.decode(payRequest.params.invoice)
logger.info({msg: 'Invoice', invoice: invoice})
return invoice
}
function verifyZapAmount(satoshis, logger) {
if (satoshis > _nostrWalletConnectBudgetZap)
throw new Error('Zap amount too large')
zaps = filterZaps(TIME_WINDOW_DAY)
const zapAmountLastDay = sumAmount(zaps)
if (zapAmountLastDay + satoshis > _nostrWalletConnectBudgetDay)
throw new Error('Zap amount over day budget')
const zapAmountLastHour = sumAmount(filterZaps(TIME_WINDOW_HOUR))
if (zapAmountLastHour + satoshis > _nostrWalletConnectBudgetHour)
throw new Error('Zap amount over hour budget')
logger.info({ msg: 'Total', hour: zapAmountLastHour, day: zapAmountLastDay })
}
function encode(prefix, hex) {
let words = bech32.toWords(buffer.Buffer.from(hex, 'hex'));
return bech32.encode(prefix, words);
}
function getTags(tags, tag) {
return tags.filter(t => t && t.length && t.length >= 2 && t[0] === tag)
}
function filterZaps(timeWindow) {
now = Date.now()
return zaps.filter(zap => zap.timestamp > now - timeWindow)
}
function sumAmount(entries) {
return entries.reduce((acc, zap) => acc + zap.amount, 0);
}
module.exports = { isWalletConnectEnabled, getWalletConnectHandler, getWalletConnectWsHandler }