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
|
import ba from 'bitcoinaverage'
import bcrypt from 'bcrypt'
import bitcoin from 'bitcoinjs-lib'
import bodyParser from 'body-parser'
import compression from 'compression'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import dotenv from 'dotenv'
import express from 'express'
import fs from 'fs'
import grpc from 'grpc'
import jwt from 'jsonwebtoken'
import reverse from 'buffer-reverse'
import zmq from 'zmq'
import cache from './cache'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
dotenv.config()
const l = console.log
;(async () => {
const restClient = ba.restfulClient(process.env.BITCOINAVERAGE_PUBLIC, process.env.BITCOINAVERAGE_SECRET)
const lna = await require('lnrpc')({ server: 'localhost:10001', tls: '/home/adam/.lnd.testa/tls.cert' })
const maca = fs.readFileSync('/home/adam/.lnd.testa/admin.macaroon')
const meta = new grpc.Metadata()
meta.add('macaroon', maca.toString('hex'))
lna.meta = meta
const lnb = await require('lnrpc')({ server: 'localhost:10002', tls: '/home/adam/.lnd.testb/tls.cert' })
const macb = fs.readFileSync('/home/adam/.lnd.testb/admin.macaroon')
const metab = new grpc.Metadata()
metab.add('macaroon', macb.toString('hex'))
lnb.meta = metab
const db = await require('./db')(lna)
const passport = require('./passport')(db)
const auth = passport.authenticate('jwt', { session: false })
const app = express()
app.enable('trust proxy')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(cors())
app.use(compression())
app.use(passport.initialize())
const server = require('http').createServer(app)
const io = require('socket.io').listen(server)
io.origins('http://localhost:8085')
const invoices = lna.subscribeInvoices({})
invoices.on('data', msg => {
io.emit('invoices', msg)
})
const invoicesb = lnb.subscribeInvoices({})
invoicesb.on('data', msg => {
io.emit('invoices', msg)
})
const zmqSock = zmq.socket('sub')
zmqSock.connect('tcp://127.0.0.1:18503')
zmqSock.subscribe('rawblock')
zmqSock.subscribe('rawtx')
zmqSock.on('message', async (topic, message, sequence) => {
const addresses = {}
await db['User'].findAll({
attributes: ['username', 'address'],
}).map(u => { addresses[u.address] = u.username })
topic = topic.toString('utf8')
message = message.toString('hex')
switch (topic) {
case 'rawtx': {
let tx = bitcoin.Transaction.fromHex(message)
let total = tx.outs.reduce((a, b) => a + b.value, 0)
l(total)
tx.outs.map(async o => {
try {
let address = bitcoin.address.fromOutputScript(o.script, bitcoin.networks.testnet)
l(address, o.value)
if (Object.keys(addresses).includes(address)) {
let user = await db['User'].findOne({
where: {
username: addresses[address],
}
})
user.balance += o.value
await user.save()
l('HIT!')
io.emit('tx', message)
}
} catch(e) { }
})
break
}
case 'rawblock': {
let block = bitcoin.Block.fromHex(message)
l(block.getHash())
break
}
}
})
app.post('/openchannel', async (req, res) => {
let user = await db['User'].findOne({
where: {
username: req.body.username,
}
})
let channel = lna.openChannelSync({
node_pubkey_string: '022ea315e5052b152579e70a90bacfd6aa7420f2ce94674d4ca8da29d709bc70fd',
local_funding_amount: user.balance,
}, async (err, data) => {
if (err) {
l(err)
return res.status(500).send(err)
}
user.balance = 0
user.channel = reverse(data.funding_txid).toString('hex')
await user.save()
res.json(data)
})
})
app.post('/sendPayment', auth, (req, res) => {
const payments = lna.sendPayment(meta, {})
payments.write({ payment_request: req.body.payreq })
payments.on('data', async m => {
req.user.channelbalance -= m.value
await req.user.save()
res.json(m)
})
})
app.post('/addInvoice', auth, async (req, res) => {
let invoice = await lnb.addInvoice({ value: req.body.amount })
await db.Invoice.create({
user_id: req.user.id,
payreq: invoice.payment_request,
})
res.json(invoice)
})
let fetchRates
(fetchRates = () => {
restClient.tickerGlobalPerSymbol('BTCCAD', (data) => {
app.set('rates', JSON.parse(data))
})
setTimeout(fetchRates, 120000)
})()
app.get('/rates', (req, res) => {
res.json(app.get('rates'))
})
app.post('/login', async (req, res) => {
try {
let user = await db.User.findOne({
where: {
username: req.body.username
}
})
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() + 9999999) })
res.json({ user, token })
} else {
res.status(401).end()
}
} catch(err) {
res.status(401).end()
}
})
app.get('/secret', passport.authenticate('jwt', { session: false }), (req, res) => {
res.json({message: 'Success! You can not see this without a token'})
})
app.get('/api/users/me',
passport.authenticate('basic', { session: false }),
function(req, res) {
res.json({ id: req.user.id, username: req.user.username });
});
app.use('/graphql', bodyParser.json(), graphqlExpress({
schema: db.gqlschema,
context: {},
tracing: true,
cacheControl: true,
}))
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }))
app.use(function (err, req, res, next) {
res.status(500)
res.send('An error occurred')
l(err.stack)
return res.end()
})
server.listen(3000)
})()
|