-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
180 lines (138 loc) · 4.18 KB
/
index.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
const Lnd = require('./lnd')
const CLightning = require('./c-lightning')
const metadata = require('./metadata')
const { EventEmitter } = require('events')
const MAX_SUBSCRIBER_CACHE = 500
module.exports = class Payment extends EventEmitter {
constructor (dazaar, payment, opts = {}) {
super()
this.dazaar = dazaar
this.payment = payment
this.destroyed = false
this.lightning = node(opts)
this.subscribers = new Map()
this.nodeInfo = {}
this.nodeInfo.address = opts.address
this._setupExtensions()
}
initMaybe (cb) {
const self = this
if (this.nodeInfo.id) return cb()
this.lightning.getNodeId(function (err, nodeId) {
if (err) return cb(err)
self.nodeInfo.id = nodeId
cb()
})
}
validate (buyerKey, cb) {
if (this.destroyed) return process.nextTick(cb, new Error('Seller is shutting down'))
const tail = this._get(buyerKey)
const timeout = setTimeout(ontimeout, 20000)
let timedout = false
if (tail.synced || tail.active()) return process.nextTick(onsynced)
tail.on('synced', onsynced)
tail.on('update', onupdate)
function ontimeout () {
timedout = true
onsynced()
}
function onupdate () {
if (tail.active()) onsynced()
}
function onsynced () {
tail.removeListener('synced', onsynced)
tail.removeListener('update', onupdate)
clearTimeout(timeout)
const time = tail.remainingTime()
if (time <= 0) return cb(new Error('No time left on subscription' + (timedout ? 'after timeout' : '')))
cb(null, {
type: 'time',
remaining: time
})
}
}
connect (opts, cb) {
this.lightning.connect(opts.buyerInfo, cb)
}
sell (request, buyerKey, cb) {
if (!cb) cb = noop
const self = this
this.connect(request, function (err, info) {
if (err && err.code !== 2) return cb(err)
self.validate(buyerKey, function (err, res) { // validate is called to initiate subscription
self.lightning.addInvoice(self._filter(buyerKey), request.amount, cb)
})
})
}
buy (buyer, amount, auth, cb) {
const self = this
this.initMaybe(oninit)
function oninit (err) {
if (err) return cb(err)
const request = {
amount,
buyerInfo: self.nodeInfo
}
const expectedInvoice = {
amount,
buyer: self.dazaar.key.toString('hex'),
seller: self.dazaar.seller.toString('hex')
}
self.dazaar.broadcast('lnd-pay-request', request)
self.lightning.requests.push(expectedInvoice)
cb()
}
}
pay (invoice, cb) {
this.lightning.payInvoice(invoice.request, cb)
}
destroy () {
if (this.destroyed) return
this.destroyed = true
for (const tail of this.subscribers.values()) {
tail.destroy()
}
}
_filter (buyer) {
return metadata(this.dazaar.key, buyer)
}
_get (buyer) {
const h = buyer.toString('hex')
if (this.subscribers.has(h)) return this.subscribers.get(h)
if (this.subscribers.size >= MAX_SUBSCRIBER_CACHE) this._gc()
const tail = this.lightning.subscription(this._filter(buyer), this.payment)
this.subscribers.set(h, tail)
return tail
}
_gc () {
for (const [h, tail] of this.subscribers) {
tail.destroy()
this.subscribers.delete(h)
return
}
}
_setupExtensions () {
const self = this
this.dazaar.receive('lnd-pay-request', function (request, stream) {
self.sell(request, stream.remotePublicKey, function (err, invoice) {
if (err) self.emit('error', err)
self.dazaar.send('lnd-invoice', invoice, stream)
})
})
this.dazaar.receive('lnd-invoice', function (invoice) {
self.pay(invoice, function (err, payment) {
if (err) self.emit('error', err)
})
})
}
static supports (payment) {
const supportedCurrencies = ['LightningBTC', 'LightningSats']
return supportedCurrencies.includes(payment.currency)
}
}
function node (opts) {
if (opts.implementation === 'lnd') return new Lnd(opts)
if (opts.implementation === 'c-lightning') return new CLightning(opts)
throw new Error('unrecognised lightning node: specify lnd or c-lightning.')
}
function noop () {}