Skip to content

Commit

Permalink
fix(send receive): bch address
Browse files Browse the repository at this point in the history
  • Loading branch information
plondon committed Jun 3, 2021
1 parent 9e0f75c commit ab7b8f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,15 @@ export default ({ api, coreSagas, networks }: { api: APIType; coreSagas: any; ne
const value = payment.value()
if (!value.to) throw new Error('missing_to_from_custodial')
if (!value.amount) throw new Error('missing_amount_from_custodial')
if (!value.selection) throw new Error('missing_selection_from_custodial')
yield call(
api.withdrawSBFunds,
utils.bch.toCashAddr(value.to[0].address),
utils.bch.isCashAddr(value.to[0].address)
? value.to[0].address
: utils.bch.toCashAddr(value.to[0].address),
'BCH',
new BigNumber(value.amount[0]).toString()
new BigNumber(value.amount[0]).toString(),
value.selection.fee
)
} else {
payment = yield payment.publish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ export default ({
address,
amount,
currency,
fee: fee?.toString()
fee: fee?.toString(),
product: 'SIMPLEBUY'
},
endPoint: '/payments/withdrawals',
headers: {
Expand Down
28 changes: 8 additions & 20 deletions packages/blockchain-wallet-v4/src/utils/bch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const formatAddr = (address, displayOnly) => {
return displayOnly ? address.split('bitcoincash:')[1] : address
}

const hasPrefix = address => address.substring(0, 12) === 'bitcoincash:'
const hasPrefix = (address) => address.substring(0, 12) === 'bitcoincash:'

export const toCashAddr = (address, displayOnly) => {
const pubKeyHash = 0
Expand All @@ -20,15 +20,9 @@ export const toCashAddr = (address, displayOnly) => {

switch (version) {
case pubKeyHash:
return formatAddr(
cashaddress.encode(cashAddrPrefix, 'pubkeyhash', hash),
displayOnly
)
return formatAddr(cashaddress.encode(cashAddrPrefix, 'pubkeyhash', hash), displayOnly)
case scriptHash:
return formatAddr(
cashaddress.encode(cashAddrPrefix, 'scripthash', hash),
displayOnly
)
return formatAddr(cashaddress.encode(cashAddrPrefix, 'scripthash', hash), displayOnly)
default:
throw new Error('toBitcoinCash: Address type not supported')
}
Expand All @@ -37,33 +31,27 @@ export const toCashAddr = (address, displayOnly) => {
}
}

export const fromCashAddr = address => {
export const fromCashAddr = (address) => {
const { hash, version } = hasPrefix(address)
? cashaddress.decode(address)
: cashaddress.decode(`bitcoincash:${address}`)
switch (version) {
case 'pubkeyhash':
return Bitcoin.address.toBase58Check(
hash,
Bitcoin.networks.bitcoin.pubKeyHash
)
return Bitcoin.address.toBase58Check(hash, Bitcoin.networks.bitcoin.pubKeyHash)
case 'scripthash':
return Bitcoin.address.toBase58Check(
hash,
Bitcoin.networks.bitcoin.scriptHash
)
return Bitcoin.address.toBase58Check(hash, Bitcoin.networks.bitcoin.scriptHash)
default:
throw new Error('fromBitcoinCash: Address type not supported')
}
}

export const isCashAddr = address => {
export const isCashAddr = (address) => {
try {
return fromCashAddr(address)
} catch (e) {
return false
}
}

export const convertFromCashAddrIfCashAddr = addr =>
export const convertFromCashAddrIfCashAddr = (addr) =>
isCashAddr(addr) ? fromCashAddr(addr) : addr

0 comments on commit ab7b8f3

Please sign in to comment.