Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/__tests__/cashMethods.test.js
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { | import { | ||||
fromSatoshisToXec, | fromSatoshisToXec, | ||||
flattenContactList, | flattenContactList, | ||||
loadStoredWallet, | loadStoredWallet, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
fromLegacyDecimals, | fromLegacyDecimals, | ||||
convertToEcashPrefix, | convertToEcashPrefix, | ||||
isLegacyMigrationRequired, | isLegacyMigrationRequired, | ||||
toLegacyCash, | |||||
toLegacyCashArray, | |||||
convertEtokenToEcashAddr, | convertEtokenToEcashAddr, | ||||
parseOpReturn, | parseOpReturn, | ||||
convertEcashtoEtokenAddr, | convertEcashtoEtokenAddr, | ||||
getHashArrayFromWallet, | getHashArrayFromWallet, | ||||
isActiveWebsocket, | isActiveWebsocket, | ||||
parseXecSendValue, | parseXecSendValue, | ||||
getChangeAddressFromInputUtxos, | getChangeAddressFromInputUtxos, | ||||
generateOpReturnScript, | generateOpReturnScript, | ||||
Show All 10 Lines | import { | ||||
calcFee, | calcFee, | ||||
toHash160, | toHash160, | ||||
generateGenesisOpReturn, | generateGenesisOpReturn, | ||||
generateSendOpReturn, | generateSendOpReturn, | ||||
generateBurnOpReturn, | generateBurnOpReturn, | ||||
getECPairFromWIF, | getECPairFromWIF, | ||||
} from 'utils/cashMethods'; | } from 'utils/cashMethods'; | ||||
import { currency } from 'components/Common/Ticker'; | import { currency } from 'components/Common/Ticker'; | ||||
import { | import { validAddressArrayInput } from '../__mocks__/mockAddressArray'; | ||||
validAddressArrayInput, | |||||
validAddressArrayInputMixedPrefixes, | |||||
validAddressArrayOutput, | |||||
validLargeAddressArrayInput, | |||||
validLargeAddressArrayOutput, | |||||
invalidAddressArrayInput, | |||||
} from '../__mocks__/mockAddressArray'; | |||||
import { | import { | ||||
mockGenesisOpReturnScript, | mockGenesisOpReturnScript, | ||||
mockSendOpReturnScript, | mockSendOpReturnScript, | ||||
mockSendOpReturnTokenUtxos, | mockSendOpReturnTokenUtxos, | ||||
mockBurnOpReturnScript, | mockBurnOpReturnScript, | ||||
mockBurnOpReturnTokenUtxos, | mockBurnOpReturnTokenUtxos, | ||||
} from '../__mocks__/mockOpReturnScript'; | } from '../__mocks__/mockOpReturnScript'; | ||||
import { | import { | ||||
▲ Show 20 Lines • Show All 262 Lines • ▼ Show 20 Lines | it(`signUtxosByAddress() successfully returns a txBuilder object for a one to many XEC tx`, () => { | ||||
); | ); | ||||
const txFee = new BigNumber(totalInputUtxoValue) | const txFee = new BigNumber(totalInputUtxoValue) | ||||
.minus(satoshisToSendOutput) | .minus(satoshisToSendOutput) | ||||
.minus( | .minus( | ||||
new BigNumber( | new BigNumber( | ||||
mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | ||||
), | ), | ||||
); // change value | ); // change value | ||||
destinationAddressAndValueArray = toLegacyCashArray(validAddressArrayInput); | destinationAddressAndValueArray = validAddressArrayInput; | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | BCH, | ||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSendOutput, | satoshisToSendOutput, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
null, | null, | ||||
▲ Show 20 Lines • Show All 592 Lines • ▼ Show 20 Lines | it(`generateTxOutput() returns a txBuilder instance for a valid one to many XEC tx`, () => { | ||||
const txFee = new BigNumber(totalInputUtxoValue) | const txFee = new BigNumber(totalInputUtxoValue) | ||||
.minus(satoshisToSend) | .minus(satoshisToSend) | ||||
.minus( | .minus( | ||||
new BigNumber( | new BigNumber( | ||||
mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | ||||
), | ), | ||||
); // change value | ); // change value | ||||
const destinationAddressAndValueArray = toLegacyCashArray( | const destinationAddressAndValueArray = validAddressArrayInput; | ||||
validAddressArrayInput, | |||||
); | |||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | BCH, | ||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSend, | satoshisToSend, | ||||
▲ Show 20 Lines • Show All 411 Lines • ▼ Show 20 Lines | test('toHash160 throws error if input address is an invalid simpleledger: address', () => { | ||||
try { | try { | ||||
toHash160(address); | toHash160(address); | ||||
} catch (err) { | } catch (err) { | ||||
errorThrown = err.message; | errorThrown = err.message; | ||||
} | } | ||||
expect(errorThrown).toStrictEqual('Invalid address: ' + address + '.'); | expect(errorThrown).toStrictEqual('Invalid address: ' + address + '.'); | ||||
}); | }); | ||||
test('toLegacyCash() converts a valid ecash: prefix address to a valid bitcoincash: prefix address', async () => { | |||||
const result = toLegacyCash( | |||||
'ecash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gtfza25mc', | |||||
); | |||||
expect(result).toStrictEqual( | |||||
'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0', | |||||
); | |||||
}); | |||||
test('toLegacyCash() converts a valid ecash: prefixless address to a valid bitcoincash: prefix address', async () => { | |||||
const result = toLegacyCash( | |||||
'qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gtfza25mc', | |||||
); | |||||
expect(result).toStrictEqual( | |||||
'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0', | |||||
); | |||||
}); | |||||
test('toLegacyCash throws error if input address has invalid checksum', async () => { | |||||
const result = toLegacyCash( | |||||
'ecash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gtfza25m', | |||||
); | |||||
expect(result).toStrictEqual( | |||||
new Error( | |||||
'ecash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gtfza25m is not a valid ecash address', | |||||
), | |||||
); | |||||
}); | |||||
test('toLegacyCash() throws error with input of etoken: address', async () => { | |||||
const result = toLegacyCash( | |||||
'etoken:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4g9htlunl0', | |||||
); | |||||
expect(result).toStrictEqual( | |||||
new Error( | |||||
'etoken:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4g9htlunl0 is not a valid ecash address', | |||||
), | |||||
); | |||||
}); | |||||
test('toLegacyCash() throws error with input of legacy address', async () => { | |||||
const result = toLegacyCash('13U6nDrkRsC3Eb1pxPhNY8XJ5W9zdp6rNk'); | |||||
expect(result).toStrictEqual( | |||||
new Error( | |||||
'13U6nDrkRsC3Eb1pxPhNY8XJ5W9zdp6rNk is not a valid ecash address', | |||||
), | |||||
); | |||||
}); | |||||
test('toLegacyCash() throws error with input of bitcoincash: address', async () => { | |||||
const result = toLegacyCash( | |||||
'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0', | |||||
); | |||||
expect(result).toStrictEqual( | |||||
new Error( | |||||
'bitcoincash:qqd3qn4zazjhygk5a2vzw2gvqgqwempr4gjykk3wa0 is not a valid ecash address', | |||||
), | |||||
); | |||||
}); | |||||
test('toLegacyCashArray throws error if the addressArray input is null', async () => { | |||||
const result = toLegacyCashArray(null); | |||||
expect(result).toStrictEqual(new Error('Invalid addressArray input')); | |||||
}); | |||||
test('toLegacyCashArray throws error if the addressArray input is empty', async () => { | |||||
const result = toLegacyCashArray([]); | |||||
expect(result).toStrictEqual(new Error('Invalid addressArray input')); | |||||
}); | |||||
test('toLegacyCashArray throws error if the addressArray input is a number', async () => { | |||||
const result = toLegacyCashArray(12345); | |||||
expect(result).toStrictEqual(new Error('Invalid addressArray input')); | |||||
}); | |||||
test('toLegacyCashArray throws error if the addressArray input is undefined', async () => { | |||||
const result = toLegacyCashArray(undefined); | |||||
expect(result).toStrictEqual(new Error('Invalid addressArray input')); | |||||
}); | |||||
test('toLegacyCashArray successfully converts a standard sized valid addressArray input', async () => { | |||||
const result = toLegacyCashArray(validAddressArrayInput); | |||||
expect(result).toStrictEqual(validAddressArrayOutput); | |||||
}); | |||||
test('toLegacyCashArray successfully converts a standard sized valid addressArray input including prefixless ecash addresses', async () => { | |||||
const result = toLegacyCashArray(validAddressArrayInputMixedPrefixes); | |||||
expect(result).toStrictEqual(validAddressArrayOutput); | |||||
}); | |||||
test('toLegacyCashArray successfully converts a large valid addressArray input', async () => { | |||||
const result = toLegacyCashArray(validLargeAddressArrayInput); | |||||
expect(result).toStrictEqual(validLargeAddressArrayOutput); | |||||
}); | |||||
test('toLegacyCashArray throws an error on an addressArray with invalid addresses', async () => { | |||||
const result = toLegacyCashArray(invalidAddressArrayInput); | |||||
expect(result).toStrictEqual( | |||||
new Error( | |||||
'ecash:qrqgwxrrrrrrrrrrrrrrrrrrrrrrrrr7zsvk is not a valid ecash address', | |||||
), | |||||
); | |||||
}); | |||||
test('parseOpReturn() successfully parses a short cashtab message', async () => { | test('parseOpReturn() successfully parses a short cashtab message', async () => { | ||||
const result = parseOpReturn(shortCashtabMessageInputHex); | const result = parseOpReturn(shortCashtabMessageInputHex); | ||||
expect(result).toStrictEqual(mockParsedShortCashtabMessageArray); | expect(result).toStrictEqual(mockParsedShortCashtabMessageArray); | ||||
}); | }); | ||||
test('parseOpReturn() successfully parses a long cashtab message where an additional PUSHDATA1 is present', async () => { | test('parseOpReturn() successfully parses a long cashtab message where an additional PUSHDATA1 is present', async () => { | ||||
const result = parseOpReturn(longCashtabMessageInputHex); | const result = parseOpReturn(longCashtabMessageInputHex); | ||||
expect(result).toStrictEqual(mockParsedLongCashtabMessageArray); | expect(result).toStrictEqual(mockParsedLongCashtabMessageArray); | ||||
▲ Show 20 Lines • Show All 267 Lines • Show Last 20 Lines |