Add layer2 address separately

This commit is contained in:
Josh Harvey 2018-06-03 12:34:26 +03:00
parent d9aaf2437f
commit d1712ce1ce
7 changed files with 85 additions and 31 deletions

View file

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

View file

@ -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) {

View file

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

View file

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

View file

@ -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

View file

@ -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) {

View file

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