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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
import axios from 'axios'
import bcrypt from 'bcrypt'
import bitcoin from 'bitcoinjs-lib'
import bodyParser from 'body-parser'
import bolt11 from 'bolt11'
import compression from 'compression'
import cookieParser from 'cookie-parser'
import core from 'bitcoin-core'
import cors from 'cors'
import crypto from 'crypto-js'
import dotenv from 'dotenv'
import express from 'express'
import fs from 'fs'
import grpc from 'grpc'
import io from 'socket.io'
import jwt from 'jsonwebtoken'
import morgan from 'morgan'
import reverse from 'buffer-reverse'
import zmq from 'zeromq'
import config from './config'
import graphqlHTTP from 'express-graphql'
dotenv.config()
const l = console.log
;(async () => {
const ln = async ({ server, tls, macaroon, channelpeers }) => {
const ln = await require('lnrpc')({ server, tls })
ln.meta = new grpc.Metadata()
ln.meta.add('macaroon', fs.readFileSync(macaroon).toString('hex'))
ln.channelpeers = channelpeers
return ln
}
const lna = await ln(config.lna)
const lnb = await ln(config.lnb)
const bc = new core(config.bitcoin)
const app = express()
app.enable('trust proxy')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(cors({ credentials: true, origin: 'http://*:*' }))
app.use(compression())
app.use(morgan('combined'))
const server = require('http').Server(app)
const socket = io(server, { origins: '*:*' })
const db = await require('./db')(lna)
const passport = require('./passport')(db)
const auth = passport.authenticate('jwt', { session: false })
const sids = {}
const seen = []
app.use(passport.initialize())
app.use('/graphql', auth, graphqlHTTP({
schema: db.gqlschema,
graphiql: true
}))
socket.use((socket, next) => {
try {
let token = socket.request.headers.cookie.match(`token=([^;]+)`)[1]
if (!token && socket.handshake.query) {
token = socket.handshake.query.token
}
let user = jwt.decode(token).username
socket.request.user = user
sids[user] = socket.id
} catch (e) { return }
next()
})
socket.sockets.on('connect', socket => {
socket.emit('success', {
message: 'success logged in!',
user: socket.request.user
})
})
const handlePayment = async msg => {
if (!msg.settled) return
let payment = await db.Payment.findOne({
include: { model: db.User, as: 'user' },
where: {
hash: msg.payment_request
}
})
if (!payment) return
payment.received = true
payment.user.channelbalance += parseInt(msg.value)
await payment.save()
await payment.user.save()
payments.push(msg.payment_request)
socket.to(sids[payment.user.username]).emit('invoice', msg)
}
const invoices = lna.subscribeInvoices({})
invoices.on('data', handlePayment)
const invoicesb = lnb.subscribeInvoices({})
invoicesb.on('data', handlePayment)
const zmqRawBlock = zmq.socket('sub')
zmqRawBlock.connect(config.bitcoin.zmqrawblock)
zmqRawBlock.subscribe('rawblock')
const zmqRawTx = zmq.socket('sub')
zmqRawTx.connect(config.bitcoin.zmqrawtx)
zmqRawTx.subscribe('rawtx')
const addresses = {}
await db.User.findAll({
attributes: ['username', 'address'],
}).map(u => { addresses[u.address] = u.username })
const payments = (await db.Payment.findAll({
attributes: ['hash']
})).map(p => p.hash)
zmqRawTx.on('message', async (topic, message, sequence) => {
topic = topic.toString('utf8')
message = message.toString('hex')
switch (topic) {
case 'rawtx': {
let tx = bitcoin.Transaction.fromHex(message)
let hash = reverse(tx.getHash()).toString('hex')
if (payments.includes(hash)) return
let total = tx.outs.reduce((a, b) => a + b.value, 0)
tx.outs.map(async o => {
try {
let network = config.bitcoin.network
if (network === 'mainnet') {
network = 'bitcoin'
}
let address = bitcoin.address.fromOutputScript(
o.script,
bitcoin.networks[network],
)
if (Object.keys(addresses).includes(address)) {
payments.push(hash)
let user = await db.User.findOne({
where: {
username: addresses[address],
}
})
user.balance += o.value
await user.save()
await db.Payment.create({
user_id: user.id,
hash: hash,
amount: o.value,
currency: 'CAD',
rate: app.get('rates').ask,
received: true,
})
socket.emit('tx', message)
}
} catch(e) {}
})
break
}
}
})
zmqRawBlock.on('message', async (topic, message, sequence) => {
topic = topic.toString('utf8')
message = message.toString('hex')
switch (topic) {
case 'rawblock': {
let block = bitcoin.Block.fromHex(message)
l('block', block.getHash().toString('hex'))
break
}
}
})
app.post('/register', async (req, res) => {
let err = m => res.status(500).send(m)
let user = req.body
if (!user.username) return err('Username required')
if (user.password.length < 2) return err('Password too short')
let exists = await db.User.count({ where: { username: user.username } })
if (exists) return err('Username taken')
user.address = (await lna.newAddress({ type: 1 }, lna.meta)).address
user.password = await bcrypt.hash(user.password, 1)
addresses[user.address] = user.username
res.send(await db.User.create(user))
})
app.post('/user', async (req, res) => {
await db['User'].update(user, { where: { username: user.username } })
})
app.post('/openchannel', auth, async (req, res) => {
let err = m => res.status(500).send(m)
if (req.user.balance < 10000) return err('Need at least 10000 satoshis for channel opening')
let pending = await lna.pendingChannels({}, lna.meta)
let busypeers = pending.pending_open_channels.map(c => c.channel.remote_node_pub)
let peer = lna.channelpeers.find(p => !busypeers.includes(p))
let sent = false
l('channeling')
if (!peer) {
res.status(500).send('All peers have pending channel requests, try again later')
return
}
try {
let amount = Math.min(req.user.balance, 16777216)
let openchannel = async (peer, amount) => {
let channel = await lna.openChannel({
node_pubkey: new Buffer(peer, 'hex'),
local_funding_amount: amount,
})
channel.on('data', async data => {
if (sent || !data.chan_pending) return
req.user.channelbalance += amount
req.user.balance -= amount
req.user.channel = reverse(data.chan_pending.txid).toString('hex')
await req.user.save()
res.send(data)
sent = true
})
channel.on('error', err => {
l('channel error', peer, err)
let msg = err.message
if (msg.startsWith('Multiple') || msg.startsWith('You gave')) {
busypeers.push(peer)
peer = lna.channelpeers.find(p => !busypeers.includes(p))
if (peer) {
return openchannel(peer, amount)
} else {
return res.status(500).send('All peers are busy, couldn\'t open a channel, wait a few blocks and try again')
}
}
if (msg.startsWith('not enough')) {
msg = 'Server wallet is busy. Wait for a block and try again'
}
return res.status(500).send(msg)
})
}
openchannel(peer, amount)
} catch (err) {
l('rpc error', peer, err)
return res.status(500).send(err)
}
})
app.post('/closechannels', auth, async (req, res) => {
req.user.balance += req.user.channelbalance
req.user.channelbalance = 0
await req.user.save()
res.send(req.user)
})
app.post('/faucet', auth, async (req, res) => {
return res.send('not for mainnet')
// await bc.walletPassphrase('kek', 30000)
// await bc.sendToAddress(req.user.address, 0.001)
// res.send('success')
})
app.get('/channels', auth, async (req, res) => {
return res.send(await lna.listChannels())
})
app.get('/peers', auth, async (req, res) => {
return res.send(await lna.listPeers())
})
app.post('/sendPayment', auth, async (req, res) => {
await req.user.reload()
let hash = req.body.payreq
let payreq = bolt11.decode(hash)
if (seen.includes(hash)) {
return res.status(500).send('Invoice has been paid, can\'t pay again')
}
if (req.user.channelbalance < payreq.satoshis) {
return res.status(500).send('Not enough satoshis')
}
const stream = lna.sendPayment(lna.meta, {})
stream.write({ payment_request: req.body.payreq })
stream.on('data', async m => {
if (m.payment_error) {
res.status(500).send(m.payment_error)
} else {
if (seen.includes(m.payment_preimage)) return
seen.push(m.payment_preimage)
let total = parseInt(m.payment_route.total_amt) + parseInt(m.payment_route.total_fees)
req.user.channelbalance -= total
await db.Payment.create({
amount: -total,
user_id: req.user.id,
hash,
rate: app.get('rates').bid,
currency: 'CAD',
})
await req.user.save()
seen.push(hash)
res.send(m)
}
})
stream.on('error', e => {
let msg = e.message
res.status(500).send(msg)
})
})
app.post('/sendCoins', auth, async (req, res) => {
await req.user.reload()
const MINFEE = 180
let { address, amount } = req.body
if (amount === req.user.balance) {
amount = req.user.balance - MINFEE
}
if (req.user.balance < amount - MINFEE) {
res.status(500).send('Not enough funds')
return
}
try {
let txid = (await lna.sendCoins({ addr: address, amount })).txid
let txhex = await bc.getRawTransaction(txid)
let tx = bitcoin.Transaction.fromHex(txhex)
let input_total = await tx.ins.reduce(async (a, input) => {
let h = await bc.getRawTransaction(reverse(input.hash).toString('hex'))
return a + bitcoin.Transaction.fromHex(h).outs[input.index].value
}, 0)
let output_total = tx.outs.reduce((a, b) => a + b.value, 0)
let fees = input_total - output_total
let total = Math.min(parseInt(amount) + fees, req.user.balance)
req.user.balance -= total
await req.user.save()
await db.Payment.create({
amount: -total,
user_id: req.user.id,
hash: txid,
rate: app.get('rates').bid,
currency: 'CAD',
})
res.send({ txid, tx, amount, fees })
} catch (e) {
l(e)
let msg = e.message
if (msg.startsWith('insufficient')) {
msg = 'Server wallet is busy. Wait a block and try again'
}
res.status(500).send(msg)
}
})
app.post('/addInvoice', auth, async (req, res) => {
let invoice = await lnb.addInvoice({ value: req.body.amount })
await db.Payment.create({
user_id: req.user.id,
hash: invoice.payment_request,
amount: req.body.amount,
currency: 'CAD',
rate: app.get('rates').ask,
tip: req.body.tip,
})
res.send(invoice)
})
let fetchRates
(fetchRates = async () => {
const nonce = Date.now().toString()
const conf = config.quad
const signature = crypto.HmacSHA256(
nonce + conf.client_id + conf.key,
conf.secret
).toString()
try {
let res = await axios.get('https://api.quadrigacx.com/v2/order_book')
let ask = res.data.asks[0][0]
l('quadriga ask price: ', ask)
app.set('rates', { ask })
} catch (e) { l(e) }
setTimeout(fetchRates, 150000)
})()
app.get('/rates', (req, res) => {
res.send(app.get('rates'))
})
app.post('/login', async (req, res) => {
try {
let user = await db.User.findOne({
where: {
username: req.body.username
}
})
if (!user) return res.status(401).end()
let result = await bcrypt.compare(req.body.password, user.password)
if (result) {
let payload = { username: user.username }
let token = jwt.sign(payload, process.env.SECRET)
res.cookie('token', token, { expires: new Date(Date.now() + 432000000) })
res.send({ user, token })
} else {
res.status(401).end()
}
} catch(err) {
l(err)
res.status(401).end()
}
})
app.use(function (err, req, res, next) {
res.status(500)
res.send('An error occurred')
l(err.stack)
return res.end()
})
server.listen(3119)
})()
|