aboutsummaryrefslogtreecommitdiffstats
path: root/index.js
blob: f36b5b92587d16b397207cfb0539375d218c6516 (plain) (blame)
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
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 core from 'bitcoin-core'
import cors from 'cors'
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 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 bc = new core({ 
    username: 'adam',
    password: 'MPJzfq97',
    network: 'testnet',
  })

  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())

  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 = {}

  socket.use((socket, next) => {
    try {
      let token = socket.request.headers.cookie.match(`;\\s*token=([^;]+)`)[1]
      let user = jwt.decode(token).username
      socket.request.user = user
      sids[user] = socket.id
      l(sids)
    } catch (e) { l(e) }
    next()
  })

  socket.sockets.on('connect', socket => {
    socket.emit('success', {
      message: 'success logged in!',
      user: socket.request.user
    })
  })

  app.use(passport.initialize())

  const handlePayment = async msg => {
    let invoice = await db['Invoice'].findOne({
      include: { model: db['User'], as: 'user' },
      where: {
        payreq: msg.payment_request
      }
    })

    if (!invoice) return

    invoice.user.channelbalance += parseInt(msg.value)
    await invoice.user.save()
 
    socket.to(sids[invoice.user.username]).emit('invoice', msg)
  } 

  const invoices = lna.subscribeInvoices({})
  invoices.on('data', handlePayment)

  const invoicesb = lnb.subscribeInvoices({})
  invoicesb.on('data', handlePayment)

  const zmqSock = zmq.socket('sub')
  zmqSock.connect('tcp://127.0.0.1:18503')
  zmqSock.subscribe('rawblock')
  zmqSock.subscribe('rawtx')

  const seen = []
  const channelpeers = [
    '039cc950286a8fa99218283d1adc2456e0d5e81be558da77dd6e85ba9a1fff5ad3',
    '0231eee2441073c86d38f6085aedaf2bb7ad3d43af4c0e2669c1edd1a7d566ce31',
    '022ea315e5052b152579e70a90bacfd6aa7420f2ce94674d4ca8da29d709bc70fd',
  ]

  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': {
        if (seen.includes(message)) return

        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!')
              socket.emit('tx', message)
              seen.push(message)
            } 
          } catch(e) { }
        })

        break
      } 

      case 'rawblock': {
        let block = bitcoin.Block.fromHex(message)
        l(block.getHash())
        break
      } 
    }
  })

  app.post('/openchannel', auth, async (req, res) => {
    let pending = await lna.pendingChannels({}, meta)
    let busypeers = pending.pending_open_channels.map(c => c.channel.remote_node_pub)
    let peer = channelpeers.find(p => !busypeers.includes(p))
    
    if (!peer) {
      return res.status(500).send(
        { error: 'All peers have pending channel opens' }
      )
    } 
    
    try {
      let channel = await lna.openChannel({
        node_pubkey: new Buffer(peer, 'hex'),
        local_funding_amount: req.user.balance,
      })
      
      channel.on('data', async data => {
        l(data)
        req.user.channelbalance += req.user.balance
        req.user.balance = 0
        req.user.channel = reverse(data.chan_pending.txid).toString('hex')
        await req.user.save()
        res.send(data)
      })
    } catch (err) {
      l(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) => {
    await bc.walletPassphrase('kek', 30000)
    await bc.sendToAddress(req.user.address, 0.001)
    res.send('success')
  })

  app.post('/sendPayment', auth, (req, res) => {
    const payments = lna.sendPayment(meta, {})
    payments.write({ payment_request: req.body.payreq })
    payments.on('data', async m => {
      if (m.payment_error) {
        l(m)
        res.status(500).send({ error: m.payment_error })
      } else {
        l(m, req.user.channelbalance)
        req.user.channelbalance -= parseInt(m.payment_route.total_amt)
        await req.user.save()
        res.send(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.send(invoice)
  })

  let fetchRates
  (fetchRates = () => {
    restClient.tickerGlobalPerSymbol('BTCCAD', (data) => {
      app.set('rates', JSON.parse(data))
    })
    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
        } 
      })

      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.send({ user, token })
      } else {
        res.status(401).end()
      }
    } catch(err) {
      l(err)
      res.status(401).end()
    }
  })

  app.get('/secret', passport.authenticate('jwt', { session: false }), (req, res) => {
    res.send({message: 'Success! You can not see this without a token'})
  })

  app.get('/api/users/me',
    passport.authenticate('basic', { session: false }),
    function(req, res) {
      res.send({ 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)
})()