lightning network support

This commit is contained in:
Josh Harvey 2018-01-28 19:10:00 +02:00
parent 1a31b27845
commit 4625ffef0f
7 changed files with 1305 additions and 101 deletions

View file

@ -173,6 +173,7 @@ function fetchData () {
{code: 'coinbase', display: 'Coinbase', class: 'ticker', cryptos: ['BTC', 'ETH', 'LTC', 'BCH']},
{code: 'mock-ticker', display: 'Mock ticker', class: 'ticker', cryptos: ALL_CRYPTOS},
{code: 'bitcoind', display: 'bitcoind', class: 'wallet', cryptos: ['BTC']},
{code: 'lnd', display: 'Lightning Network', class: 'wallet', cryptos: ['BTC']},
{code: 'geth', display: 'geth', class: 'wallet', cryptos: ['ETH']},
{code: 'zcashd', display: 'zcashd', class: 'wallet', cryptos: ['ZEC']},
{code: 'litecoind', display: 'litecoind', class: 'wallet', cryptos: ['LTC']},

View file

@ -216,6 +216,7 @@ function preProcess (oldTx, newTx, pi) {
.then(newTxHd => {
return pi.newAddress(newTxHd)
.then(_.set('toAddress', _, newTxHd))
.then(_.unset('isLightning'))
})
.then(addressedTx => {
const rec = {to_address: addressedTx.toAddress}

View file

@ -24,3 +24,4 @@ register('NoDataError')
register('InsufficientFundsError')
register('StaleTxError')
register('RatchetError')
register('NotImplementedError')

View file

@ -254,7 +254,9 @@ function plugins (settings, deviceId) {
cryptoCode: tx.cryptoCode,
label: 'TX ' + Date.now(),
account: 'deposit',
hdIndex: tx.hdIndex
hdIndex: tx.hdIndex,
cryptoAtoms: tx.cryptoAtoms,
isLightning: tx.isLightning
}
return wallet.newAddress(settings, info)
}

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

1303
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,8 @@
"bignumber.js": "^4.0.2",
"bip39": "^2.3.1",
"bitcoind-rpc": "^0.7.0",
"bitcore-lib": "^0.15.0",
"bitcore-lib-cash": "git+https://github.com/bitpay/bitcore-lib.git#cash",
"bitgo": "3.4.11",
"body-parser": "^1.15.1",
"coinbase": "^2.0.6",
@ -27,6 +29,7 @@
"helmet": "^3.8.1",
"inquirer": "^3.2.1",
"kraken-api": "github:DeX3/npm-kraken-api",
"lnd-async": "^1.0.1",
"lodash": "^4.17.2",
"make-dir": "^1.0.0",
"mem": "^1.1.0",
@ -50,9 +53,7 @@
"uuid": "^3.1.0",
"web3": "^0.19.1",
"winston": "^2.3.0",
"ws": "^3.1.0",
"bitcore-lib": "^0.15.0",
"bitcore-lib-cash": "git+https://github.com/bitpay/bitcore-lib.git#cash"
"ws": "^3.1.0"
},
"repository": {
"type": "git",