Migrate plugins to lib directory
This commit is contained in:
parent
09b29bba56
commit
e7ab8223c2
27 changed files with 869 additions and 858 deletions
91
lib/plugins/wallet/bitgo/bitgo.js
Normal file
91
lib/plugins/wallet/bitgo/bitgo.js
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
const BitGo = require('bitgo')
|
||||
const BN = require('../../../bn')
|
||||
|
||||
const pjson = require('../../../package.json')
|
||||
const userAgent = 'Lamassu-Server/' + pjson.version
|
||||
|
||||
const NAME = 'BitGo'
|
||||
|
||||
function buildBitgo (account) {
|
||||
return new BitGo.BitGo({accessToken: account.token, env: 'prod', userAgent: userAgent})
|
||||
}
|
||||
|
||||
function getWallet (account) {
|
||||
const bitgo = buildBitgo(account)
|
||||
return bitgo.wallets().get({ id: account.walletId })
|
||||
}
|
||||
|
||||
function checkCryptoCode (cryptoCode) {
|
||||
if (cryptoCode !== 'BTC') {
|
||||
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
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') {
|
||||
err.name = 'InsufficientFunds'
|
||||
}
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function balance (account, cryptoCode) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => getWallet(account))
|
||||
.then(wallet => BN(wallet.wallet.spendableConfirmedBalance))
|
||||
}
|
||||
|
||||
function newAddress (account, cryptoCode, info) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.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)
|
||||
}
|
||||
|
||||
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'}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
NAME,
|
||||
balance,
|
||||
sendCoins,
|
||||
newAddress,
|
||||
getStatus
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue