Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 295 Lines • ▼ Show 20 Lines | ) => { | ||||
// Send dust transaction representing tokens being sent. | // Send dust transaction representing tokens being sent. | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.SLP.Address.toLegacyAddress(tokenReceiverAddress), | BCH.SLP.Address.toLegacyAddress(tokenReceiverAddress), | ||||
546, | 546, | ||||
); | ); | ||||
// Return any token change back to the sender. | // Return any token change back to the sender. | ||||
if (slpSendObj.outputs > 1) { | if (slpSendObj.outputs > 1) { | ||||
// Try to send this to Path1899 to move all utxos off legacy addresses | |||||
if (wallet.Path1899.legacyAddress) { | |||||
transactionBuilder.addOutput( | |||||
wallet.Path1899.legacyAddress, | |||||
546, | |||||
); | |||||
} else { | |||||
// If you can't, send it back from whence it came | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.SLP.Address.toLegacyAddress( | BCH.SLP.Address.toLegacyAddress( | ||||
tokenUtxosBeingSpent[0].address, | tokenUtxosBeingSpent[0].address, | ||||
), | ), | ||||
546, | 546, | ||||
); | ); | ||||
} | } | ||||
} | |||||
// get byte count to calculate fee. paying 1 sat | // get byte count to calculate fee. paying 1 sat | ||||
// Note: This may not be totally accurate. Just guessing on the byteCount size. | // Note: This may not be totally accurate. Just guessing on the byteCount size. | ||||
const txFee = calcFee( | const txFee = calcFee( | ||||
BCH, | BCH, | ||||
tokenUtxosBeingSpent, | tokenUtxosBeingSpent, | ||||
5, | 5, | ||||
1.1 * currency.defaultFee, | 1.1 * currency.defaultFee, | ||||
); | ); | ||||
// amount to send back to the sending address. It's the original amount - 1 sat/byte for tx size | // amount to send back to the sending address. It's the original amount - 1 sat/byte for tx size | ||||
const remainder = originalAmount - txFee - 546 * 2; | const remainder = originalAmount - txFee - 546 * 2; | ||||
if (remainder < 1) { | if (remainder < 1) { | ||||
throw new Error('Selected UTXO does not have enough satoshis'); | throw new Error('Selected UTXO does not have enough satoshis'); | ||||
} | } | ||||
// Last output: send the BCH change back to the wallet. | // Last output: send the BCH change back to the wallet. | ||||
// If Path1899, send it to Path1899 address | |||||
if (wallet.Path1899.legacyAddress) { | |||||
transactionBuilder.addOutput( | |||||
wallet.Path1899.legacyAddress, | |||||
remainder, | |||||
); | |||||
} else { | |||||
// Otherwise send it back from whence it came | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toLegacyAddress(largestBchUtxo.address), | BCH.Address.toLegacyAddress(largestBchUtxo.address), | ||||
remainder, | remainder, | ||||
); | ); | ||||
} | |||||
// Sign the transaction with the private key for the BCH UTXO paying the fees. | // Sign the transaction with the private key for the BCH UTXO paying the fees. | ||||
let redeemScript; | let redeemScript; | ||||
transactionBuilder.sign( | transactionBuilder.sign( | ||||
0, | 0, | ||||
bchECPair, | bchECPair, | ||||
redeemScript, | redeemScript, | ||||
transactionBuilder.hashTypes.SIGHASH_ALL, | transactionBuilder.hashTypes.SIGHASH_ALL, | ||||
originalAmount, | originalAmount, | ||||
); | ); | ||||
// Sign each token UTXO being consumed. | // Sign each token UTXO being consumed. | ||||
for (let i = 0; i < tokenUtxosBeingSpent.length; i++) { | for (let i = 0; i < tokenUtxosBeingSpent.length; i++) { | ||||
const thisUtxo = tokenUtxosBeingSpent[i]; | const thisUtxo = tokenUtxosBeingSpent[i]; | ||||
const accounts = [wallet.Path245, wallet.Path145]; | const accounts = [wallet.Path245, wallet.Path145, wallet.Path1899]; | ||||
const utxoEcPair = BCH.ECPair.fromWIF( | const utxoEcPair = BCH.ECPair.fromWIF( | ||||
accounts | accounts | ||||
.filter(acc => acc.cashAddress === thisUtxo.address) | .filter(acc => acc.cashAddress === thisUtxo.address) | ||||
.pop().fundingWif, | .pop().fundingWif, | ||||
); | ); | ||||
transactionBuilder.sign( | transactionBuilder.sign( | ||||
1 + i, | 1 + i, | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | ) => { | ||||
if (!values || values.length === 0) { | if (!values || values.length === 0) { | ||||
return null; | return null; | ||||
} | } | ||||
const value = values.reduce( | const value = values.reduce( | ||||
(previous, current) => new BigNumber(current).plus(previous), | (previous, current) => new BigNumber(current).plus(previous), | ||||
new BigNumber(0), | new BigNumber(0), | ||||
); | ); | ||||
const REMAINDER_ADDR = wallet.Path145.cashAddress; | const REMAINDER_ADDR = wallet.Path1899.cashAddress; | ||||
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 BCH.TransactionBuilder(); | ||||
else transactionBuilder = new BCH.TransactionBuilder('testnet'); | else transactionBuilder = new BCH.TransactionBuilder('testnet'); | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |