From d1712ce1cec1b709b1e4ef84dc62bee74fa2b464 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 3 Jun 2018 12:34:26 +0300 Subject: [PATCH] Add layer2 address separately --- lib/cash-out/cash-out-atomic.js | 9 ++-- lib/layer2.js | 15 +++++- lib/plugins/layer2/strike/strike.js | 8 ++- lib/plugins/wallet/lnd/lnd.js | 15 ++---- lib/plugins/wallet/mock-wallet/mock-wallet.js | 4 +- lib/wallet.js | 52 ++++++++++++++----- .../1528017752387-add-layer2-address.js | 13 +++++ 7 files changed, 85 insertions(+), 31 deletions(-) create mode 100644 migrations/1528017752387-add-layer2-address.js diff --git a/lib/cash-out/cash-out-atomic.js b/lib/cash-out/cash-out-atomic.js index fd438875..369ef42c 100644 --- a/lib/cash-out/cash-out-atomic.js +++ b/lib/cash-out/cash-out-atomic.js @@ -42,11 +42,14 @@ function preProcess (t, oldTx, newTx, pi) { .then(isHd => nextHd(t, isHd, newTx)) .then(newTxHd => { return pi.newAddress(newTxHd) - .then(_.set('toAddress', _, newTxHd)) - .then(_.unset('isLightning')) + .then(_.merge(newTxHd)) }) .then(addressedTx => { - const rec = {to_address: addressedTx.toAddress} + const rec = { + to_address: addressedTx.toAddress, + layer_2_address: addressedTx.layer2Address + } + return cashOutActions.logAction(t, 'provisionAddress', rec, addressedTx) }) .catch(err => { diff --git a/lib/layer2.js b/lib/layer2.js index ccef42bc..c3cbed9d 100644 --- a/lib/layer2.js +++ b/lib/layer2.js @@ -3,6 +3,8 @@ const ph = require('./plugin-helper') function fetch (settings, cryptoCode) { const plugin = configManager.cryptoScoped(cryptoCode, settings.config).layer2 + if (plugin === 'no-layer2') return Promise.resolve() + const layer2 = ph.load(ph.LAYER2, plugin) const account = settings.accounts[plugin] @@ -11,12 +13,21 @@ function fetch (settings, cryptoCode) { function newAddress (settings, info) { return fetch(settings, info.cryptoCode) - .then(r => r.layer2.newAddress(r.account, info)) + .then(r => { + if (!r) return + return r.layer2.newAddress(r.account, info) + }) } function getStatus (settings, tx) { + const toAddress = tx.layer2Address + if (!toAddress) return Promise.resolve({status: 'notSeen'}) + return fetch(settings, tx.cryptoCode) - .then(r => r.layer2.getStatus(r.account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)) + .then(r => { + if (!r) return {status: 'notSeen'} + return r.layer2.getStatus(r.account, toAddress, tx.cryptoAtoms, tx.cryptoCode) + }) } function cryptoNetwork (settings, cryptoCode) { diff --git a/lib/plugins/layer2/strike/strike.js b/lib/plugins/layer2/strike/strike.js index 4ef18cb0..f42c7cf5 100644 --- a/lib/plugins/layer2/strike/strike.js +++ b/lib/plugins/layer2/strike/strike.js @@ -1,12 +1,16 @@ const axios = require('axios') const _ = require('lodash/fp') +const options = require('../../../options') + module.exports = { newAddress, getStatus, cryptoNetwork } +axios.defaults.baseURL = options.strike.baseUrl + function cryptoNetwork (account, cryptoCode) { return Promise.resolve('test') } @@ -19,7 +23,7 @@ function checkCryptoCode (cryptoCode) { function getCharge (account, chargeId) { return axios({ method: 'get', - url: `https://api.strike.acinq.co/api/v1/charges/${chargeId}`, + url: `v1/charges/${chargeId}`, auth: {username: account.token, password: ''} }).then(_.get('data')) } @@ -33,7 +37,7 @@ function createCharge (account, info) { return axios({ method: 'post', - url: 'https://api.strike.acinq.co/api/v1/charges', + url: 'v1/charges', auth: {username: account.token, password: ''}, data }).then(_.get('data')) diff --git a/lib/plugins/wallet/lnd/lnd.js b/lib/plugins/wallet/lnd/lnd.js index 52750d27..5e9d82f9 100644 --- a/lib/plugins/wallet/lnd/lnd.js +++ b/lib/plugins/wallet/lnd/lnd.js @@ -74,16 +74,11 @@ function getStatus (account, toAddress, requested, cryptoCode) { return connect() .then(c => { - if (isLightning) { - return c.lookupInvoice({r_hash_str: rHashStr}) - .then(r => { - if (r.settled) return {status: 'confirmed'} - return {status: 'notSeen'} - }) - } - - // Note: this must be handled outside of lnd - return {status: 'notSeen'} + return c.lookupInvoice({r_hash_str: rHashStr}) + .then(r => { + if (r.settled) return {status: 'confirmed'} + return {status: 'notSeen'} + }) }) }) } diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index 54ac9924..d18ac38f 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -5,8 +5,8 @@ const coinUtils = require('../../../coin-utils') const NAME = 'FakeWallet' const SECONDS = 1000 -const PUBLISH_TIME = 1 * SECONDS -const AUTHORIZE_TIME = PUBLISH_TIME + 1 * SECONDS +const PUBLISH_TIME = 3 * SECONDS +const AUTHORIZE_TIME = PUBLISH_TIME + 5 * SECONDS const CONFIRM_TIME = AUTHORIZE_TIME + 120 * SECONDS let t0 diff --git a/lib/wallet.js b/lib/wallet.js index 140a34b3..7a7f5bcf 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -40,12 +40,6 @@ function fetchWallet (settings, cryptoCode) { }) } -function isLayer2 (tx) { - return _.isNil(tx.isLightning) - ? layer2.isLayer2Address(tx.toAddress) - : tx.isLightning -} - const lastBalance = {} function _balance (settings, cryptoCode) { @@ -81,10 +75,17 @@ function sendCoins (settings, toAddress, cryptoAtoms, cryptoCode) { } function newAddress (settings, info) { - if (isLayer2(info)) return layer2.newAddress(settings, info) - - return fetchWallet(settings, info.cryptoCode) + const walletAddressPromise = fetchWallet(settings, info.cryptoCode) .then(r => r.wallet.newAddress(r.account, info)) + + return Promise.all([ + walletAddressPromise, + layer2.newAddress(settings, info) + ]) + .then(([toAddress, layer2Address]) => ({ + toAddress, + layer2Address + })) } function newFunding (settings, cryptoCode, address) { @@ -97,11 +98,38 @@ function newFunding (settings, cryptoCode, address) { }) } -function getWalletStatus (settings, tx) { - if (isLayer2(tx)) return layer2.getStatus(settings, tx) +function mergeStatus (a, b) { + if (!a) return b + if (!b) return a - return fetchWallet(settings, tx.cryptoCode) + return {status: mergeStatusMode(a.status, b.status)} +} + +function mergeStatusMode (a, b) { + const cleared = ['authorized', 'confirmed', 'instant'] + if (_.includes(a, cleared)) return a + if (_.includes(b, cleared)) return b + + if (a === 'published') return a + if (b === 'published') return b + + if (a === 'rejected') return a + if (b === 'rejected') return b + + return 'notSeen' +} + +function getWalletStatus (settings, tx) { + const walletStatusPromise = fetchWallet(settings, tx.cryptoCode) .then(r => r.wallet.getStatus(r.account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)) + + return Promise.all([ + walletStatusPromise, + layer2.getStatus(settings, tx) + ]) + .then(([walletStatus, layer2Status]) => { + return mergeStatus(walletStatus, layer2Status) + }) } function authorizeZeroConf (settings, tx, machineId) { diff --git a/migrations/1528017752387-add-layer2-address.js b/migrations/1528017752387-add-layer2-address.js new file mode 100644 index 00000000..6c19bf24 --- /dev/null +++ b/migrations/1528017752387-add-layer2-address.js @@ -0,0 +1,13 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + 'alter table cash_out_txs add column layer_2_address text null', + 'alter table cash_out_actions add column layer_2_address text null' + ] + db.multi(sql, next) +} + +exports.down = function (next) { + next() +}