From bd1128ddc87d918eba9d6cf24a4b1f6ebcff5e45 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Wed, 5 Dec 2018 23:59:56 +0000 Subject: [PATCH] FIX: accounting for txs --- class/User.js | 23 +++++++++++++++++++++-- controllers/api.js | 27 +++++++++++++-------------- package.json | 2 +- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/class/User.js b/class/User.js index 3dce782..494acdb 100644 --- a/class/User.js +++ b/class/User.js @@ -6,9 +6,10 @@ export class User { * * @param {Redis} redis */ - constructor(redis, bitcoindrpc) { + constructor(redis, bitcoindrpc, lightning) { this._redis = redis; this._bitcoindrpc = bitcoindrpc; + this._lightning = lightning; this._userid = false; this._login = false; this._password = false; @@ -87,6 +88,23 @@ export class User { return await this._redis.get('bitcoin_address_for_' + this._userid); } + /** + * Asks LND for new address, and imports it to bitcoind + * + * @returns {Promise} + */ + async generateAddress() { + let self = this; + return new Promise(function(resolve, reject) { + self._lightning.newAddress({ type: 0 }, async function(err, response) { + if (err) return reject('LND failure'); + await self.addAddress(response.address); + self._bitcoindrpc.request('importaddress', [response.address, response.address, false]); + resolve(); + }); + }); + } + async getBalance() { return (await this._redis.get('balance_for_' + this._userid)) * 1; } @@ -110,6 +128,7 @@ export class User { */ async getTxs() { let addr = await this.getAddress(); + if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user'); let txs = await this._bitcoindrpc.request('listtransactions', [addr, 100500, 0, true]); txs = txs.result; let result = []; @@ -141,6 +160,7 @@ export class User { */ async getPendingTxs() { let addr = await this.getAddress(); + if (!addr) throw new Error('cannot get transactions: no onchain address assigned to user'); let txs = await this._bitcoindrpc.request('listtransactions', [addr, 100500, 0, true]); txs = txs.result; let result = []; @@ -179,7 +199,6 @@ export class User { */ async accountForPosibleTxids() { let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1); - console.log(':::::::::::imported_txids', imported_txids); let onchain_txs = await this.getTxs(); for (let tx of onchain_txs) { if (tx.type !== 'bitcoind_tx') continue; diff --git a/controllers/api.js b/controllers/api.js index a90fa48..32885d7 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -10,7 +10,7 @@ var Redis = require('ioredis'); var redis = new Redis(config.redis); redis.monitor(function(err, monitor) { monitor.on('monitor', function(time, args, source, database) { - console.log('REDIS', args); + console.log('REDIS', JSON.stringify(args)); }); }); @@ -153,7 +153,7 @@ router.post('/payinvoice', async function(req, res) { }); router.get('/getbtc', async function(req, res) { - let u = new User(redis); + let u = new User(redis, bitcoinclient, lightning); await u.loadByAuthorization(req.headers.authorization); if (!u.getUserId()) { @@ -162,23 +162,20 @@ router.get('/getbtc', async function(req, res) { let address = await u.getAddress(); if (!address) { - lightning.newAddress({ type: 0 }, async function(err, response) { - if (err) return errorLnd(res); - await u.addAddress(response.address); - res.send([{ address: response.address }]); - bitcoinclient.request('importaddress', [response.address, response.address, false]); - }); - } else { - res.send([{ address }]); - bitcoinclient.request('importaddress', [address, address, false]); + await u.generateAddress(); + address = await u.getAddress(); } + + res.send([{ address }]); }); router.get('/balance', async function(req, res) { - let u = new User(redis, bitcoinclient); + let u = new User(redis, bitcoinclient, lightning); if (!(await u.loadByAuthorization(req.headers.authorization))) { return errorBadAuth(res); } + + if (!(await u.getAddress())) await u.generateAddress(); // onchain address needed further await u.accountForPosibleTxids(); let balance = await u.getBalance(); res.send({ BTC: { AvailableBalance: balance } }); @@ -197,22 +194,24 @@ router.get('/getinfo', async function(req, res) { }); router.get('/gettxs', async function(req, res) { - let u = new User(redis, bitcoinclient); + let u = new User(redis, bitcoinclient, lightning); if (!(await u.loadByAuthorization(req.headers.authorization))) { return errorBadAuth(res); } + if (!(await u.getAddress())) await u.generateAddress(); // onchain addr needed further await u.accountForPosibleTxids(); let txs = await u.getTxs(); res.send(txs); }); router.get('/getpending', async function(req, res) { - let u = new User(redis, bitcoinclient); + let u = new User(redis, bitcoinclient, lightning); if (!(await u.loadByAuthorization(req.headers.authorization))) { return errorBadAuth(res); } + if (!(await u.getAddress())) await u.generateAddress(); // onchain address needed further await u.accountForPosibleTxids(); let txs = await u.getPendingTxs(); res.send(txs); diff --git a/package.json b/package.json index 03da3d1..08fdf78 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "echo \"Error: no test specified\" && exit 1", "dev": "nodemon node_modules/.bin/babel-node index.js", "start": "node_modules/.bin/babel-node index.js", - "lint": "./node_modules/.bin/eslint ./ --fix" + "lint": "./node_modules/.bin/eslint ./ controllers/ class/ --fix" }, "author": "Igor Korsakov ", "license": "MIT",