From 1e67f3585aa7edc821afe809402108b220437fa8 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 25 Jan 2024 02:20:37 -0600 Subject: [PATCH] some frost clean up --- lib/db/isar/main_db.dart | 2 + .../restore/restore_frost_ms_wallet_view.dart | 6 +-- .../frost_attempt_sign_config_view.dart | 17 +++---- .../frost_ms/frost_complete_sign_view.dart | 4 +- .../frost_continue_sign_config_view.dart | 18 +++---- .../frost_import_sign_config_view.dart | 4 +- .../send_view/frost_ms/frost_send_view.dart | 2 +- lib/route_generator.dart | 1 + .../wallet/impl/bitcoin_frost_wallet.dart | 49 +++++++++++++++---- 9 files changed, 69 insertions(+), 34 deletions(-) diff --git a/lib/db/isar/main_db.dart b/lib/db/isar/main_db.dart index 528c99f98..ac5a544f4 100644 --- a/lib/db/isar/main_db.dart +++ b/lib/db/isar/main_db.dart @@ -21,6 +21,7 @@ import 'package:stackwallet/models/isar/stack_theme.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/stack_file_system.dart'; +import 'package:stackwallet/wallets/isar/models/frost_wallet_info.dart'; import 'package:stackwallet/wallets/isar/models/spark_coin.dart'; import 'package:stackwallet/wallets/isar/models/token_wallet_info.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; @@ -67,6 +68,7 @@ class MainDB { SparkCoinSchema, WalletInfoMetaSchema, TokenWalletInfoSchema, + FrostWalletInfoSchema, ], directory: (await StackFileSystem.applicationIsarDirectory()).path, // inspector: kDebugMode, diff --git a/lib/pages/add_wallet_views/frost_ms/restore/restore_frost_ms_wallet_view.dart b/lib/pages/add_wallet_views/frost_ms/restore/restore_frost_ms_wallet_view.dart index e99655f06..e316f22c8 100644 --- a/lib/pages/add_wallet_views/frost_ms/restore/restore_frost_ms_wallet_view.dart +++ b/lib/pages/add_wallet_views/frost_ms/restore/restore_frost_ms_wallet_view.dart @@ -4,7 +4,7 @@ import 'package:barcode_scan2/barcode_scan2.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:frostdart/frostdart.dart'; +import 'package:frostdart/frostdart.dart' as frost; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages/home_view/home_view.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_home_view.dart'; @@ -70,7 +70,7 @@ class _RestoreFrostMsWalletViewState final keys = keysFieldController.text; final config = configFieldController.text; - final myNameIndex = getParticipantIndexFromKeys(serializedKeys: keys); + final myNameIndex = frost.getParticipantIndexFromKeys(serializedKeys: keys); final participants = Frost.getParticipants(multisigConfig: config); final myName = participants[myNameIndex]; @@ -92,7 +92,7 @@ class _RestoreFrostMsWalletViewState knownSalts: [], participants: participants, myName: myName, - threshold: multisigThreshold( + threshold: frost.multisigThreshold( multisigConfig: config, ), ); diff --git a/lib/pages/send_view/frost_ms/frost_attempt_sign_config_view.dart b/lib/pages/send_view/frost_ms/frost_attempt_sign_config_view.dart index 64b33e9f9..7cdf4a69b 100644 --- a/lib/pages/send_view/frost_ms/frost_attempt_sign_config_view.dart +++ b/lib/pages/send_view/frost_ms/frost_attempt_sign_config_view.dart @@ -7,13 +7,13 @@ import 'package:stackwallet/pages/send_view/frost_ms/frost_continue_sign_config_ import 'package:stackwallet/pages/wallet_view/transaction_views/transaction_details_view.dart'; import 'package:stackwallet/providers/frost_wallet/frost_wallet_providers.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart'; -import 'package:stackwallet/services/coins/bitcoin/frost_wallet.dart'; import 'package:stackwallet/services/frost.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; +import 'package:stackwallet/wallets/wallet/impl/bitcoin_frost_wallet.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; @@ -72,15 +72,14 @@ class _FrostAttemptSignConfigViewState @override void initState() { - final wallet = ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) - .wallet as FrostWallet; + final wallet = + ref.read(pWallets).getWallet(widget.walletId) as BitcoinFrostWallet; + final frostInfo = wallet.frostInfo; - myName = wallet.myName; - threshold = wallet.threshold; - participantsWithoutMe = wallet.participants; - myIndex = participantsWithoutMe.indexOf(wallet.myName); + myName = frostInfo.myName; + threshold = frostInfo.threshold; + participantsWithoutMe = frostInfo.participants; + myIndex = participantsWithoutMe.indexOf(frostInfo.myName); myPreprocess = ref.read(pFrostAttemptSignData.state).state!.preprocess; participantsWithoutMe.removeAt(myIndex); diff --git a/lib/pages/send_view/frost_ms/frost_complete_sign_view.dart b/lib/pages/send_view/frost_ms/frost_complete_sign_view.dart index c63dca04d..6478495c0 100644 --- a/lib/pages/send_view/frost_ms/frost_complete_sign_view.dart +++ b/lib/pages/send_view/frost_ms/frost_complete_sign_view.dart @@ -139,8 +139,8 @@ class _FrostCompleteSignViewState extends ConsumerState { Exception? ex; final txData = await showLoading( whileFuture: ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) + .read(pWallets) + .getWallet(widget.walletId) .confirmSend( txData: ref.read(pFrostTxData.state).state!, ), diff --git a/lib/pages/send_view/frost_ms/frost_continue_sign_config_view.dart b/lib/pages/send_view/frost_ms/frost_continue_sign_config_view.dart index 9d6494c62..732fb2f82 100644 --- a/lib/pages/send_view/frost_ms/frost_continue_sign_config_view.dart +++ b/lib/pages/send_view/frost_ms/frost_continue_sign_config_view.dart @@ -9,13 +9,13 @@ import 'package:stackwallet/pages/wallet_view/wallet_view.dart'; import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart'; import 'package:stackwallet/providers/frost_wallet/frost_wallet_providers.dart'; import 'package:stackwallet/providers/global/wallets_provider.dart'; -import 'package:stackwallet/services/coins/bitcoin/frost_wallet.dart'; import 'package:stackwallet/services/frost.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; +import 'package:stackwallet/wallets/wallet/impl/bitcoin_frost_wallet.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; @@ -61,17 +61,17 @@ class _FrostContinueSignViewState extends ConsumerState { @override void initState() { - final wallet = ref - .read(walletsChangeNotifierProvider) - .getManager(widget.walletId) - .wallet as FrostWallet; + final wallet = + ref.read(pWallets).getWallet(widget.walletId) as BitcoinFrostWallet; - myName = wallet.myName; - participantsAll = wallet.participants; - myIndex = wallet.participants.indexOf(wallet.myName); + final frostInfo = wallet.frostInfo; + + myName = frostInfo.myName; + participantsAll = frostInfo.participants; + myIndex = frostInfo.participants.indexOf(frostInfo.myName); myShare = ref.read(pFrostContinueSignData.state).state!.share; - participantsWithoutMe = wallet.participants + participantsWithoutMe = frostInfo.participants .toSet() .intersection( ref.read(pFrostSelectParticipantsUnordered.state).state!.toSet()) diff --git a/lib/pages/send_view/frost_ms/frost_import_sign_config_view.dart b/lib/pages/send_view/frost_ms/frost_import_sign_config_view.dart index b390e0b67..c89b6846a 100644 --- a/lib/pages/send_view/frost_ms/frost_import_sign_config_view.dart +++ b/lib/pages/send_view/frost_ms/frost_import_sign_config_view.dart @@ -92,7 +92,9 @@ class _FrostImportSignConfigViewState // TODO add more data from 'data' and display to user ? ref.read(pFrostTxData.notifier).state = TxData( frostMSConfig: config, - recipients: data.recipients, + recipients: data.recipients + .map((e) => (address: e.address, amount: e.amount, isChange: false)) + .toList(), utxos: utxos.toSet(), ); diff --git a/lib/pages/send_view/frost_ms/frost_send_view.dart b/lib/pages/send_view/frost_ms/frost_send_view.dart index cf64d8e54..1865556b7 100644 --- a/lib/pages/send_view/frost_ms/frost_send_view.dart +++ b/lib/pages/send_view/frost_ms/frost_send_view.dart @@ -84,7 +84,7 @@ class _FrostSendViewState extends ConsumerState { final recipients = recipientWidgetIndexes .map((i) => ref.read(pRecipient(i).state).state) - .map((e) => (address: e!.address, amount: e.amount!, isChange: false)) + .map((e) => (address: e!.address, amount: e!.amount!, isChange: false)) .toList(growable: false); final txData = await wallet.frostCreateSignConfig( diff --git a/lib/route_generator.dart b/lib/route_generator.dart index 87e29d64c..26e24653c 100644 --- a/lib/route_generator.dart +++ b/lib/route_generator.dart @@ -83,6 +83,7 @@ import 'package:stackwallet/pages/receive_view/addresses/wallet_addresses_view.d import 'package:stackwallet/pages/receive_view/generate_receiving_uri_qr_code_view.dart'; import 'package:stackwallet/pages/receive_view/receive_view.dart'; import 'package:stackwallet/pages/send_view/confirm_transaction_view.dart'; +import 'package:stackwallet/pages/send_view/frost_ms/frost_send_view.dart'; import 'package:stackwallet/pages/send_view/send_view.dart'; import 'package:stackwallet/pages/send_view/token_send_view.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/about_view.dart'; diff --git a/lib/wallets/wallet/impl/bitcoin_frost_wallet.dart b/lib/wallets/wallet/impl/bitcoin_frost_wallet.dart index 4058b8b59..be3b7b821 100644 --- a/lib/wallets/wallet/impl/bitcoin_frost_wallet.dart +++ b/lib/wallets/wallet/impl/bitcoin_frost_wallet.dart @@ -29,12 +29,6 @@ import 'package:stackwallet/wallets/models/tx_data.dart'; import 'package:stackwallet/wallets/wallet/wallet.dart'; class BitcoinFrostWallet extends Wallet { - @override - int get isarTransactionVersion => 2; - - @override - bool get supportsMultiRecipient => true; - BitcoinFrostWallet(CryptoCurrencyNetwork network) : super(BitcoinFrost(network) as T); @@ -89,7 +83,9 @@ class BitcoinFrostWallet extends Wallet { await _saveMultisigId(multisigId); await _saveMultisigConfig(multisigConfig); - await mainDB.isar.frostWalletInfo.put(frostWalletInfo); + await mainDB.isar.writeTxn(() async { + await mainDB.isar.frostWalletInfo.put(frostWalletInfo); + }); final keys = frost.deserializeKeys(keys: serializedKeys); @@ -299,6 +295,9 @@ class BitcoinFrostWallet extends Wallet { // ==================== Overrides ============================================ + @override + bool get supportsMultiRecipient => true; + @override int get isarTransactionVersion => 2; @@ -527,8 +526,40 @@ class BitcoinFrostWallet extends Wallet { @override Future checkSaveInitialReceivingAddress() async { - // should not be needed for frost as we explicitly save the address - // on new init and restore + final address = await getCurrentReceivingAddress(); + if (address == null) { + final serializedKeys = await getSerializedKeys(); + if (serializedKeys != null) { + final keys = frost.deserializeKeys(keys: serializedKeys); + + final addressString = frost.addressForKeys( + network: cryptoCurrency.network == CryptoCurrencyNetwork.main + ? Network.Mainnet + : Network.Testnet, + keys: keys, + ); + + final publicKey = frost.scriptPubKeyForKeys(keys: keys); + + final address = Address( + walletId: walletId, + value: addressString, + publicKey: publicKey.toUint8ListFromHex, + derivationIndex: 0, + derivationPath: null, + subType: AddressSubType.receiving, + type: AddressType.frostMS, + ); + + await mainDB.updateOrPutAddresses([address]); + } else { + Logging.instance.log( + "$runtimeType.checkSaveInitialReceivingAddress() failed due" + " to missing serialized keys", + level: LogLevel.Fatal, + ); + } + } } @override