Merge pull request #223 from RafaelTaranto/bitgo-api-v2

Bitgo api v2
This commit is contained in:
Josh Harvey 2018-12-10 00:39:45 +02:00 committed by GitHub
commit 8e1f9e0b83
3 changed files with 1196 additions and 471 deletions

View file

@ -11,12 +11,14 @@ const NAME = 'BitGo'
function buildBitgo (account) {
const env = account.environment === 'test' ? 'test' : 'prod'
return new BitGo.BitGo({accessToken: account.token, env, userAgent: userAgent})
return new BitGo.BitGo({ accessToken: account.token, env, userAgent: userAgent })
}
function getWallet (account) {
const bitgo = buildBitgo(account)
return bitgo.wallets().get({ id: account.walletId })
const coin = account.environment === 'test' ? 'tbtc' : 'btc'
return bitgo.coin(coin).wallets().get({ id: account.walletId })
}
function checkCryptoCode (cryptoCode) {
@ -36,13 +38,13 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
amount: cryptoAtoms.toNumber(),
walletPassphrase: account.walletPassphrase
}
return wallet.sendCoins(params)
return wallet.send(params)
})
.then(result => {
return result.hash
})
.catch(err => {
if (err.message === 'Insufficient funds') throw new E.InsufficientFundsError()
if (err.message === 'insufficient funds') throw new E.InsufficientFundsError()
throw err
})
}
@ -50,7 +52,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
function balance (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account))
.then(wallet => BN(wallet.wallet.spendableConfirmedBalance))
.then(wallet => BN(wallet._wallet.spendableBalanceString))
}
function newAddress (account, info) {
@ -63,7 +65,7 @@ function newAddress (account, info) {
// If a label was provided, set the label
if (info.label) {
return wallet.setLabel({ address: address, label: info.label })
return wallet.updateAddress({ address: address, label: info.label })
.then(() => address)
}
@ -75,12 +77,12 @@ function newAddress (account, info) {
function getStatus (account, toAddress, requested, cryptoCode) {
const bitgo = buildBitgo(account)
return checkCryptoCode(cryptoCode)
.then(() => bitgo.blockchain().getAddress({address: toAddress}))
.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'}
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' }
})
}
@ -92,10 +94,10 @@ function newFunding (account, cryptoCode) {
return wallet.createAddress()
.then(result => {
const fundingAddress = result.address
return wallet.setLabel({address: fundingAddress, label: 'Funding Address'})
return wallet.updateAddress({ address: fundingAddress, label: 'Funding Address' })
.then(() => ({
fundingPendingBalance: BN(wallet.wallet.balance),
fundingConfirmedBalance: BN(wallet.wallet.confirmedBalance),
fundingPendingBalance: BN(wallet._wallet.balanceString),
fundingConfirmedBalance: BN(wallet._wallet.confirmedBalanceString),
fundingAddress
}))
})