merge lightning
This commit is contained in:
commit
6fa6ac1647
92 changed files with 2688 additions and 2651 deletions
|
|
@ -1,6 +1,5 @@
|
|||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const bs58check = require('bs58check')
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
|
@ -27,8 +26,8 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function accountBalance (account, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -37,93 +36,73 @@ function balance (account, cryptoCode) {
|
|||
return accountBalance(account, cryptoCode, 1)
|
||||
}
|
||||
|
||||
function bchToBtcVersion (version) {
|
||||
if (version === 0x1c) return 0x00
|
||||
if (version === 0x28) return 0x05
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
// Bitcoin-ABC only accepts BTC style addresses at this point,
|
||||
// so we need to convert
|
||||
function bchToBtcAddress (address) {
|
||||
const buf = bs58check.decode(address)
|
||||
const version = buf[0]
|
||||
buf[0] = bchToBtcVersion(version)
|
||||
return bs58check.encode(buf)
|
||||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
const btcAddress = bchToBtcAddress(address)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [btcAddress, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => fetch('getnewaddress'))
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
const btcAddress = bchToBtcAddress(address)
|
||||
|
||||
return fetch('getreceivedbyaddress', [btcAddress, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 1))
|
||||
.then(() => addressBalance(address, 1))
|
||||
}
|
||||
|
||||
function pendingBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 0))
|
||||
.then(() => addressBalance(address, 0))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
function cryptoNetwork (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
|
||||
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -24,10 +24,10 @@ function checkCryptoCode (cryptoCode) {
|
|||
return Promise.resolve()
|
||||
}
|
||||
|
||||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
function accountBalance (account, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -40,69 +40,69 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => fetch('getnewaddress'))
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 1))
|
||||
.then(() => addressBalance(address, 1))
|
||||
}
|
||||
|
||||
function pendingBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 0))
|
||||
.then(() => addressBalance(address, 0))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
function cryptoNetwork (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
|
||||
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -29,83 +29,83 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => {
|
||||
const params = {
|
||||
address: address,
|
||||
amount: cryptoAtoms.toNumber(),
|
||||
walletPassphrase: account.walletPassphrase
|
||||
}
|
||||
return wallet.sendCoins(params)
|
||||
})
|
||||
.then(result => {
|
||||
return result.hash
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.message === 'Insufficient funds') throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => {
|
||||
const params = {
|
||||
address: address,
|
||||
amount: cryptoAtoms.toNumber(),
|
||||
walletPassphrase: account.walletPassphrase
|
||||
}
|
||||
return wallet.sendCoins(params)
|
||||
})
|
||||
.then(result => {
|
||||
return result.hash
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.message === 'Insufficient funds') throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function balance (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => BN(wallet.wallet.spendableConfirmedBalance))
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => BN(wallet.wallet.spendableConfirmedBalance))
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => {
|
||||
return wallet.createAddress()
|
||||
.then(result => {
|
||||
const address = result.address
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => {
|
||||
return wallet.createAddress()
|
||||
.then(result => {
|
||||
const address = result.address
|
||||
|
||||
// If a label was provided, set the label
|
||||
if (info.label) {
|
||||
return wallet.setLabel({ address: address, label: info.label })
|
||||
.then(() => address)
|
||||
}
|
||||
// If a label was provided, set the label
|
||||
if (info.label) {
|
||||
return wallet.setLabel({ address: address, label: info.label })
|
||||
.then(() => address)
|
||||
}
|
||||
|
||||
return address
|
||||
return address
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
const bitgo = buildBitgo(account)
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => bitgo.blockchain().getAddress({address: toAddress}))
|
||||
.then(rec => {
|
||||
if (rec.balance === 0) return {status: 'notSeen'}
|
||||
if (requested.gt(rec.balance)) return {status: 'insufficientFunds'}
|
||||
if (requested.gt(rec.confirmedBalance)) return {status: 'authorized'}
|
||||
return {status: 'confirmed'}
|
||||
})
|
||||
.then(() => bitgo.blockchain().getAddress({address: toAddress}))
|
||||
.then(rec => {
|
||||
if (rec.balance === 0) return {status: 'notSeen'}
|
||||
if (requested.gt(rec.balance)) return {status: 'insufficientFunds'}
|
||||
if (requested.gt(rec.confirmedBalance)) return {status: 'authorized'}
|
||||
return {status: 'confirmed'}
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
return getWallet(account)
|
||||
.then(wallet => {
|
||||
return wallet.createAddress()
|
||||
.then(result => {
|
||||
const fundingAddress = result.address
|
||||
return wallet.setLabel({address: fundingAddress, label: 'Funding Address'})
|
||||
.then(() => ({
|
||||
fundingPendingBalance: BN(wallet.wallet.balance),
|
||||
fundingConfirmedBalance: BN(wallet.wallet.confirmedBalance),
|
||||
fundingAddress
|
||||
}))
|
||||
})
|
||||
.then(() => {
|
||||
return getWallet(account)
|
||||
.then(wallet => {
|
||||
return wallet.createAddress()
|
||||
.then(result => {
|
||||
const fundingAddress = result.address
|
||||
return wallet.setLabel({address: fundingAddress, label: 'Funding Address'})
|
||||
.then(() => ({
|
||||
fundingPendingBalance: BN(wallet.wallet.balance),
|
||||
fundingConfirmedBalance: BN(wallet.wallet.confirmedBalance),
|
||||
fundingAddress
|
||||
}))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function cryptoNetwork (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => account.environment === 'test' ? 'test' : 'main')
|
||||
.then(() => account.environment === 'test' ? 'test' : 'main')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -41,64 +41,64 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => fetch('getnewaddress'))
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 1))
|
||||
.then(() => addressBalance(address, 1))
|
||||
}
|
||||
|
||||
function pendingBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 0))
|
||||
.then(() => addressBalance(address, 0))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ function privateKey (account) {
|
|||
|
||||
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
|
||||
.then(pify(web3.eth.sendRawTransaction))
|
||||
.then(pify(web3.eth.sendRawTransaction))
|
||||
}
|
||||
|
||||
function checkCryptoCode (cryptoCode) {
|
||||
|
|
@ -52,7 +52,7 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function balance (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => pendingBalance(defaultAddress(account)))
|
||||
.then(() => pendingBalance(defaultAddress(account)))
|
||||
}
|
||||
|
||||
const pendingBalance = address => _balance(true, address)
|
||||
|
|
@ -81,31 +81,31 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
|
|||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(arr => {
|
||||
const gas = arr[0]
|
||||
const gasPrice = arr[1]
|
||||
const txCount = arr[2]
|
||||
.then(arr => {
|
||||
const gas = arr[0]
|
||||
const gasPrice = arr[1]
|
||||
const txCount = arr[2]
|
||||
|
||||
const toSend = includesFee
|
||||
? amount.minus(gasPrice.times(gas))
|
||||
: amount
|
||||
const toSend = includesFee
|
||||
? amount.minus(gasPrice.times(gas))
|
||||
: amount
|
||||
|
||||
const rawTx = {
|
||||
nonce: txCount,
|
||||
gasPrice: hex(gasPrice),
|
||||
gasLimit: gas,
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
value: hex(toSend)
|
||||
}
|
||||
const rawTx = {
|
||||
nonce: txCount,
|
||||
gasPrice: hex(gasPrice),
|
||||
gasLimit: gas,
|
||||
to: toAddress,
|
||||
from: fromAddress,
|
||||
value: hex(toSend)
|
||||
}
|
||||
|
||||
const tx = new Tx(rawTx)
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
const tx = new Tx(rawTx)
|
||||
const privateKey = wallet.getPrivateKey()
|
||||
|
||||
tx.sign(privateKey)
|
||||
tx.sign(privateKey)
|
||||
|
||||
return '0x' + tx.serialize().toString('hex')
|
||||
})
|
||||
return '0x' + tx.serialize().toString('hex')
|
||||
})
|
||||
}
|
||||
|
||||
function defaultWallet (account) {
|
||||
|
|
@ -121,12 +121,12 @@ function sweep (account, cryptoCode, hdIndex) {
|
|||
const fromAddress = wallet.getChecksumAddressString()
|
||||
|
||||
return confirmedBalance(fromAddress)
|
||||
.then(r => {
|
||||
if (r.eq(0)) return
|
||||
.then(r => {
|
||||
if (r.eq(0)) return
|
||||
|
||||
return generateTx(defaultAddress(account), wallet, r, true)
|
||||
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
|
||||
})
|
||||
return generateTx(defaultAddress(account), wallet, r, true)
|
||||
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
|
|
@ -136,17 +136,17 @@ function newAddress (account, info) {
|
|||
|
||||
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(cryptoAtoms)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(cryptoAtoms)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress)
|
||||
.then(pending => {
|
||||
if (pending.gte(cryptoAtoms)) return {status: 'published'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress)
|
||||
.then(pending => {
|
||||
if (pending.gte(cryptoAtoms)) return {status: 'published'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function paymentHdNode (account) {
|
||||
|
|
@ -165,19 +165,19 @@ function defaultHdNode (account) {
|
|||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const fundingAddress = defaultAddress(account)
|
||||
.then(() => {
|
||||
const fundingAddress = defaultAddress(account)
|
||||
|
||||
const promises = [
|
||||
pendingBalance(fundingAddress),
|
||||
confirmedBalance(fundingAddress)
|
||||
]
|
||||
const promises = [
|
||||
pendingBalance(fundingAddress),
|
||||
confirmedBalance(fundingAddress)
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
})
|
||||
return Promise.all(promises)
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -41,64 +41,64 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => fetch('getnewaddress'))
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 1))
|
||||
.then(() => addressBalance(address, 1))
|
||||
}
|
||||
|
||||
function pendingBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 0))
|
||||
.then(() => addressBalance(address, 0))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function balance (acount, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(connect)
|
||||
.then(c => c.channelBalance({}))
|
||||
.then(_.get('balance'))
|
||||
.then(BN)
|
||||
.then(r => r.shift(unitScale).round())
|
||||
.then(connect)
|
||||
.then(c => c.channelBalance({}))
|
||||
.then(_.get('balance'))
|
||||
.then(BN)
|
||||
.then(r => r.shift(unitScale).round())
|
||||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
|
|
@ -53,37 +53,37 @@ function newFunding (account, cryptoCode) {
|
|||
|
||||
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}`)
|
||||
}
|
||||
.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'))
|
||||
})
|
||||
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)
|
||||
.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 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'}
|
||||
})
|
||||
}
|
||||
|
||||
// Note: this must be handled outside of lnd
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,12 +19,12 @@ function _balance (cryptoCode) {
|
|||
|
||||
function balance (account, cryptoCode) {
|
||||
return Promise.resolve()
|
||||
.then(() => _balance(cryptoCode))
|
||||
.then(() => _balance(cryptoCode))
|
||||
}
|
||||
|
||||
function pendingBalance (account, cryptoCode) {
|
||||
return balance(account, cryptoCode)
|
||||
.then(b => b.mul(1.1))
|
||||
.then(b => b.mul(1.1))
|
||||
}
|
||||
|
||||
function confirmedBalance (account, cryptoCode) {
|
||||
|
|
@ -69,11 +69,11 @@ function newFunding (account, cryptoCode) {
|
|||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||
|
|
@ -83,7 +83,8 @@ function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
|
|||
if (elapsed < AUTHORIZE_TIME) return Promise.resolve({status: 'published'})
|
||||
if (elapsed < CONFIRM_TIME) return Promise.resolve({status: 'authorized'})
|
||||
|
||||
console.log('[%s] DEBUG: Mock wallet has confirmed transaction: %s', cryptoCode, toAddress)
|
||||
console.log('[%s] DEBUG: Mock wallet has confirmed transaction [%s]', cryptoCode, toAddress.slice(0, 5))
|
||||
|
||||
return Promise.resolve({status: 'confirmed'})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ function checkCryptoCode (cryptoCode) {
|
|||
|
||||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -41,64 +41,64 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
.then(() => fetch('sendtoaddress', [address, coins]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => fetch('getnewaddress'))
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
.then(r => BN(r).shift(unitScale).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 1))
|
||||
.then(() => addressBalance(address, 1))
|
||||
}
|
||||
|
||||
function pendingBalance (address, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => addressBalance(address, 0))
|
||||
.then(() => addressBalance(address, 0))
|
||||
}
|
||||
|
||||
function getStatus (account, toAddress, requested, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
.then(() => confirmedBalance(toAddress, cryptoCode))
|
||||
.then(confirmed => {
|
||||
if (confirmed.gte(requested)) return {status: 'confirmed'}
|
||||
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
return pendingBalance(toAddress, cryptoCode)
|
||||
.then(pending => {
|
||||
if (pending.gte(requested)) return {status: 'authorized'}
|
||||
if (pending.gt(0)) return {status: 'insufficientFunds'}
|
||||
return {status: 'notSeen'}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function newFunding (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
.then(() => {
|
||||
const promises = [
|
||||
accountBalance(account, cryptoCode, 0),
|
||||
accountBalance(account, cryptoCode, 1),
|
||||
newAddress(account, {cryptoCode})
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
return Promise.all(promises)
|
||||
})
|
||||
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
|
||||
fundingPendingBalance,
|
||||
fundingConfirmedBalance,
|
||||
fundingAddress
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue