Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/cashMethods.js
import { currency } from 'components/Common/Ticker'; | import { currency } from 'components/Common/Ticker'; | ||||
import { | import { | ||||
isValidXecAddress, | isValidXecAddress, | ||||
isValidEtokenAddress, | isValidEtokenAddress, | ||||
isValidContactList, | isValidContactList, | ||||
isValidBchAddress, | isValidBchAddress, | ||||
} from 'utils/validation'; | } from 'utils/validation'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import cashaddr from 'ecashaddrjs'; | import cashaddr from 'ecashaddrjs'; | ||||
import bs58 from 'bs58'; | import bs58 from 'bs58'; | ||||
import * as slpMdm from 'slp-mdm'; | import * as slpMdm from 'slp-mdm'; | ||||
import Bitcoin from '@psf/bitcoincashjs-lib'; | |||||
import coininfo from 'utils/coininfo'; | |||||
// function is based on BCH-JS' generateBurnOpReturn() however it's been trimmed down for Cashtab use | // function is based on BCH-JS' generateBurnOpReturn() however it's been trimmed down for Cashtab use | ||||
// Reference: https://github.com/Permissionless-Software-Foundation/bch-js/blob/62e56c832b35731880fe448269818b853c76dd80/src/slp/tokentype1.js#L217 | // Reference: https://github.com/Permissionless-Software-Foundation/bch-js/blob/62e56c832b35731880fe448269818b853c76dd80/src/slp/tokentype1.js#L217 | ||||
export const generateBurnOpReturn = (tokenUtxos, burnQty) => { | export const generateBurnOpReturn = (tokenUtxos, burnQty) => { | ||||
try { | try { | ||||
if (!tokenUtxos || !burnQty) { | if (!tokenUtxos || !burnQty) { | ||||
throw new Error('Invalid burn token parameter'); | throw new Error('Invalid burn token parameter'); | ||||
} // sendToken component already prevents burning of a value greater than the token utxo total held by the wallet | } // sendToken component already prevents burning of a value greater than the token utxo total held by the wallet | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | export const getUtxoWif = (utxo, wallet) => { | ||||
} | } | ||||
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; | ||||
return wif; | return wif; | ||||
}; | }; | ||||
// Reference https://github.com/Permissionless-Software-Foundation/bch-js/blob/master/src/ecpair.js#L24 | |||||
// Modified for mainnet only | |||||
export const getECPairFromWIF = wif => { | |||||
let xec = coininfo.bitcoincash.main; | |||||
const xecBitcoinJSLib = xec.toBitcoinJS(); | |||||
return Bitcoin.ECPair.fromWIF(wif, xecBitcoinJSLib); | |||||
}; | |||||
export const signUtxosByAddress = (BCH, 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 utxoEcPair = BCH.ECPair.fromWIF( | |||||
accounts.filter(acc => acc.cashAddress === utxo.address).pop() | const wif = accounts | ||||
.fundingWif, | .filter(acc => acc.cashAddress === utxo.address) | ||||
); | .pop().fundingWif; | ||||
console.log(`wif`, wif); | |||||
const utxoEcPair = BCH.ECPair.fromWIF(wif); | |||||
const localUtxoEcPair = getECPairFromWIF(wif); | |||||
console.log(`utxoEcPair`, utxoEcPair); | |||||
console.log(`localUtxoEcPair`, localUtxoEcPair); | |||||
console.log(`stringified`, JSON.stringify(utxoEcPair)); | |||||
if (JSON.stringify(utxoEcPair) === JSON.stringify(localUtxoEcPair)) { | |||||
console.log(`local method matches legacy method`); | |||||
} | |||||
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 1,057 Lines • Show Last 20 Lines |