stacker.news/worker/nostr.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-02-14 22:58:12 +00:00
const { getInvoice } = require('ln-service')
const { Relay, signId, calculateId, getPublicKey } = require('nostr')
const nostrOptions = { startAfter: 5, retryLimit: 21, retryBackoff: true }
2023-02-15 17:20:26 +00:00
function nip57 ({ boss, lnd, models }) {
2023-02-14 22:58:12 +00:00
return async function ({ data: { hash } }) {
console.log('running nip57')
2023-02-15 17:20:26 +00:00
let inv, lnInv
2023-02-14 22:58:12 +00:00
try {
2023-02-15 17:20:26 +00:00
lnInv = await getInvoice({ id: hash, lnd })
inv = await models.invoice.findUnique({
where: {
hash
}
})
2023-02-14 22:58:12 +00:00
} catch (err) {
console.log(err)
// on lnd related errors, we manually retry which so we don't exponentially backoff
await boss.send('nip57', { hash }, nostrOptions)
return
}
try {
2023-02-15 17:20:26 +00:00
// if parsing fails it's not a zap
console.log('zapping', inv.desc)
const desc = JSON.parse(inv.desc)
2023-02-14 22:58:12 +00:00
const ptag = desc.tags.filter(t => t?.length >= 2 && t[0] === 'p')[0]
const etag = desc.tags.filter(t => t?.length >= 2 && t[0] === 'e')[0]
const relays = desc.tags.find(t => t?.length >= 2 && t[0] === 'relays').slice(1)
const tags = [ptag]
if (etag) {
tags.push(etag)
}
2023-02-15 17:20:26 +00:00
tags.push(['bolt11', lnInv.request])
tags.push(['description', inv.desc])
tags.push(['preimage', lnInv.secret])
2023-02-14 22:58:12 +00:00
const e = {
kind: 9735,
pubkey: getPublicKey(process.env.NOSTR_PRIVATE_KEY),
2023-02-15 17:20:26 +00:00
created_at: Math.floor(new Date(lnInv.confirmed_at).getTime() / 1000),
2023-02-14 22:58:12 +00:00
content: '',
tags
}
e.id = await calculateId(e)
e.sig = await signId(process.env.NOSTR_PRIVATE_KEY, e.id)
2023-02-15 17:20:26 +00:00
console.log('zap note', e, relays)
await Promise.allSettled(
relays.map(r => new Promise((resolve, reject) => {
const timeout = 1000
const relay = Relay(r)
2023-02-14 22:58:12 +00:00
2023-02-15 17:20:26 +00:00
function timedout () {
relay.close()
console.log('failed to send to', r)
reject(new Error('relay timeout'))
}
2023-02-14 22:58:12 +00:00
2023-02-15 17:20:26 +00:00
let timer = setTimeout(timedout, timeout)
2023-02-14 22:58:12 +00:00
2023-02-15 17:20:26 +00:00
relay.on('open', () => {
clearTimeout(timer)
timer = setTimeout(timedout, timeout)
relay.send(['EVENT', e])
})
2023-02-14 22:58:12 +00:00
2023-02-15 17:20:26 +00:00
relay.on('ok', () => {
clearTimeout(timer)
relay.close()
console.log('sent zap to', r)
resolve()
})
})))
2023-02-14 22:58:12 +00:00
} catch (e) {
console.log(e)
}
2023-02-15 17:20:26 +00:00
console.log('done running nip57')
2023-02-14 22:58:12 +00:00
}
}
module.exports = { nip57 }