diff --git a/web/cashtab/src/utils/__mocks__/incrementalUtxoMocks.js b/web/cashtab/src/utils/__mocks__/incrementalUtxoMocks.js new file mode 100644 index 000000000..7e644ac80 --- /dev/null +++ b/web/cashtab/src/utils/__mocks__/incrementalUtxoMocks.js @@ -0,0 +1,538 @@ +export const previousUtxosObjUtxoArray = [ + { + height: 680782, + tx_hash: + '525457276f1b6984170c9b35a8312d4988fce495723eabadd2afcdb3b872b2f1', + tx_pos: 1, + value: 546, + }, + { + height: 680784, + tx_hash: + '28f061fee068d3b9cb578141bac3d4d9ec4eccebec680464bf0aafaac414811f', + tx_pos: 1, + value: 546, + }, + { + height: 680784, + tx_hash: + '5fa3ffccea55c968beb7d214c563c92336ce2bbccbb714ba819848a7f7060bdb', + tx_pos: 1, + value: 546, + }, + { + height: 680784, + tx_hash: + 'daa98a872b7d88fefd2257b006db001ef82a601f3943b92e0c753076598a7b75', + tx_pos: 1, + value: 546, + }, + { + height: 681190, + tx_hash: + 'e9dca9aa954131a0004325fff11dfddcd6e5843c468116cf4d38cb264032cdc0', + tx_pos: 2, + value: 546, + }, + { + height: 681191, + tx_hash: + 'b35c502f388cdfbdd6841b7a73e973149b3c8deca76295a3e4665939e0562796', + tx_pos: 2, + value: 546, + }, + { + height: 685181, + tx_hash: + '7987f68aa70d29ac0e0ac31d74354a8b1cd515c9893f6a5cdc7a3bf505e08b05', + tx_pos: 1, + value: 546, + }, + { + height: 686546, + tx_hash: + 'bd84598096c113cd2110bc1748dd0613a933e2ddc440654c12ca4db4659933ed', + tx_pos: 1, + value: 546, + }, + { + height: 687240, + tx_hash: + 'cd9e5bc5fc041e46e8ce01ddb232c54fe48f1fb4a7288f10fdd03a6c2af875e1', + tx_pos: 2, + value: 546, + }, + { + height: 688194, + tx_hash: + '3de671a7107d3803d78f7f4a4e5c794d0903a8d28d16076445c084943c1e2db8', + tx_pos: 1, + value: 546, + }, + { + height: 688449, + tx_hash: + 'ab5079e9d24c33b31893cb98d409d24acdc396b5ab751e4c428d2463e991030c', + tx_pos: 2, + value: 546, + }, + { + height: 692599, + tx_hash: + '0158981b89b75bd923d511aaaaccd94b8d1d86babeeb69c29e3caf71e33bcc11', + tx_pos: 1, + value: 546, + }, + { + height: 692599, + tx_hash: + '1ef9ad7d3e01fd9d83983eac92eefb4900b343225a80c29bff025deff9aab57c', + tx_pos: 1, + value: 546, + }, + { + height: 693606, + tx_hash: + '9989f6f4941d7cf3206b327d957b022b41bf7e449a11fd5dd5cf1e9bc93f1ecf', + tx_pos: 2, + value: 546, + }, + { + height: 699216, + tx_hash: + '6f4e602620f5df257df8655f5834d5cfbbb73f62601c69afa96198f8ab4c2680', + tx_pos: 2, + value: 546, + }, + { + height: 699359, + tx_hash: + 'b99cb29050779d4f185c3c31c22e664436966314c8b260075b38bbb453180603', + tx_pos: 2, + value: 546, + }, + { + height: 700185, + tx_hash: + '71e458d9fd68a72fd5b13e2c758c6ba246495fa2933764876221450c096938b8', + tx_pos: 2, + value: 546, + }, + { + height: 700469, + tx_hash: + '41b9da9a5719b7bf61a02a598a37ee918a4da01e6ff5b1fb5366221ee93fd498', + tx_pos: 2, + value: 546, + }, + { + height: 700469, + tx_hash: + '6e24e89b6d5284138c69777527760500b99614631bca7f2a5c38f4648dae9524', + tx_pos: 1, + value: 546, + }, + { + height: 700469, + tx_hash: + 'bab327965a4fd423a383859b021ea2971987ceaa6fa3bc3994c3a3266a237db5', + tx_pos: 2, + value: 546, + }, + { + height: 700572, + tx_hash: + '431f527f657b399d8753fb63aee6c806ca0f8907d93606c46b36a33dcb5cb5b9', + tx_pos: 2, + value: 546, + }, + { + height: 700677, + tx_hash: + 'da9460ce4b1c92b4f6ef4e4a6bc2d05539f49d02b17681389d9ce22b8dca50f0', + tx_pos: 1, + value: 546, + }, + { + height: 700915, + tx_hash: + 'ef80e1ceeada69a9639c320c1fba47ea4417cd3aad1be1635c3472ce28aaef33', + tx_pos: 2, + value: 546, + }, + { + height: 701079, + tx_hash: + '0d5408adeefc0d9468d957a0a2bca1b63c371e68e61b3fd9c30de60058471935', + tx_pos: 1, + value: 546, + }, + { + height: 701079, + tx_hash: + '6397497c053e5c641ae624d4af80e8aa931a0e7b018f17a9543afed9b705cf29', + tx_pos: 1, + value: 546, + }, + { + height: 701079, + tx_hash: + 'c665bfd2353940648b018a3126ddbc7ac309729c7ca4598ebd7941930fd80b60', + tx_pos: 1, + value: 546, + }, + { + height: 701079, + tx_hash: + 'ebf864950d862ebb53e121350d15c8b34b2374eb22afffb98fcb655b38441d59', + tx_pos: 1, + value: 546, + }, + { + height: 701079, + tx_hash: + 'fe10460f822163c33515f3a853c1470d68223c9c0e8f8cbc6c954ca537129f30', + tx_pos: 1, + value: 546, + }, + { + height: 701189, + tx_hash: + '3656afe8682997be4cab4275e4bbec3f81c8aa264cec206a7215d449ee6b9af4', + tx_pos: 1, + value: 546, + }, + { + height: 701189, + tx_hash: + '87656bf2c2f2d46d16ba6b41b4ff488a3eff1e852c64bc921322f580e375f3cb', + tx_pos: 1, + value: 546, + }, + { + height: 701191, + tx_hash: + 'c212e45f21418fa7fd5bbf2941892353c1d6ddb9d6d16ff12fba3f7919c37b43', + tx_pos: 1, + value: 546, + }, + { + height: 701194, + tx_hash: + 'ff61be814b18f60a640169c5d70b42ce29bd9caf2f5e5592655e924760634c1e', + tx_pos: 1, + value: 546, + }, + { + height: 701208, + tx_hash: + '0e9179929b71d8a94ce9de75434d9e0901eacf3b2b882fa02a56eab450d0bd0b', + tx_pos: 1, + value: 546, + }, + { + height: 701211, + tx_hash: + '4ad31e5ab9cfcead7d8b48b81a542044e44e63124eb96d6463fe4bbe5b77e9ad', + tx_pos: 1, + value: 546, + }, + { + height: 701211, + tx_hash: + '72d4827a9a0b9adac9430ba799cb049af14fd79df11569b4e1a4741ac114b84d', + tx_pos: 1, + value: 546, + }, + { + height: 701221, + tx_hash: + '42d3e2d97604f09c002df701f964adacacd28bc328acc0066a2563d63f522681', + tx_pos: 1, + value: 546, + }, + { + height: 701223, + tx_hash: + '890bd4d72e75c4123b73dc81b9f4f89716fabe456a9047f9a5a5ef4a5162d218', + tx_pos: 2, + value: 546, + }, + { + height: 709251, + tx_hash: + '9e8483407944d9b75c331ebd6178b0cabc3e8c3b5bb0492b7b2256c8740f655a', + tx_pos: 1, + value: 546, + }, + { + height: 709259, + tx_hash: + '4f4fc78f7a008fc109789722d89fe95fe75ca1f15af625f24ae4ec74d420552e', + tx_pos: 1, + value: 546, + }, + { + height: 709668, + tx_hash: + 'da371839612b153543d0cffb09e0220dca7c7acfebda660785807b269bd0341c', + tx_pos: 1, + value: 546, + }, + { + height: 710065, + tx_hash: + '117939de3822734df69fb5cc27a6429860ee2f7a78917603da8b8aebba2a9150', + tx_pos: 1, + value: 546, + }, + { + height: 711088, + tx_hash: + '982ca55c84510e4184ff5a6e7fc310a1de7833e8c617b46014f962ed89bf0f57', + tx_pos: 2, + value: 546, + }, + { + height: 711227, + tx_hash: + 'e26db37d5c64b265514cd5cbb9d5194a7f2967b5974d167236d46be4954e435c', + tx_pos: 2, + value: 546, + }, + { + height: 715111, + tx_hash: + 'b39fdb53e21d67fa5fd3a11122f1452f15884047f2b80e8efe633c3b520b7a39', + tx_pos: 1, + value: 546, + }, + { + height: 715815, + tx_hash: + '3515f4a9851ad44124e0ddf6149344deb27a97720fc7e5254a9d2c86da7415a9', + tx_pos: 1, + value: 546, + }, + { + height: 715815, + tx_hash: + '6fb6122742cac8fd1df2d68997fdfa4c077bc22d9ef4a336bfb63d24225f9060', + tx_pos: 1, + value: 546, + }, + { + height: 715816, + tx_hash: + '2936188a41f22a3e0a47d13296147fb3f9ddd2f939fe6382904d21a610e8e49c', + tx_pos: 1, + value: 546, + }, + { + height: 717055, + tx_hash: + '18c0360f0db5399223cbed48f55c4cee9d9914c8a4a7dedcf9172a36201e9896', + tx_pos: 1, + value: 546, + }, + { + height: 717653, + tx_hash: + '3adbf501e21c711d20118e003711168eb39f560c01f4c6d6736fa3f3fceaa577', + tx_pos: 1, + value: 546, + }, + { + height: 717824, + tx_hash: + 'c0fe05d7bf71cd0f476ea18cdd4ecb26e1b9a33c911f4aaf143b2b18bc3b5f4f', + tx_pos: 1, + value: 546, + }, + { + height: 718091, + tx_hash: + '905cc5662cad77df56c3770863634ce498dde9d4772dc494d33b7ce3f36fa66c', + tx_pos: 2, + value: 546, + }, + { + height: 718280, + tx_hash: + 'f31f4ad7bf035cfb587a07a12ec60937cb8cbeafa7e4d7ed4f3276fea26fcfec', + tx_pos: 1, + value: 546, + }, + { + height: 718790, + tx_hash: + '67faa4753da2940d053f32edcda2c052a16c683aeb73f10cfde5c18266c14fe2', + tx_pos: 2, + value: 546, + }, + { + height: 720056, + tx_hash: + '9c6363fb537d529f512a12d292ea9682fe7159e6bf5ebfec5b7067b401d2dba4', + tx_pos: 1, + value: 546, + }, + { + height: 720070, + tx_hash: + '4eed87ba70864d9daa46d201c47db4513f77e5d4cc01256ab4dcc6dae9dfa055', + tx_pos: 1, + value: 546, + }, + { + height: 720070, + tx_hash: + '7975514a3185cbb70900e9767e5fcc91c86913cb1d2ad9a28474253875271e33', + tx_pos: 1, + value: 546, + }, + { + height: 720070, + tx_hash: + 'e10ae7a1bc78561ed367d59f150aebc13ef2054ba62f1a0db08fc7612d5ed58b', + tx_pos: 1, + value: 546, + }, + { + height: 720070, + tx_hash: + 'fb71c88bd5369cb8278f49ac672a9721833c36fc69143848b46ae15860339ea6', + tx_pos: 1, + value: 546, + }, + { + height: 720078, + tx_hash: + 'c3c6c6fb1619d001c29f17a701d042bc6b983e71113822aeeb66ca434fd9fa6c', + tx_pos: 1, + value: 546, + }, + { + height: 720951, + tx_hash: + 'fb50eac73a4fd5e2a701e0dbf4e575cea9c083e061b1db722e057164c7317e5b', + tx_pos: 2, + value: 546, + }, + { + height: 721083, + tx_hash: + 'dfb3dbf90fd87f6d66465ff05a61ddf1e1ca30900fadfe9cd4b73468649935ed', + tx_pos: 2, + value: 546, + }, + { + height: 724822, + tx_hash: + 'ed0dab39d5e976e433a705785726901dc83daa7d579412c18ee997341de010d3', + tx_pos: 1, + value: 546, + }, + { + height: 725143, + tx_hash: + 'e99296764134d6ea9ba7521490563762cfaf1541854ba9babc26c0df8665ac32', + tx_pos: 1, + value: 546, + }, + { + height: 725871, + tx_hash: + '82a3fe0b03ab07a564351443634da1b1ed3960e4771c59b6f8abbf7ef4b3258d', + tx_pos: 1, + value: 546, + }, + { + height: 725882, + tx_hash: + '1db1bef70013d178d7912731435029f9c8588f1d0089944c53eccffd255b5efc', + tx_pos: 2, + value: 546, + }, + { + height: 0, + tx_hash: + '6c70ada5dfda75788b48ad6da211a3ea56b4b2bad8fa807954b553742e62c73d', + tx_pos: 0, + value: 60000, + }, + { + height: 0, + tx_hash: + '94b34b291f3c77d008f3521462563ea1656542f9f8908d18a489edf5eda27fdb', + tx_pos: 0, + value: 900, + }, + { + height: 0, + tx_hash: + '9e6d5d346effd3b57bd68d95a9bfc9de1e5787f110589aa3b88d7852eebb425f', + tx_pos: 1, + value: 673, + }, + { + height: 0, + tx_hash: + '3c89d42ff868c74546ba819aaf4e5c5d5e5c63437d91c9c1cf5406ccbec3d952', + tx_pos: 2, + value: 546, + }, + { + height: 0, + tx_hash: + '3c89d42ff868c74546ba819aaf4e5c5d5e5c63437d91c9c1cf5406ccbec3d952', + tx_pos: 3, + value: 95212726, + }, +]; + +export const includedUtxoAlpha = { + height: 680784, + tx_hash: '28f061fee068d3b9cb578141bac3d4d9ec4eccebec680464bf0aafaac414811f', + tx_pos: 1, + value: 546, +}; +export const includedUtxoBeta = { + height: 0, + tx_hash: '3c89d42ff868c74546ba819aaf4e5c5d5e5c63437d91c9c1cf5406ccbec3d952', + tx_pos: 3, + value: 95212726, +}; + +export const excludedUtxoAlpha = { + // Note this is includedUtxoAlpha with tx_pos of 0 instead of 1 + height: 680784, + tx_hash: '28f061fee068d3b9cb578141bac3d4d9ec4eccebec680464bf0aafaac414811f', + tx_pos: 0, + value: 546, +}; +export const excludedUtxoBeta = { + // Note this is includedUtxoBeta with value 95212725 instead of 95212726 + height: 0, + tx_hash: '3c89d42ff868c74546ba819aaf4e5c5d5e5c63437d91c9c1cf5406ccbec3d952', + tx_pos: 3, + value: 95212725, +}; + +export const validUtxo = { + height: 724992, + tx_hash: '8d4bdedb7c4443412e0c2f316a330863aef54d9ba73560ca60cca6408527b247', + tx_pos: 0, + value: 10200, +}; +export const invalidUtxoMissingHeight = { + tx_hash: '8d4bdedb7c4443412e0c2f316a330863aef54d9ba73560ca60cca6408527b247', + tx_pos: 0, + value: 10200, +}; +export const invalidUtxoTxidUndefined = { + height: 724992, + tx_hash: undefined, + tx_pos: 0, + value: 10200, +}; diff --git a/web/cashtab/src/utils/__tests__/cashMethods.test.js b/web/cashtab/src/utils/__tests__/cashMethods.test.js index 942c6267c..575537e63 100644 --- a/web/cashtab/src/utils/__tests__/cashMethods.test.js +++ b/web/cashtab/src/utils/__tests__/cashMethods.test.js @@ -1,348 +1,378 @@ import { ValidationError } from 'ecashaddrjs'; import { fromSmallestDenomination, batchArray, flattenBatchedHydratedUtxos, loadStoredWallet, isValidStoredWallet, fromLegacyDecimals, convertToEcashPrefix, checkNullUtxosForTokenStatus, confirmNonEtokenUtxos, isLegacyMigrationRequired, toLegacyCash, toLegacyToken, toLegacyCashArray, parseOpReturn, + isExcludedUtxo, } from '@utils/cashMethods'; import { unbatchedArray, arrayBatchedByThree, } from '../__mocks__/mockBatchedArrays'; import { validAddressArrayInput, validAddressArrayInputMixedPrefixes, validAddressArrayOutput, validLargeAddressArrayInput, validLargeAddressArrayOutput, invalidAddressArrayInput, } from '../__mocks__/mockAddressArray'; import { unflattenedHydrateUtxosResponse, flattenedHydrateUtxosResponse, } from '../__mocks__/flattenBatchedHydratedUtxosMocks'; import { cachedUtxos, utxosLoadedFromCache, } from '../__mocks__/mockCachedUtxos'; import { validStoredWallet, invalidStoredWallet, } from '../__mocks__/mockStoredWallets'; import { mockTxDataResults, mockNonEtokenUtxos, mockTxDataResultsWithEtoken, mockHydratedUtxosWithNullValues, mockHydratedUtxosWithNullValuesSetToFalse, } from '../__mocks__/nullUtxoMocks'; import { missingPath1899Wallet, missingPublicKeyInPath1899Wallet, missingPublicKeyInPath145Wallet, missingPublicKeyInPath245Wallet, notLegacyWallet, } from '../__mocks__/mockLegacyWallets'; import { shortCashtabMessageInputHex, longCashtabMessageInputHex, shortExternalMessageInputHex, longExternalMessageInputHex, shortSegmentedExternalMessageInputHex, longSegmentedExternalMessageInputHex, mixedSegmentedExternalMessageInputHex, mockParsedShortCashtabMessageArray, mockParsedLongCashtabMessageArray, mockParsedShortExternalMessageArray, mockParsedLongExternalMessageArray, mockParsedShortSegmentedExternalMessageArray, mockParsedLongSegmentedExternalMessageArray, mockParsedMixedSegmentedExternalMessageArray, eTokenInputHex, mockParsedETokenOutputArray, } from '../__mocks__/mockOpReturnParsedArray'; +import { + excludedUtxoAlpha, + excludedUtxoBeta, + includedUtxoAlpha, + includedUtxoBeta, + previousUtxosObjUtxoArray, +} from '../__mocks__/incrementalUtxoMocks'; + describe('Correctly executes cash utility functions', () => { it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 2`, () => { expect(fromSmallestDenomination(1, 2)).toBe(0.01); }); it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 2`, () => { expect(fromSmallestDenomination(1000000012345678, 2)).toBe( 10000000123456.78, ); }); it(`Correctly converts smallest base unit to smallest decimal for cashDecimals = 8`, () => { expect(fromSmallestDenomination(1, 8)).toBe(0.00000001); }); it(`Correctly converts largest base unit to smallest decimal for cashDecimals = 8`, () => { expect(fromSmallestDenomination(1000000012345678, 8)).toBe( 10000000.12345678, ); }); it(`Correctly converts an array of length 10 to an array of 4 arrays, each with max length 3`, () => { expect(batchArray(unbatchedArray, 3)).toStrictEqual( arrayBatchedByThree, ); }); it(`If array length is less than batch size, return original array as first and only element of new array`, () => { expect(batchArray(unbatchedArray, 20)).toStrictEqual([unbatchedArray]); }); it(`Flattens hydrateUtxos from Promise.all() response into array that can be parsed by getSlpBalancesAndUtxos`, () => { expect( flattenBatchedHydratedUtxos(unflattenedHydrateUtxosResponse), ).toStrictEqual(flattenedHydrateUtxosResponse); }); it(`Accepts a cachedWalletState that has not preserved BigNumber object types, and returns the same wallet state with BigNumber type re-instituted`, () => { expect(loadStoredWallet(cachedUtxos)).toStrictEqual( utxosLoadedFromCache, ); }); it(`Recognizes a stored wallet as valid if it has all required fields`, () => { expect(isValidStoredWallet(validStoredWallet)).toBe(true); }); it(`Recognizes a stored wallet as invalid if it is missing required fields`, () => { expect(isValidStoredWallet(invalidStoredWallet)).toBe(false); }); it(`Converts a legacy BCH amount to an XEC amount`, () => { expect(fromLegacyDecimals(0.00000546, 2)).toStrictEqual(5.46); }); it(`Leaves a legacy BCH amount unchanged if currency.cashDecimals is 8`, () => { expect(fromLegacyDecimals(0.00000546, 8)).toStrictEqual(0.00000546); }); it(`convertToEcashPrefix converts a bitcoincash: prefixed address to an ecash: prefixed address`, () => { expect( convertToEcashPrefix( 'bitcoincash:qz2708636snqhsxu8wnlka78h6fdp77ar5ulhz04hr', ), ).toBe('ecash:qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'); }); it(`convertToEcashPrefix returns an ecash: prefix address unchanged`, () => { expect( convertToEcashPrefix( 'ecash:qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035', ), ).toBe('ecash:qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'); }); it(`toLegacyToken returns an etoken: prefix address as simpleledger:`, () => { expect( toLegacyToken('etoken:qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'), ).toBe('simpleledger:qz2708636snqhsxu8wnlka78h6fdp77ar5syue64fa'); }); it(`toLegacyToken returns an prefixless valid etoken address in simpleledger: format with prefix`, () => { expect( toLegacyToken('qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'), ).toBe('simpleledger:qz2708636snqhsxu8wnlka78h6fdp77ar5syue64fa'); }); it(`Correctly parses utxo vout tx data to confirm the transactions are not eToken txs`, () => { expect(checkNullUtxosForTokenStatus(mockTxDataResults)).toStrictEqual( mockNonEtokenUtxos, ); }); it(`Correctly parses utxo vout tx data and screens out an eToken by asm field`, () => { expect( checkNullUtxosForTokenStatus(mockTxDataResultsWithEtoken), ).toStrictEqual([]); }); it(`Changes isValid from 'null' to 'false' for confirmed nonEtokenUtxos`, () => { expect( confirmNonEtokenUtxos( mockHydratedUtxosWithNullValues, mockNonEtokenUtxos, ), ).toStrictEqual(mockHydratedUtxosWithNullValuesSetToFalse); }); it(`Recognizes a wallet with missing Path1889 is a Legacy Wallet and requires migration`, () => { expect(isLegacyMigrationRequired(missingPath1899Wallet)).toBe(true); }); it(`Recognizes a wallet with missing PublicKey in Path1889 is a Legacy Wallet and requires migration`, () => { expect( isLegacyMigrationRequired(missingPublicKeyInPath1899Wallet), ).toBe(true); }); it(`Recognizes a wallet with missing PublicKey in Path145 is a Legacy Wallet and requires migration`, () => { expect(isLegacyMigrationRequired(missingPublicKeyInPath145Wallet)).toBe( true, ); }); it(`Recognizes a wallet with missing PublicKey in Path245 is a Legacy Wallet and requires migration`, () => { expect(isLegacyMigrationRequired(missingPublicKeyInPath245Wallet)).toBe( true, ); }); it(`Recognizes a latest, current wallet that does not require migration`, () => { expect(isLegacyMigrationRequired(notLegacyWallet)).toBe(false); }); 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 () => { const result = parseOpReturn(shortCashtabMessageInputHex); expect(result).toStrictEqual(mockParsedShortCashtabMessageArray); }); test('parseOpReturn() successfully parses a long cashtab message where an additional PUSHDATA1 is present', async () => { const result = parseOpReturn(longCashtabMessageInputHex); expect(result).toStrictEqual(mockParsedLongCashtabMessageArray); }); test('parseOpReturn() successfully parses a short external message', async () => { const result = parseOpReturn(shortExternalMessageInputHex); expect(result).toStrictEqual(mockParsedShortExternalMessageArray); }); test('parseOpReturn() successfully parses a long external message where an additional PUSHDATA1 is present', async () => { const result = parseOpReturn(longExternalMessageInputHex); expect(result).toStrictEqual(mockParsedLongExternalMessageArray); }); test('parseOpReturn() successfully parses an external message that is segmented into separate short parts', async () => { const result = parseOpReturn(shortSegmentedExternalMessageInputHex); expect(result).toStrictEqual( mockParsedShortSegmentedExternalMessageArray, ); }); test('parseOpReturn() successfully parses an external message that is segmented into separate long parts', async () => { const result = parseOpReturn(longSegmentedExternalMessageInputHex); expect(result).toStrictEqual( mockParsedLongSegmentedExternalMessageArray, ); }); test('parseOpReturn() successfully parses an external message that is segmented into separate long and short parts', async () => { const result = parseOpReturn(mixedSegmentedExternalMessageInputHex); expect(result).toStrictEqual( mockParsedMixedSegmentedExternalMessageArray, ); }); test('parseOpReturn() successfully parses an eToken output', async () => { const result = parseOpReturn(eTokenInputHex); expect(result).toStrictEqual(mockParsedETokenOutputArray); }); + + test('isExcludedUtxo returns true for a utxo with different tx_pos and same txid as an existing utxo in the set', async () => { + expect( + isExcludedUtxo(excludedUtxoAlpha, previousUtxosObjUtxoArray), + ).toBe(true); + }); + test('isExcludedUtxo returns true for a utxo with different value and same txid as an existing utxo in the set', async () => { + expect( + isExcludedUtxo(excludedUtxoBeta, previousUtxosObjUtxoArray), + ).toBe(true); + }); + test('isExcludedUtxo returns false for a utxo with different tx_pos and same txid', async () => { + expect( + isExcludedUtxo(includedUtxoAlpha, previousUtxosObjUtxoArray), + ).toBe(false); + }); + test('isExcludedUtxo returns false for a utxo with different value and same txid', async () => { + expect( + isExcludedUtxo(includedUtxoBeta, previousUtxosObjUtxoArray), + ).toBe(false); + }); }); diff --git a/web/cashtab/src/utils/__tests__/validation.test.js b/web/cashtab/src/utils/__tests__/validation.test.js index f3f80a636..fc0769525 100644 --- a/web/cashtab/src/utils/__tests__/validation.test.js +++ b/web/cashtab/src/utils/__tests__/validation.test.js @@ -1,369 +1,394 @@ import { shouldRejectAmountInput, fiatToCrypto, isValidTokenName, isValidTokenTicker, isValidTokenDecimals, isValidTokenInitialQty, isValidTokenDocumentUrl, isValidTokenStats, isValidCashtabSettings, isValidXecAddress, isValidEtokenAddress, isValidXecSendAmount, isValidSendToMany, + isValidUtxo, } from '../validation'; import { currency } from '@components/Common/Ticker.js'; import { fromSmallestDenomination } from '@utils/cashMethods'; import { stStatsValid, noCovidStatsValid, noCovidStatsInvalid, cGenStatsValid, } from '../__mocks__/mockTokenStats'; +import { + validUtxo, + invalidUtxoMissingHeight, + invalidUtxoTxidUndefined, +} from '../__mocks__/incrementalUtxoMocks'; + describe('Validation utils', () => { it(`Returns 'false' if ${currency.ticker} send amount is a valid send amount`, () => { expect(shouldRejectAmountInput('10', currency.ticker, 20.0, 300)).toBe( false, ); }); it(`Returns 'false' if ${currency.ticker} send amount is a valid send amount in USD`, () => { // Here, user is trying to send $170 USD, where 1 BCHA = $20 USD, and the user has a balance of 15 BCHA or $300 expect(shouldRejectAmountInput('170', 'USD', 20.0, 15)).toBe(false); }); it(`Returns not a number if ${currency.ticker} send amount is not a number`, () => { const expectedValidationError = `Amount must be a number`; expect( shouldRejectAmountInput('Not a number', currency.ticker, 20.0, 3), ).toBe(expectedValidationError); }); it(`Returns amount must be greater than 0 if ${currency.ticker} send amount is 0`, () => { const expectedValidationError = `Amount must be greater than 0`; expect(shouldRejectAmountInput('0', currency.ticker, 20.0, 3)).toBe( expectedValidationError, ); }); it(`Returns amount must be greater than 0 if ${currency.ticker} send amount is less than 0`, () => { const expectedValidationError = `Amount must be greater than 0`; expect( shouldRejectAmountInput('-0.031', currency.ticker, 20.0, 3), ).toBe(expectedValidationError); }); it(`Returns balance error if ${currency.ticker} send amount is greater than user balance`, () => { const expectedValidationError = `Amount cannot exceed your ${currency.ticker} balance`; expect(shouldRejectAmountInput('17', currency.ticker, 20.0, 3)).toBe( expectedValidationError, ); }); it(`Returns balance error if ${currency.ticker} send amount is greater than user balance`, () => { const expectedValidationError = `Amount cannot exceed your ${currency.ticker} balance`; expect(shouldRejectAmountInput('17', currency.ticker, 20.0, 3)).toBe( expectedValidationError, ); }); it(`Returns error if ${ currency.ticker } send amount is less than ${fromSmallestDenomination( currency.dustSats, ).toString()} minimum`, () => { const expectedValidationError = `Send amount must be at least ${fromSmallestDenomination( currency.dustSats, ).toString()} ${currency.ticker}`; expect( shouldRejectAmountInput( ( fromSmallestDenomination(currency.dustSats).toString() - 0.00000001 ).toString(), currency.ticker, 20.0, 3, ), ).toBe(expectedValidationError); }); it(`Returns error if ${ currency.ticker } send amount is less than ${fromSmallestDenomination( currency.dustSats, ).toString()} minimum in fiat currency`, () => { const expectedValidationError = `Send amount must be at least ${fromSmallestDenomination( currency.dustSats, ).toString()} ${currency.ticker}`; expect( shouldRejectAmountInput('0.0000005', 'USD', 0.00005, 1000000), ).toBe(expectedValidationError); }); it(`Returns balance error if ${currency.ticker} send amount is greater than user balance with fiat currency selected`, () => { const expectedValidationError = `Amount cannot exceed your ${currency.ticker} balance`; // Here, user is trying to send $170 USD, where 1 BCHA = $20 USD, and the user has a balance of 5 BCHA or $100 expect(shouldRejectAmountInput('170', 'USD', 20.0, 5)).toBe( expectedValidationError, ); }); it(`Returns precision error if ${currency.ticker} send amount has more than ${currency.cashDecimals} decimal places`, () => { const expectedValidationError = `${currency.ticker} transactions do not support more than ${currency.cashDecimals} decimal places`; expect( shouldRejectAmountInput('17.123456789', currency.ticker, 20.0, 35), ).toBe(expectedValidationError); }); it(`Returns expected crypto amount with ${currency.cashDecimals} decimals of precision even if inputs have higher precision`, () => { expect(fiatToCrypto('10.97231694823432', 20.3231342349234234, 8)).toBe( '0.53989295', ); }); it(`Returns expected crypto amount with ${currency.cashDecimals} decimals of precision even if inputs have higher precision`, () => { expect(fiatToCrypto('10.97231694823432', 20.3231342349234234, 2)).toBe( '0.54', ); }); it(`Returns expected crypto amount with ${currency.cashDecimals} decimals of precision even if inputs have lower precision`, () => { expect(fiatToCrypto('10.94', 10, 8)).toBe('1.09400000'); }); it(`Accepts a valid ${currency.tokenTicker} token name`, () => { expect(isValidTokenName('Valid token name')).toBe(true); }); it(`Accepts a valid ${currency.tokenTicker} token name that is a stringified number`, () => { expect(isValidTokenName('123456789')).toBe(true); }); it(`Rejects ${currency.tokenTicker} token name if longer than 68 characters`, () => { expect( isValidTokenName( 'This token name is not valid because it is longer than 68 characters which is really pretty long for a token name when you think about it and all', ), ).toBe(false); }); it(`Rejects ${currency.tokenTicker} token name if empty string`, () => { expect(isValidTokenName('')).toBe(false); }); it(`Accepts a 4-char ${currency.tokenTicker} token ticker`, () => { expect(isValidTokenTicker('DOGE')).toBe(true); }); it(`Accepts a 12-char ${currency.tokenTicker} token ticker`, () => { expect(isValidTokenTicker('123456789123')).toBe(true); }); it(`Rejects ${currency.tokenTicker} token ticker if empty string`, () => { expect(isValidTokenTicker('')).toBe(false); }); it(`Rejects ${currency.tokenTicker} token ticker if > 12 chars`, () => { expect(isValidTokenTicker('1234567891234')).toBe(false); }); it(`Accepts ${currency.tokenDecimals} if zero`, () => { expect(isValidTokenDecimals('0')).toBe(true); }); it(`Accepts ${currency.tokenDecimals} if between 0 and 9 inclusive`, () => { expect(isValidTokenDecimals('9')).toBe(true); }); it(`Rejects ${currency.tokenDecimals} if empty string`, () => { expect(isValidTokenDecimals('')).toBe(false); }); it(`Rejects ${currency.tokenDecimals} if non-integer`, () => { expect(isValidTokenDecimals('1.7')).toBe(false); }); it(`Accepts ${currency.tokenDecimals} initial genesis quantity at minimum amount for 3 decimal places`, () => { expect(isValidTokenInitialQty('0.001', '3')).toBe(true); }); it(`Accepts ${currency.tokenDecimals} initial genesis quantity at minimum amount for 9 decimal places`, () => { expect(isValidTokenInitialQty('0.000000001', '9')).toBe(true); }); it(`Accepts ${currency.tokenDecimals} initial genesis quantity at amount below 100 billion`, () => { expect(isValidTokenInitialQty('1000', '0')).toBe(true); }); it(`Accepts highest possible ${currency.tokenDecimals} initial genesis quantity at amount below 100 billion`, () => { expect(isValidTokenInitialQty('99999999999.999999999', '9')).toBe(true); }); it(`Accepts ${currency.tokenDecimals} initial genesis quantity if decimal places equal tokenDecimals`, () => { expect(isValidTokenInitialQty('0.123', '3')).toBe(true); }); it(`Accepts ${currency.tokenDecimals} initial genesis quantity if decimal places are less than tokenDecimals`, () => { expect(isValidTokenInitialQty('0.12345', '9')).toBe(true); }); it(`Rejects ${currency.tokenDecimals} initial genesis quantity of zero`, () => { expect(isValidTokenInitialQty('0', '9')).toBe(false); }); it(`Rejects ${currency.tokenDecimals} initial genesis quantity if tokenDecimals is not valid`, () => { expect(isValidTokenInitialQty('0', '')).toBe(false); }); it(`Rejects ${currency.tokenDecimals} initial genesis quantity if 100 billion or higher`, () => { expect(isValidTokenInitialQty('100000000000', '0')).toBe(false); }); it(`Rejects ${currency.tokenDecimals} initial genesis quantity if it has more decimal places than tokenDecimals`, () => { expect(isValidTokenInitialQty('1.5', '0')).toBe(false); }); it(`Accepts a valid ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('cashtabapp.com')).toBe(true); }); it(`Accepts a valid ${currency.tokenTicker} token document URL including special URL characters`, () => { expect(isValidTokenDocumentUrl('https://cashtabapp.com/')).toBe(true); }); it(`Accepts a blank string as a valid ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('')).toBe(true); }); it(`Rejects ${currency.tokenTicker} token name if longer than 68 characters`, () => { expect( isValidTokenDocumentUrl( 'http://www.ThisTokenDocumentUrlIsActuallyMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchTooLong.com/', ), ).toBe(false); }); it(`Accepts a domain input with https protocol as ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('https://google.com')).toBe(true); }); it(`Accepts a domain input with http protocol as ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('http://test.com')).toBe(true); }); it(`Accepts a domain input with a primary and secondary top level domain as ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('http://test.co.uk')).toBe(true); }); it(`Accepts a domain input with just a subdomain as ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('www.test.co.uk')).toBe(true); }); it(`Rejects a domain input with no top level domain, protocol or subdomain ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl('mywebsite')).toBe(false); }); it(`Rejects a domain input as numbers ${currency.tokenTicker} token document URL`, () => { expect(isValidTokenDocumentUrl(12345)).toBe(false); }); it(`Correctly validates token stats for token created before the ${currency.ticker} fork`, () => { expect(isValidTokenStats(stStatsValid)).toBe(true); }); it(`Correctly validates token stats for token created after the ${currency.ticker} fork`, () => { expect(isValidTokenStats(noCovidStatsValid)).toBe(true); }); it(`Correctly validates token stats for token with no minting baton`, () => { expect(isValidTokenStats(cGenStatsValid)).toBe(true); }); it(`Recognizes a token stats object with missing required keys as invalid`, () => { expect(isValidTokenStats(noCovidStatsInvalid)).toBe(false); }); it(`Recognizes a valid cashtab settings object`, () => { expect(isValidCashtabSettings({ fiatCurrency: 'usd' })).toBe(true); }); it(`Rejects a cashtab settings object for an unsupported currency`, () => { expect(isValidCashtabSettings({ fiatCurrency: 'xau' })).toBe(false); }); it(`Rejects a corrupted cashtab settings object for an unsupported currency`, () => { expect(isValidCashtabSettings({ fiatCurrencyWrongLabel: 'usd' })).toBe( false, ); }); it(`isValidXecAddress correctly validates a valid XEC address with ecash: prefix`, () => { const addr = 'ecash:qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'; expect(isValidXecAddress(addr)).toBe(true); }); it(`isValidXecAddress correctly validates a valid XEC address without ecash: prefix`, () => { const addr = 'qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'; expect(isValidXecAddress(addr)).toBe(true); }); it(`isValidXecAddress rejects a valid legacy address`, () => { const addr = '1Efd9z9GRVJK2r73nUpFmBnsKUmfXNm2y2'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a valid bitcoincash: address`, () => { const addr = 'bitcoincash:qz2708636snqhsxu8wnlka78h6fdp77ar5ulhz04hr'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a valid etoken: address with prefix`, () => { const addr = 'etoken:qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a valid etoken: address without prefix`, () => { const addr = 'qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a valid simpleledger: address with prefix`, () => { const addr = 'simpleledger:qrujw0wrzncyxw8q3d0xkfet4jafrqhk6csev0v6y3'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a valid simpleledger: address without prefix`, () => { const addr = 'qrujw0wrzncyxw8q3d0xkfet4jafrqhk6csev0v6y3'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects an invalid address`, () => { const addr = 'wtf is this definitely not an address'; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects a null input`, () => { const addr = null; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidXecAddress rejects an empty string input`, () => { const addr = ''; expect(isValidXecAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a valid XEC address with ecash: prefix`, () => { const addr = 'ecash:qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a valid XEC address without ecash: prefix`, () => { const addr = 'qz2708636snqhsxu8wnlka78h6fdp77ar59jrf5035'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a valid legacy address`, () => { const addr = '1Efd9z9GRVJK2r73nUpFmBnsKUmfXNm2y2'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a valid bitcoincash: address`, () => { const addr = 'bitcoincash:qz2708636snqhsxu8wnlka78h6fdp77ar5ulhz04hr'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress correctly validates a valid etoken: address with prefix`, () => { const addr = 'etoken:qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'; expect(isValidEtokenAddress(addr)).toBe(true); }); it(`isValidEtokenAddress correctly validates a valid etoken: address without prefix`, () => { const addr = 'qz2708636snqhsxu8wnlka78h6fdp77ar5tv2tzg4r'; expect(isValidEtokenAddress(addr)).toBe(true); }); it(`isValidEtokenAddress rejects a valid simpleledger: address with prefix`, () => { const addr = 'simpleledger:qrujw0wrzncyxw8q3d0xkfet4jafrqhk6csev0v6y3'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a valid simpleledger: address without prefix`, () => { const addr = 'qrujw0wrzncyxw8q3d0xkfet4jafrqhk6csev0v6y3'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects an invalid address`, () => { const addr = 'wtf is this definitely not an address'; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects a null input`, () => { const addr = null; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidEtokenAddress rejects an empty string input`, () => { const addr = ''; expect(isValidEtokenAddress(addr)).toBe(false); }); it(`isValidXecSendAmount accepts the dust minimum`, () => { const testXecSendAmount = fromSmallestDenomination(currency.dustSats); expect(isValidXecSendAmount(testXecSendAmount)).toBe(true); }); it(`isValidXecSendAmount accepts arbitrary number above dust minimum`, () => { const testXecSendAmount = fromSmallestDenomination(currency.dustSats) + 1.75; expect(isValidXecSendAmount(testXecSendAmount)).toBe(true); }); it(`isValidXecSendAmount rejects zero`, () => { const testXecSendAmount = 0; expect(isValidXecSendAmount(testXecSendAmount)).toBe(false); }); it(`isValidXecSendAmount rejects a non-number string`, () => { const testXecSendAmount = 'not a number'; expect(isValidXecSendAmount(testXecSendAmount)).toBe(false); }); it(`isValidXecSendAmount accepts arbitrary number above dust minimum as a string`, () => { const testXecSendAmount = `${ fromSmallestDenomination(currency.dustSats) + 1.75 }`; expect(isValidXecSendAmount(testXecSendAmount)).toBe(true); }); it(`isValidXecSendAmount rejects null`, () => { const testXecSendAmount = null; expect(isValidXecSendAmount(testXecSendAmount)).toBe(false); }); it(`isValidXecSendAmount rejects undefined`, () => { const testXecSendAmount = undefined; expect(isValidXecSendAmount(testXecSendAmount)).toBe(false); }); + it(`isValidUtxo returns true for a valid utxo`, () => { + expect(isValidUtxo(validUtxo)).toBe(true); + }); + it(`isValidUtxo returns false for missing height`, () => { + expect(isValidUtxo(invalidUtxoMissingHeight)).toBe(false); + }); + it(`isValidUtxo returns false for undefined tx_hash`, () => { + expect(isValidUtxo(invalidUtxoTxidUndefined)).toBe(false); + }); + it(`isValidUtxo returns false for null`, () => { + expect(isValidUtxo(null)).toBe(false); + }); + it(`isValidUtxo returns false for undefined`, () => { + expect(isValidUtxo()).toBe(false); + }); + it(`isValidUtxo returns false for empty object`, () => { + expect(isValidUtxo({})).toBe(false); + }); }); diff --git a/web/cashtab/src/utils/cashMethods.js b/web/cashtab/src/utils/cashMethods.js index 09cda54d7..9ff344663 100644 --- a/web/cashtab/src/utils/cashMethods.js +++ b/web/cashtab/src/utils/cashMethods.js @@ -1,489 +1,529 @@ import { currency } from '@components/Common/Ticker'; -import { isValidXecAddress, isValidEtokenAddress } from '@utils/validation'; +import { + isValidXecAddress, + isValidEtokenAddress, + isValidUtxo, +} from '@utils/validation'; import BigNumber from 'bignumber.js'; import cashaddr from 'ecashaddrjs'; export function parseOpReturn(hexStr) { if ( !hexStr || typeof hexStr !== 'string' || hexStr.substring(0, 2) !== currency.opReturn.opReturnPrefixHex ) { return false; } hexStr = hexStr.slice(2); // remove the first byte i.e. 6a /* * @Return: resultArray is structured as follows: * resultArray[0] is the transaction type i.e. eToken prefix, cashtab prefix, external message itself if unrecognized prefix * resultArray[1] is the actual cashtab message or the 2nd part of an external message * resultArray[2 - n] are the additional messages for future protcols */ let resultArray = []; let message = ''; let hexStrLength = hexStr.length; for (let i = 0; hexStrLength !== 0; i++) { // part 1: check the preceding byte value for the subsequent message let byteValue = hexStr.substring(0, 2); let msgByteSize = 0; if (byteValue === currency.opReturn.opPushDataOne) { // if this byte is 4c then the next byte is the message byte size - retrieve the message byte size only msgByteSize = parseInt(hexStr.substring(2, 4), 16); // hex base 16 to decimal base 10 hexStr = hexStr.slice(4); // strip the 4c + message byte size info } else { // take the byte as the message byte size msgByteSize = parseInt(hexStr.substring(0, 2), 16); // hex base 16 to decimal base 10 hexStr = hexStr.slice(2); // strip the message byte size info } // part 2: parse the subsequent message based on bytesize const msgCharLength = 2 * msgByteSize; message = hexStr.substring(0, msgCharLength); if (i === 0 && message === currency.opReturn.appPrefixesHex.eToken) { // add the extracted eToken prefix to array then exit loop resultArray[i] = currency.opReturn.appPrefixesHex.eToken; break; } else if ( i === 0 && message === currency.opReturn.appPrefixesHex.cashtab ) { // add the extracted Cashtab prefix to array resultArray[i] = currency.opReturn.appPrefixesHex.cashtab; } else if ( i === 0 && message === currency.opReturn.appPrefixesHex.cashtabEncrypted ) { // add the Cashtab encryption prefix to array resultArray[i] = currency.opReturn.appPrefixesHex.cashtabEncrypted; } else { // this is either an external message or a subsequent cashtab message loop to extract the message resultArray[i] = message; } // strip out the parsed message hexStr = hexStr.slice(msgCharLength); hexStrLength = hexStr.length; } return resultArray; } export const fromLegacyDecimals = ( amount, cashDecimals = currency.cashDecimals, ) => { // Input 0.00000546 BCH // Output 5.46 XEC or 0.00000546 BCH, depending on currency.cashDecimals const amountBig = new BigNumber(amount); const conversionFactor = new BigNumber(10 ** (8 - cashDecimals)); const amountSmallestDenomination = amountBig .times(conversionFactor) .toNumber(); return amountSmallestDenomination; }; export const fromSmallestDenomination = ( amount, cashDecimals = currency.cashDecimals, ) => { const amountBig = new BigNumber(amount); const multiplier = new BigNumber(10 ** (-1 * cashDecimals)); const amountInBaseUnits = amountBig.times(multiplier); return amountInBaseUnits.toNumber(); }; export const toSmallestDenomination = ( sendAmount, cashDecimals = currency.cashDecimals, ) => { // Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places // Example, for an 8 decimal place currency like Bitcoin // Input: a BigNumber of the amount of Bitcoin to be sent // Output: a BigNumber of the amount of satoshis to be sent, or false if input is invalid // Validate // Input should be a BigNumber with no more decimal places than cashDecimals const isValidSendAmount = BigNumber.isBigNumber(sendAmount) && sendAmount.dp() <= cashDecimals; if (!isValidSendAmount) { return false; } const conversionFactor = new BigNumber(10 ** cashDecimals); const sendAmountSmallestDenomination = sendAmount.times(conversionFactor); return sendAmountSmallestDenomination; }; export const batchArray = (inputArray, batchSize) => { // take an array of n elements, return an array of arrays each of length batchSize const batchedArray = []; for (let i = 0; i < inputArray.length; i += batchSize) { const tempArray = inputArray.slice(i, i + batchSize); batchedArray.push(tempArray); } return batchedArray; }; export const flattenBatchedHydratedUtxos = batchedHydratedUtxoDetails => { // Return same result as if only the bulk API call were made // to do this, just need to move all utxos under one slpUtxos /* given [ { slpUtxos: [ { utxos: [], address: '', } ], }, { slpUtxos: [ { utxos: [], address: '', } ], } ] return [ { slpUtxos: [ { utxos: [], address: '' }, { utxos: [], address: '' }, ] } */ const flattenedBatchedHydratedUtxos = { slpUtxos: [] }; for (let i = 0; i < batchedHydratedUtxoDetails.length; i += 1) { const theseSlpUtxos = batchedHydratedUtxoDetails[i].slpUtxos[0]; flattenedBatchedHydratedUtxos.slpUtxos.push(theseSlpUtxos); } return flattenedBatchedHydratedUtxos; }; export const loadStoredWallet = walletStateFromStorage => { // Accept cached tokens array that does not save BigNumber type of BigNumbers // Return array with BigNumbers converted // See BigNumber.js api for how to create a BigNumber object from an object // https://mikemcl.github.io/bignumber.js/ const liveWalletState = walletStateFromStorage; const { slpBalancesAndUtxos, tokens } = liveWalletState; for (let i = 0; i < tokens.length; i += 1) { const thisTokenBalance = tokens[i].balance; thisTokenBalance._isBigNumber = true; tokens[i].balance = new BigNumber(thisTokenBalance); } // Also confirm balance is correct // Necessary step in case currency.decimals changed since last startup const balancesRebased = normalizeBalance(slpBalancesAndUtxos); liveWalletState.balances = balancesRebased; return liveWalletState; }; export const normalizeBalance = slpBalancesAndUtxos => { const totalBalanceInSatoshis = slpBalancesAndUtxos.nonSlpUtxos.reduce( (previousBalance, utxo) => previousBalance + utxo.value, 0, ); return { totalBalanceInSatoshis, totalBalance: fromSmallestDenomination(totalBalanceInSatoshis), }; }; export const isValidStoredWallet = walletStateFromStorage => { return ( typeof walletStateFromStorage === 'object' && 'state' in walletStateFromStorage && typeof walletStateFromStorage.state === 'object' && 'balances' in walletStateFromStorage.state && 'utxos' in walletStateFromStorage.state && 'hydratedUtxoDetails' in walletStateFromStorage.state && 'slpBalancesAndUtxos' in walletStateFromStorage.state && 'tokens' in walletStateFromStorage.state ); }; export const getWalletState = wallet => { if (!wallet || !wallet.state) { return { balances: { totalBalance: 0, totalBalanceInSatoshis: 0 }, hydratedUtxoDetails: {}, tokens: [], slpBalancesAndUtxos: {}, parsedTxHistory: [], utxos: [], }; } return wallet.state; }; export function convertToEcashPrefix(bitcoincashPrefixedAddress) { // Prefix-less addresses may be valid, but the cashaddr.decode function used below // will throw an error without a prefix. Hence, must ensure prefix to use that function. const hasPrefix = bitcoincashPrefixedAddress.includes(':'); if (hasPrefix) { // Is it bitcoincash: or simpleledger: const { type, hash, prefix } = cashaddr.decode( bitcoincashPrefixedAddress, ); let newPrefix; if (prefix === 'bitcoincash') { newPrefix = 'ecash'; } else if (prefix === 'simpleledger') { newPrefix = 'etoken'; } else { return bitcoincashPrefixedAddress; } const convertedAddress = cashaddr.encode(newPrefix, type, hash); return convertedAddress; } else { return bitcoincashPrefixedAddress; } } export function toLegacyCash(addr) { // Confirm input is a valid ecash address const isValidInput = isValidXecAddress(addr); if (!isValidInput) { return new Error(`${addr} is not a valid ecash address`); } // Check for ecash: prefix const isPrefixedXecAddress = addr.slice(0, 6) === 'ecash:'; // If no prefix, assume it is checksummed for an ecash: prefix const testedXecAddr = isPrefixedXecAddress ? addr : `ecash:${addr}`; let legacyCashAddress; try { const { type, hash } = cashaddr.decode(testedXecAddr); legacyCashAddress = cashaddr.encode(currency.legacyPrefix, type, hash); } catch (err) { return err; } return legacyCashAddress; } export function toLegacyCashArray(addressArray) { let cleanArray = []; // array of bch converted addresses to be returned if ( addressArray === null || addressArray === undefined || !addressArray.length || addressArray === '' ) { return new Error('Invalid addressArray input'); } const arrayLength = addressArray.length; for (let i = 0; i < arrayLength; i++) { let addressValueArr = addressArray[i].split(','); let address = addressValueArr[0]; let value = addressValueArr[1]; // NB that toLegacyCash() includes address validation; will throw error for invalid address input const legacyAddress = toLegacyCash(address); if (legacyAddress instanceof Error) { return legacyAddress; } let convertedArrayData = legacyAddress + ',' + value + '\n'; cleanArray.push(convertedArrayData); } return cleanArray; } export function toLegacyToken(addr) { // Confirm input is a valid ecash address const isValidInput = isValidEtokenAddress(addr); if (!isValidInput) { return new Error(`${addr} is not a valid etoken address`); } // Check for ecash: prefix const isPrefixedEtokenAddress = addr.slice(0, 7) === 'etoken:'; // If no prefix, assume it is checksummed for an ecash: prefix const testedEtokenAddr = isPrefixedEtokenAddress ? addr : `etoken:${addr}`; let legacyTokenAddress; try { const { type, hash } = cashaddr.decode(testedEtokenAddr); legacyTokenAddress = cashaddr.encode('simpleledger', type, hash); } catch (err) { return err; } return legacyTokenAddress; } export const confirmNonEtokenUtxos = (hydratedUtxos, nonEtokenUtxos) => { // scan through hydratedUtxoDetails for (let i = 0; i < hydratedUtxos.length; i += 1) { // Find utxos with txids matching nonEtokenUtxos if (nonEtokenUtxos.includes(hydratedUtxos[i].txid)) { // Confirm that such utxos are not eToken utxos hydratedUtxos[i].isValid = false; } } return hydratedUtxos; }; export const checkNullUtxosForTokenStatus = txDataResults => { const nonEtokenUtxos = []; for (let j = 0; j < txDataResults.length; j += 1) { const thisUtxoTxid = txDataResults[j].txid; const thisUtxoVout = txDataResults[j].details.vout; // Iterate over outputs for (let k = 0; k < thisUtxoVout.length; k += 1) { const thisOutput = thisUtxoVout[k]; if (thisOutput.scriptPubKey.type === 'nulldata') { const asmOutput = thisOutput.scriptPubKey.asm; if (asmOutput.includes('OP_RETURN 5262419')) { // then it's an eToken tx that has not been properly validated // Do not include it in nonEtokenUtxos // App will ignore it until SLPDB is able to validate it /* console.log( `utxo ${thisUtxoTxid} requires further eToken validation, ignoring`, );*/ } else { // Otherwise it's just an OP_RETURN tx that SLPDB has some issue with // It should still be in the user's utxo set // Include it in nonEtokenUtxos /* console.log( `utxo ${thisUtxoTxid} is not an eToken tx, adding to nonSlpUtxos`, ); */ nonEtokenUtxos.push(thisUtxoTxid); } } } } return nonEtokenUtxos; }; /* Converts a serialized buffer containing encrypted data into an object * that can be interpreted by the ecies-lite library. * * For reference on the parsing logic in this function refer to the link below on the segment of * ecies-lite's encryption function where the encKey, macKey, iv and cipher are sliced and concatenated * https://github.com/tibetty/ecies-lite/blob/8fd97e80b443422269d0223ead55802378521679/index.js#L46-L55 * * A similar PSF implmentation can also be found at: * https://github.com/Permissionless-Software-Foundation/bch-encrypt-lib/blob/master/lib/encryption.js * * For more detailed overview on the ecies encryption scheme, see https://cryptobook.nakov.com/asymmetric-key-ciphers/ecies-public-key-encryption */ export const convertToEncryptStruct = encryptionBuffer => { // based on ecies-lite's encryption logic, the encryption buffer is concatenated as follows: // [ epk + iv + ct + mac ] whereby: // - The first 32 or 64 chars of the encryptionBuffer is the epk // - Both iv and ct params are 16 chars each, hence their combined substring is 32 chars from the end of the epk string // - within this combined iv/ct substring, the first 16 chars is the iv param, and ct param being the later half // - The mac param is appended to the end of the encryption buffer // validate input buffer if (!encryptionBuffer) { throw new Error( 'cashmethods.convertToEncryptStruct() error: input must be a buffer', ); } try { // variable tracking the starting char position for string extraction purposes let startOfBuf = 0; // *** epk param extraction *** // The first char of the encryptionBuffer indicates the type of the public key // If the first char is 4, then the public key is 64 chars // If the first char is 3 or 2, then the public key is 32 chars // Otherwise this is not a valid encryption buffer compatible with the ecies-lite library let publicKey; switch (encryptionBuffer[0]) { case 4: publicKey = encryptionBuffer.slice(0, 65); // extract first 64 chars as public key break; case 3: case 2: publicKey = encryptionBuffer.slice(0, 33); // extract first 32 chars as public key break; default: throw new Error(`Invalid type: ${encryptionBuffer[0]}`); } // *** iv and ct param extraction *** startOfBuf += publicKey.length; // sets the starting char position to the end of the public key (epk) in order to extract subsequent iv and ct substrings const encryptionTagLength = 32; // the length of the encryption tag (i.e. mac param) computed from each block of ciphertext, and is used to verify no one has tampered with the encrypted data const ivCtSubstring = encryptionBuffer.slice( startOfBuf, encryptionBuffer.length - encryptionTagLength, ); // extract the substring containing both iv and ct params, which is after the public key but before the mac param i.e. the 'encryption tag' const ivbufParam = ivCtSubstring.slice(0, 16); // extract the first 16 chars of substring as the iv param const ctbufParam = ivCtSubstring.slice(16); // extract the last 16 chars as substring the ct param // *** mac param extraction *** const macParam = encryptionBuffer.slice( encryptionBuffer.length - encryptionTagLength, encryptionBuffer.length, ); // extract the mac param appended to the end of the buffer return { iv: ivbufParam, epk: publicKey, ct: ctbufParam, mac: macParam, }; } catch (err) { console.error(`useBCH.convertToEncryptStruct() error: `, err); throw err; } }; export const getPublicKey = async (BCH, address) => { try { const publicKey = await BCH.encryption.getPubKey(address); return publicKey.publicKey; } catch (err) { if (err['error'] === 'No transaction history.') { throw new Error( 'Cannot send an encrypted message to a wallet with no outgoing transactions', ); } else { throw err; } } }; export const isLegacyMigrationRequired = wallet => { // If the wallet does not have Path1899, // Or each Path1899, Path145, Path245 does not have a public key // Then it requires migration if ( !wallet.Path1899 || !wallet.Path1899.publicKey || !wallet.Path145.publicKey || !wallet.Path245.publicKey ) { return true; } return false; }; + +export const isExcludedUtxo = (utxo, utxoArray) => { + /* + utxo is a single utxo of model + { + height: 724992 + tx_hash: "8d4bdedb7c4443412e0c2f316a330863aef54d9ba73560ca60cca6408527b247" + tx_pos: 0 + value: 10200 + } + + utxoArray is an array of utxos + */ + let isExcludedUtxo = true; + const { tx_hash, tx_pos, value } = utxo; + for (let i = 0; i < utxoArray.length; i += 1) { + const thisUtxo = utxoArray[i]; + // NOTE + // You can't match height, as this changes from 0 to blockheight after confirmation + //const thisUtxoHeight = thisUtxo.height; + const thisUtxoTxid = thisUtxo.tx_hash; + const thisUtxoTxPos = thisUtxo.tx_pos; + const thisUtxoValue = thisUtxo.value; + // If you find a utxo such that each object key is identical + if ( + tx_hash === thisUtxoTxid && + tx_pos === thisUtxoTxPos && + value === thisUtxoValue + ) { + // Then this utxo is not excluded from the array + isExcludedUtxo = false; + } + } + + return isExcludedUtxo; +}; diff --git a/web/cashtab/src/utils/validation.js b/web/cashtab/src/utils/validation.js index 010c39786..8bc966630 100644 --- a/web/cashtab/src/utils/validation.js +++ b/web/cashtab/src/utils/validation.js @@ -1,235 +1,253 @@ import BigNumber from 'bignumber.js'; import { currency } from '@components/Common/Ticker.js'; import { fromSmallestDenomination } from '@utils/cashMethods'; import cashaddr from 'ecashaddrjs'; // Validate cash amount export const shouldRejectAmountInput = ( cashAmount, selectedCurrency, fiatPrice, totalCashBalance, ) => { // Take cashAmount as input, a string from form input let error = false; let testedAmount = new BigNumber(cashAmount); if (selectedCurrency !== currency.ticker) { // Ensure no more than currency.cashDecimals decimal places testedAmount = new BigNumber(fiatToCrypto(cashAmount, fiatPrice)); } // Validate value for > 0 if (isNaN(testedAmount)) { error = 'Amount must be a number'; } else if (testedAmount.lte(0)) { error = 'Amount must be greater than 0'; } else if ( testedAmount.lt(fromSmallestDenomination(currency.dustSats).toString()) ) { error = `Send amount must be at least ${fromSmallestDenomination( currency.dustSats, ).toString()} ${currency.ticker}`; } else if (testedAmount.gt(totalCashBalance)) { error = `Amount cannot exceed your ${currency.ticker} balance`; } else if (!isNaN(testedAmount) && testedAmount.toString().includes('.')) { if ( testedAmount.toString().split('.')[1].length > currency.cashDecimals ) { error = `${currency.ticker} transactions do not support more than ${currency.cashDecimals} decimal places`; } } // return false if no error, or string error msg if error return error; }; export const fiatToCrypto = ( fiatAmount, fiatPrice, cashDecimals = currency.cashDecimals, ) => { let cryptoAmount = new BigNumber(fiatAmount) .div(new BigNumber(fiatPrice)) .toFixed(cashDecimals); return cryptoAmount; }; export const isValidTokenName = tokenName => { return ( typeof tokenName === 'string' && tokenName.length > 0 && tokenName.length < 68 ); }; export const isValidTokenTicker = tokenTicker => { return ( typeof tokenTicker === 'string' && tokenTicker.length > 0 && tokenTicker.length < 13 ); }; export const isValidTokenDecimals = tokenDecimals => { return ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes( tokenDecimals, ); }; export const isValidTokenInitialQty = (tokenInitialQty, tokenDecimals) => { const minimumQty = new BigNumber(1 / 10 ** tokenDecimals); const tokenIntialQtyBig = new BigNumber(tokenInitialQty); return ( tokenIntialQtyBig.gte(minimumQty) && tokenIntialQtyBig.lt(100000000000) && tokenIntialQtyBig.dp() <= tokenDecimals ); }; export const isValidTokenDocumentUrl = tokenDocumentUrl => { const urlPattern = new RegExp( '^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string '(\\#[-a-z\\d_]*)?$', 'i', ); // fragment locator const urlTestResult = urlPattern.test(tokenDocumentUrl); return ( tokenDocumentUrl === '' || (typeof tokenDocumentUrl === 'string' && tokenDocumentUrl.length >= 0 && tokenDocumentUrl.length < 68 && urlTestResult) ); }; export const isValidTokenStats = tokenStats => { return ( typeof tokenStats === 'object' && 'timestampUnix' in tokenStats && 'documentUri' in tokenStats && 'containsBaton' in tokenStats && 'initialTokenQty' in tokenStats && 'totalMinted' in tokenStats && 'totalBurned' in tokenStats && 'circulatingSupply' in tokenStats ); }; export const isValidCashtabSettings = settings => { try { const isValid = typeof settings === 'object' && Object.prototype.hasOwnProperty.call(settings, 'fiatCurrency') && currency.settingsValidation.fiatCurrency.includes( settings.fiatCurrency, ); return isValid; } catch (err) { return false; } }; export const isValidXecAddress = addr => { /* Returns true for a valid XEC address Valid XEC address: - May or may not have prefix `ecash:` - Checksum must validate for prefix `ecash:` An eToken address is not considered a valid XEC address */ if (!addr) { return false; } let isValidXecAddress; let isPrefixedXecAddress; // Check for possible prefix if (addr.includes(':')) { // Test for 'ecash:' prefix isPrefixedXecAddress = addr.slice(0, 6) === 'ecash:'; // Any address including ':' that doesn't start explicitly with 'ecash:' is invalid if (!isPrefixedXecAddress) { isValidXecAddress = false; return isValidXecAddress; } } else { isPrefixedXecAddress = false; } // If no prefix, assume it is checksummed for an ecash: prefix const testedXecAddr = isPrefixedXecAddress ? addr : `ecash:${addr}`; try { const decoded = cashaddr.decode(testedXecAddr); if (decoded.prefix === 'ecash') { isValidXecAddress = true; } } catch (err) { isValidXecAddress = false; } return isValidXecAddress; }; export const isValidEtokenAddress = addr => { /* Returns true for a valid eToken address Valid eToken address: - May or may not have prefix `etoken:` - Checksum must validate for prefix `etoken:` An XEC address is not considered a valid eToken address */ if (!addr) { return false; } let isValidEtokenAddress; let isPrefixedEtokenAddress; // Check for possible prefix if (addr.includes(':')) { // Test for 'etoken:' prefix isPrefixedEtokenAddress = addr.slice(0, 7) === 'etoken:'; // Any token address including ':' that doesn't start explicitly with 'etoken:' is invalid if (!isPrefixedEtokenAddress) { isValidEtokenAddress = false; return isValidEtokenAddress; } } else { isPrefixedEtokenAddress = false; } // If no prefix, assume it is checksummed for an etoken: prefix const testedEtokenAddr = isPrefixedEtokenAddress ? addr : `etoken:${addr}`; try { const decoded = cashaddr.decode(testedEtokenAddr); if (decoded.prefix === 'etoken') { isValidEtokenAddress = true; } } catch (err) { isValidEtokenAddress = false; } return isValidEtokenAddress; }; export const isValidXecSendAmount = xecSendAmount => { // A valid XEC send amount must be a number higher than the app dust limit return ( xecSendAmount !== null && typeof xecSendAmount !== 'undefined' && !isNaN(parseFloat(xecSendAmount)) && parseFloat(xecSendAmount) >= fromSmallestDenomination(currency.dustSats) ); }; + +export const isValidUtxo = utxo => { + let isValidUtxo = false; + try { + isValidUtxo = + 'height' in utxo && + typeof utxo.height === 'number' && + 'tx_hash' in utxo && + typeof utxo.tx_hash === 'string' && + 'tx_pos' in utxo && + typeof utxo.tx_pos === 'number' && + 'value' in utxo && + typeof utxo.value === 'number'; + } catch (err) { + return false; + } + return isValidUtxo; +};