Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { currency } from 'components/Common/Ticker'; | import { currency } from 'components/Common/Ticker'; | ||||
import { isValidTokenStats } from 'utils/validation'; | import { isValidTokenStats } from 'utils/validation'; | ||||
import SlpWallet from 'minimal-slp-wallet'; | import SlpWallet from 'minimal-slp-wallet'; | ||||
import { | import { | ||||
toSmallestDenomination, | toSmallestDenomination, | ||||
fromSmallestDenomination, | fromSmallestDenomination, | ||||
batchArray, | batchArray, | ||||
flattenBatchedHydratedUtxos, | flattenBatchedHydratedUtxos, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
checkNullUtxosForTokenStatus, | checkNullUtxosForTokenStatus, | ||||
confirmNonEtokenUtxos, | confirmNonEtokenUtxos, | ||||
convertToEncryptStruct, | convertToEncryptStruct, | ||||
getPublicKey, | getPublicKey, | ||||
parseOpReturn, | parseOpReturn, | ||||
convertToEcashPrefix, | |||||
} from 'utils/cashMethods'; | } from 'utils/cashMethods'; | ||||
import cashaddr from 'ecashaddrjs'; | import cashaddr from 'ecashaddrjs'; | ||||
import ecies from 'ecies-lite'; | import ecies from 'ecies-lite'; | ||||
import wif from 'wif'; | import wif from 'wif'; | ||||
const XECJS = require('../xec-js'); | |||||
// @TODO: initialize this to app startup in useWallet | |||||
const xecjs = new XECJS({ restURL: 'https://rest.kingbch.com/v4' }); | |||||
export default function useBCH() { | export default function useBCH() { | ||||
const SEND_BCH_ERRORS = { | const SEND_BCH_ERRORS = { | ||||
INSUFFICIENT_FUNDS: 0, | INSUFFICIENT_FUNDS: 0, | ||||
NETWORK_ERROR: 1, | NETWORK_ERROR: 1, | ||||
INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | ||||
DOUBLE_SPENDING: 18, | DOUBLE_SPENDING: 18, | ||||
MAX_UNCONFIRMED_TXS: 64, | MAX_UNCONFIRMED_TXS: 64, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 1,439 Lines • ▼ Show 20 Lines | ) => { | ||||
} | } | ||||
} | } | ||||
const inputUtxos = []; | const inputUtxos = []; | ||||
let transactionBuilder; | let transactionBuilder; | ||||
// instance of transaction builder | // instance of transaction builder | ||||
if (process.env.REACT_APP_NETWORK === `mainnet`) | if (process.env.REACT_APP_NETWORK === `mainnet`) | ||||
transactionBuilder = new BCH.TransactionBuilder(); | transactionBuilder = new xecjs.TransactionBuilder(); | ||||
else transactionBuilder = new BCH.TransactionBuilder('testnet'); | else transactionBuilder = new xecjs.TransactionBuilder('testnet'); | ||||
const satoshisToSend = toSmallestDenomination(value); | const satoshisToSend = toSmallestDenomination(value); | ||||
// Throw validation error if toSmallestDenomination returns false | // Throw validation error if toSmallestDenomination returns false | ||||
if (!satoshisToSend) { | if (!satoshisToSend) { | ||||
const error = new Error( | const error = new Error( | ||||
`Invalid decimal places for send amount`, | `Invalid decimal places for send amount`, | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | ) => { | ||||
BCH.Address.isCashAddress(REMAINDER_ADDR); | BCH.Address.isCashAddress(REMAINDER_ADDR); | ||||
} catch (err) { | } catch (err) { | ||||
isValidChangeAddress = false; | isValidChangeAddress = false; | ||||
} | } | ||||
if (!isValidChangeAddress) { | if (!isValidChangeAddress) { | ||||
REMAINDER_ADDR = wallet.Path1899.cashAddress; | REMAINDER_ADDR = wallet.Path1899.cashAddress; | ||||
} | } | ||||
// ensure change address is always in eCash format after validation | |||||
REMAINDER_ADDR = convertToEcashPrefix(REMAINDER_ADDR); | |||||
// amount to send back to the remainder address. | // amount to send back to the remainder address. | ||||
const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | ||||
if (remainder.lt(0)) { | if (remainder.lt(0)) { | ||||
const error = new Error(`Insufficient funds`); | const error = new Error(`Insufficient funds`); | ||||
error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | ||||
throw error; | throw error; | ||||
} | } | ||||
if (isOneToMany) { | if (isOneToMany) { | ||||
// for one to many mode, add the multiple outputs from the array | // for one to many mode, add the multiple outputs from the array | ||||
let arrayLength = destinationAddressAndValueArray.length; | let arrayLength = destinationAddressAndValueArray.length; | ||||
for (let i = 0; i < arrayLength; i++) { | for (let i = 0; i < arrayLength; i++) { | ||||
// add each send tx from the array as an output | // add each send tx from the array as an output | ||||
let outputAddress = | let outputAddress = | ||||
destinationAddressAndValueArray[i].split(',')[0]; | destinationAddressAndValueArray[i].split(',')[0]; | ||||
let outputValue = new BigNumber( | let outputValue = new BigNumber( | ||||
destinationAddressAndValueArray[i].split(',')[1], | destinationAddressAndValueArray[i].split(',')[1], | ||||
); | ); | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toCashAddress(outputAddress), | outputAddress, | ||||
parseInt(toSmallestDenomination(outputValue)), | parseInt(toSmallestDenomination(outputValue)), | ||||
); | ); | ||||
} | } | ||||
} else { | } else { | ||||
// for one to one mode, add output w/ single address and amount to send | // for one to one mode, add output w/ single address and amount to send | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toCashAddress(destinationAddress), | destinationAddress, | ||||
parseInt(toSmallestDenomination(value)), | parseInt(toSmallestDenomination(value)), | ||||
); | ); | ||||
} | } | ||||
if (remainder.gte(new BigNumber(currency.dustSats))) { | if (remainder.gte(new BigNumber(currency.dustSats))) { | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
REMAINDER_ADDR, | REMAINDER_ADDR, | ||||
parseInt(remainder), | parseInt(remainder), | ||||
▲ Show 20 Lines • Show All 85 Lines • Show Last 20 Lines |