Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/cashMethods.js
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | |||||
// Modified for mainnet only | // Modified for mainnet only | ||||
export const getECPairFromWIF = wif => { | export const getECPairFromWIF = wif => { | ||||
let xec = coininfo.bitcoincash.main; | let xec = coininfo.bitcoincash.main; | ||||
const xecBitcoinJSLib = xec.toBitcoinJS(); | const xecBitcoinJSLib = xec.toBitcoinJS(); | ||||
return Bitcoin.ECPair.fromWIF(wif, xecBitcoinJSLib); | return Bitcoin.ECPair.fromWIF(wif, xecBitcoinJSLib); | ||||
}; | }; | ||||
export const signUtxosByAddress = (inputUtxos, wallet, txBuilder) => { | export const signUtxosByAddress = (BCH, inputUtxos, wallet, txBuilder) => { | ||||
for (let i = 0; i < inputUtxos.length; i++) { | for (let i = 0; i < inputUtxos.length; i++) { | ||||
const utxo = inputUtxos[i]; | const utxo = inputUtxos[i]; | ||||
const accounts = [wallet.Path245, wallet.Path145, wallet.Path1899]; | const accounts = [wallet.Path245, wallet.Path145, wallet.Path1899]; | ||||
const wif = accounts | const wif = accounts | ||||
.filter(acc => acc.cashAddress === utxo.address) | .filter(acc => acc.cashAddress === utxo.address) | ||||
.pop().fundingWif; | .pop().fundingWif; | ||||
const utxoECPair = getECPairFromWIF(wif); | const utxoECPair = BCH.ECPair.fromWIF(wif); | ||||
txBuilder.sign( | txBuilder.sign( | ||||
i, | i, | ||||
utxoECPair, | utxoECPair, | ||||
undefined, | undefined, | ||||
txBuilder.hashTypes.SIGHASH_ALL, | txBuilder.hashTypes.SIGHASH_ALL, | ||||
parseInt(utxo.value), | parseInt(utxo.value), | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 536 Lines • ▼ Show 20 Lines | if ( | ||||
!wallet || | !wallet || | ||||
// txBuilder.transaction.tx.ins is empty until the inputUtxos are signed | // txBuilder.transaction.tx.ins is empty until the inputUtxos are signed | ||||
txBuilder.transaction.tx.outs.length === 0 | txBuilder.transaction.tx.outs.length === 0 | ||||
) { | ) { | ||||
throw new Error('Invalid buildTx parameter'); | throw new Error('Invalid buildTx parameter'); | ||||
} | } | ||||
// Sign each XEC UTXO being consumed and refresh transactionBuilder | // Sign each XEC UTXO being consumed and refresh transactionBuilder | ||||
txBuilder = signUtxosByAddress(inputUtxos, wallet, txBuilder); | txBuilder = signUtxosByAddress(BCH, inputUtxos, wallet, txBuilder); | ||||
let hex; | let hex; | ||||
try { | try { | ||||
// build tx | // build tx | ||||
const tx = txBuilder.build(); | const tx = txBuilder.build(); | ||||
// output rawhex | // output rawhex | ||||
hex = tx.toHex(); | hex = tx.toHex(); | ||||
} catch (err) { | } catch (err) { | ||||
▲ Show 20 Lines • Show All 504 Lines • Show Last 20 Lines |