aboutsummaryrefslogtreecommitdiffstats
path: root/android/app/src/main
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-10-17 15:31:08 -0700
committerAdam Soltys <[email protected]>2022-10-17 15:31:08 -0700
commit5e3d4133760962db28c4f3a595f0566bd59391ff (patch)
tree8a6c360bfa818ddf2b3724c926c3e5f8ceab374c /android/app/src/main
parentb45baf7b603c19af7c9f0c98a0df291ca2c305a8 (diff)
downloadcoinos-ui-5e3d4133760962db28c4f3a595f0566bd59391ff.tar.gz
coinos-ui-5e3d4133760962db28c4f3a595f0566bd59391ff.zip
thanks page
Diffstat (limited to 'android/app/src/main')
0 files changed, 0 insertions, 0 deletions
141' href='#n141'>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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
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 fb from 'fb'
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 Sequelize from 'sequelize'
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', async socket => {
    if (app.get('rates'))
      socket.emit('rate', app.get('rates').ask)
    socket.on('getuser', async (data, callback) => {
      callback(await db.User.findOne({
        where: {
          username: 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.balance += parseInt(msg.value)
    payment.rate = app.get('rates').ask

    await payment.save()
    await payment.user.save()
    payments.push(msg.payment_request)
 
    socket.to(sids[payment.user.username]).emit('invoice', msg)
    socket.to(sids[payment.user.username]).emit('user', payment.user)
  } 

  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) => {
    message = message.toString('hex')

    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)) {
          try {
            payments.push(hash)

            let user = await db.User.findOne({
              where: {
                username: addresses[address],
              }
            })

            let invoices = await db.Payment.findAll({
              limit: 1,
              where: {
                hash: address,
                received: null,
                amount: {
                  [Sequelize.Op.gt]: 0
                } 
              },
              order: [ [ 'createdAt', 'DESC' ]]
            })

            let tip = null
            if (invoices.length) tip = invoices[0].tip

            let confirmed = false

            if (user.friend) {
              user.balance += o.value
              confirmed = true
            } else {
              user.pending += o.value
            }

            await user.save()
            socket.to(sids[user.username]).emit('user', user)
            
            await db.Payment.create({
              user_id: user.id,
              hash,
              amount: o.value,
              currency: 'CAD',
              rate: app.get('rates').ask,
              received: true,
              tip,
              confirmed,
            })

            socket.to(sids[user.username]).emit('tx', message)
          } catch (e) { l(e) }
        } 
      } catch(e) {}
    })
  })

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

        let payments = await db.Payment.findAll({
          include: { model: db.User, as: 'user' },
          where: { confirmed: false },
        }).map(async p => {
          if ((await bc.getRawTransaction(p.hash, true)).confirmations > 0) {
            let user = p.user
            p.confirmed = true
            user.balance += p.amount
            user.pending -= p.amount
            await user.save() 
            await p.save()
            socket.to(sids[user.username]).emit('user', user)
          }
        })
        
        socket.emit('block', message)
        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.balance += amount
          req.user.balance -= amount
          req.user.channel = reverse(data.chan_pending.txid).toString('hex')
          await req.user.save()
          socket.to(sids[req.user.username]).emit('user', req.user)
          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') || msg.startsWith('peer')) {
            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.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) => {
    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.balance < 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)
        req.user.balance -= total

        await db.Payment.create({
          amount: -total,
          user_id: req.user.id,
          hash,
          rate: app.get('rates').ask,
          currency: 'CAD',
        })

        await req.user.save()
        socket.to(sids[req.user.username]).emit('user', req.user)

        if (payreq.payeeNodeKey === config.lnb.id) {
          let invoice = await lna.addInvoice({ value: payreq.satoshis })
          let payback = lnb.sendPayment(lnb.meta, {})
          let { payment_request } = invoice
          payback.write({ payment_request })
        }

        seen.push(hash)
        res.send(m)
      }
    })

    stream.on('error', e => {
      let msg = e.message

      res.status(500).send(msg)
    })
  }) 

  app.post('/payUser', auth, async (req, res) => {
    let { payuser, amount } = req.body

    let user = await db.User.findOne({
      where: {
        username: payuser
      } 
    })

    if (!user) return res.status(401).end()
    let err = m => res.status(500).send(m)

    let invoice
    try {
      invoice = await lnb.addInvoice({ value: amount })
    } catch (e) {
      return err(e.message)
    } 

    let hash = invoice.payment_request
    
    await db.Payment.create({
      user_id: user.id,
      hash,
      amount,
      currency: 'CAD',
      rate: app.get('rates').ask,
      tip: 0,
    })

    req.url = '/sendPayment'
    req.body.payreq = invoice.payment_request
    return app._router.handle(req, res)
  })

  app.post('/sendCoins', auth, async (req, res) => {
    const MINFEE = 3000

    let { address, amount } = req.body

    if (amount === req.user.balance) {
      amount = req.user.balance - MINFEE
    } 

    if (amount > req.user.balance)
      return res.status(401).send('Insufficient funds')

    try {
      await bc.walletPassphrase(config.bitcoin.walletpass, 300)
      let txid = await bc.sendToAddress(address, (amount / 100000000).toFixed(8))
      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()
      socket.to(sids[req.user.username]).emit('user', req.user)

      await db.Payment.create({
        amount: -total,
        user_id: req.user.id,
        hash: txid,
        rate: app.get('rates').ask,
        currency: 'CAD',
      })

      res.send({ txid, tx, amount, fees })
    } catch (e) {
      l(e)
      res.status(500).send(e.message)
    } 
  }) 

  app.post('/addInvoice', auth, async (req, res) => {
    let err = m => res.status(500).send(m)
    let { amount, address, tip } = req.body

    let invoice
    try {
      invoice = await lnb.addInvoice({ value: amount })
    } catch (e) {
      return err(e.message)
    } 

    let hash = invoice.payment_request
    if (address) hash = address
    
    await db.Payment.create({
      user_id: req.user.id,
      hash,
      amount,
      currency: 'CAD',
      rate: app.get('rates').ask,
      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]
      let now = new Date()
      let ts = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
      l(ts, 'quadriga ask price:', ask)
      app.set('rates', { ask })
      socket.emit('rate', ask)
    } catch (e) { l(e) }

    setTimeout(fetchRates, 30000)
  })()

  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.post('/facebookLogin', async (req, res) => {
    let { accessToken, userID } = req.body

    let url = `https://graph.facebook.com/debug_token?input_token=${accessToken}&access_token=${config.facebook.appToken}`
    let check = await axios.get(url)
    if (!check.data.data.is_valid) return res.status(401).end()

    try {
      let user = await db.User.findOne({
        where: {
          username: req.body.userID
        } 
      })

      if (!user) {
        user = await db.User.create(user)
        user.username = userID
        user.fbtoken = accessToken
        user.address = (await lna.newAddress({ type: 1 }, lna.meta)).address
        user.password = await bcrypt.hash(accessToken, 1)
        user.balance = 0
        user.pending = 0
        let friends = (await fb.api(`/${userID}/friends?access_token=${accessToken}`)).data
        if (friends.find(f => f.id === config.facebook.specialFriend)) {
          user.friend = true
          user.limit = 200
        }
        await user.save()
        addresses[user.address] = user.username
      } 

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

  app.post('/buy', auth, async (req, res) => {
    const stripe = require('stripe')(config.stripe);
    const { token, amount, sats } = req.body
    let dollarAmount = parseInt(amount / 100)

    if (dollarAmount > req.user.limit) return res.status(401).end()

    try {
      const charge = await stripe.charges.create({
        amount,
        currency: 'cad',
        description: 'Bitcoin',
        source: token,
      })

      req.user.balance += parseInt(sats)
      req.user.limit -= dollarAmount
      await req.user.save()
      socket.to(sids[req.user.username]).emit('user', req.user)

      await db.Payment.create({
        user_id: req.user.id,
        hash: charge.balance_transaction,
        amount: parseInt(sats),
        currency: 'CAD',
        rate: app.get('rates').ask,
        received: true,
        tip: 0
      })

      res.send(`Bought ${amount}`)
    } catch (e) {
      res.status(500).send(e)
    } 
  })

  app.use(function (err, req, res, next) {
    res.status(500)
    res.send('An error occurred')
    l(err.stack)
    return res.end()
  })

  server.listen(config.port)
})()