Add ZEC, BCH, and LTC to BitGO wallet plugin (#240)

* Add ZEC, BCH and LTC to BitGO wallet plugin

* Convert to legacyAddress on bitgo plugin
This commit is contained in:
Rafael Taranto 2018-12-30 11:48:40 -03:00 committed by Josh Harvey
parent b254f7f2be
commit e59ec4ed28
5 changed files with 144 additions and 32 deletions

View file

@ -1,4 +1,5 @@
const BitGo = require('bitgo')
const { toLegacyAddress, toCashAddress } = require('bchaddrjs')
const BN = require('../../../bn')
@ -8,35 +9,50 @@ const pjson = require('../../../../package.json')
const userAgent = 'Lamassu-Server/' + pjson.version
const NAME = 'BitGo'
const SUPPORTED_COINS = ['BTC', 'ZEC', 'LTC', 'BCH']
function buildBitgo (account) {
const env = account.environment === 'test' ? 'test' : 'prod'
return new BitGo.BitGo({ accessToken: account.token, env, userAgent: userAgent })
}
function getWallet (account) {
function getWallet (account, cryptoCode) {
const bitgo = buildBitgo(account)
const coin = account.environment === 'test' ? 'tbtc' : 'btc'
const coin = account.environment === 'test' ? `t${cryptoCode.toLowerCase()}` : cryptoCode.toLowerCase()
return bitgo.coin(coin).wallets().get({ id: account.walletId })
return bitgo.coin(coin).wallets().get({ id: account[`${cryptoCode}WalletId`] })
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BTC') {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve()
}
function getLegacyAddress (address, cryptoCode) {
const BCH_CODES = ['BCH', 'TBCH']
if (!BCH_CODES.includes(cryptoCode)) return address
return toLegacyAddress(address)
}
function getCashAddress (address, cryptoCode) {
const BCH_CODES = ['BCH', 'TBCH']
if (!BCH_CODES.includes(cryptoCode)) return address
return toCashAddress(address)
}
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account))
.then(() => getWallet(account, cryptoCode))
.then(wallet => {
const params = {
address: address,
address: getLegacyAddress(address, cryptoCode),
amount: cryptoAtoms.toNumber(),
walletPassphrase: account.walletPassphrase
walletPassphrase: account[`${cryptoCode}WalletPassphrase`]
}
return wallet.send(params)
})
@ -51,13 +67,13 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
function balance (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account))
.then(() => getWallet(account, cryptoCode))
.then(wallet => BN(wallet._wallet.spendableBalanceString))
}
function newAddress (account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => getWallet(account))
.then(() => getWallet(account, info.cryptoCode))
.then(wallet => {
return wallet.createAddress()
.then(result => {
@ -66,10 +82,10 @@ function newAddress (account, info) {
// If a label was provided, set the label
if (info.label) {
return wallet.updateAddress({ address: address, label: info.label })
.then(() => address)
.then(() => getCashAddress(address, info.cryptoCode))
}
return address
return getCashAddress(address, info.cryptoCode)
})
})
}
@ -77,7 +93,7 @@ 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: getLegacyAddress(toAddress, cryptoCode) }))
.then(rec => {
if (rec.balance === 0) return { status: 'notSeen' }
if (requested.gt(rec.balance)) return { status: 'insufficientFunds' }
@ -89,7 +105,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
function newFunding (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
return getWallet(account)
return getWallet(account, cryptoCode)
.then(wallet => {
return wallet.createAddress()
.then(result => {
@ -98,7 +114,7 @@ function newFunding (account, cryptoCode) {
.then(() => ({
fundingPendingBalance: BN(wallet._wallet.balanceString),
fundingConfirmedBalance: BN(wallet._wallet.confirmedBalanceString),
fundingAddress
fundingAddress: getCashAddress(fundingAddress, cryptoCode)
}))
})
})