From 0e974940310ea819cba2d6d8d5f5aa48469e35b5 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 16 Feb 2018 14:47:23 -0800 Subject: open channel endpoint --- index.js | 48 +++++++++++++++++++++++++++++++++++------------- package.json | 1 + yarn.lock | 12 ++++-------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index cab8ad61..6e771dc6 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import ba from 'bitcoinaverage' +import bcrypt from 'bcrypt' import bitcoin from 'bitcoinjs-lib' import bodyParser from 'body-parser' -import cache from './cache' import compression from 'compression' import cookieParser from 'cookie-parser' import cors from 'cors' @@ -9,12 +9,15 @@ import dotenv from 'dotenv' import express from 'express' import fs from 'fs' import grpc from 'grpc' -import passport from './passport' import jwt from 'jsonwebtoken' -import bcrypt from 'bcrypt' -import { graphqlExpress, graphiqlExpress } from 'apollo-server-express' +import reverse from 'buffer-reverse' import zmq from 'zmq' +import cache from './cache' +import passport from './passport' + +import { graphqlExpress, graphiqlExpress } from 'apollo-server-express' + dotenv.config() const l = console.log @@ -52,7 +55,7 @@ const l = console.log attributes: ['username', 'address'], }).map(u => { addresses[u.address] = u.username }) - console.log(addresses) + l(addresses) const zmqSock = zmq.socket('sub') zmqSock.connect('tcp://127.0.0.1:18503') @@ -67,11 +70,11 @@ const l = console.log case 'rawtx': { let tx = bitcoin.Transaction.fromHex(message) let total = tx.outs.reduce((a, b) => a + b.value, 0) - console.log(total) + l(total) tx.outs.map(async o => { try { let address = bitcoin.address.fromOutputScript(o.script, bitcoin.networks.testnet) - console.log(address, o.value) + l(address, o.value) if (Object.keys(addresses).includes(address)) { let user = await db['User'].findOne({ where: { @@ -81,7 +84,7 @@ const l = console.log user.balance += o.value await user.save() - console.log('HIT!') + l('HIT!') io.emit('tx', message) } } catch(e) { } @@ -92,7 +95,7 @@ const l = console.log case 'rawblock': { let block = bitcoin.Block.fromHex(message) - console.log(block.getHash()) + l(block.getHash()) break } } @@ -112,16 +115,35 @@ const l = console.log let channel = lnrpc.openChannelSync({ node_pubkey_string: '022ea315e5052b152579e70a90bacfd6aa7420f2ce94674d4ca8da29d709bc70fd', local_funding_amount: user.balance, - }, meta, (err, data) => { + }, meta, 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.get('/channelbalance', async (req, res) => { + let user = await db['User'].findOne({ + where: { + username: req.query.username, + } + }) + + let data = await lnrpc.listChannels({}, meta) + let channels = data.channels.filter(c => c.channel_point.includes(user.channel)) + if (channels.length) { + res.json({ balance: channels[0].local_balance }) + } else { + res.status(500).send({ error: 'No channel found' }) + } + }) + app.post('/sendPayment', async (req, res) => { const payments = lnrpc.sendPayment(meta, {}) payments.write({ payment_request: req.body.payreq }) @@ -160,11 +182,11 @@ const l = console.log res.status(401).end() } }).catch((err) => { - console.log(err) + l(err) res.status(401).end() }) }).catch((err) => { - console.log(err) + l(err) res.status(401).end() }) }) @@ -191,7 +213,7 @@ const l = console.log app.use(function (err, req, res, next) { res.status(500) res.send('An error occurred') - console.error(err.stack) + l(err.stack) return res.end() }) diff --git a/package.json b/package.json index e7477d04..a5b7a120 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "body-parser": "^1.5.0", "bolt11": "^1.0.0", "browser-sync": "^2.11.0", + "buffer-reverse": "^1.0.1", "compression": "^1.7.1", "connect-assets": "*", "connect-redis": "3.1.0", diff --git a/yarn.lock b/yarn.lock index 1a925a61..948544a9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1343,6 +1343,10 @@ buffer-equal-constant-time@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819" +buffer-reverse@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/buffer-reverse/-/buffer-reverse-1.0.1.tgz#49283c8efa6f901bc01fa3304d06027971ae2f60" + buffer-shims@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51" @@ -3773,14 +3777,6 @@ limiter@^1.0.5: napa "^3.0.0" pkg-dir "^2.0.0" -"lnrpc@https://github.com/asoltys/lnrpc#d3f6196": - version "0.0.3" - resolved "https://github.com/asoltys/lnrpc#d3f6196af87d4cc86d59d4b39627d9368ee0e988" - dependencies: - grpc "^1.6.0" - napa "^3.0.0" - pkg-dir "^2.0.0" - load-json-file@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0" -- cgit v1.2.3