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

1635
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,7 @@
"bitcoind-rpc": "^0.7.0", "bitcoind-rpc": "^0.7.0",
"bitcore-lib": "^0.15.0", "bitcore-lib": "^0.15.0",
"bitcore-lib-cash": "git+https://github.com/bitpay/bitcore-lib.git#cash", "bitcore-lib-cash": "git+https://github.com/bitpay/bitcore-lib.git#cash",
"bitgo": "3.4.11", "bitgo": "4.40.2",
"body-parser": "^1.15.1", "body-parser": "^1.15.1",
"coinbase": "^2.0.6", "coinbase": "^2.0.6",
"console-log-level": "^1.4.0", "console-log-level": "^1.4.0",