lightning network support
This commit is contained in:
parent
1a31b27845
commit
4625ffef0f
7 changed files with 1305 additions and 101 deletions
89
lib/plugins/wallet/lnd/lnd.js
Normal file
89
lib/plugins/wallet/lnd/lnd.js
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
const lnd = require('lnd-async')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
const options = require('../../../options')
|
||||
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
|
||||
const unitScale = cryptoRec.unitScale
|
||||
|
||||
module.exports = {
|
||||
balance,
|
||||
sendCoins,
|
||||
newAddress,
|
||||
getStatus,
|
||||
newFunding,
|
||||
cryptoNetwork
|
||||
}
|
||||
|
||||
function connect () {
|
||||
return lnd.connect(options.lnd || {})
|
||||
}
|
||||
|
||||
function cryptoNetwork (account, cryptoCode) {
|
||||
return Promise.resolve('test')
|
||||
}
|
||||
|
||||
function checkCryptoCode (cryptoCode) {
|
||||
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
function balance (acount, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(connect)
|
||||
.then(c => c.channelBalance({}))
|
||||
.then(_.get('balance'))
|
||||
.then(BN)
|
||||
.then(r => r.shift(unitScale).round())
|
||||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
// Not implemented yet
|
||||
return Promise.reject(new E.NotImplementedError())
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
// Not implemented yet
|
||||
return Promise.reject(new E.NotImplementedError())
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(connect)
|
||||
.then(c => {
|
||||
if (info.isLightning) {
|
||||
return c.addInvoice({memo: 'Lamassu cryptomat deposit', value: info.cryptoAtoms.toNumber()})
|
||||
.then(r => `${r.r_hash.toString('hex')}:${r.payment_request}`)
|
||||
}
|
||||
|
||||
return c.newAddress({type: 2})
|
||||
.then(_.get('address'))
|
||||
})
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const parts = _.split(':', toAddress)
|
||||
const isLightning = _.size(parts) === 2
|
||||
const rHashStr = isLightning && _.head(parts)
|
||||
|
||||
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'}
|
||||
})
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue