From 2f5650ce307aee8f74e1bdaae3b7b03800357794 Mon Sep 17 00:00:00 2001 From: Likho Date: Thu, 15 Sep 2022 21:41:12 +0200 Subject: [PATCH] WIP: BCH and NMC tests --- .../coins/bitcoincash/bitcoincash_wallet.dart | 8 +- .../coins/namecoin/namecoin_wallet.dart | 2 + .../bitcoincash_history_sample_data.dart | 120 + .../bitcoincash/bitcoincash_wallet_test.dart | 59 +- .../bitcoincash_wallet_test_parameters.dart | 13 + .../namecoin_history_sample_data.dart | 95 + .../namecoin_transaction_data_samples.dart | 355 ++ .../namecoin/namecoin_utxo_sample_data.dart | 58 + .../coins/namecoin/namecoin_wallet_test.dart | 4448 +++++++++++++++++ .../namecoin/namecoin_wallet_test.mocks.dart | 352 ++ .../namecoin_wallet_test_parameters.dart | 0 11 files changed, 5484 insertions(+), 26 deletions(-) create mode 100644 test/services/coins/bitcoincash/bitcoincash_wallet_test_parameters.dart create mode 100644 test/services/coins/namecoin/namecoin_history_sample_data.dart create mode 100644 test/services/coins/namecoin/namecoin_transaction_data_samples.dart create mode 100644 test/services/coins/namecoin/namecoin_utxo_sample_data.dart create mode 100644 test/services/coins/namecoin/namecoin_wallet_test.dart create mode 100644 test/services/coins/namecoin/namecoin_wallet_test.mocks.dart create mode 100644 test/services/coins/namecoin/namecoin_wallet_test_parameters.dart diff --git a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart index e5da66fc8..89f44ad11 100644 --- a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart +++ b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart @@ -1506,6 +1506,7 @@ class BitcoinCashWallet extends CoinServiceAPI { batches[batchNumber] = {}; } final scripthash = _convertToScriptHash(allAddresses[i], _network); + print("SCRIPT_HASH_FOR_ADDRESS ${allAddresses[i]} IS $scripthash"); batches[batchNumber]!.addAll({ scripthash: [scripthash] }); @@ -1681,15 +1682,20 @@ class BitcoinCashWallet extends CoinServiceAPI { }) async { try { final Map> args = {}; + print("Address $addresses"); for (final entry in addresses.entries) { args[entry.key] = [_convertToScriptHash(entry.value, _network)]; } - final response = await electrumXClient.getBatchHistory(args: args); + print("Args ${jsonEncode(args)}"); + + final response = await electrumXClient.getBatchHistory(args: args); + print("Response ${jsonEncode(response)}"); final Map result = {}; for (final entry in response.entries) { result[entry.key] = entry.value.length; } + print("result ${jsonEncode(result)}"); return result; } catch (e, s) { Logging.instance.log( diff --git a/lib/services/coins/namecoin/namecoin_wallet.dart b/lib/services/coins/namecoin/namecoin_wallet.dart index 7ed8d6bfb..379fda4e2 100644 --- a/lib/services/coins/namecoin/namecoin_wallet.dart +++ b/lib/services/coins/namecoin/namecoin_wallet.dart @@ -1858,6 +1858,8 @@ class NamecoinWallet extends CoinServiceAPI { batches[batchNumber] = {}; } final scripthash = _convertToScriptHash(allAddresses[i], _network); + + print("SCRIPT_HASH_FOR_ADDRESS ${allAddresses[i]} IS $scripthash"); batches[batchNumber]!.addAll({ scripthash: [scripthash] }); diff --git a/test/services/coins/bitcoincash/bitcoincash_history_sample_data.dart b/test/services/coins/bitcoincash/bitcoincash_history_sample_data.dart index e69de29bb..c34f68865 100644 --- a/test/services/coins/bitcoincash/bitcoincash_history_sample_data.dart +++ b/test/services/coins/bitcoincash/bitcoincash_history_sample_data.dart @@ -0,0 +1,120 @@ +final Map> historyBatchArgs0 = { + "k_0_0": ["4061323fc54ad0fd2fb6d3fd3af583068d7a733f562242a71e00ea7a82fb482b"], + "k_0_1": ["04818da846fe5e03ac993d2e0c1ccc3848ff6073c3aba6a572df4efc5432ae8b"], + "k_0_2": ["a0345933dd4146905a279f9aa35c867599fec2c52993a8f5da3a477acd0ebcfc"], + "k_0_3": ["607bc74daf946bfd9d593606f4393e44555a3dd0b529ddd08a0422be7955912e"], + "k_0_4": ["449dfb82e6f09f7e190f21fe63aaad5ccb854ba1f44f0a6622f6d71fff19fc63"], + "k_0_5": ["3643e3fe26e0b08dcbc89c47efce3b3264f361160341e3c2a6c73681dde12d39"], + "k_0_6": ["6daca5039b35adcbe62441b68eaaa48e9b0a806ab5a34314bd394b9b5c9289e5"], + "k_0_7": ["113f3d214f202795fdc3dccc6942395812270e787abb88fe4ddfa14f33d62d6f"], + "k_0_8": ["5dea575b85959647509d2ab3c92cda3776a4deba444486a7925ae3b71306e7e3"], + "k_0_9": ["5e2e6d3b43dfa29fabf66879d9ba67e4bb2f9f7ed10cfbb75e0b445eb4b84287"], + "k_0_10": [ + "1bfe42869b6b1e5efa1e1b47f382615e3d27e3e66e9cc8ae46b71ece067b4d37" + ], + "k_0_11": ["e0b38e944c5343e67c807a334fcf4b6563a6311447c99a105a0cf2cc3594ad11"] +}; + +final Map> historyBatchArgs1 = { + "k_0_0": ["4061323fc54ad0fd2fb6d3fd3af583068d7a733f562242a71e00ea7a82fb482b"], + "k_0_1": ["04818da846fe5e03ac993d2e0c1ccc3848ff6073c3aba6a572df4efc5432ae8b"], + "k_0_2": ["a0345933dd4146905a279f9aa35c867599fec2c52993a8f5da3a477acd0ebcfc"], + "k_0_3": ["607bc74daf946bfd9d593606f4393e44555a3dd0b529ddd08a0422be7955912e"], + "k_0_4": ["449dfb82e6f09f7e190f21fe63aaad5ccb854ba1f44f0a6622f6d71fff19fc63"], + "k_0_5": ["3643e3fe26e0b08dcbc89c47efce3b3264f361160341e3c2a6c73681dde12d39"], + "k_0_6": ["6daca5039b35adcbe62441b68eaaa48e9b0a806ab5a34314bd394b9b5c9289e5"], + "k_0_7": ["113f3d214f202795fdc3dccc6942395812270e787abb88fe4ddfa14f33d62d6f"], + "k_0_8": ["5dea575b85959647509d2ab3c92cda3776a4deba444486a7925ae3b71306e7e3"], + "k_0_9": ["5e2e6d3b43dfa29fabf66879d9ba67e4bb2f9f7ed10cfbb75e0b445eb4b84287"], + "k_0_10": [ + "1bfe42869b6b1e5efa1e1b47f382615e3d27e3e66e9cc8ae46b71ece067b4d37" + ], + "k_0_11": ["e0b38e944c5343e67c807a334fcf4b6563a6311447c99a105a0cf2cc3594ad11"] +}; + +final Map>> historyBatchResponse = { + "k_0_0": [], + "s_0_0": [{}, {}], + "w_0_0": [], + "k_0_1": [{}], + "s_0_1": [], + "w_0_1": [{}, {}, {}], + "k_0_2": [], + "s_0_2": [], + "w_0_2": [], + "k_0_3": [], + "s_0_3": [], + "w_0_3": [], + "k_0_4": [], + "s_0_4": [], + "w_0_4": [], + "k_0_5": [], + "s_0_5": [], + "w_0_5": [], + "k_0_6": [], + "s_0_6": [], + "w_0_6": [], + "k_0_7": [], + "s_0_7": [], + "w_0_7": [], + "k_0_8": [], + "s_0_8": [], + "w_0_8": [], + "k_0_9": [], + "s_0_9": [], + "w_0_9": [], + "k_0_10": [], + "s_0_10": [], + "w_0_10": [], + "k_0_11": [], + "s_0_11": [], + "w_0_11": [] +}; + +final Map>> emptyHistoryBatchResponse = { + "k_0_0": [], + "s_0_0": [], + "w_0_0": [], + "k_0_1": [], + "s_0_1": [], + "w_0_1": [], + "k_0_2": [], + "s_0_2": [], + "w_0_2": [], + "k_0_3": [], + "s_0_3": [], + "w_0_3": [], + "k_0_4": [], + "s_0_4": [], + "w_0_4": [], + "k_0_5": [], + "s_0_5": [], + "w_0_5": [], + "k_0_6": [], + "s_0_6": [], + "w_0_6": [], + "k_0_7": [], + "s_0_7": [], + "w_0_7": [], + "k_0_8": [], + "s_0_8": [], + "w_0_8": [], + "k_0_9": [], + "s_0_9": [], + "w_0_9": [], + "k_0_10": [], + "s_0_10": [], + "w_0_10": [], + "k_0_11": [], + "s_0_11": [], + "w_0_11": [] +}; + +final List activeScriptHashes = [ + "11663d093cb17dfbed4a96d148b22d3e094b31d23c639c2814beb79f2ab0ca75", + "06593b2d896751e8dda288bb6587b6bb6a1dee71d82a85457f5654f781e37b12", + "a328ae88ebce63c0010709ae900c199df2b585cdebce53a6291886dfdcc28c63", + "26f92666caebb9a17b14f5b573b385348cdc80065472b8961091f3226d2f650f", + "2f18558e5d3015cb6578aee1c3e4b645725fa4e1d26ce22cb31c9949f3b4957c", + "bf5a6c56814e80eed11e1e459801515f8c2b83da812568aa9dc26e6356f6965b", +]; diff --git a/test/services/coins/bitcoincash/bitcoincash_wallet_test.dart b/test/services/coins/bitcoincash/bitcoincash_wallet_test.dart index 0e39892b1..a9b402f60 100644 --- a/test/services/coins/bitcoincash/bitcoincash_wallet_test.dart +++ b/test/services/coins/bitcoincash/bitcoincash_wallet_test.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:bitcoindart/bitcoindart.dart'; import 'package:decimal/decimal.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -16,6 +18,7 @@ import 'package:stackwallet/services/price.dart'; import 'package:stackwallet/services/transaction_notification_tracker.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; +import 'package:stackwallet/utilities/prefs.dart'; // import '../../../cached_electrumx_test.mocks.dart'; // import '../../../screen_tests/settings_view/settings_subviews/wallet_settings_view_screen_test.mocks.dart'; @@ -845,15 +848,15 @@ void main() { await bch?.initializeExisting(); expect( Address.validateAddress( - await bch!.currentReceivingAddress, bitcoincashtestnet), + await bch!.currentReceivingAddress, bitcoincash), true); expect( Address.validateAddress( - await bch!.currentReceivingAddress, bitcoincashtestnet), + await bch!.currentReceivingAddress, bitcoincash), true); expect( Address.validateAddress( - await bch!.currentReceivingAddress, bitcoincashtestnet), + await bch!.currentReceivingAddress, bitcoincash), true); verifyNever(client?.ping()).called(0); @@ -896,8 +899,7 @@ void main() { expect(addresses?.length, 2); for (int i = 0; i < 2; i++) { - expect( - Address.validateAddress(addresses![i], bitcoincashtestnet), true); + expect(Address.validateAddress(addresses![i], bitcoincash), true); } verifyNever(client?.ping()).called(0); @@ -1833,8 +1835,8 @@ void main() { }); when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); when(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .thenAnswer((realInvocation) async {}); @@ -1902,7 +1904,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(2); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); verify(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .called(1); @@ -1927,6 +1929,13 @@ void main() { "hash_function": "sha256", "services": [] }); + + // when(client?.getBatchHistory(args: anyNamed("args"))) + // .thenAnswer((thing) async { + // print(jsonEncode(thing.namedArguments.entries.first.value)); + // return {}; + // }); + when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => emptyHistoryBatchResponse); when(client?.getBatchHistory(args: historyBatchArgs1)) @@ -1942,7 +1951,7 @@ void main() { height: 4000); expect(await bch?.mnemonic, TEST_MNEMONIC.split(" ")); - + // verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); @@ -1993,7 +2002,7 @@ void main() { bch = BitcoinCashWallet( walletId: testWalletId, walletName: testWalletName, - coin: Coin.bitcoincashTestNet, + coin: Coin.bitcoincash, client: client!, cachedClient: cachedClient!, tracker: tracker!, @@ -2082,8 +2091,8 @@ void main() { }); when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); final wallet = await Hive.openBox(testWalletId); @@ -2101,7 +2110,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); expect(secureStore?.interactions, 6); expect(secureStore?.writes, 3); @@ -2127,8 +2136,8 @@ void main() { }); when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); when(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .thenAnswer((realInvocation) async {}); @@ -2196,7 +2205,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(2); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); verify(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .called(1); @@ -2224,8 +2233,8 @@ void main() { when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); when(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .thenAnswer((realInvocation) async {}); @@ -2285,7 +2294,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(2); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); verify(cachedClient?.clearSharedTransactionCache(coin: Coin.bitcoincash)) .called(1); @@ -2779,8 +2788,8 @@ void main() { }); when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); final wallet = await Hive.openBox(testWalletId); @@ -2797,7 +2806,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); expect(secureStore?.interactions, 6); expect(secureStore?.writes, 3); @@ -2824,8 +2833,8 @@ void main() { }); when(client?.getBatchHistory(args: historyBatchArgs0)) .thenAnswer((_) async => historyBatchResponse); - when(client?.getBatchHistory(args: historyBatchArgs1)) - .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); when(client?.getHistory(scripthash: anyNamed("scripthash"))) .thenThrow(Exception("some exception")); @@ -2842,7 +2851,7 @@ void main() { verify(client?.getServerFeatures()).called(1); verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); - verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); verify(client?.getBlockHeadTip()).called(1); verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); diff --git a/test/services/coins/bitcoincash/bitcoincash_wallet_test_parameters.dart b/test/services/coins/bitcoincash/bitcoincash_wallet_test_parameters.dart new file mode 100644 index 000000000..c47053955 --- /dev/null +++ b/test/services/coins/bitcoincash/bitcoincash_wallet_test_parameters.dart @@ -0,0 +1,13 @@ +const TEST_MNEMONIC = + "market smart dolphin hero liberty frog bubble tilt river electric ensure orient"; + +const ROOT_WIF = "KxpP3c45bRPbPzecYfistA5u3Z4SSGCCiCS4pjWrS5LF1g4mXC66"; +const NODE_WIF_44 = "KyaVkL3X4jUdq16CBrTR72VyQkLuczNyi9v6ztwZKdTm3dt7x57o"; +const NODE_WIF_49 = "L3jFGqrCRMENcCofASf8m6vMNfYutxdqLe733NwEyXiKTBkkgqyg"; +const NODE_WIF_84 = "cU1E2J1eHbQh36TCtsqneAsKaXRwRhSs74GEDPXWCGY7f1xpsymM"; + +const FILL_ADDRESSES_EXPECTED_CHANGE_MAINNET = + "{0: {p2pkh: {publicKey: 02a1a969c63ed41662a23afc640f76ee4a53c35fba4a13ff871931849abdc0670c, wif: Kws4oRj4Z3uT4WNejbuLsWvqTeso4dGciVu75jjd5fa36Tst48ER, address: 1JfZd6cSNr924p4ZwewG1CeRw7AqoYQ6uE}, p2sh: {publicKey: 020cebf05d41da01c00fbb7f7c04cf85741188da0e5ee22ae9fac6e7b5098b8cac, wif: L3jFGqrCRMENcCofASf8m6vMNfYutxdqLe733NwEyXiKTBkkgqyg, address: 3ANTVqufTH1tLAuoQHhng8jndRsA9hcNy7}, p2wpkh: {publicKey: 0301def02c073852fc8c28182749be431ac22a2699b51c896fbc48457107c10c65, wif: L3UwhkWiXSAyCWYVjaFCyMhGcqeDU8soCmqhuFYFadcVQepKKcjy, address: bc1qwt76574cgnhjv0nx4f52qylyla0t50d60znk5u}}, 1: {p2pkh: {publicKey: 02617b5f7868bd0402a3e3ff6fa224aede55f13218bbef9987467455d068d629cd, wif: KzDwZXPyNhPix2nV5wri4QaGj6Tge7znvf49D5tgL7EvAnYV6Pas, address: 1vFHF5q21GccoBwrB4zEUAs9i3Bfx797U}, p2sh: {publicKey: 03b4c1e17b7257850fd2c0cf69acd397616835de29867cfe047bf1064b9803f773, wif: L1v4hFFe8EvUX7DBVxnfh7Y22A8nNwh7py42vfr5TU1yAAa9Eadb, address: 3LNBen8sNa6kXsWuMc6tynHBQr3vUHn3nL}, p2wpkh: {publicKey: 038b646c426ed75204bd92f55f0519fb89c41a503844aecf187a55eb00a5d8b5eb, wif: L1jQHTyFP4sNyqdKeooJzw3AUghPeSmGoW7UmZwDsS1Js4UxiGyh, address: bc1qn2x7h96kufgfjxtkhsnq03jqwqde8zasffqvd2}}, 2: {p2pkh: {publicKey: 031643235b0355ac366ade3ec125e466be42602a6167ba1cc68f39c38929f3c785, wif: KzqPH523RyAKwzRDg8PqjvNVDV8Fex2qrKxwwLGcvc9XZHfvaErb, address: 1EwEMkfeivF8nEkG3tWhBSqibnDjoREBoF}, p2sh: {publicKey: 02312b44be7de07c21bed9bd8b0c2f3d64be41c5f5120a463fcb85371b3f44cce1, wif: KxxRzHcF6QkApcLtbyxeyQrHgnstCqCPPfLiLfJhee9PVinisekA, address: 3MPyLcCkGPKyZULKSzkYDZQxxMmG8HNRPi}, p2wpkh: {publicKey: 031a10a60de8563b1be55e8274538d6ec6375d19764e81303999de2605634ea15a, wif: L2u4AYxEKkDamcXjAfXviuTPuRuWe1aN9zXEX1PSpzrmqGtSHQqe, address: bc1qpcn5wd2cx7syc28r0t4w4ym8yy6fck87nrxn4p}}, 3: {p2pkh: {publicKey: 02f552c7b15e90df9ff99f35e8b5bd84eff0bdef6cb6c75e13d1df81d334c6b786, wif: Kxi7qVxEqwaBhR3tuewpfi8EDqqR7fBzgQUDambVWGPEP3oG9JUM, address: 1MifgAa6CzqmTF4euVSD2DivD3xUGDbuA5}, p2sh: {publicKey: 02a7ba8279be4c182292b855cbf4349dba68f8a776b9b8e04e05585ac9f605e7f1, wif: L3qZiaviwh76RvKj5i69vu75upS3E62qmEk6BSJRgSVjw7FmJNDW, address: 34pa7nXRZ3XahoFKhhnaLy4DqzfC9v5aSm}, p2wpkh: {publicKey: 03d712c240ccd578b8fb59acf88a8fbacf2ab14301a900e7907c739244e61029bd, wif: L3ezWHMUvdwprhChDVn7iVV8otpLUBrdkZB7X97GEbiVZbxJkDPc, address: bc1qemkha74vvmlz8yg56tesswpz77wg07m9wu23sc}}, 4: {p2pkh: {publicKey: 03d86c453c6b8ef6239db8fc270c76a9b08faf2c58f3de74cbe2cb0bc0cef7139a, wif: KzNpx8vgcNbYcqPZxHFwvKSRwPpyZNhmF4Eqa4k1wuWJZovwaXNC, address: 1BxYVw7u85QWVoVCHJAJkCCZZjWeXYW6EA}, p2sh: {publicKey: 020e9095a31b9c1e580b3773d34d1f752f9c4de42feb1c3480f5234a2392762fbc, wif: Ky6NQbHkhBLS3LDLmCuu3TFBVU1HNah7TYUiLhmc3u8HdZgpKYSs, address: 33tciSXesiShRULW4bds5BE8358GsJc8CA}, p2wpkh: {publicKey: 02908673d298d4929a724ff9627c3fc710e27f0d727fbb0f2741c00dac1d792120, wif: L5QYEMxjY42c9JnpEQCDUbqCUfZ1vKoELe41rREuGzszcZgzVepx, address: bc1qpl7rh3ykw5h9823wv3uw0jrehlty6dfjp65ykt}}, 5: {p2pkh: {publicKey: 0368fda0552db99cfac045f57327cecd9b365d8813982dda051f1156ceefacd80b, wif: Ky8KYn2vMmWiMw6QEnE6JsM3uwQgAZBjiUHB938MVtnSgpiDjh7o, address: 14g637PwocgKr3S949WDitY7Bj3L1Bttcb}, p2sh: {publicKey: 03a1c4977ec9e9e02ba011cf56cee34c1617a7e05e85183a4207efd6425079a914, wif: KzDtpdJFLQWLSRdiEzhrqE8rzDaRi43tkxJ6K9ANeGcExQMNWYLz, address: 34Z5gAgPiUsfA9VHdAss2AD5h5Xk5TEp9K}, p2wpkh: {publicKey: 028c44701f2e918ba9853737aa1c6c7b5302a4c9198bf0eb22a75fb6fd3ef4dc9e, wif: Kx7wWb9xvNTaiWkzEmLqJ7gsQEaD6VPJTodr71rQwtpCU5dZDbEP, address: bc1q0ja83w4jwplrxjggcy8ltnrz70qrhnsmfwjv47}}, 6: {p2pkh: {publicKey: 03844bb44c430540dd2fd42630200a059f06480e024b7a06ea628b5094f48b944f, wif: Kwe6hMa7t9qcWW7fHBFTpBDDyzBdWzkHjhngiTLRKZzBjUqqppRJ, address: 1JNghjiaNWrWgvAJnmGyyFEbQMWV35QT1D}, p2sh: {publicKey: 03add39947c251a1f42f2fe4dbf41ea6dfed2bcf56bbe5ab6c44213235b68a1d0a, wif: L27TRwwmSB98trgkBduvx1yKvmYbu77ffcF6BQKADfD94vNwvW4W, address: 3AnduCZuFcXFtBjnz7Y6vRJBwYvfeNKJtR}, p2wpkh: {publicKey: 02883642dad55a3f5003440ff7e066c97624539841643e900db80eaac7ebab7270, wif: L4sisqYU1xSzbnx25xuyLefifGL5BoGDXh5Vsex9UCBSDHhFYt8h, address: bc1qdjjssjd6vl6e3egqts7qxcj6dqrs6krgj4dv3q}}, 7: {p2pkh: {publicKey: 03487700edb11cf39b095b17da1f44f702398bd707dfdace4d88ee0227a48c8a0c, wif: L3UmMi9h8RQ8Y8jKATFjum77kc9ng6Jr71fq1H9rGxxdspKGXt12, address: 1Q1FMnBJxQ5uh2UDQbvwQ1y1KMpqVd3whq}, p2sh: {publicKey: 03a600d4880ce8a53287f25b6534f97a689ba7006f665ad1fe3e2bb8d3c9620ec5, wif: KwDwKEifSPJeQKTFonmyBTvCNuApuT9y3s7PqmnFkZBFkxSaQ6BQ, address: 3J9GqZ2b5raBNyGmuBg8ttyg5x8zaqSrMp}, p2wpkh: {publicKey: 035b91a2c439d6cc34a706449b8d9843721cc499aec4b38ebdd211978a52431fc0, wif: KzJZE96Hjwn2dtuYpBgMoR9s86TgQ3apRsNTxGZTpyW2RVA6HppV, address: bc1qs2df496wpp4339qjuypc2l2r6e0z7vndz70ac7}}, 8: {p2pkh: {publicKey: 0254ff6bad342ae9af2e04ee9d876682771ad86a76336a817eadb2e4e5bf68070a, wif: KxhFsZNv8XDUxLECR9y965eWFSGBbc2FrTkRvuyRPwPM9xxeCfV4, address: 1JD8uzAKQo9DgR2om6gS98S92cgqscZ3Ur}, p2sh: {publicKey: 02f574d6a16300e4d497647814fd2b0fa2eaba32af19fcf65821f70743bc566f9d, wif: KypeLKRPCoN9eZQ8awgBca47JyD6scST44Pg4woLpDiyT3hy9wZA, address: 3BqGdLjubq2vAGRDNRkxk5uMqd6FPoMg7N}, p2wpkh: {publicKey: 0227cb798c29365364027e5df67f6890985fd89ecbca571e51bb6260272cc66ec1, wif: Kxrpxu5kNAdy9jqz7etcCg716gsEk1K5weiMLKg1fhLPdxitvzsy, address: bc1qhpvcc08dxa7m5ztkxsfsplmj69yzsg6p3dna63}}, 9: {p2pkh: {publicKey: 02b8b1a447c71bb4ed82fd3719ec9e665df5c9c6e31c7a14beb56c65f355a08bc4, wif: Ky9kWxSwqBQP9ShkZNCfLwDqpzqnDEvQG3NjfYY3A3VFE4nPLqrs, address: 16EpYJQoM1K2ZKFUEMBAJubiNsMghkvBUx}, p2sh: {publicKey: 03f6cc36b17f382490d1a08bc9f7c28302c704c69cb6c153d2828710cd93a1c215, wif: L1zutoVai5EqS2LoAaEq16R2rcooiP6HAdA5zETwbvuYBky3neKw, address: 3HBjEYyPH9H8JvuVbibNaZ2bJyD8564r1Y}, p2wpkh: {publicKey: 030f021f31d9eac4279f8ae12a512885769978dc93a74e0e4c91eba994a7d02cda, wif: L26bxcZZ8PLaa23X9myLgzh3hAYxjQ9MsSfiz49yiEdys7dzr66z, address: bc1q6hxsggrtcrvcdxpzu7e8qv2m9md3zaghjjsxut}}}"; + +const FILL_ADDRESSES_EXPECTED_RECEIVING_MAINNET = + "{0: {p2pkh: {publicKey: 0359bc5f4918d68ab3730886c4fdebec1cfdeba9db1170582da7072183d7afce99, wif: KyaVkL3X4jUdq16CBrTR72VyQkLuczNyi9v6ztwZKdTm3dt7x57o, address: 1GDtrDP62zQETQESHt48Z82afXWcvX8qNv}, p2sh: {publicKey: 02addc76ef5845c0e74e5ada1d97568d0ee76856031e60a084f6f6a0e7be51d84b, wif: L3jFGqrCRMENcCofASf8m6vMNfYutxdqLe733NwEyXiKTBkkgqyg, address: 3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk}, p2wpkh: {publicKey: 02e5b585efaec9e6486f4b8afb3415ffd71a89566c1f84bb5331c4cddc905b7555, wif: KzCvDQSH6qqUFhPnbM42zbLK3qPeHXKVVpc6A5bAqADanHQFqypQ, address: bc1qz36w7sv4lnt09saurf94lwk5tc6qsyhky9yfgm}}, 1: {p2pkh: {publicKey: 036070efb466bfbf689efe1e7a27fa405bdbb16fa4c836f7b71feeb7ec9f5c5db4, wif: L3aA8PRqcj1iuZnQMJ3M5x68zhywgwgGbzPtDsQMBWRmqUaKyyBQ, address: 1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw}, p2sh: {publicKey: 02b8f46d7741d709bc1dc81386edaf4e6dad8b78b1fb9ec0c5b1f8e08e7d72f395, wif: L1v4hFFe8EvUX7DBVxnfh7Y22A8nNwh7py42vfr5TU1yAAa9Eadb, address: 3Ggn4xZmXjMCDUdQSTjxTi1PdVrtDGUy5Z}, p2wpkh: {publicKey: 03627f4374d7c992c40d743786028f94b1e1ca354b94ffa776b51ba50c80fba1d7, wif: L5gAb3ABSYTBTL85FhoRxHa7dJMtz9sVLHWkEY7kYKXT2cDg71Ym, address: bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0}}, 2: {p2pkh: {publicKey: 03f2171f073a8fbf5b844a20e5b6ae356a07e9e6dc5af24cd4b3e54a1599eb4137, wif: L27AJt6ZRU4D3sf7Ls1HLq2ruF8c6iYq9iJVvW8Xm2kMKrKcPwzZ, address: 1Mu5SqaUBHB1HkyvEtjHjrNQVaai2MoFFz}, p2sh: {publicKey: 024e6d7230df59dc28f94ab7bd45c5aed53ac0ea96e8a1ee46edcca5594fa3ec66, wif: KxxRzHcF6QkApcLtbyxeyQrHgnstCqCPPfLiLfJhee9PVinisekA, address: 3K8CQxXWp2kjbGcnub4sh935H5s1AfrFmp}, p2wpkh: {publicKey: 0365b55bdae2d0ada31bfc73371f7106e0e20563dff0763c48da8ce2ca70346bd8, wif: KxttkPfRdY9uwEqgHGPo9d7Bb3qTsBzHSgmv8EVTS2Gbh88GgJZQ, address: bc1qsegc09enu2ts4dg7lnxee6tv8fy78m89cfzdy9}}, 3: {p2pkh: {publicKey: 020d00dd73194f8f087a3740594f2eeaa545224fb634462e06d281ad5759e77cbd, wif: L5Mdvj7tkY4Sx5JTiB1WtQJPogqXGLunaFAubAQMvuAK3TjuXXxD, address: 16d7AeqhmspaMyeJKF3cDwxjkLUTFZQdTn}, p2sh: {publicKey: 038d2aec8c0da1bafc3e51fe7fdd3aa60f121983071d97ed754c05f24c54d7d09b, wif: L3qZiaviwh76RvKj5i69vu75upS3E62qmEk6BSJRgSVjw7FmJNDW, address: 3QwDpsqwb5VfKQVUVLzaDwgC51pea1ymHr}, p2wpkh: {publicKey: 02f144cd8c1d6966db09da86bf662933480ad6e7f2862f7878780f21bc1448d563, wif: Ky9zeEcFB7td3dPYCiXxrMPCQkKyfm4feaWp5PAWxY7kBwCccUxz, address: bc1q3h5llpmhvr89el03dktd936jqsn7t6pytr2nlr}}, 4: {p2pkh: {publicKey: 038d6142ee0db16d4ff23c95d1c157428d48e11fae7752ddacaf6cccce6db61fc6, wif: Ky41iFhmo2Y5mYp1UALsYqRhhKajEy7AxLJcSDYRVdwW7iv3vmTK, address: 1EFyHH7G4Hk8aCjGWPer1Qc9e8iZsbfZC8}, p2sh: {publicKey: 03a1624a73009c17ceefe299ca998ee941b501692b2f3a032e037b50726415c78b, wif: Ky6NQbHkhBLS3LDLmCuu3TFBVU1HNah7TYUiLhmc3u8HdZgpKYSs, address: 38YQK65m9A278nCbWKTx3EJtyK9Vega1Z9}, p2wpkh: {publicKey: 031e816e4a35c686ee30387e01c26341927806674986a57ee5755114f3ee59d560, wif: L5AiWbctHW9EyaD5vyzCrxKmKppdMwdRFTvB1TUKLWvibo5R3QXa, address: bc1qj69ku48uu6lqu6uhd62a37zvuwta2dlc28frxc}}, 5: {p2pkh: {publicKey: 024697dbd5ad644c285fb9ce25a38d0f8b48de9dfbda146eba8f5659184fb2779c, wif: L3yze1iu58vmF7GyoUHqnmYCm4qLsBoGiyRgBPbt68gB3kKnQfCv, address: 16ZbDHYV97o6xeXoQMWus1a8NMNyxqHZon}, p2sh: {publicKey: 021b442ef944676b70e6ef23ecb932c015b6934ff5d094db88820735be36f6d807, wif: KzDtpdJFLQWLSRdiEzhrqE8rzDaRi43tkxJ6K9ANeGcExQMNWYLz, address: 3LFx71JddSzs8qE51P4JHHjxsgiSTZdk4z}, p2wpkh: {publicKey: 0307ffe6b1c0dfca40b6126140691a4d907a053dcfd97b816cc7ff86775486e612, wif: L3F1KPjtaA4uyfcmMSH2sFWs2TW2aX6Lcn8vetrdqtwgKgvfvVGg, address: bc1ql66zvg82lk425g8j4jx5flt255n8a9up328z3l}}, 6: {p2pkh: {publicKey: 02a5087e407ad853720a7010c1870bbc35a9ee09d90777c617801c983aaccc8523, wif: L1fLMxyhSKSsLskB3iBcfbfCinVr6MRE9YYVKXfCbLjPLZLAUpb7, address: 1FzukoiU7vXb7invLNLCCLAFdfK2gmP45A}, p2sh: {publicKey: 02d4f8bc88178a215b5c95e079b7c1ff0b62ec2884d5ffa71122683557a16615f3, wif: L27TRwwmSB98trgkBduvx1yKvmYbu77ffcF6BQKADfD94vNwvW4W, address: 3GpAXutnaxCYuV2MSgSiXQTSafE7Aouifs}, p2wpkh: {publicKey: 03f7eb3138cbaa7baabdef7b903ff1a3369a8d7937c15ae17dac7084aa4cfd8afe, wif: KyWW73wu11xVnmFKMzq9AuxQ3TXw2KjwCzxUa9WbYx2Ee5oh2Nk1, address: bc1q4ev5cas22cn8rna3a7je238p3plgqqdjv9xt0e}}, 7: {p2pkh: {publicKey: 0366efa2d1624fd9ff0d9feffa39ff47c10ef196fe209421dcebf5d07dcb8907ce, wif: KwxWpA4scQ7RVKDFKjXvKWn6PdVXpr6nzW9T6ju6VP3MmjBRb5eK, address: 13qhwGjBzshL724ZZNv2C2XmUNpFoDLx4Z}, p2sh: {publicKey: 03b08cabbcaa97c20bf30e0830c8c7a2361ba63d1ba987032ee7cd97d666b62692, wif: KwDwKEifSPJeQKTFonmyBTvCNuApuT9y3s7PqmnFkZBFkxSaQ6BQ, address: 39ZZhvQJGwohf2ndBGXYWW31t1E91j9wcB}, p2wpkh: {publicKey: 03a353139ce07e31b33f1c5b0ece6f08ac117339a1a56aa1b3af011216cf2cbf88, wif: KwL9LaYjNvwWiYgDiUBLnVW7W5j2FyWqmzrioTA2QRgwdz6bUTQH, address: bc1q96wmh0qc2lwauzxv7la206d03zysc9zfw3ajkd}}, 8: {p2pkh: {publicKey: 032234e3903ebcd72c081119fe4b4c2d98e3fd3940b5b6cd7737f5f4d3240dcb7d, wif: L5kwyvViXwBcFqzJL5cffcJR7S1vEHxHuKHAQrAq6FtknvJG4Cx3, address: 1LPGD9sExsBMTLGxnTTEDYBHrtRSZCKNrN}, p2sh: {publicKey: 02c1ca19626e4784b2ce19b68d2887e4c9c733176db189e536b71a79c07178b4af, wif: KypeLKRPCoN9eZQ8awgBca47JyD6scST44Pg4woLpDiyT3hy9wZA, address: 3FXRAzFAgVMDcqbv6WE9Ki7yBACrRAh2Wn}, p2wpkh: {publicKey: 020fd4e2cd43f98366dd60fcc8902564c532c9396c35ebb840b63c965deed16330, wif: L4EgRsvLsju7uVXsTEchYnGdwmFrxhbE9SkMuvnaMDSMxmopNg8C, address: bc1q354ttmgwjzjf7jmym2aw9p5geeq9lhk8vcffap}}, 9: {p2pkh: {publicKey: 03dd5e2f3b828bdc2653ce7a7ce34ebfa8118077b647bdf4922c5d15f4546dc419, wif: Ky4f8LEYkXMkLiYX4t2PR6V2vJigyCoYTYATu4B5pnjDDywUD2U7, address: 13GXUQDr91Z5prRxAQQFHUvKNqs2hEWrkJ}, p2sh: {publicKey: 034a4cf3a57083ade346ecaab24dc931e2a991107c7c42cf170d1c6e3b84560e68, wif: L1zutoVai5EqS2LoAaEq16R2rcooiP6HAdA5zETwbvuYBky3neKw, address: 3FstRy5yCE7a3wCNS8SgFKi4SF3zWK8vB6}, p2wpkh: {publicKey: 020cb048fc44a2db09be875ccd938634c557b19e2062fbc55372783be1116737e7, wif: KyDq3LHf9NkUWChPxVEAV3dQkX89GJWUkSw8cSVLooXronEwKouy, address: bc1qn99xewnyhqcleyj03f2y0crdhqmxhm5cmextw8}}}"; diff --git a/test/services/coins/namecoin/namecoin_history_sample_data.dart b/test/services/coins/namecoin/namecoin_history_sample_data.dart new file mode 100644 index 000000000..baa0535ec --- /dev/null +++ b/test/services/coins/namecoin/namecoin_history_sample_data.dart @@ -0,0 +1,95 @@ +final Map> historyBatchArgs0 = { + "k_0_0": ["bd84ae7e09414b0ccf5dcbf70a1f89f2fd42119a98af35dd4ecc80210fed0487"], + "k_0_1": ["3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6"] +}; +final Map> historyBatchArgs1 = { + "k_0_0": ["dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7"], + "k_0_1": ["71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d"] +}; +final Map> historyBatchArgs2 = { + "k_0_0": ["c7e700f7e23a85bbdd9de86d502322a933607ee7ea7e16adaf02e477cdd849b9"] +}; + +final Map>> historyBatchResponse = { + "k_0_0": [], + "s_0_0": [{}, {}], + "w_0_0": [], + "k_0_1": [{}], + "s_0_1": [], + "w_0_1": [{}, {}, {}], + "k_0_2": [], + "s_0_2": [], + "w_0_2": [], + "k_0_3": [], + "s_0_3": [], + "w_0_3": [], + "k_0_4": [], + "s_0_4": [], + "w_0_4": [], + "k_0_5": [], + "s_0_5": [], + "w_0_5": [], + "k_0_6": [], + "s_0_6": [], + "w_0_6": [], + "k_0_7": [], + "s_0_7": [], + "w_0_7": [], + "k_0_8": [], + "s_0_8": [], + "w_0_8": [], + "k_0_9": [], + "s_0_9": [], + "w_0_9": [], + "k_0_10": [], + "s_0_10": [], + "w_0_10": [], + "k_0_11": [], + "s_0_11": [], + "w_0_11": [] +}; + +final Map>> emptyHistoryBatchResponse = { + "k_0_0": [], + "s_0_0": [], + "w_0_0": [], + "k_0_1": [], + "s_0_1": [], + "w_0_1": [], + "k_0_2": [], + "s_0_2": [], + "w_0_2": [], + "k_0_3": [], + "s_0_3": [], + "w_0_3": [], + "k_0_4": [], + "s_0_4": [], + "w_0_4": [], + "k_0_5": [], + "s_0_5": [], + "w_0_5": [], + "k_0_6": [], + "s_0_6": [], + "w_0_6": [], + "k_0_7": [], + "s_0_7": [], + "w_0_7": [], + "k_0_8": [], + "s_0_8": [], + "w_0_8": [], + "k_0_9": [], + "s_0_9": [], + "w_0_9": [], + "k_0_10": [], + "s_0_10": [], + "w_0_10": [], + "k_0_11": [], + "s_0_11": [], + "w_0_11": [] +}; + +final List activeScriptHashes = [ + "83b744ccb88827d544081c1a03ea782a7d00d6224ff9fddb7d0fbad399e1cae7", + "86906979fc9107d06d560275d7de8305b69d7189c3206ac9070ad76e6abff874", + "5baba32b1899d5e740838559ef39b7d8e9ba302bd24b732eeedd4c0e6ec65b51", +]; diff --git a/test/services/coins/namecoin/namecoin_transaction_data_samples.dart b/test/services/coins/namecoin/namecoin_transaction_data_samples.dart new file mode 100644 index 000000000..2199cfcc3 --- /dev/null +++ b/test/services/coins/namecoin/namecoin_transaction_data_samples.dart @@ -0,0 +1,355 @@ +import 'package:stackwallet/models/paymint/transactions_model.dart'; + +final transactionData = TransactionData.fromMap({ + "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6": tx1, + "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7": tx2, + "71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d": tx3, + "c7e700f7e23a85bbdd9de86d502322a933607ee7ea7e16adaf02e477cdd849b9": tx4, +}); + +final tx1 = Transaction( + txid: "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6", + confirmedStatus: true, + confirmations: 212, + txType: "Received", + amount: 1000000, + fees: 23896, + height: 629633, + address: "nc1qwfda4s9qmdqpnykgpjf85n09ath983srtuxcqx", + timestamp: 1663093275, + worthNow: "0.00", + worthAtBlockTimestamp: "0.00", + inputSize: 2, + outputSize: 2, + inputs: [ + Input( + txid: "290904699ccbebd0921c4acc4f7a10f41141ee6a07bc64ebca5674c1e5ee8dfa", + vout: 1, + ), + Input( + txid: "bd84ae7e09414b0ccf5dcbf70a1f89f2fd42119a98af35dd4ecc80210fed0487", + vout: 0, + ), + ], + outputs: [ + Output( + scriptpubkeyAddress: "nc1qwfda4s9qmdqpnykgpjf85n09ath983srtuxcqx", + value: 1000000, + ), + Output( + scriptpubkeyAddress: "nc1qp7h7fxcnkqcpul202z6nh8yjy8jpt39jcpeapj", + value: 29853562, + ) + ], +); + +final tx2 = Transaction( + txid: "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7", + confirmedStatus: true, + confirmations: 150, + txType: "Sent", + amount: 988567, + fees: 11433, + height: 629695, + address: "nc1qraffwaq3cxngwp609e03ynwsx8ykgjnjve9f3y", + timestamp: 1663142110, + worthNow: "0.00", + worthAtBlockTimestamp: "0.00", + inputSize: 1, + outputSize: 1, + inputs: [ + Input( + txid: "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6", + vout: 0, + ), + ], + outputs: [ + Output( + scriptpubkeyAddress: "nc1qraffwaq3cxngwp609e03ynwsx8ykgjnjve9f3y", + value: 988567, + ), + ], +); + +final tx3 = Transaction( + txid: "71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d", + confirmedStatus: true, + confirmations: 147, + txType: "Received", + amount: 988567, + fees: 11433, + height: 629699, + address: "nc1qw4srwqq2semrxje4x6zcrg53g07q0pr3yqv5kr", + timestamp: 1663145287, + worthNow: "0.00", + worthAtBlockTimestamp: "0.00", + inputSize: 2, + outputSize: 1, + inputs: [ + Input( + txid: "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7", + vout: 0, + ), + Input( + txid: "80f8c6de5be2243013348219bbb7043a6d8d00ddc716baf6a69eab517f9a6fc1", + vout: 1, + ), + ], + outputs: [ + Output( + scriptpubkeyAddress: "nc1qw4srwqq2semrxje4x6zcrg53g07q0pr3yqv5kr", + value: 1000000, + ), + Output( + scriptpubkeyAddress: "nc1qsgr7u4hd22rc64r9vlef69en9wzlvmjt8dzyrm", + value: 28805770, + ), + ], +); + +final tx4 = Transaction( + txid: "c7e700f7e23a85bbdd9de86d502322a933607ee7ea7e16adaf02e477cdd849b9", + confirmedStatus: true, + confirmations: 130, + txType: "Sent", + amount: 988567, + fees: 11433, + height: 629717, + address: "nc1qmdt0fxhpwx7x5ymmm9gvh229adu0kmtukfcsjk", + timestamp: 1663155739, + worthNow: "0.00", + worthAtBlockTimestamp: "0.00", + inputSize: 1, + outputSize: 1, + inputs: [ + Input( + txid: "71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d", + vout: 0, + ), + ], + outputs: [ + Output( + scriptpubkeyAddress: "nc1qmdt0fxhpwx7x5ymmm9gvh229adu0kmtukfcsjk", + value: 988567, + ), + ], +); + +final tx1Raw = { + "txid": "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6", + "hash": "40c8dd876cf111dc00d3aa2fedc93a77c18b391931939d4f99a760226cbff675", + "version": 2, + "size": 394, + "vsize": 232, + "weight": 925, + "locktime": 0, + "vin": [ + { + "txid": + "290904699ccbebd0921c4acc4f7a10f41141ee6a07bc64ebca5674c1e5ee8dfa", + "vout": 1, + "scriptSig": { + "asm": "001466d2173325f3d379c6beb0a4949e937308edb152", + "hex": "16001466d2173325f3d379c6beb0a4949e937308edb152" + }, + "txinwitness": [ + "3044022062d0f32dc051ed1e91889a96070121c77d895f69d2ed5a307d8b320e0352186702206a0c2613e708e5ef8a935aba61b8fa14ddd6ca4e9a80a8b4ded126a879217dd101", + "0303cd92ed121ef22398826af055f3006769210e019f8fb43bd2f5556282d84997" + ], + "sequence": 4294967295 + }, + { + "txid": + "bd84ae7e09414b0ccf5dcbf70a1f89f2fd42119a98af35dd4ecc80210fed0487", + "vout": 0, + "scriptSig": {"asm": "", "hex": ""}, + "txinwitness": [ + "3045022100e8814706766a2d7588908c51209c3b7095241bbc681febdd6b317b7e9b6ea97502205c33c63e4d8a675c19122bfe0057afce2159e6bd86f2c9aced214de77099dc8b01", + "03c35212e3a4c0734735eccae9219987dc78d9cf6245ab247942d430d0a01d61be" + ], + "sequence": 4294967295 + } + ], + "vout": [ + { + "value": 0.01, + "n": 0, + "scriptPubKey": { + "asm": "0 725bdac0a0db401992c80c927a4de5eaee53c603", + "hex": "0014725bdac0a0db401992c80c927a4de5eaee53c603", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qwfda4s9qmdqpnykgpjf85n09ath983srtuxcqx"] + } + }, + { + "value": 0.29853562, + "n": 1, + "scriptPubKey": { + "asm": "0 0fafe49b13b0301e7d4f50b53b9c9221e415c4b2", + "hex": "00140fafe49b13b0301e7d4f50b53b9c9221e415c4b2", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qp7h7fxcnkqcpul202z6nh8yjy8jpt39jcpeapj"] + } + } + ], + "hex": + "02000000000102fa8deee5c17456caeb64bc076aee4111f4107a4fcc4a1c92d0ebcb9c69040929010000001716001466d2173325f3d379c6beb0a4949e937308edb152ffffffff8704ed0f2180cc4edd35af989a1142fdf2891f0af7cb5dcf0c4b41097eae84bd0000000000ffffffff0240420f0000000000160014725bdac0a0db401992c80c927a4de5eaee53c6037a87c701000000001600140fafe49b13b0301e7d4f50b53b9c9221e415c4b202473044022062d0f32dc051ed1e91889a96070121c77d895f69d2ed5a307d8b320e0352186702206a0c2613e708e5ef8a935aba61b8fa14ddd6ca4e9a80a8b4ded126a879217dd101210303cd92ed121ef22398826af055f3006769210e019f8fb43bd2f5556282d8499702483045022100e8814706766a2d7588908c51209c3b7095241bbc681febdd6b317b7e9b6ea97502205c33c63e4d8a675c19122bfe0057afce2159e6bd86f2c9aced214de77099dc8b012103c35212e3a4c0734735eccae9219987dc78d9cf6245ab247942d430d0a01d61be00000000", + "blockhash": + "c9f53cc7cbf654cbcc400e17b33e03a32706d6e6647ad7085c688540f980a378", + "confirmations": 212, + "time": 1663093275, + "blocktime": 1663093275 +}; + +final tx2Raw = { + "txid": "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7", + "hash": "32dbc0d21327e0cb94ec6069a8d235affd99689ffc5f68959bfb720bafc04bcf", + "version": 2, + "size": 192, + "vsize": 110, + "weight": 438, + "locktime": 0, + "vin": [ + { + "txid": + "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6", + "vout": 0, + "scriptSig": {"asm": "", "hex": ""}, + "txinwitness": [ + "30450221009d58ebfaab8eae297910bca93a7fd48f94ce52a1731cf27fb4c043368fa10e8d02207e88f5d868113d9567999793be0a5b752ad704d04224046839763cefe46463a501", + "02f6ca5274b59dfb014f6a0d690671964290dac7f97fe825f723204e6cb8daf086" + ], + "sequence": 4294967295 + } + ], + "vout": [ + { + "value": 0.00988567, + "n": 0, + "scriptPubKey": { + "asm": "0 1f52977411c1a687074f2e5f124dd031c9644a72", + "hex": "00141f52977411c1a687074f2e5f124dd031c9644a72", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qraffwaq3cxngwp609e03ynwsx8ykgjnjve9f3y"] + } + } + ], + "hex": + "02000000000101c6ccf4ddc2a21434ed634636378923d01014b2d3b2f124999f3e7c88d043f53e0000000000ffffffff0197150f00000000001600141f52977411c1a687074f2e5f124dd031c9644a72024830450221009d58ebfaab8eae297910bca93a7fd48f94ce52a1731cf27fb4c043368fa10e8d02207e88f5d868113d9567999793be0a5b752ad704d04224046839763cefe46463a5012102f6ca5274b59dfb014f6a0d690671964290dac7f97fe825f723204e6cb8daf08600000000", + "blockhash": + "ae1129ee834853c45b9edbb7228497c7fa423d7d1bdec8fd155f9e3c429c84d3", + "confirmations": 150, + "time": 1663142110, + "blocktime": 1663142110 +}; + +final tx3Raw = { + "txid": "71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d", + "hash": "bb25567e1ffb2fd6ec9aa3925a7a8dd3055a29521f7811b2b2bc01ce7d8a216e", + "version": 2, + "size": 370, + "vsize": 208, + "weight": 832, + "locktime": 0, + "vin": [ + { + "txid": + "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7", + "vout": 0, + "scriptSig": {"asm": "", "hex": ""}, + "txinwitness": [ + "304402203535cf570aca7c1acfa6e8d2f43e0b188b76d0b7a75ffca448e6af953ffe8b6302202ea52b312aaaf6d615d722bd92535d1e8b25fa9584a8dbe34dfa1ea9c18105ca01", + "038b68078a95f73f8710e8464dec52c61f9e21675ddf69d4f61b93cc417cf73d74" + ], + "sequence": 4294967295 + }, + { + "txid": + "80f8c6de5be2243013348219bbb7043a6d8d00ddc716baf6a69eab517f9a6fc1", + "vout": 1, + "scriptSig": {"asm": "", "hex": ""}, + "txinwitness": [ + "3044022045268613674326251c46caeaf435081ca753e4ee2018d79480c4930ad7d5e19f022050090a9add82e7272b8206b9d369675e7e9a5f1396fc93490143f0053666102901", + "028e2ede901e69887cb80603c8e207839f61a477d59beff17705162a2045dd974e" + ], + "sequence": 4294967295 + } + ], + "vout": [ + { + "value": 0.01, + "n": 0, + "scriptPubKey": { + "asm": "0 756037000a8676334b35368581a29143fc078471", + "hex": "0014756037000a8676334b35368581a29143fc078471", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qw4srwqq2semrxje4x6zcrg53g07q0pr3yqv5kr"] + } + }, + { + "value": 0.2880577, + "n": 1, + "scriptPubKey": { + "asm": "0 8207ee56ed52878d546567f29d17332b85f66e4b", + "hex": "00148207ee56ed52878d546567f29d17332b85f66e4b", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qsgr7u4hd22rc64r9vlef69en9wzlvmjt8dzyrm"] + } + } + ], + "hex": + "02000000000102d7609f2ebf00afdc6b8cda9a5e92b4b9a0b8aaafadf890fbf99721854395fadf0000000000ffffffffc16f9a7f51ab9ea6f6ba16c7dd008d6d3a04b7bb198234133024e25bdec6f8800100000000ffffffff0240420f0000000000160014756037000a8676334b35368581a29143fc0784718a8ab701000000001600148207ee56ed52878d546567f29d17332b85f66e4b0247304402203535cf570aca7c1acfa6e8d2f43e0b188b76d0b7a75ffca448e6af953ffe8b6302202ea52b312aaaf6d615d722bd92535d1e8b25fa9584a8dbe34dfa1ea9c18105ca0121038b68078a95f73f8710e8464dec52c61f9e21675ddf69d4f61b93cc417cf73d7402473044022045268613674326251c46caeaf435081ca753e4ee2018d79480c4930ad7d5e19f022050090a9add82e7272b8206b9d369675e7e9a5f1396fc93490143f005366610290121028e2ede901e69887cb80603c8e207839f61a477d59beff17705162a2045dd974e00000000", + "blockhash": + "98f388ba99e3b6fc421c23edf3c699ada082b01e5a5d130af7550b7fa6184f2f", + "confirmations": 147, + "time": 1663145287, + "blocktime": 1663145287 +}; + +final tx4Raw = { + "txid": "c7e700f7e23a85bbdd9de86d502322a933607ee7ea7e16adaf02e477cdd849b9", + "hash": "c6b544ddd7d901fcc7218208a6cfc8e1819c403a22cc8a1f1a7029aafa427925", + "version": 2, + "size": 192, + "vsize": 110, + "weight": 438, + "locktime": 0, + "vin": [ + { + "txid": + "71b56532e9e7321bd8c30d0f8b14530743049d2f3edd5623065c46eee1dda04d", + "vout": 0, + "scriptSig": {"asm": "", "hex": ""}, + "txinwitness": [ + "3045022100c664c6ad206999e019954c5206a26c2eca1ae2572288c0f78074c279a4a210ce022017456fdf85f744d694fa2e4638acee782d809268ea4808c04d91da3ac4fe7fd401", + "035456b63e86c0a6235cb3debfb9654966a4c2362ec678ae3b9beec53d31a25eba" + ], + "sequence": 4294967295 + } + ], + "vout": [ + { + "value": 0.00988567, + "n": 0, + "scriptPubKey": { + "asm": "0 db56f49ae171bc6a137bd950cba945eb78fb6d7c", + "hex": "0014db56f49ae171bc6a137bd950cba945eb78fb6d7c", + "reqSigs": 1, + "type": "witness_v0_keyhash", + "addresses": ["nc1qmdt0fxhpwx7x5ymmm9gvh229adu0kmtukfcsjk"] + } + } + ], + "hex": + "020000000001014da0dde1ee465c062356dd3e2f9d04430753148b0f0dc3d81b32e7e93265b5710000000000ffffffff0197150f0000000000160014db56f49ae171bc6a137bd950cba945eb78fb6d7c02483045022100c664c6ad206999e019954c5206a26c2eca1ae2572288c0f78074c279a4a210ce022017456fdf85f744d694fa2e4638acee782d809268ea4808c04d91da3ac4fe7fd40121035456b63e86c0a6235cb3debfb9654966a4c2362ec678ae3b9beec53d31a25eba00000000", + "blockhash": + "6f60029ff3a32ca2d7e7e23c02b9cb35f61e7f9481992f9c3ded2c60c7b1de9b", + "confirmations": 130, + "time": 1663155739, + "blocktime": 1663155739 +}; diff --git a/test/services/coins/namecoin/namecoin_utxo_sample_data.dart b/test/services/coins/namecoin/namecoin_utxo_sample_data.dart new file mode 100644 index 000000000..d54f00f24 --- /dev/null +++ b/test/services/coins/namecoin/namecoin_utxo_sample_data.dart @@ -0,0 +1,58 @@ +import 'package:stackwallet/models/paymint/utxo_model.dart'; + +final Map>> batchGetUTXOResponse0 = { + "some id 0": [ + { + "tx_pos": 0, + "value": 988567, + "tx_hash": + "32dbc0d21327e0cb94ec6069a8d235affd99689ffc5f68959bfb720bafc04bcf", + "height": 629695 + }, + { + "tx_pos": 0, + "value": 1000000, + "tx_hash": + "40c8dd876cf111dc00d3aa2fedc93a77c18b391931939d4f99a760226cbff675", + "height": 629633 + }, + ], + "some id 1": [], +}; + +final utxoList = [ + UtxoObject( + txid: "dffa9543852197f9fb90f8adafaab8a0b9b4925e9ada8c6bdcaf00bf2e9f60d7", + vout: 0, + status: Status( + confirmed: true, + confirmations: 150, + blockHeight: 629695, + blockTime: 1663142110, + blockHash: + "32dbc0d21327e0cb94ec6069a8d235affd99689ffc5f68959bfb720bafc04bcf", + ), + value: 988567, + fiatWorth: "\$0", + txName: "nc1qraffwaq3cxngwp609e03ynwsx8ykgjnjve9f3y", + blocked: false, + isCoinbase: false, + ), + UtxoObject( + txid: "3ef543d0887c3e9f9924f1b2d3b21410d0238937364663ed3414a2c2ddf4ccc6", + vout: 0, + status: Status( + confirmed: true, + confirmations: 212, + blockHeight: 629633, + blockTime: 1663093275, + blockHash: + "40c8dd876cf111dc00d3aa2fedc93a77c18b391931939d4f99a760226cbff675", + ), + value: 1000000, + fiatWorth: "\$0", + txName: "nc1qwfda4s9qmdqpnykgpjf85n09ath983srtuxcqx", + blocked: false, + isCoinbase: false, + ), +]; diff --git a/test/services/coins/namecoin/namecoin_wallet_test.dart b/test/services/coins/namecoin/namecoin_wallet_test.dart new file mode 100644 index 000000000..82238310b --- /dev/null +++ b/test/services/coins/namecoin/namecoin_wallet_test.dart @@ -0,0 +1,4448 @@ +import 'package:bitcoindart/bitcoindart.dart'; +import 'package:decimal/decimal.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:hive/hive.dart'; +import 'package:hive_test/hive_test.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart'; +import 'package:stackwallet/electrumx_rpc/electrumx.dart'; +import 'package:stackwallet/models/paymint/fee_object_model.dart'; +import 'package:stackwallet/models/paymint/transactions_model.dart'; +import 'package:stackwallet/models/paymint/utxo_model.dart'; +import 'package:stackwallet/services/coins/namecoin/namecoin_wallet.dart'; +import 'package:stackwallet/services/price.dart'; +import 'package:stackwallet/services/transaction_notification_tracker.dart'; +import 'package:stackwallet/utilities/enums/coin_enum.dart'; +import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; +import 'package:tuple/tuple.dart'; + +import 'namecoin_history_sample_data.dart'; +import 'namecoin_transaction_data_samples.dart'; +import 'namecoin_utxo_sample_data.dart'; +import 'namecoin_wallet_test.mocks.dart'; +import 'namecoin_wallet_test_parameters.dart'; + +@GenerateMocks( + [ElectrumX, CachedElectrumX, PriceAPI, TransactionNotificationTracker]) +void main() { + group("namecoin constants", () { + test("namecoin minimum confirmations", () async { + expect(MINIMUM_CONFIRMATIONS, 2); + }); + test("namecoin dust limit", () async { + expect(DUST_LIMIT, 294); + }); + test("namecoin mainnet genesis block hash", () async { + expect(GENESIS_HASH_MAINNET, + "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); + }); + }); + + test("namecoin DerivePathType enum", () { + expect(DerivePathType.values.length, 3); + expect(DerivePathType.values.toString(), + "[DerivePathType.bip44, DerivePathType.bip49, DerivePathType.bip84]"); + }); + + group("bip32 node/root", () { + // test("getBip32Root", () { + // final root = getBip32Root(TEST_MNEMONIC, namecoin); + // expect(root.toWIF(), ROOT_WIF); + // }); + + // test("getBip32NodeFromRoot", () { + // final root = getBip32Root(TEST_MNEMONIC, namecoin); + // // two mainnet + // final node44 = getBip32NodeFromRoot(0, 0, root, DerivePathType.bip44); + // expect(node44.toWIF(), NODE_WIF_44); + // final node49 = getBip32NodeFromRoot(0, 0, root, DerivePathType.bip49); + // expect(node49.toWIF(), NODE_WIF_49); + // // and one on testnet + // final node84 = getBip32NodeFromRoot( + // 0, 0, getBip32Root(TEST_MNEMONIC, testnet), DerivePathType.bip84); + // expect(node84.toWIF(), NODE_WIF_84); + // // a bad derive path + // bool didThrow = false; + // try { + // getBip32NodeFromRoot(0, 0, root, null); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // // finally an invalid network + // didThrow = false; + // final invalidNetwork = NetworkType( + // messagePrefix: '\x18hello world\n', + // bech32: 'gg', + // bip32: Bip32Type(public: 0x055521e, private: 0x055555), + // pubKeyHash: 0x55, + // scriptHash: 0x55, + // wif: 0x00); + // try { + // getBip32NodeFromRoot(0, 0, getBip32Root(TEST_MNEMONIC, invalidNetwork), + // DerivePathType.bip44); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // }); + + // test("basic getBip32Node", () { + // final node = + // getBip32Node(0, 0, TEST_MNEMONIC, testnet, DerivePathType.bip84); + // expect(node.toWIF(), NODE_WIF_84); + // }); + }); + + // group("validate testnet namecoin addresses", () { + // MockElectrumX? client; + // MockCachedElectrumX? cachedClient; + // MockPriceAPI? priceAPI; + // FakeSecureStorage? secureStore; + // MockTransactionNotificationTracker? tracker; + // + // NamecoinWallet? testnetWallet; + // + // setUp(() { + // client = MockElectrumX(); + // cachedClient = MockCachedElectrumX(); + // priceAPI = MockPriceAPI(); + // secureStore = FakeSecureStorage(); + // tracker = MockTransactionNotificationTracker(); + // + // testnetWallet = NamecoinWallet( + // walletId: "validateAddressTestNet", + // walletName: "validateAddressTestNet", + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // }); + // + // test("valid testnet namecoin legacy/p2pkh address", () { + // expect( + // testnetWallet?.validateAddress("mhqpGtwhcR6gFuuRjLTpHo41919QfuGy8Y"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("valid testnet namecoin p2sh-p2wpkh address", () { + // expect( + // testnetWallet?.validateAddress("2Mugf9hpSYdQPPLNtWiU2utCi6cM9v5Pnro"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("valid testnet namecoin p2wpkh address", () { + // expect( + // testnetWallet + // ?.validateAddress("tb1qzzlm6mnc8k54mx6akehl8p9ray8r439va5ndyq"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid testnet namecoin legacy/p2pkh address", () { + // expect( + // testnetWallet?.validateAddress("16YB85zQHjro7fqjR2hMcwdQWCX8jNVtr5"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid testnet namecoin p2sh-p2wpkh address", () { + // expect( + // testnetWallet?.validateAddress("3Ns8HuQmkyyKnVixk2yQtG7pN3GcJ6xctk"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid testnet namecoin p2wpkh address", () { + // expect( + // testnetWallet + // ?.validateAddress("bc1qc5ymmsay89r6gr4fy2kklvrkuvzyln4shdvjhf"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // }); + + group("validate mainnet namecoin addresses", () { + MockElectrumX? client; + MockCachedElectrumX? cachedClient; + MockPriceAPI? priceAPI; + FakeSecureStorage? secureStore; + MockTransactionNotificationTracker? tracker; + + NamecoinWallet? mainnetWallet; + + setUp(() { + client = MockElectrumX(); + cachedClient = MockCachedElectrumX(); + priceAPI = MockPriceAPI(); + secureStore = FakeSecureStorage(); + tracker = MockTransactionNotificationTracker(); + + mainnetWallet = NamecoinWallet( + walletId: "validateAddressMainNet", + walletName: "validateAddressMainNet", + coin: Coin.namecoin, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + }); + + // test("valid mainnet legacy/p2pkh address type", () { + // expect( + // mainnetWallet?.addressType( + // address: "nc1qmdt0fxhpwx7x5ymmm9gvh229adu0kmtukfcsjk"), + // DerivePathType.bip44); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("valid mainnet p2sh-p2wpkh address type", () { + // expect( + // mainnetWallet?.addressType( + // address: "3Ns8HuQmkyyKnVixk2yQtG7pN3GcJ6xctk"), + // DerivePathType.bip49); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + + test("valid mainnet bech32 p2wpkh address type", () { + expect( + mainnetWallet?.addressType( + address: "bc1qc5ymmsay89r6gr4fy2kklvrkuvzyln4shdvjhf"), + DerivePathType.bip84); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(tracker); + verifyNoMoreInteractions(priceAPI); + }); + + // test("invalid base58 address type", () { + // expect( + // () => mainnetWallet?.addressType( + // address: "mhqpGtwhcR6gFuuRjLTpHo41919QfuGy8Y"), + // throwsArgumentError); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + + test("invalid bech32 address type", () { + expect( + () => mainnetWallet?.addressType( + address: "tb1qzzlm6mnc8k54mx6akehl8p9ray8r439va5ndyq"), + throwsArgumentError); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(tracker); + verifyNoMoreInteractions(priceAPI); + }); + + test("address has no matching script", () { + expect( + () => mainnetWallet?.addressType( + address: "mpMk94ETazqonHutyC1v6ajshgtP8oiFKU"), + throwsArgumentError); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(tracker); + verifyNoMoreInteractions(priceAPI); + }); + + // test("valid mainnet namecoin legacy/p2pkh address", () { + // expect( + // mainnetWallet?.validateAddress("16YB85zQHjro7fqjR2hMcwdQWCX8jNVtr5"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("valid mainnet namecoin p2sh-p2wpkh address", () { + // expect( + // mainnetWallet?.validateAddress("3Ns8HuQmkyyKnVixk2yQtG7pN3GcJ6xctk"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("valid mainnet namecoin p2wpkh address", () { + // expect( + // mainnetWallet + // ?.validateAddress("bc1qc5ymmsay89r6gr4fy2kklvrkuvzyln4shdvjhf"), + // true); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid mainnet namecoin legacy/p2pkh address", () { + // expect( + // mainnetWallet?.validateAddress("mhqpGtwhcR6gFuuRjLTpHo41919QfuGy8Y"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid mainnet namecoin p2sh-p2wpkh address", () { + // expect( + // mainnetWallet?.validateAddress("2Mugf9hpSYdQPPLNtWiU2utCi6cM9v5Pnro"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("invalid mainnet namecoin p2wpkh address", () { + // expect( + // mainnetWallet + // ?.validateAddress("tb1qzzlm6mnc8k54mx6akehl8p9ray8r439va5ndyq"), + // false); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + }); + + group("testNetworkConnection", () { + MockElectrumX? client; + MockCachedElectrumX? cachedClient; + MockPriceAPI? priceAPI; + FakeSecureStorage? secureStore; + MockTransactionNotificationTracker? tracker; + + NamecoinWallet? btc; + + setUp(() { + client = MockElectrumX(); + cachedClient = MockCachedElectrumX(); + priceAPI = MockPriceAPI(); + secureStore = FakeSecureStorage(); + tracker = MockTransactionNotificationTracker(); + + btc = NamecoinWallet( + walletId: "testNetworkConnection", + walletName: "testNetworkConnection", + coin: Coin.namecoin, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + }); + + test("attempted connection fails due to server error", () async { + when(client?.ping()).thenAnswer((_) async => false); + final bool? result = await btc?.testNetworkConnection(); + expect(result, false); + expect(secureStore?.interactions, 0); + verify(client?.ping()).called(1); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("attempted connection fails due to exception", () async { + when(client?.ping()).thenThrow(Exception); + final bool? result = await btc?.testNetworkConnection(); + expect(result, false); + expect(secureStore?.interactions, 0); + verify(client?.ping()).called(1); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("attempted connection test success", () async { + when(client?.ping()).thenAnswer((_) async => true); + final bool? result = await btc?.testNetworkConnection(); + expect(result, true); + expect(secureStore?.interactions, 0); + verify(client?.ping()).called(1); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + }); + + group("basic getters, setters, and functions", () { + final testWalletId = "BTCtestWalletID"; + final testWalletName = "BTCWallet"; + + MockElectrumX? client; + MockCachedElectrumX? cachedClient; + MockPriceAPI? priceAPI; + FakeSecureStorage? secureStore; + MockTransactionNotificationTracker? tracker; + + NamecoinWallet? btc; + + setUp(() async { + client = MockElectrumX(); + cachedClient = MockCachedElectrumX(); + priceAPI = MockPriceAPI(); + secureStore = FakeSecureStorage(); + tracker = MockTransactionNotificationTracker(); + + btc = NamecoinWallet( + walletId: testWalletId, + walletName: testWalletName, + coin: Coin.namecoin, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + }); + + test("get networkType main", () async { + expect(Coin.namecoin, Coin.namecoin); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get networkType test", () async { + btc = NamecoinWallet( + walletId: testWalletId, + walletName: testWalletName, + coin: Coin.bitcoinTestNet, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + expect(Coin.bitcoinTestNet, Coin.bitcoinTestNet); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get cryptoCurrency", () async { + expect(Coin.namecoin, Coin.namecoin); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get coinName", () async { + expect(Coin.namecoin, Coin.namecoin); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get coinTicker", () async { + expect(Coin.namecoin, Coin.namecoin); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get and set walletName", () async { + expect(Coin.namecoin, Coin.namecoin); + btc?.walletName = "new name"; + expect(btc?.walletName, "new name"); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("estimateTxFee", () async { + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 1), 356); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 900), 356); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 999), 356); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 1000), 356); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 1001), 712); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 1699), 712); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 2000), 712); + expect(btc?.estimateTxFee(vSize: 356, feeRatePerKB: 12345), 4628); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get fees succeeds", () async { + when(client?.ping()).thenAnswer((_) async => true); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_TESTNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.estimateFee(blocks: 1)) + .thenAnswer((realInvocation) async => Decimal.zero); + when(client?.estimateFee(blocks: 5)) + .thenAnswer((realInvocation) async => Decimal.one); + when(client?.estimateFee(blocks: 20)) + .thenAnswer((realInvocation) async => Decimal.ten); + + final fees = await btc?.fees; + expect(fees, isA()); + expect(fees?.slow, 1000000000); + expect(fees?.medium, 100000000); + expect(fees?.fast, 0); + + verify(client?.estimateFee(blocks: 1)).called(1); + verify(client?.estimateFee(blocks: 5)).called(1); + verify(client?.estimateFee(blocks: 20)).called(1); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get fees fails", () async { + when(client?.ping()).thenAnswer((_) async => true); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.estimateFee(blocks: 1)) + .thenAnswer((realInvocation) async => Decimal.zero); + when(client?.estimateFee(blocks: 5)) + .thenAnswer((realInvocation) async => Decimal.one); + when(client?.estimateFee(blocks: 20)) + .thenThrow(Exception("some exception")); + + bool didThrow = false; + try { + await btc?.fees; + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + verify(client?.estimateFee(blocks: 1)).called(1); + verify(client?.estimateFee(blocks: 5)).called(1); + verify(client?.estimateFee(blocks: 20)).called(1); + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + // test("get maxFee", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.estimateFee(blocks: 20)) + // .thenAnswer((realInvocation) async => Decimal.zero); + // when(client?.estimateFee(blocks: 5)) + // .thenAnswer((realInvocation) async => Decimal.one); + // when(client?.estimateFee(blocks: 1)) + // .thenAnswer((realInvocation) async => Decimal.ten); + // + // final maxFee = await btc?.maxFee; + // expect(maxFee, 1000000000); + // + // verify(client?.estimateFee(blocks: 1)).called(1); + // verify(client?.estimateFee(blocks: 5)).called(1); + // verify(client?.estimateFee(blocks: 20)).called(1); + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(tracker); + // verifyNoMoreInteractions(priceAPI); + // }); + }); + + group("Bitcoin service class functions that depend on shared storage", () { + final testWalletId = "BTCtestWalletID"; + final testWalletName = "BTCWallet"; + + bool hiveAdaptersRegistered = false; + + MockElectrumX? client; + MockCachedElectrumX? cachedClient; + MockPriceAPI? priceAPI; + FakeSecureStorage? secureStore; + MockTransactionNotificationTracker? tracker; + + NamecoinWallet? btc; + + setUp(() async { + await setUpTestHive(); + if (!hiveAdaptersRegistered) { + hiveAdaptersRegistered = true; + + // Registering Transaction Model Adapters + Hive.registerAdapter(TransactionDataAdapter()); + Hive.registerAdapter(TransactionChunkAdapter()); + Hive.registerAdapter(TransactionAdapter()); + Hive.registerAdapter(InputAdapter()); + Hive.registerAdapter(OutputAdapter()); + + // Registering Utxo Model Adapters + Hive.registerAdapter(UtxoDataAdapter()); + Hive.registerAdapter(UtxoObjectAdapter()); + Hive.registerAdapter(StatusAdapter()); + + final wallets = await Hive.openBox('wallets'); + await wallets.put('currentWalletName', testWalletName); + } + + client = MockElectrumX(); + cachedClient = MockCachedElectrumX(); + priceAPI = MockPriceAPI(); + secureStore = FakeSecureStorage(); + tracker = MockTransactionNotificationTracker(); + + btc = NamecoinWallet( + walletId: testWalletId, + walletName: testWalletName, + coin: Coin.namecoin, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + }); + + // test("initializeWallet no network", () async { + // when(client?.ping()).thenAnswer((_) async => false); + // expect(await btc?.initializeWallet(), false); + // expect(secureStore?.interactions, 0); + // verify(client?.ping()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("initializeWallet no network exception", () async { + // when(client?.ping()).thenThrow(Exception("Network connection failed")); + // final wallets = await Hive.openBox(testWalletId); + // expect(await btc?.initializeExisting(), false); + // expect(secureStore?.interactions, 0); + // verify(client?.ping()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + test("initializeWallet mainnet throws bad network", () async { + when(client?.ping()).thenAnswer((_) async => true); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + // await btc?.initializeNew(); + final wallets = await Hive.openBox(testWalletId); + + expectLater(() => btc?.initializeExisting(), throwsA(isA())) + .then((_) { + expect(secureStore?.interactions, 0); + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + }); + + test("initializeWallet throws mnemonic overwrite exception", () async { + when(client?.ping()).thenAnswer((_) async => true); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + await secureStore?.write( + key: "${testWalletId}_mnemonic", value: "some mnemonic"); + + final wallets = await Hive.openBox(testWalletId); + expectLater(() => btc?.initializeExisting(), throwsA(isA())) + .then((_) { + expect(secureStore?.interactions, 1); + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + }); + + // test("initializeWallet testnet throws bad network", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // + // expectLater(() => btc?.initializeWallet(), throwsA(isA())) + // .then((_) { + // expect(secureStore?.interactions, 0); + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // }); + + // test("getCurrentNode", () async { + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // expect(await btc?.initializeWallet(), true); + // + // bool didThrow = false; + // try { + // await btc?.getCurrentNode(); + // } catch (_) { + // didThrow = true; + // } + // // expect no nodes on a fresh wallet unless set in db externally + // expect(didThrow, true); + // + // // set node + // final wallet = await Hive.openBox(testWalletId); + // await wallet.put("nodes", { + // "default": { + // "id": "some nodeID", + // "ipAddress": "some address", + // "port": "9000", + // "useSSL": true, + // } + // }); + // await wallet.put("activeNodeID_Bitcoin", "default"); + // + // // try fetching again + // final node = await btc?.getCurrentNode(); + // expect(node.toString(), + // "ElectrumXNode: {address: some address, port: 9000, name: default, useSSL: true}"); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("initializeWallet new main net wallet", () async { + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // expect(await btc?.initializeWallet(), true); + // + // final wallet = await Hive.openBox(testWalletId); + // + // expect(await wallet.get("addressBookEntries"), {}); + // expect(await wallet.get('notes'), null); + // expect(await wallet.get("id"), testWalletId); + // expect(await wallet.get("preferredFiatCurrency"), null); + // expect(await wallet.get("blocked_tx_hashes"), ["0xdefault"]); + // + // final changeAddressesP2PKH = await wallet.get("changeAddressesP2PKH"); + // expect(changeAddressesP2PKH, isA>()); + // expect(changeAddressesP2PKH.length, 1); + // expect(await wallet.get("changeIndexP2PKH"), 0); + // final changeAddressesP2SH = await wallet.get("changeAddressesP2SH"); + // expect(changeAddressesP2SH, isA>()); + // expect(changeAddressesP2SH.length, 1); + // expect(await wallet.get("changeIndexP2SH"), 0); + // final changeAddressesP2WPKH = await wallet.get("changeAddressesP2WPKH"); + // expect(changeAddressesP2WPKH, isA>()); + // expect(changeAddressesP2WPKH.length, 1); + // expect(await wallet.get("changeIndexP2WPKH"), 0); + // + // final receivingAddressesP2PKH = + // await wallet.get("receivingAddressesP2PKH"); + // expect(receivingAddressesP2PKH, isA>()); + // expect(receivingAddressesP2PKH.length, 1); + // expect(await wallet.get("receivingIndexP2PKH"), 0); + // final receivingAddressesP2SH = await wallet.get("receivingAddressesP2SH"); + // expect(receivingAddressesP2SH, isA>()); + // expect(receivingAddressesP2SH.length, 1); + // expect(await wallet.get("receivingIndexP2SH"), 0); + // final receivingAddressesP2WPKH = + // await wallet.get("receivingAddressesP2WPKH"); + // expect(receivingAddressesP2WPKH, isA>()); + // expect(receivingAddressesP2WPKH.length, 1); + // expect(await wallet.get("receivingIndexP2WPKH"), 0); + // + // final p2pkhReceiveDerivations = jsonDecode(await secureStore?.read( + // key: "${testWalletId}_receiveDerivationsP2PKH")); + // expect(p2pkhReceiveDerivations.length, 1); + // final p2shReceiveDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2SH")); + // expect(p2shReceiveDerivations.length, 1); + // final p2wpkhReceiveDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2WPKH")); + // expect(p2wpkhReceiveDerivations.length, 1); + // + // final p2pkhChangeDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2PKH")); + // expect(p2pkhChangeDerivations.length, 1); + // final p2shChangeDerivations = jsonDecode( + // await secureStore.read(key: "${testWalletId}_changeDerivationsP2SH")); + // expect(p2shChangeDerivations.length, 1); + // final p2wpkhChangeDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2WPKH")); + // expect(p2wpkhChangeDerivations.length, 1); + // + // expect(secureStore?.interactions, 26); // 20 in reality + 6 in this test + // expect(secureStore?.reads, 19); // 13 in reality + 6 in this test + // expect(secureStore?.writes, 7); + // expect(secureStore?.deletes, 0); + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("initializeWallet existing main net wallet", () async { + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getBatchHistory(args: anyNamed("args"))) + // .thenAnswer((_) async => {}); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // // init new wallet + // expect(await btc?.initializeWallet(), true); + // + // // fetch data to compare later + // final newWallet = await Hive.openBox(testWalletId); + // + // final addressBookEntries = await newWallet.get("addressBookEntries"); + // final notes = await newWallet.get('notes'); + // final wID = await newWallet.get("id"); + // final currency = await newWallet.get("preferredFiatCurrency"); + // final blockedHashes = await newWallet.get("blocked_tx_hashes"); + // + // final changeAddressesP2PKH = await newWallet.get("changeAddressesP2PKH"); + // final changeIndexP2PKH = await newWallet.get("changeIndexP2PKH"); + // final changeAddressesP2SH = await newWallet.get("changeAddressesP2SH"); + // final changeIndexP2SH = await newWallet.get("changeIndexP2SH"); + // final changeAddressesP2WPKH = + // await newWallet.get("changeAddressesP2WPKH"); + // final changeIndexP2WPKH = await newWallet.get("changeIndexP2WPKH"); + // + // final receivingAddressesP2PKH = + // await newWallet.get("receivingAddressesP2PKH"); + // final receivingIndexP2PKH = await newWallet.get("receivingIndexP2PKH"); + // final receivingAddressesP2SH = + // await newWallet.get("receivingAddressesP2SH"); + // final receivingIndexP2SH = await newWallet.get("receivingIndexP2SH"); + // final receivingAddressesP2WPKH = + // await newWallet.get("receivingAddressesP2WPKH"); + // final receivingIndexP2WPKH = await newWallet.get("receivingIndexP2WPKH"); + // + // final p2pkhReceiveDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2PKH")); + // final p2shReceiveDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2SH")); + // final p2wpkhReceiveDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2WPKH")); + // + // final p2pkhChangeDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2PKH")); + // final p2shChangeDerivations = jsonDecode( + // await secureStore.read(key: "${testWalletId}_changeDerivationsP2SH")); + // final p2wpkhChangeDerivations = jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2WPKH")); + // + // // exit new wallet + // await btc?.exit(); + // + // // open existing/created wallet + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.namecoin, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // + // // init existing + // expect(await btc?.initializeWallet(), true); + // + // // compare data to ensure state matches state of previously closed wallet + // final wallet = await Hive.openBox(testWalletId); + // + // expect(await wallet.get("addressBookEntries"), addressBookEntries); + // expect(await wallet.get('notes'), notes); + // expect(await wallet.get("id"), wID); + // expect(await wallet.get("preferredFiatCurrency"), currency); + // expect(await wallet.get("blocked_tx_hashes"), blockedHashes); + // + // expect(await wallet.get("changeAddressesP2PKH"), changeAddressesP2PKH); + // expect(await wallet.get("changeIndexP2PKH"), changeIndexP2PKH); + // expect(await wallet.get("changeAddressesP2SH"), changeAddressesP2SH); + // expect(await wallet.get("changeIndexP2SH"), changeIndexP2SH); + // expect(await wallet.get("changeAddressesP2WPKH"), changeAddressesP2WPKH); + // expect(await wallet.get("changeIndexP2WPKH"), changeIndexP2WPKH); + // + // expect( + // await wallet.get("receivingAddressesP2PKH"), receivingAddressesP2PKH); + // expect(await wallet.get("receivingIndexP2PKH"), receivingIndexP2PKH); + // expect( + // await wallet.get("receivingAddressesP2SH"), receivingAddressesP2SH); + // expect(await wallet.get("receivingIndexP2SH"), receivingIndexP2SH); + // expect(await wallet.get("receivingAddressesP2WPKH"), + // receivingAddressesP2WPKH); + // expect(await wallet.get("receivingIndexP2WPKH"), receivingIndexP2WPKH); + // + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2PKH")), + // p2pkhReceiveDerivations); + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2SH")), + // p2shReceiveDerivations); + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_receiveDerivationsP2WPKH")), + // p2wpkhReceiveDerivations); + // + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2PKH")), + // p2pkhChangeDerivations); + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2SH")), + // p2shChangeDerivations); + // expect( + // jsonDecode(await secureStore.read( + // key: "${testWalletId}_changeDerivationsP2WPKH")), + // p2wpkhChangeDerivations); + // + // expect(secureStore?.interactions, 32); // 20 in reality + 12 in this test + // expect(secureStore?.reads, 25); // 13 in reality + 12 in this test + // expect(secureStore?.writes, 7); + // expect(secureStore?.deletes, 0); + // verify(client?.ping()).called(2); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // // test("get fiatPrice", () async { + // // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // // await Hive.openBox(testWalletId); + // // expect(await btc.basePrice, Decimal.fromInt(10)); + // // verify(priceAPI.getBitcoinPrice(baseCurrency: "USD")).called(1); + // // verifyNoMoreInteractions(client); + // // verifyNoMoreInteractions(cachedClient); + // // verifyNoMoreInteractions(priceAPI); + // // }); + // + // test("get current receiving addresses", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // await btc?.initializeWallet(); + // expect( + // Address.validateAddress(await btc!.currentReceivingAddress, testnet), + // true); + // expect( + // Address.validateAddress( + // await btc!.currentReceivingAddressP2SH, testnet), + // true); + // expect( + // Address.validateAddress( + // await btc!.currentLegacyReceivingAddress, testnet), + // true); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("get allOwnAddresses", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // await btc?.initializeWallet(); + // final addresses = await btc?.allOwnAddresses; + // expect(addresses, isA>()); + // expect(addresses?.length, 6); + // + // for (int i = 0; i < 6; i++) { + // expect(Address.validateAddress(addresses[i], testnet), true); + // } + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("get utxos and balances", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // + // when(client?.getBatchUTXOs(args: anyNamed("args"))) + // .thenAnswer((_) async => batchGetUTXOResponse0); + // + // when(client?.estimateFee(blocks: 10)) + // .thenAnswer((realInvocation) async => Decimal.zero); + // when(client?.estimateFee(blocks: 5)) + // .thenAnswer((realInvocation) async => Decimal.one); + // when(client?.estimateFee(blocks: 1)) + // .thenAnswer((realInvocation) async => Decimal.ten); + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // + // when(cachedClient?.getTransaction( + // txHash: tx1.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx1Raw); + // when(cachedClient?.getTransaction( + // txHash: tx2.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx2Raw); + // when(cachedClient?.getTransaction( + // txHash: tx3.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx3Raw); + // when(cachedClient?.getTransaction( + // txHash: tx4.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx4Raw); + // + // await btc?.initializeNew(); + // await btc?.initializeExisting(); + // final utxoData = await btc?.utxoData; + // expect(utxoData, isA()); + // expect(utxoData.toString(), + // r"{totalUserCurrency: $0.0076497, satoshiBalance: 76497, bitcoinBalance: 0.00076497, unspentOutputArray: [{txid: 88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c, vout: 0, value: 17000, fiat: $0.0017, blocked: false, status: {confirmed: true, blockHash: 00000000000000198ca8300deab26c5c1ec1df0da5afd30c9faabd340d8fc194, blockHeight: 437146, blockTime: 1652994245, confirmations: 100}}, {txid: b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528, vout: 0, value: 36037, fiat: $0.0036037, blocked: false, status: {confirmed: false, blockHash: 000000000000003db63ad679a539f2088dcc97a149c99ca790ce0c5f7b5acff0, blockHeight: 441696, blockTime: 1652923129, confirmations: 0}}, {txid: dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3, vout: 1, value: 14714, fiat: $0.0014714, blocked: false, status: {confirmed: false, blockHash: 0000000000000030bec9bc58a3ab4857de1cc63cfed74204a6be57f125fb2fa7, blockHeight: 437146, blockTime: 1652888705, confirmations: 0}}, {txid: b39bac02b65af46a49e2985278fe24ca00dd5d627395d88f53e35568a04e10fa, vout: 0, value: 8746, fiat: $0.0008746, blocked: false, status: {confirmed: true, blockHash: 0000000039b80e9a10b7bcaf0f193b51cb870a4febe9b427c1f41a3f42eaa80b, blockHeight: 441696, blockTime: 1652993683, confirmations: 22861}}]}"); + // + // final outputs = await btc?.unspentOutputs; + // expect(outputs, isA>()); + // expect(outputs?.length, 4); + // + // final availableBalance = await btc?.availableBalance; + // expect(availableBalance, Decimal.parse("0.00025746")); + // + // final totalBalance = await btc?.totalBalance; + // expect(totalBalance, Decimal.parse("0.00076497")); + // + // final pendingBalance = await btc?.pendingBalance; + // expect(pendingBalance, Decimal.parse("0.00050751")); + // + // final balanceMinusMaxFee = await btc?.balanceMinusMaxFee; + // expect(balanceMinusMaxFee, Decimal.parse("-9.99974254")); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.estimateFee(blocks: 1)).called(1); + // verify(client?.estimateFee(blocks: 5)).called(1); + // verify(client?.estimateFee(blocks: 10)).called(1); + // verify(client?.getBatchUTXOs(args: anyNamed("args"))).called(1); + // // verify(priceAPI.getBitcoinPrice(baseCurrency: "USD")).called(1); + // verify(cachedClient?.getTransaction( + // txHash: tx1.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // txHash: tx2.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // txHash: tx3.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // txHash: tx4.txid, + // coin: Coin.bitcoinTestNet, + // callOutSideMainIsolate: false)) + // .called(1); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("get utxos - multiple batches", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // + // when(client?.getBatchUTXOs(args: anyNamed("args"))) + // .thenAnswer((_) async => {}); + // + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((realInvocation) async => Decimal.fromInt(10)); + // + // await btc?.initializeNew(); + // await btc?.initializeExisting(); + // + // // add some extra addresses to make sure we have more than the single batch size of 10 + // final wallet = await Hive.openBox(DB); + // final addresses = await wallet.get("receivingAddressesP2WPKH"); + // addresses.add("tb1qpfl2uz3jvazy9wr4vqhwluyhgtd29rsmghpqxp"); + // addresses.add("tb1qznt3psdpcyz8lwj7xxl6q78hjw2mj095nd4gxu"); + // addresses.add("tb1q7yjjyh9h4uy7j0wdtcmptw3g083kxrqlvgjz86"); + // addresses.add("tb1qt05shktwcq7kgxccva20cfwt47kav9s6n8yr9p"); + // addresses.add("tb1q4nk5wdylywl4dg2a45naae7u08vtgyujqfrv58"); + // addresses.add("tb1qxwccgfq9tmd6lx823cuejuea9wdzpaml9wkapm"); + // addresses.add("tb1qk88negkdqusr8tpj0hpvs98lq6ka4vyw6kfnqf"); + // addresses.add("tb1qw0jzneqwp0t4ah9w3za4k9d8d4tz8y3zxqmtgx"); + // addresses.add("tb1qccqjlpndx46sv7t6uurlyyjre5vwjfdzzlf2vd"); + // addresses.add("tb1q3hfpe69rrhr5348xd04rfz9g3h22yk64pwur8v"); + // addresses.add("tb1q4rp373202aur96a28lp0pmts6kp456nka45e7d"); + // await wallet.put("receivingAddressesP2WPKH", addresses); + // + // final utxoData = await btc?.utxoData; + // expect(utxoData, isA()); + // + // final outputs = await btc?.unspentOutputs; + // expect(outputs, isA>()); + // expect(outputs?.length, 0); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchUTXOs(args: anyNamed("args"))).called(2); + // // verify(priceAPI.getBitcoinPrice(baseCurrency: "USD")).called(1); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("get utxos fails", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // + // when(client?.getBatchUTXOs(args: anyNamed("args"))) + // .thenThrow(Exception("some exception")); + // + // await btc?.initializeWallet(); + // final utxoData = await btc?.utxoData; + // expect(utxoData, isA()); + // expect(utxoData.toString(), + // r"{totalUserCurrency: $0.00, satoshiBalance: 0, bitcoinBalance: 0, unspentOutputArray: []}"); + // + // final outputs = await btc?.unspentOutputs; + // expect(outputs, isA>()); + // expect(outputs?.length, 0); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchUTXOs(args: anyNamed("args"))).called(1); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("chain height fetch, update, and get", () async { + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_TESTNET, + // "hash_function": "sha256", + // "services": [] + // }); + // await btc?.initializeWallet(); + // + // // get stored + // expect(await btc?.storedChainHeight, 0); + // + // // fetch fails + // when(client?.getBlockHeadTip()).thenThrow(Exception("Some exception")); + // expect(await btc?.chainHeight, -1); + // + // // fetch succeeds + // when(client?.getBlockHeadTip()).thenAnswer((realInvocation) async => { + // "height": 100, + // "hex": "some block hex", + // }); + // expect(await btc?.chainHeight, 100); + // + // // update + // await btc?.updateStoredChainHeight(newHeight: 1000); + // + // // fetch updated + // expect(await btc?.storedChainHeight, 1000); + // + // verify(client?.ping()).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBlockHeadTip()).called(2); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("fetch and update useBiometrics", () async { + // // get + // expect(await btc?.useBiometrics, false); + // + // // then update + // await btc?.updateBiometricsUsage(true); + // + // // finally check updated + // expect(await btc?.useBiometrics, true); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("getTxCount succeeds", () async { + // when(client?.getHistory( + // scripthash: + // "4e94cc7b4a85791445260ae4403233b6a4784185f9716d73f136c6642615fce9")) + // .thenAnswer((realInvocation) async => [ + // { + // "height": 200004, + // "tx_hash": + // "acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412" + // }, + // { + // "height": 215008, + // "tx_hash": + // "f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403" + // } + // ]); + // + // final count = + // await btc?.getTxCount(address: "3Ns8HuQmkyyKnVixk2yQtG7pN3GcJ6xctk"); + // + // expect(count, 2); + // + // verify(client?.getHistory( + // scripthash: + // "4e94cc7b4a85791445260ae4403233b6a4784185f9716d73f136c6642615fce9")) + // .called(1); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("getTxCount fails", () async { + // when(client?.getHistory( + // scripthash: + // "4e94cc7b4a85791445260ae4403233b6a4784185f9716d73f136c6642615fce9")) + // .thenThrow(Exception("some exception")); + // + // bool didThrow = false; + // try { + // await btc?.getTxCount(address: "3Ns8HuQmkyyKnVixk2yQtG7pN3GcJ6xctk"); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // + // verify(client?.getHistory( + // scripthash: + // "4e94cc7b4a85791445260ae4403233b6a4784185f9716d73f136c6642615fce9")) + // .called(1); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("_checkCurrentReceivingAddressesForTransactions succeeds", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((realInvocation) async => [ + // { + // "height": 200004, + // "tx_hash": + // "acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412" + // }, + // { + // "height": 215008, + // "tx_hash": + // "f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403" + // } + // ]); + // + // await btc?.initializeWallet(); + // + // bool didThrow = false; + // try { + // await btc?.checkCurrentReceivingAddressesForTransactions(); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, false); + // + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(3); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.ping()).called(1); + // + // expect(secureStore?.interactions, 29); + // expect(secureStore?.reads, 19); + // expect(secureStore?.writes, 10); + // expect(secureStore?.deletes, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("_checkCurrentReceivingAddressesForTransactions fails", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenThrow(Exception("some exception")); + // final wallet = await Hive.openBox(testWalletId); + // + // await btc?.initializeNew(); + // await btc?.initializeExisting(); + // + // bool didThrow = false; + // try { + // await btc?.checkCurrentReceivingAddressesForTransactions(); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.ping()).called(1); + // + // expect(secureStore?.interactions, 20); + // expect(secureStore?.reads, 13); + // expect(secureStore?.writes, 7); + // expect(secureStore?.deletes, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("_checkCurrentChangeAddressesForTransactions succeeds", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((realInvocation) async => [ + // { + // "height": 200004, + // "tx_hash": + // "acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412" + // }, + // { + // "height": 215008, + // "tx_hash": + // "f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403" + // } + // ]); + // + // await btc?.initializeWallet(); + // + // bool didThrow = false; + // try { + // await btc?.checkCurrentChangeAddressesForTransactions(); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, false); + // + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(3); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.ping()).called(1); + // + // expect(secureStore?.interactions, 29); + // expect(secureStore?.reads, 19); + // expect(secureStore?.writes, 10); + // expect(secureStore?.deletes, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("_checkCurrentChangeAddressesForTransactions fails", () async { + // when(client?.ping()).thenAnswer((_) async => true); + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenThrow(Exception("some exception")); + // + // await btc?.initializeWallet(); + // + // bool didThrow = false; + // try { + // await btc?.checkCurrentChangeAddressesForTransactions(); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); + // verify(client?.getServerFeatures()).called(1); + // verify(client?.ping()).called(1); + // + // expect(secureStore?.interactions, 20); + // expect(secureStore?.reads, 13); + // expect(secureStore?.writes, 7); + // expect(secureStore?.deletes, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("getAllTxsToWatch", () async { + // TestWidgetsFlutterBinding.ensureInitialized(); + // var notifications = {"show": 0}; + // const MethodChannel('dexterous.com/flutter/local_notifications') + // .setMockMethodCallHandler((call) async { + // notifications[call.method]++; + // }); + // + // btc?.pastUnconfirmedTxs = { + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // "b39bac02b65af46a49e2985278fe24ca00dd5d627395d88f53e35568a04e10fa", + // }; + // + // await btc?.getAllTxsToWatch(transactionData); + // expect(notifications.length, 1); + // expect(notifications["show"], 3); + // + // expect(btc?.unconfirmedTxs, { + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // 'dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3', + // }); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("refreshIfThereIsNewData true A", () async { + // when(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).thenAnswer((_) async => tx2Raw); + // when(client?.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // )).thenAnswer((_) async => tx1Raw); + // + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // final wallet = await Hive.openBox(testWalletId); + // await wallet.put('receivingAddressesP2PKH', []); + // await wallet.put('receivingAddressesP2SH', [ + // "2Mv83bPh2HzPRXptuQg9ejbKpSp87Zi52zT", + // ]); + // await wallet.put('receivingAddressesP2WPKH', [ + // "tb1q3ywehep0ykrkaqkt0hrgsqyns4mnz2ls8nxfzg", + // ]); + // + // await wallet.put('changeAddressesP2PKH', []); + // await wallet.put('changeAddressesP2SH', []); + // await wallet.put('changeAddressesP2WPKH', []); + // + // btc?.unconfirmedTxs = { + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c" + // }; + // + // final result = await btc?.refreshIfThereIsNewData(); + // + // expect(result, true); + // + // verify(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).called(1); + // verify(client.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // )).called(1); + // + // expect(secureStore.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("refreshIfThereIsNewData true B", () async { + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((_) async => Decimal.fromInt(10)); + // + // when(client?.getBatchHistory(args: anyNamed("args"))) + // .thenAnswer((realInvocation) async { + // final uuids = Map>.from( + // realInvocation.namedArguments.values.first) + // .keys + // .toList(growable: false); + // return { + // uuids[0]: [ + // { + // "tx_hash": + // "dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3", + // "height": 2226003 + // }, + // { + // "tx_hash": + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // "height": 2226102 + // } + // ], + // uuids[1]: [ + // { + // "tx_hash": + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // "height": 2226326 + // } + // ], + // }; + // }); + // + // when(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).thenAnswer((_) async => tx2Raw); + // when(client?.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // )).thenAnswer((_) async => tx1Raw); + // + // when(cachedClient?.getTransaction( + // tx_hash: + // "dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx3Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx3Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx1Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "6261002b30122ab3b2ba8c481134e8a3ce08a3a1a429b8ebb3f28228b100ac1a", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx5Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "717080fc0054f655260b1591a0059bf377a589a98284173d20a1c8f3316c086e", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx6Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "1baec51e7630e3640ccf0e34f160c8ad3eb6021ecafe3618a1afae328f320f53", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx7Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "b39bac02b65af46a49e2985278fe24ca00dd5d627395d88f53e35568a04e10fa", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx4Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "46b1f19763ac68e39b8218429f4e29b150f850901562fe44a05fade9e0acd65f", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx8Raw); + // + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // final wallet = await Hive.openBox(testWalletId); + // await wallet.put('receivingAddressesP2PKH', []); + // await wallet.put('receivingAddressesP2SH', [ + // "2Mv83bPh2HzPRXptuQg9ejbKpSp87Zi52zT", + // ]); + // await wallet.put('receivingAddressesP2WPKH', [ + // "tb1q3ywehep0ykrkaqkt0hrgsqyns4mnz2ls8nxfzg", + // ]); + // + // await wallet.put('changeAddressesP2PKH', []); + // await wallet.put('changeAddressesP2SH', []); + // await wallet.put('changeAddressesP2WPKH', []); + // + // btc.unconfirmedTxs = { + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // }; + // + // final result = await btc?.refreshIfThereIsNewData(); + // + // expect(result, true); + // + // verify(client?.getBatchHistory(args: anyNamed("args"))).called(2); + // verify(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: anyNamed("tx_hash"), + // verbose: true, + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .called(9); + // // verify(priceAPI.getBitcoinPrice(baseCurrency: "USD")).called(1); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("refreshIfThereIsNewData false A", () async { + // // when(priceAPI.getBitcoinPrice(baseCurrency: "USD")) + // // .thenAnswer((_) async => Decimal.fromInt(10)); + // + // when(client?.getBatchHistory(args: anyNamed("args"))) + // .thenAnswer((realInvocation) async { + // final uuids = Map>.from( + // realInvocation.namedArguments.values.first) + // .keys + // .toList(growable: false); + // return { + // uuids[0]: [ + // { + // "tx_hash": + // "dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3", + // "height": 2226003 + // }, + // { + // "tx_hash": + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // "height": 2226102 + // } + // ], + // uuids[1]: [ + // { + // "tx_hash": + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // "height": 2226326 + // } + // ], + // }; + // }); + // + // when(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).thenAnswer((_) async => tx2Raw); + // when(client?.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // )).thenAnswer((_) async => tx1Raw); + // + // when(cachedClient?.getTransaction( + // tx_hash: + // "dcca229760b44834478f0b266c9b3f5801e0139fdecacdc0820e447289a006d3", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx3Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx2Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "88b7b5077d940dde1bc63eba37a09dec8e7b9dad14c183a2e879a21b6ec0ac1c", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx1Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "6261002b30122ab3b2ba8c481134e8a3ce08a3a1a429b8ebb3f28228b100ac1a", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx5Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "717080fc0054f655260b1591a0059bf377a589a98284173d20a1c8f3316c086e", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx6Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "1baec51e7630e3640ccf0e34f160c8ad3eb6021ecafe3618a1afae328f320f53", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx7Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "b39bac02b65af46a49e2985278fe24ca00dd5d627395d88f53e35568a04e10fa", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx4Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "46b1f19763ac68e39b8218429f4e29b150f850901562fe44a05fade9e0acd65f", + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx8Raw); + // + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // final wallet = await Hive.openBox(testWalletId); + // await wallet.put('receivingAddressesP2PKH', []); + // await wallet.put('receivingAddressesP2SH', [ + // "2Mv83bPh2HzPRXptuQg9ejbKpSp87Zi52zT", + // ]); + // await wallet.put('receivingAddressesP2WPKH', [ + // "tb1q3ywehep0ykrkaqkt0hrgsqyns4mnz2ls8nxfzg", + // ]); + // + // await wallet.put('changeAddressesP2PKH', []); + // await wallet.put('changeAddressesP2SH', []); + // await wallet.put('changeAddressesP2WPKH', []); + // + // btc?.unconfirmedTxs = { + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // }; + // + // final result = await btc?.refreshIfThereIsNewData(); + // + // expect(result, false); + // + // verify(client?.getBatchHistory(args: anyNamed("args"))).called(2); + // verify(client?.getTransaction( + // tx_hash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: anyNamed("tx_hash"), + // verbose: true, + // coinName: "tBitcoin", + // callOutSideMainIsolate: false)) + // .called(15); + // // verify(priceAPI.getBitcoinPrice(baseCurrency: "USD")).called(1); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + // test("refreshIfThereIsNewData false B", () async { + // when(client?.getBatchHistory(args: anyNamed("args"))) + // .thenThrow(Exception("some exception")); + // + // when(client?.getTransaction( + // txHash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).thenAnswer((_) async => tx2Raw); + // + // btc = NamecoinWallet( + // walletId: testWalletId, + // walletName: testWalletName, + // coin: Coin.bitcoinTestNet, + // client: client!, + // cachedClient: cachedClient!, + // tracker: tracker!, + // priceAPI: priceAPI, + // secureStore: secureStore, + // ); + // final wallet = await Hive.openBox(testWalletId); + // await wallet.put('receivingAddressesP2PKH', []); + // await wallet.put('receivingAddressesP2SH', [ + // "2Mv83bPh2HzPRXptuQg9ejbKpSp87Zi52zT", + // ]); + // await wallet.put('receivingAddressesP2WPKH', [ + // "tb1q3ywehep0ykrkaqkt0hrgsqyns4mnz2ls8nxfzg", + // ]); + // + // await wallet.put('changeAddressesP2PKH', []); + // await wallet.put('changeAddressesP2SH', []); + // await wallet.put('changeAddressesP2WPKH', []); + // + // btc?.txTracker = { + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // }; + // + // // btc?.unconfirmedTxs = { + // // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // // }; + // + // final result = await btc?.refreshIfThereIsNewData(); + // + // expect(result, false); + // + // verify(client?.getBatchHistory(args: anyNamed("args"))).called(1); + // verify(client?.getTransaction( + // txHash: + // "b2f75a017a7435f1b8c2e080a865275d8f80699bba68d8dce99a94606e7b3528", + // )).called(1); + // + // expect(secureStore?.interactions, 0); + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + test( + "recoverFromMnemonic using empty seed on mainnet fails due to bad genesis hash match", + () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_TESTNET, + "hash_function": "sha256", + "services": [] + }); + + bool hasThrown = false; + try { + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, true); + + verify(client?.getServerFeatures()).called(1); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test( + "recoverFromMnemonic using empty seed on testnet fails due to bad genesis hash match", + () async { + btc = NamecoinWallet( + walletId: testWalletId, + walletName: testWalletName, + coin: Coin.bitcoinTestNet, + client: client!, + cachedClient: cachedClient!, + tracker: tracker!, + priceAPI: priceAPI, + secureStore: secureStore, + ); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + + bool hasThrown = false; + try { + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, true); + + verify(client?.getServerFeatures()).called(1); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test( + "recoverFromMnemonic using empty seed on mainnet fails due to attempted overwrite of mnemonic", + () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + + await secureStore?.write( + key: "${testWalletId}_mnemonic", value: "some mnemonic words"); + + bool hasThrown = false; + try { + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, true); + + verify(client?.getServerFeatures()).called(1); + + expect(secureStore?.interactions, 2); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("recoverFromMnemonic using empty seed on mainnet succeeds", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + // await DB.instance.init(); + final wallet = await Hive.openBox(testWalletId); + bool hasThrown = false; + try { + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, false); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(1); + + expect(secureStore?.interactions, 20); + expect(secureStore?.writes, 7); + expect(secureStore?.reads, 13); + expect(secureStore?.deletes, 0); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("get mnemonic list", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => emptyHistoryBatchResponse); + + final wallet = await Hive.openBox(testWalletId); + + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + expect(await btc?.mnemonic, TEST_MNEMONIC.split(" ")); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(1); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("recoverFromMnemonic using non empty seed on mainnet succeeds", + () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => historyBatchResponse); + + List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + if (realInvocation.namedArguments.values.first.length == 1) { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + } + + return historyBatchResponse; + }); + + await Hive.openBox(testWalletId); + + bool hasThrown = false; + try { + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, false); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(1); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + expect(activeScriptHashes.contains(map.values.first.first as String), + true); + } + + expect(secureStore?.interactions, 14); + expect(secureStore?.writes, 7); + expect(secureStore?.reads, 7); + expect(secureStore?.deletes, 0); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("fullRescan succeeds", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => historyBatchResponse); + when(cachedClient?.clearSharedTransactionCache(coin: Coin.namecoin)) + .thenAnswer((realInvocation) async {}); + + List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + if (realInvocation.namedArguments.values.first.length == 1) { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + } + + return historyBatchResponse; + }); + + final wallet = await Hive.openBox(testWalletId); + + // restore so we have something to rescan + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + // fetch valid wallet data + final preReceivingAddressesP2PKH = + await wallet.get('receivingAddressesP2PKH'); + final preReceivingAddressesP2SH = + await wallet.get('receivingAddressesP2SH'); + final preReceivingAddressesP2WPKH = + await wallet.get('receivingAddressesP2WPKH'); + final preChangeAddressesP2PKH = await wallet.get('changeAddressesP2PKH'); + final preChangeAddressesP2SH = await wallet.get('changeAddressesP2SH'); + final preChangeAddressesP2WPKH = + await wallet.get('changeAddressesP2WPKH'); + final preReceivingIndexP2PKH = await wallet.get('receivingIndexP2PKH'); + final preReceivingIndexP2SH = await wallet.get('receivingIndexP2SH'); + final preReceivingIndexP2WPKH = await wallet.get('receivingIndexP2WPKH'); + final preChangeIndexP2PKH = await wallet.get('changeIndexP2PKH'); + final preChangeIndexP2SH = await wallet.get('changeIndexP2SH'); + final preChangeIndexP2WPKH = await wallet.get('changeIndexP2WPKH'); + final preUtxoData = await wallet.get('latest_utxo_model'); + final preReceiveDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2PKH"); + final preChangeDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2PKH"); + final preReceiveDerivationsStringP2SH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2SH"); + final preChangeDerivationsStringP2SH = + await secureStore?.read(key: "${testWalletId}_changeDerivationsP2SH"); + final preReceiveDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2WPKH"); + final preChangeDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2WPKH"); + + // destroy the data that the rescan will fix + await wallet.put( + 'receivingAddressesP2PKH', ["some address", "some other address"]); + await wallet.put( + 'receivingAddressesP2SH', ["some address", "some other address"]); + await wallet.put( + 'receivingAddressesP2WPKH', ["some address", "some other address"]); + await wallet + .put('changeAddressesP2PKH', ["some address", "some other address"]); + await wallet + .put('changeAddressesP2SH', ["some address", "some other address"]); + await wallet + .put('changeAddressesP2WPKH', ["some address", "some other address"]); + await wallet.put('receivingIndexP2PKH', 123); + await wallet.put('receivingIndexP2SH', 123); + await wallet.put('receivingIndexP2WPKH', 123); + await wallet.put('changeIndexP2PKH', 123); + await wallet.put('changeIndexP2SH', 123); + await wallet.put('changeIndexP2WPKH', 123); + await secureStore?.write( + key: "${testWalletId}_receiveDerivationsP2PKH", value: "{}"); + await secureStore?.write( + key: "${testWalletId}_changeDerivationsP2PKH", value: "{}"); + await secureStore?.write( + key: "${testWalletId}_receiveDerivationsP2SH", value: "{}"); + await secureStore?.write( + key: "${testWalletId}_changeDerivationsP2SH", value: "{}"); + await secureStore?.write( + key: "${testWalletId}_receiveDerivationsP2WPKH", value: "{}"); + await secureStore?.write( + key: "${testWalletId}_changeDerivationsP2WPKH", value: "{}"); + + bool hasThrown = false; + try { + await btc?.fullRescan(2, 1000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, false); + + // fetch wallet data again + final receivingAddressesP2PKH = + await wallet.get('receivingAddressesP2PKH'); + final receivingAddressesP2SH = await wallet.get('receivingAddressesP2SH'); + final receivingAddressesP2WPKH = + await wallet.get('receivingAddressesP2WPKH'); + final changeAddressesP2PKH = await wallet.get('changeAddressesP2PKH'); + final changeAddressesP2SH = await wallet.get('changeAddressesP2SH'); + final changeAddressesP2WPKH = await wallet.get('changeAddressesP2WPKH'); + final receivingIndexP2PKH = await wallet.get('receivingIndexP2PKH'); + final receivingIndexP2SH = await wallet.get('receivingIndexP2SH'); + final receivingIndexP2WPKH = await wallet.get('receivingIndexP2WPKH'); + final changeIndexP2PKH = await wallet.get('changeIndexP2PKH'); + final changeIndexP2SH = await wallet.get('changeIndexP2SH'); + final changeIndexP2WPKH = await wallet.get('changeIndexP2WPKH'); + final utxoData = await wallet.get('latest_utxo_model'); + final receiveDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2PKH"); + final changeDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2PKH"); + final receiveDerivationsStringP2SH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2SH"); + final changeDerivationsStringP2SH = + await secureStore?.read(key: "${testWalletId}_changeDerivationsP2SH"); + final receiveDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2WPKH"); + final changeDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2WPKH"); + + expect(preReceivingAddressesP2PKH, receivingAddressesP2PKH); + expect(preReceivingAddressesP2SH, receivingAddressesP2SH); + expect(preReceivingAddressesP2WPKH, receivingAddressesP2WPKH); + expect(preChangeAddressesP2PKH, changeAddressesP2PKH); + expect(preChangeAddressesP2SH, changeAddressesP2SH); + expect(preChangeAddressesP2WPKH, changeAddressesP2WPKH); + expect(preReceivingIndexP2PKH, receivingIndexP2PKH); + expect(preReceivingIndexP2SH, receivingIndexP2SH); + expect(preReceivingIndexP2WPKH, receivingIndexP2WPKH); + expect(preChangeIndexP2PKH, changeIndexP2PKH); + expect(preChangeIndexP2SH, changeIndexP2SH); + expect(preChangeIndexP2WPKH, changeIndexP2WPKH); + expect(preUtxoData, utxoData); + expect(preReceiveDerivationsStringP2PKH, receiveDerivationsStringP2PKH); + expect(preChangeDerivationsStringP2PKH, changeDerivationsStringP2PKH); + expect(preReceiveDerivationsStringP2SH, receiveDerivationsStringP2SH); + expect(preChangeDerivationsStringP2SH, changeDerivationsStringP2SH); + expect(preReceiveDerivationsStringP2WPKH, receiveDerivationsStringP2WPKH); + expect(preChangeDerivationsStringP2WPKH, changeDerivationsStringP2WPKH); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(2); + verify(cachedClient?.clearSharedTransactionCache(coin: Coin.namecoin)) + .called(1); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + expect(activeScriptHashes.contains(map.values.first.first as String), + true); + } + + expect(secureStore?.writes, 25); + expect(secureStore?.reads, 32); + expect(secureStore?.deletes, 6); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("fullRescan fails", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => historyBatchResponse); + when(cachedClient?.clearSharedTransactionCache(coin: Coin.namecoin)) + .thenAnswer((realInvocation) async {}); + + List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + if (realInvocation.namedArguments.values.first.length == 1) { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + } + + return historyBatchResponse; + }); + + final wallet = await Hive.openBox(testWalletId); + + // restore so we have something to rescan + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + // fetch wallet data + final preReceivingAddressesP2PKH = + await wallet.get('receivingAddressesP2PKH'); + final preReceivingAddressesP2SH = + await wallet.get('receivingAddressesP2SH'); + final preReceivingAddressesP2WPKH = + await wallet.get('receivingAddressesP2WPKH'); + final preChangeAddressesP2PKH = await wallet.get('changeAddressesP2PKH'); + final preChangeAddressesP2SH = await wallet.get('changeAddressesP2SH'); + final preChangeAddressesP2WPKH = + await wallet.get('changeAddressesP2WPKH'); + final preReceivingIndexP2PKH = await wallet.get('receivingIndexP2PKH'); + final preReceivingIndexP2SH = await wallet.get('receivingIndexP2SH'); + final preReceivingIndexP2WPKH = await wallet.get('receivingIndexP2WPKH'); + final preChangeIndexP2PKH = await wallet.get('changeIndexP2PKH'); + final preChangeIndexP2SH = await wallet.get('changeIndexP2SH'); + final preChangeIndexP2WPKH = await wallet.get('changeIndexP2WPKH'); + final preUtxoData = await wallet.get('latest_utxo_model'); + final preReceiveDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2PKH"); + final preChangeDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2PKH"); + final preReceiveDerivationsStringP2SH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2SH"); + final preChangeDerivationsStringP2SH = + await secureStore?.read(key: "${testWalletId}_changeDerivationsP2SH"); + final preReceiveDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2WPKH"); + final preChangeDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2WPKH"); + + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenThrow(Exception("fake exception")); + + bool hasThrown = false; + try { + await btc?.fullRescan(2, 1000); + } catch (_) { + hasThrown = true; + } + expect(hasThrown, true); + + // fetch wallet data again + final receivingAddressesP2PKH = + await wallet.get('receivingAddressesP2PKH'); + final receivingAddressesP2SH = await wallet.get('receivingAddressesP2SH'); + final receivingAddressesP2WPKH = + await wallet.get('receivingAddressesP2WPKH'); + final changeAddressesP2PKH = await wallet.get('changeAddressesP2PKH'); + final changeAddressesP2SH = await wallet.get('changeAddressesP2SH'); + final changeAddressesP2WPKH = await wallet.get('changeAddressesP2WPKH'); + final receivingIndexP2PKH = await wallet.get('receivingIndexP2PKH'); + final receivingIndexP2SH = await wallet.get('receivingIndexP2SH'); + final receivingIndexP2WPKH = await wallet.get('receivingIndexP2WPKH'); + final changeIndexP2PKH = await wallet.get('changeIndexP2PKH'); + final changeIndexP2SH = await wallet.get('changeIndexP2SH'); + final changeIndexP2WPKH = await wallet.get('changeIndexP2WPKH'); + final utxoData = await wallet.get('latest_utxo_model'); + final receiveDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2PKH"); + final changeDerivationsStringP2PKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2PKH"); + final receiveDerivationsStringP2SH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2SH"); + final changeDerivationsStringP2SH = + await secureStore?.read(key: "${testWalletId}_changeDerivationsP2SH"); + final receiveDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_receiveDerivationsP2WPKH"); + final changeDerivationsStringP2WPKH = await secureStore?.read( + key: "${testWalletId}_changeDerivationsP2WPKH"); + + expect(preReceivingAddressesP2PKH, receivingAddressesP2PKH); + expect(preReceivingAddressesP2SH, receivingAddressesP2SH); + expect(preReceivingAddressesP2WPKH, receivingAddressesP2WPKH); + expect(preChangeAddressesP2PKH, changeAddressesP2PKH); + expect(preChangeAddressesP2SH, changeAddressesP2SH); + expect(preChangeAddressesP2WPKH, changeAddressesP2WPKH); + expect(preReceivingIndexP2PKH, receivingIndexP2PKH); + expect(preReceivingIndexP2SH, receivingIndexP2SH); + expect(preReceivingIndexP2WPKH, receivingIndexP2WPKH); + expect(preChangeIndexP2PKH, changeIndexP2PKH); + expect(preChangeIndexP2SH, changeIndexP2SH); + expect(preChangeIndexP2WPKH, changeIndexP2WPKH); + expect(preUtxoData, utxoData); + expect(preReceiveDerivationsStringP2PKH, receiveDerivationsStringP2PKH); + expect(preChangeDerivationsStringP2PKH, changeDerivationsStringP2PKH); + expect(preReceiveDerivationsStringP2SH, receiveDerivationsStringP2SH); + expect(preChangeDerivationsStringP2SH, changeDerivationsStringP2SH); + expect(preReceiveDerivationsStringP2WPKH, receiveDerivationsStringP2WPKH); + expect(preChangeDerivationsStringP2WPKH, changeDerivationsStringP2WPKH); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(2); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(2); + verify(cachedClient?.clearSharedTransactionCache(coin: Coin.namecoin)) + .called(1); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + expect(activeScriptHashes.contains(map.values.first.first as String), + true); + } + + expect(secureStore?.writes, 19); + expect(secureStore?.reads, 32); + expect(secureStore?.deletes, 12); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + // test("fetchBuildTxData succeeds", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to trigger all change code branches + // final chg44 = await secureStore?.read( + // key: testWalletId + "_changeDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_changeDerivationsP2PKH", + // value: chg44?.replaceFirst("1vFHF5q21GccoBwrB4zEUAs9i3Bfx797U", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final chg49 = + // await secureStore?.read(key: testWalletId + "_changeDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_changeDerivationsP2SH", + // value: chg49?.replaceFirst("3ANTVqufTH1tLAuoQHhng8jndRsA9hcNy7", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final chg84 = await secureStore?.read( + // key: testWalletId + "_changeDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_changeDerivationsP2WPKH", + // value: chg84?.replaceFirst( + // "bc1qn2x7h96kufgfjxtkhsnq03jqwqde8zasffqvd2", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final data = await btc?.fetchBuildTxData(utxoList); + // + // expect(data?.length, 3); + // expect( + // data?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // ?.length, + // 2); + // expect( + // data?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // .length, + // 3); + // expect( + // data?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // .length, + // 2); + // expect( + // data?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // ["output"], + // isA()); + // expect( + // data?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["output"], + // isA()); + // expect( + // data?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // ["output"], + // isA()); + // expect( + // data?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // ["keyPair"], + // isA()); + // expect( + // data?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["keyPair"], + // isA()); + // expect( + // data?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // ["keyPair"], + // isA()); + // expect( + // data?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["redeemScript"], + // isA()); + // + // // modify addresses to trigger all receiving code branches + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final data2 = await btc?.fetchBuildTxData(utxoList); + // + // expect(data2?.length, 3); + // expect( + // data2?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // .length, + // 2); + // expect( + // data2?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // .length, + // 3); + // expect( + // data2?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // .length, + // 2); + // expect( + // data2?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // ["output"], + // isA()); + // expect( + // data2?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["output"], + // isA()); + // expect( + // data2?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // ["output"], + // isA()); + // expect( + // data2?["2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703"] + // ["keyPair"], + // isA()); + // expect( + // data2?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["keyPair"], + // isA()); + // expect( + // data2?["3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4"] + // ["keyPair"], + // isA()); + // expect( + // data2?["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["redeemScript"], + // isA()); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(2); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(2); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(2); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 38); + // expect(secureStore?.writes, 13); + // expect(secureStore?.reads, 25); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("fetchBuildTxData throws", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenThrow(Exception("some exception")); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // bool didThrow = false; + // try { + // await btc?.fetchBuildTxData(utxoList); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 14); + // expect(secureStore?.writes, 7); + // expect(secureStore?.reads, 7); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("build transaction succeeds", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final data = await btc?.fetchBuildTxData(utxoList); + // + // final txData = await btc?.buildTransaction( + // utxosToUse: utxoList, + // utxoSigningData: data!, + // recipients: ["bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc"], + // satoshiAmounts: [13000]); + // + // expect(txData?.length, 2); + // expect(txData?["hex"], isA()); + // expect(txData?["vSize"], isA()); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 26); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 16); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("build transaction fails", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final data = await btc?.fetchBuildTxData(utxoList); + // + // // give bad data toi build tx + // data["ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7"] + // ["keyPair"] = null; + // + // bool didThrow = false; + // try { + // await btc?.buildTransaction( + // utxosToUse: utxoList, + // utxoSigningData: data!, + // recipients: ["bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc"], + // satoshiAmounts: [13000]); + // } catch (_) { + // didThrow = true; + // } + // expect(didThrow, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 26); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 16); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("two output coinSelection succeeds", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((_) async => [ + // {"height": 1000, "tx_hash": "some tx hash"} + // ]); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 18000, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, isA>()); + // expect(result.length > 0, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); + // + // expect(secureStore?.interactions, 29); + // expect(secureStore?.writes, 11); + // expect(secureStore?.reads, 18); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("one output option A coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((_) async => [ + // {"height": 1000, "tx_hash": "some tx hash"} + // ]); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 18500, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, isA>()); + // expect(result.length > 0, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 26); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 16); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("one output option B coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((_) async => [ + // {"height": 1000, "tx_hash": "some tx hash"} + // ]); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = + // await secureStore?.read(key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 18651, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, isA>()); + // expect(result.length > 0, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 26); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 16); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("insufficient funds option A coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = + // await secureStore?.read(key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 20000, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, 1); + // + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 20); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 10); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("insufficient funds option B coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = + // await secureStore?.read(key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 19000, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, 2); + // + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // + // expect(secureStore?.interactions, 20); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 10); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("insufficient funds option C coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient.?getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = + // await secureStore?.read(key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // final result = await btc?.coinSelection( + // 18900, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, 2); + // + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // + // expect(secureStore?.interactions, 26); + // expect(secureStore?.writes, 10); + // expect(secureStore?.reads, 16); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("check for more outputs coinSelection", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = + // await secureStore?.read(key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((_) async => [ + // {"height": 1000, "tx_hash": "some tx hash"} + // ]); + // + // final result = await btc?.coinSelection( + // 11900, 1000, "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // utxos: utxoList); + // + // expect(result, isA>()); + // expect(result.length > 0, true); + // + // verify(client?.getServerFeatures()).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(2); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(2); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); + // + // expect(secureStore?.interactions, 33); + // expect(secureStore?.writes, 11); + // expect(secureStore?.reads, 22); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + // + // test("prepareSend and confirmSend succeed", () async { + // when(client?.getServerFeatures()).thenAnswer((_) async => { + // "hosts": {}, + // "pruning": null, + // "server_version": "Unit tests", + // "protocol_min": "1.4", + // "protocol_max": "1.4.2", + // "genesis_hash": GENESIS_HASH_MAINNET, + // "hash_function": "sha256", + // "services": [] + // }); + // when(client?.getBatchHistory(args: historyBatchArgs0)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getBatchHistory(args: historyBatchArgs1)) + // .thenAnswer((_) async => historyBatchResponse); + // when(client?.getHistory(scripthash: anyNamed("scripthash"))) + // .thenAnswer((_) async => [ + // {"height": 1000, "tx_hash": "some tx hash"} + // ]); + // when(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx9Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx10Raw); + // when(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .thenAnswer((_) async => tx11Raw); + // + // // recover to fill data + // await btc?.recoverFromMnemonic( + // mnemonic: TEST_MNEMONIC, + // maxUnusedAddressGap: 2, + // maxNumberOfIndexesToCheck: 1000); + // + // // modify addresses to properly mock data to build a tx + // final rcv44 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2PKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2PKH", + // value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + // "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + // final rcv49 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2SH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2SH", + // value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + // "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + // final rcv84 = await secureStore?.read( + // key: testWalletId + "_receiveDerivationsP2WPKH"); + // await secureStore?.write( + // key: testWalletId + "_receiveDerivationsP2WPKH", + // value: rcv84?.replaceFirst( + // "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + // "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + // + // btc?.outputsList = utxoList; + // + // final result = await btc?.prepareSend( + // toAddress: "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + // amount: 15000); + // + // expect(result, isA>()); + // expect(result?.length! > 0, true); + // + // when(client?.broadcastTransaction( + // rawTx: result!["hex"], requestID: anyNamed("requestID"))) + // .thenAnswer((_) async => "some txHash"); + // + // final sentResult = await btc?.confirmSend(txData: result!); + // expect(sentResult, "some txHash"); + // + // verify(client?.getServerFeatures()).called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // tx_hash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coinName: "Bitcoin", + // callOutSideMainIsolate: false)) + // .called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + // verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + // verify(client?.broadcastTransaction( + // rawTx: result!["hex"], requestID: anyNamed("requestID"))) + // .called(1); + // verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(1); + // + // expect(secureStore?.interactions, 29); + // expect(secureStore?.writes, 11); + // expect(secureStore?.reads, 18); + // expect(secureStore?.deletes, 0); + // + // verifyNoMoreInteractions(client); + // verifyNoMoreInteractions(cachedClient); + // verifyNoMoreInteractions(priceAPI); + // }); + + test("prepareSend fails", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => historyBatchResponse); + + List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + if (realInvocation.namedArguments.values.first.length == 1) { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + } + + return historyBatchResponse; + }); + + await Hive.openBox(testWalletId); + + when(cachedClient?.getTransaction( + txHash: + "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + coin: Coin.namecoin)) + .thenAnswer((_) async => tx9Raw); + when(cachedClient?.getTransaction( + txHash: + "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + coin: Coin.namecoin)) + .thenAnswer((_) async => tx10Raw); + when(cachedClient?.getTransaction( + txHash: + "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + coin: Coin.namecoin, + )).thenAnswer((_) async => tx11Raw); + + // recover to fill data + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + // modify addresses to properly mock data to build a tx + final rcv44 = await secureStore?.read( + key: testWalletId + "_receiveDerivationsP2PKH"); + await secureStore?.write( + key: testWalletId + "_receiveDerivationsP2PKH", + value: rcv44?.replaceFirst("1RMSPixoLPuaXuhR2v4HsUMcRjLncKDaw", + "16FuTPaeRSPVxxCnwQmdyx2PQWxX6HWzhQ")); + final rcv49 = await secureStore?.read( + key: testWalletId + "_receiveDerivationsP2SH"); + await secureStore?.write( + key: testWalletId + "_receiveDerivationsP2SH", + value: rcv49?.replaceFirst("3AV74rKfibWmvX34F99yEvUcG4LLQ9jZZk", + "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g")); + final rcv84 = await secureStore?.read( + key: testWalletId + "_receiveDerivationsP2WPKH"); + await secureStore?.write( + key: testWalletId + "_receiveDerivationsP2WPKH", + value: rcv84?.replaceFirst( + "bc1qggtj4ka8jsaj44hhd5mpamx7mp34m2d3w7k0m0", + "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc")); + + btc?.outputsList = utxoList; + + bool didThrow = false; + try { + await btc?.prepareSend( + address: "bc1q42lja79elem0anu8q8s3h2n687re9jax556pcc", + satoshiAmount: 15000); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + verify(client?.getServerFeatures()).called(1); + + /// verify transaction no matching calls + + // verify(cachedClient?.getTransaction( + // txHash: + // "2087ce09bc316877c9f10971526a2bffa3078d52ea31752639305cdcd8230703", + // coin: Coin.namecoin, + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // txHash: + // "ed32c967a0e86d51669ac21c2bb9bc9c50f0f55fbacdd8db21d0a986fba93bd7", + // coin: Coin.namecoin, + // callOutSideMainIsolate: false)) + // .called(1); + // verify(cachedClient?.getTransaction( + // txHash: + // "3f0032f89ac44b281b50314cff3874c969c922839dddab77ced54e86a21c3fd4", + // coin: Coin.namecoin, + // callOutSideMainIsolate: false)) + // .called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(1); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + expect(activeScriptHashes.contains(map.values.first.first as String), + true); + } + + expect(secureStore?.interactions, 20); + expect(secureStore?.writes, 10); + expect(secureStore?.reads, 10); + expect(secureStore?.deletes, 0); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("confirmSend no hex", () async { + bool didThrow = false; + try { + await btc?.confirmSend(txData: {"some": "strange map"}); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("confirmSend hex is not string", () async { + bool didThrow = false; + try { + await btc?.confirmSend(txData: {"hex": true}); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("confirmSend hex is string but missing other data", () async { + bool didThrow = false; + try { + await btc?.confirmSend(txData: {"hex": "a string"}); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + verify(client?.broadcastTransaction( + rawTx: "a string", requestID: anyNamed("requestID"))) + .called(1); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("confirmSend fails due to vSize being greater than fee", () async { + bool didThrow = false; + try { + await btc + ?.confirmSend(txData: {"hex": "a string", "fee": 1, "vSize": 10}); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + verify(client?.broadcastTransaction( + rawTx: "a string", requestID: anyNamed("requestID"))) + .called(1); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + test("confirmSend fails when broadcast transactions throws", () async { + when(client?.broadcastTransaction( + rawTx: "a string", requestID: anyNamed("requestID"))) + .thenThrow(Exception("some exception")); + + bool didThrow = false; + try { + await btc + ?.confirmSend(txData: {"hex": "a string", "fee": 10, "vSize": 10}); + } catch (_) { + didThrow = true; + } + + expect(didThrow, true); + + verify(client?.broadcastTransaction( + rawTx: "a string", requestID: anyNamed("requestID"))) + .called(1); + + expect(secureStore?.interactions, 0); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(tracker); + verifyNoMoreInteractions(priceAPI); + }); + // + // // this test will create a non mocked electrumx client that will try to connect + // // to the provided ipAddress below. This will throw a bunch of errors + // // which what we want here as actually calling electrumx calls here is unwanted. + // // test("listen to NodesChangedEvent", () async { + // // btc = NamecoinWallet( + // // walletId: testWalletId, + // // walletName: testWalletName, + // // networkType: BasicNetworkType.test, + // // client: client, + // // cachedClient: cachedClient, + // // priceAPI: priceAPI, + // // secureStore: secureStore, + // // ); + // // + // // // set node + // // final wallet = await Hive.openBox(testWalletId); + // // await wallet.put("nodes", { + // // "default": { + // // "id": "some nodeID", + // // "ipAddress": "some address", + // // "port": "9000", + // // "useSSL": true, + // // } + // // }); + // // await wallet.put("activeNodeID_Bitcoin", "default"); + // // + // // final a = btc.cachedElectrumXClient; + // // + // // // return when refresh is called on node changed trigger + // // btc.longMutex = true; + // // + // // GlobalEventBus.instance + // // .fire(NodesChangedEvent(NodesChangedEventType.updatedCurrentNode)); + // // + // // // make sure event has processed before continuing + // // await Future.delayed(Duration(seconds: 5)); + // // + // // final b = btc.cachedElectrumXClient; + // // + // // expect(identical(a, b), false); + // // + // // await btc.exit(); + // // + // // expect(secureStore.interactions, 0); + // // verifyNoMoreInteractions(client); + // // verifyNoMoreInteractions(cachedClient); + // // verifyNoMoreInteractions(priceAPI); + // // }); + + test("refresh wallet mutex locked", () async { + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getBatchHistory(args: historyBatchArgs0)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs1)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs2)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs3)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs4)) + .thenAnswer((_) async => historyBatchResponse); + when(client?.getBatchHistory(args: historyBatchArgs5)) + .thenAnswer((_) async => historyBatchResponse); + + List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + if (realInvocation.namedArguments.values.first.length == 1) { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + } + + return historyBatchResponse; + }); + + await Hive.openBox(testWalletId); + + // recover to fill data + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + btc?.refreshMutex = true; + + await btc?.refresh(); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs0)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs1)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs2)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs3)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs4)).called(1); + verify(client?.getBatchHistory(args: historyBatchArgs5)).called(1); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + expect(activeScriptHashes.contains(map.values.first.first as String), + true); + } + + expect(secureStore?.interactions, 14); + expect(secureStore?.writes, 7); + expect(secureStore?.reads, 7); + expect(secureStore?.deletes, 0); + + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(tracker); + verifyNoMoreInteractions(priceAPI); + }); + + test("refresh wallet normally", () async { + when(client?.getBlockHeadTip()).thenAnswer((realInvocation) async => + {"height": 520481, "hex": "some block hex"}); + when(client?.getServerFeatures()).thenAnswer((_) async => { + "hosts": {}, + "pruning": null, + "server_version": "Unit tests", + "protocol_min": "1.4", + "protocol_max": "1.4.2", + "genesis_hash": GENESIS_HASH_MAINNET, + "hash_function": "sha256", + "services": [] + }); + when(client?.getHistory(scripthash: anyNamed("scripthash"))) + .thenAnswer((_) async => []); + when(client?.estimateFee(blocks: anyNamed("blocks"))) + .thenAnswer((_) async => Decimal.one); + + when(priceAPI?.getPricesAnd24hChange(baseCurrency: "USD")) + .thenAnswer((_) async => {Coin.namecoin: Tuple2(Decimal.one, 0.3)}); + + final List dynamicArgValues = []; + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((realInvocation) async { + dynamicArgValues.add(realInvocation.namedArguments.values.first); + return historyBatchResponse; + }); + + await Hive.openBox(testWalletId); + + // recover to fill data + await btc?.recoverFromMnemonic( + mnemonic: TEST_MNEMONIC, + maxUnusedAddressGap: 2, + maxNumberOfIndexesToCheck: 1000, + height: 4000); + + when(client?.getBatchHistory(args: anyNamed("args"))) + .thenAnswer((_) async => {}); + when(client?.getBatchUTXOs(args: anyNamed("args"))) + .thenAnswer((_) async => emptyHistoryBatchResponse); + + await btc?.refresh(); + + verify(client?.getServerFeatures()).called(1); + verify(client?.getHistory(scripthash: anyNamed("scripthash"))).called(4); + verify(client?.estimateFee(blocks: anyNamed("blocks"))).called(3); + verify(client?.getBlockHeadTip()).called(1); + verify(priceAPI?.getPricesAnd24hChange(baseCurrency: "USD")).called(2); + + for (final arg in dynamicArgValues) { + final map = Map>.from(arg as Map); + + verify(client?.getBatchHistory(args: map)).called(1); + } + + expect(secureStore?.interactions, 14); + expect(secureStore?.writes, 7); + expect(secureStore?.reads, 7); + expect(secureStore?.deletes, 0); + + // verifyNoMoreInteractions(client); + verifyNoMoreInteractions(cachedClient); + verifyNoMoreInteractions(priceAPI); + }); + + tearDown(() async { + await tearDownTestHive(); + }); + }); +} diff --git a/test/services/coins/namecoin/namecoin_wallet_test.mocks.dart b/test/services/coins/namecoin/namecoin_wallet_test.mocks.dart new file mode 100644 index 000000000..d86949a61 --- /dev/null +++ b/test/services/coins/namecoin/namecoin_wallet_test.mocks.dart @@ -0,0 +1,352 @@ +// Mocks generated by Mockito 5.2.0 from annotations +// in stackwallet/test/services/coins/namecoin/namecoin_wallet_test.dart. +// Do not manually edit this file. + +import 'dart:async' as _i6; + +import 'package:decimal/decimal.dart' as _i2; +import 'package:http/http.dart' as _i4; +import 'package:mockito/mockito.dart' as _i1; +import 'package:stackwallet/electrumx_rpc/cached_electrumx.dart' as _i7; +import 'package:stackwallet/electrumx_rpc/electrumx.dart' as _i5; +import 'package:stackwallet/services/price.dart' as _i9; +import 'package:stackwallet/services/transaction_notification_tracker.dart' + as _i11; +import 'package:stackwallet/utilities/enums/coin_enum.dart' as _i8; +import 'package:stackwallet/utilities/prefs.dart' as _i3; +import 'package:tuple/tuple.dart' as _i10; + +// ignore_for_file: type=lint +// ignore_for_file: avoid_redundant_argument_values +// ignore_for_file: avoid_setters_without_getters +// ignore_for_file: comment_references +// ignore_for_file: implementation_imports +// ignore_for_file: invalid_use_of_visible_for_testing_member +// ignore_for_file: prefer_const_constructors +// ignore_for_file: unnecessary_parenthesis +// ignore_for_file: camel_case_types + +class _FakeDecimal_0 extends _i1.Fake implements _i2.Decimal {} + +class _FakePrefs_1 extends _i1.Fake implements _i3.Prefs {} + +class _FakeClient_2 extends _i1.Fake implements _i4.Client {} + +/// A class which mocks [ElectrumX]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockElectrumX extends _i1.Mock implements _i5.ElectrumX { + MockElectrumX() { + _i1.throwOnMissingStub(this); + } + + @override + set failovers(List<_i5.ElectrumXNode>? _failovers) => + super.noSuchMethod(Invocation.setter(#failovers, _failovers), + returnValueForMissingStub: null); + @override + int get currentFailoverIndex => + (super.noSuchMethod(Invocation.getter(#currentFailoverIndex), + returnValue: 0) as int); + @override + set currentFailoverIndex(int? _currentFailoverIndex) => super.noSuchMethod( + Invocation.setter(#currentFailoverIndex, _currentFailoverIndex), + returnValueForMissingStub: null); + @override + String get host => + (super.noSuchMethod(Invocation.getter(#host), returnValue: '') as String); + @override + int get port => + (super.noSuchMethod(Invocation.getter(#port), returnValue: 0) as int); + @override + bool get useSSL => + (super.noSuchMethod(Invocation.getter(#useSSL), returnValue: false) + as bool); + @override + _i6.Future request( + {String? command, + List? args = const [], + Duration? connectionTimeout = const Duration(seconds: 60), + String? requestID, + int? retries = 2}) => + (super.noSuchMethod( + Invocation.method(#request, [], { + #command: command, + #args: args, + #connectionTimeout: connectionTimeout, + #requestID: requestID, + #retries: retries + }), + returnValue: Future.value()) as _i6.Future); + @override + _i6.Future>> batchRequest( + {String? command, + Map>? args, + Duration? connectionTimeout = const Duration(seconds: 60), + int? retries = 2}) => + (super.noSuchMethod( + Invocation.method(#batchRequest, [], { + #command: command, + #args: args, + #connectionTimeout: connectionTimeout, + #retries: retries + }), + returnValue: Future>>.value( + >[])) + as _i6.Future>>); + @override + _i6.Future ping({String? requestID, int? retryCount = 1}) => + (super.noSuchMethod( + Invocation.method( + #ping, [], {#requestID: requestID, #retryCount: retryCount}), + returnValue: Future.value(false)) as _i6.Future); + @override + _i6.Future> getBlockHeadTip({String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getBlockHeadTip, [], {#requestID: requestID}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future> getServerFeatures({String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getServerFeatures, [], {#requestID: requestID}), + returnValue: + Future>.value({})) as _i6 + .Future>); + @override + _i6.Future broadcastTransaction({String? rawTx, String? requestID}) => + (super.noSuchMethod( + Invocation.method(#broadcastTransaction, [], + {#rawTx: rawTx, #requestID: requestID}), + returnValue: Future.value('')) as _i6.Future); + @override + _i6.Future> getBalance( + {String? scripthash, String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getBalance, [], + {#scripthash: scripthash, #requestID: requestID}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future>> getHistory( + {String? scripthash, String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getHistory, [], + {#scripthash: scripthash, #requestID: requestID}), + returnValue: Future>>.value( + >[])) + as _i6.Future>>); + @override + _i6.Future>>> getBatchHistory( + {Map>? args}) => + (super.noSuchMethod( + Invocation.method(#getBatchHistory, [], {#args: args}), + returnValue: Future>>>.value( + >>{})) as _i6 + .Future>>>); + @override + _i6.Future>> getUTXOs( + {String? scripthash, String? requestID}) => + (super.noSuchMethod( + Invocation.method( + #getUTXOs, [], {#scripthash: scripthash, #requestID: requestID}), + returnValue: Future>>.value( + >[])) as _i6 + .Future>>); + @override + _i6.Future>>> getBatchUTXOs( + {Map>? args}) => + (super.noSuchMethod(Invocation.method(#getBatchUTXOs, [], {#args: args}), + returnValue: Future>>>.value( + >>{})) as _i6 + .Future>>>); + @override + _i6.Future> getTransaction( + {String? txHash, bool? verbose = true, String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getTransaction, [], + {#txHash: txHash, #verbose: verbose, #requestID: requestID}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future> getAnonymitySet( + {String? groupId = r'1', + String? blockhash = r'', + String? requestID}) => + (super.noSuchMethod( + Invocation.method(#getAnonymitySet, [], { + #groupId: groupId, + #blockhash: blockhash, + #requestID: requestID + }), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future getMintData({dynamic mints, String? requestID}) => + (super.noSuchMethod( + Invocation.method( + #getMintData, [], {#mints: mints, #requestID: requestID}), + returnValue: Future.value()) as _i6.Future); + @override + _i6.Future> getUsedCoinSerials( + {String? requestID, int? startNumber}) => + (super.noSuchMethod( + Invocation.method(#getUsedCoinSerials, [], + {#requestID: requestID, #startNumber: startNumber}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future getLatestCoinId({String? requestID}) => (super.noSuchMethod( + Invocation.method(#getLatestCoinId, [], {#requestID: requestID}), + returnValue: Future.value(0)) as _i6.Future); + @override + _i6.Future> getFeeRate({String? requestID}) => (super + .noSuchMethod(Invocation.method(#getFeeRate, [], {#requestID: requestID}), + returnValue: + Future>.value({})) as _i6 + .Future>); + @override + _i6.Future<_i2.Decimal> estimateFee({String? requestID, int? blocks}) => + (super.noSuchMethod( + Invocation.method( + #estimateFee, [], {#requestID: requestID, #blocks: blocks}), + returnValue: Future<_i2.Decimal>.value(_FakeDecimal_0())) + as _i6.Future<_i2.Decimal>); + @override + _i6.Future<_i2.Decimal> relayFee({String? requestID}) => (super.noSuchMethod( + Invocation.method(#relayFee, [], {#requestID: requestID}), + returnValue: Future<_i2.Decimal>.value(_FakeDecimal_0())) + as _i6.Future<_i2.Decimal>); +} + +/// A class which mocks [CachedElectrumX]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockCachedElectrumX extends _i1.Mock implements _i7.CachedElectrumX { + MockCachedElectrumX() { + _i1.throwOnMissingStub(this); + } + + @override + String get server => + (super.noSuchMethod(Invocation.getter(#server), returnValue: '') + as String); + @override + int get port => + (super.noSuchMethod(Invocation.getter(#port), returnValue: 0) as int); + @override + bool get useSSL => + (super.noSuchMethod(Invocation.getter(#useSSL), returnValue: false) + as bool); + @override + _i3.Prefs get prefs => (super.noSuchMethod(Invocation.getter(#prefs), + returnValue: _FakePrefs_1()) as _i3.Prefs); + @override + List<_i5.ElectrumXNode> get failovers => + (super.noSuchMethod(Invocation.getter(#failovers), + returnValue: <_i5.ElectrumXNode>[]) as List<_i5.ElectrumXNode>); + @override + _i6.Future> getAnonymitySet( + {String? groupId, String? blockhash = r'', _i8.Coin? coin}) => + (super.noSuchMethod( + Invocation.method(#getAnonymitySet, [], + {#groupId: groupId, #blockhash: blockhash, #coin: coin}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future> getTransaction( + {String? txHash, _i8.Coin? coin, bool? verbose = true}) => + (super.noSuchMethod( + Invocation.method(#getTransaction, [], + {#txHash: txHash, #coin: coin, #verbose: verbose}), + returnValue: + Future>.value({})) + as _i6.Future>); + @override + _i6.Future> getUsedCoinSerials( + {_i8.Coin? coin, int? startNumber = 0}) => + (super.noSuchMethod( + Invocation.method(#getUsedCoinSerials, [], + {#coin: coin, #startNumber: startNumber}), + returnValue: Future>.value([])) + as _i6.Future>); + @override + _i6.Future clearSharedTransactionCache({_i8.Coin? coin}) => + (super.noSuchMethod( + Invocation.method(#clearSharedTransactionCache, [], {#coin: coin}), + returnValue: Future.value(), + returnValueForMissingStub: Future.value()) as _i6.Future); +} + +/// A class which mocks [PriceAPI]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockPriceAPI extends _i1.Mock implements _i9.PriceAPI { + MockPriceAPI() { + _i1.throwOnMissingStub(this); + } + + @override + _i4.Client get client => (super.noSuchMethod(Invocation.getter(#client), + returnValue: _FakeClient_2()) as _i4.Client); + @override + void resetLastCalledToForceNextCallToUpdateCache() => super.noSuchMethod( + Invocation.method(#resetLastCalledToForceNextCallToUpdateCache, []), + returnValueForMissingStub: null); + @override + _i6.Future>> + getPricesAnd24hChange({String? baseCurrency}) => (super.noSuchMethod( + Invocation.method( + #getPricesAnd24hChange, [], {#baseCurrency: baseCurrency}), + returnValue: + Future>>.value( + <_i8.Coin, _i10.Tuple2<_i2.Decimal, double>>{})) + as _i6.Future>>); +} + +/// A class which mocks [TransactionNotificationTracker]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockTransactionNotificationTracker extends _i1.Mock + implements _i11.TransactionNotificationTracker { + MockTransactionNotificationTracker() { + _i1.throwOnMissingStub(this); + } + + @override + String get walletId => + (super.noSuchMethod(Invocation.getter(#walletId), returnValue: '') + as String); + @override + List get pendings => + (super.noSuchMethod(Invocation.getter(#pendings), returnValue: []) + as List); + @override + List get confirmeds => (super + .noSuchMethod(Invocation.getter(#confirmeds), returnValue: []) + as List); + @override + bool wasNotifiedPending(String? txid) => + (super.noSuchMethod(Invocation.method(#wasNotifiedPending, [txid]), + returnValue: false) as bool); + @override + _i6.Future addNotifiedPending(String? txid) => + (super.noSuchMethod(Invocation.method(#addNotifiedPending, [txid]), + returnValue: Future.value(), + returnValueForMissingStub: Future.value()) as _i6.Future); + @override + bool wasNotifiedConfirmed(String? txid) => + (super.noSuchMethod(Invocation.method(#wasNotifiedConfirmed, [txid]), + returnValue: false) as bool); + @override + _i6.Future addNotifiedConfirmed(String? txid) => + (super.noSuchMethod(Invocation.method(#addNotifiedConfirmed, [txid]), + returnValue: Future.value(), + returnValueForMissingStub: Future.value()) as _i6.Future); +} diff --git a/test/services/coins/namecoin/namecoin_wallet_test_parameters.dart b/test/services/coins/namecoin/namecoin_wallet_test_parameters.dart new file mode 100644 index 000000000..e69de29bb