CakeWallet/lib/view_model/payjoin_details_view_model.dart

123 lines
4.2 KiB
Dart
Raw Permalink Normal View History

implement-payjoin (#1949) * Initial Payjoin * Initial Payjoin * More payjoin stuff * Minor fixes * Minor fixes * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Fix minor bug causes by data inconsistency in the btc utxos * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Initial Payjoin * Initial Payjoin * More payjoin stuff * Minor fixes * Minor fixes * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Fix minor bug causes by data inconsistency in the btc utxos * Minor cleanup * Minor cleanup * Minor cleanup * Minor cleanup * Fix Rebase issues * Move PJ Receiver to isolate * Add Payjoin Setting * Payjoin Sender are now isolated * Added Payjoin sessions to tx overview. Fix Fee issue with payjoin * Clean up code * Fix taproot for payjoin * Fix CI Errors * Add Payjoin UI elements and details page * Add Payjoin UI elements and details page * Fix Translations * feat: Detect Payjoin URIs in pasted text and show to the User sending Payjoin * feat: rename pjUri to payjoinURI for more code clarity * Update res/values/strings_pl.arb Co-authored-by: cyan <cyjan@mrcyjanek.net> * Update cw_bitcoin/lib/payjoin/manager.dart Co-authored-by: cyan <cyjan@mrcyjanek.net> * Update cw_bitcoin/lib/payjoin/manager.dart Co-authored-by: cyan <cyjan@mrcyjanek.net> * feat: Disable Payjoin per default * feat: Disable Payjoin fully if disabled or no Inputs available * feat: Resume Payjoin if app comes back to foreground * chore: Revert overly aggressive code formats * feat: show correct Payjoin amount for receivers * feat: Improved payjoin status * feat: Show payjoin errors on payjoin details screen * deps: update flutter to 3.27.4 * feat: Revert localisations * bug: Remove duplicate transaction id on payjoin details * style: remove double await in payjoin sender * refactor(cw_bitcoin): Refactor method signatures and convert constructor to factory * refactor(cw_bitcoin): Refactor wallet service and PSBT signer for cleaner code Removed unnecessary `CakeHive` dependency and refactored `BitcoinWallet` initialization to use `payjoinSessionSource`. Improved code readability in `PsbtSigner` by reformatting lines and simplifying constructor methods for `UtxoWithPrivateKey`. * fix: Resume Payjoin Sessions and load PJUri after sleep * feat: Add "Copy Payjoin URL button" to receive screen * fix: Add "Payjoin enabled"-Box below QR Code on the receive screen * fix: Set payjoin_enabled color to black independent of the theme * refactor: Payjoin session management and cleanup unused code. --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com> Co-authored-by: cyan <cyjan@mrcyjanek.net>
2025-05-12 19:33:14 +02:00
import 'dart:async';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/screens/trade_details/trade_details_list_card.dart';
import 'package:cake_wallet/src/screens/trade_details/trade_details_status_item.dart';
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/utils/date_formatter.dart';
import 'package:cw_core/payjoin_session.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:flutter/widgets.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:mobx/mobx.dart';
part 'payjoin_details_view_model.g.dart';
class PayjoinDetailsViewModel = PayjoinDetailsViewModelBase
with _$PayjoinDetailsViewModel;
abstract class PayjoinDetailsViewModelBase with Store {
PayjoinDetailsViewModelBase(
this.payjoinSessionId,
this.transactionInfo, {
required this.payjoinSessionSource,
required this.settingsStore,
}) : items = ObservableList<StandartListItem>(),
payjoinSession = payjoinSessionSource.get(payjoinSessionId)! {
listener = payjoinSessionSource.watch().listen((e) {
if (e.key == payjoinSessionId) _updateItems();
});
_updateItems();
}
final Box<PayjoinSession> payjoinSessionSource;
final SettingsStore settingsStore;
final String payjoinSessionId;
final TransactionInfo? transactionInfo;
@observable
late PayjoinSession payjoinSession;
final ObservableList<StandartListItem> items;
late final StreamSubscription<BoxEvent> listener;
Timer? timer;
@action
void _updateItems() {
final dateFormat = DateFormatter.withCurrentLocal();
items.clear();
items.addAll([
DetailsListStatusItem(
title: S.current.status,
value: _getStatusString(),
status: payjoinSession.status,
),
TradeDetailsListCardItem(
id: "${payjoinSession.isSenderSession ? S.current.outgoing : S.current.incoming} Payjoin",
createdAt:
dateFormat.format(payjoinSession.inProgressSince!).toString(),
pair:
'${bitcoin!.formatterBitcoinAmountToString(amount: payjoinSession.amount.toInt())} BTC',
onTap: (_) {},
),
if (payjoinSession.error?.isNotEmpty == true)
StandartListItem(
title: S.current.error,
value: payjoinSession.error!,
),
if (payjoinSession.txId?.isNotEmpty == true)
StandartListItem(
title: S.current.transaction_details_transaction_id,
value: payjoinSession.txId!,
key: ValueKey('standard_list_item_transaction_details_id_key'),
)
]);
if (transactionInfo != null) {
items.addAll([
StandartListItem(
title: S.current.transaction_details_date,
value: dateFormat.format(transactionInfo!.date),
key: ValueKey('standard_list_item_transaction_details_date_key'),
),
StandartListItem(
title: S.current.confirmations,
value: transactionInfo!.confirmations.toString(),
key: ValueKey('standard_list_item_transaction_confirmations_key'),
),
StandartListItem(
title: S.current.transaction_details_height,
value: '${transactionInfo!.height}',
key: ValueKey('standard_list_item_transaction_details_height_key'),
),
if (transactionInfo!.feeFormatted()?.isNotEmpty ?? false)
StandartListItem(
title: S.current.transaction_details_fee,
value: transactionInfo!.feeFormatted()!,
key: ValueKey('standard_list_item_transaction_details_fee_key'),
),
]);
}
}
String _getStatusString() {
switch (payjoinSession.status) {
case 'success':
if (transactionInfo?.isPending == true)
return S.current.payjoin_request_awaiting_tx;
return S.current.successful;
case 'inProgress':
return S.current.payjoin_request_in_progress;
case 'unrecoverable':
return S.current.error;
default:
return payjoinSession.status;
}
}
}