Skip to content

Commit

Permalink
feat: payjoin is disabled if utxos are empty for the selected wallet –>
Browse files Browse the repository at this point in the history
Close #357
  • Loading branch information
ethicnology committed Dec 23, 2024
1 parent 209e4c8 commit e9816ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
76 changes: 20 additions & 56 deletions lib/receive/bloc/receive_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ class ReceiveCubit extends Cubit<ReceiveState> {
final PayjoinManager _payjoinManager;

Future<void> updatePayjoinEndpoint(String payjoinEndpoint) async {
emit(
state.copyWith(
payjoinEndpoint: payjoinEndpoint,
),
);
emit(state.copyWith(payjoinEndpoint: payjoinEndpoint));
return;
}

Expand All @@ -45,7 +41,6 @@ class ReceiveCubit extends Cubit<ReceiveState> {
state.copyWith(
walletBloc: walletBloc,
defaultAddress: null,
// privateLabel: '',
savedDescription: '',
description: '',
),
Expand All @@ -60,24 +55,19 @@ class ReceiveCubit extends Cubit<ReceiveState> {
emit(state.copyWith(paymentNetwork: PaymentNetwork.bitcoin));
}

// final watchOnly = walletBloc.state.wallet!.watchOnly();
// if (watchOnly)
// emit(state.copyWith(paymentNetwork: ReceivePaymentNetwork.bitcoin));
await isPayjoinEnabled();
await loadAddress();

if (state.paymentNetwork == PaymentNetwork.bitcoin &&
state.defaultAddress != null) {
state.defaultAddress != null &&
state.isPayjoin) {
receivePayjoin(
state.walletBloc!.state.wallet!.isTestnet(),
state.defaultAddress!.address,
);
} else {
// Clear payjoin receiver
emit(
state.copyWith(
payjoinReceiver: null,
),
);
emit(state.copyWith(payjoinReceiver: null));
}
}

Expand All @@ -86,8 +76,6 @@ class ReceiveCubit extends Cubit<ReceiveState> {
bool isTestnet, {
bool onStart = false,
}) {
// if (!isTestnet) return;

if (!state.allowedSwitch(selectedPaymentNetwork)) return;

if (onStart) {
Expand Down Expand Up @@ -124,33 +112,6 @@ class ReceiveCubit extends Cubit<ReceiveState> {
emit(state.copyWith(switchToInstant: true));
return;
}

// if (walletType == BBWalletType.instant &&
// currentPayNetwork != ReceivePaymentNetwork.bitcoin &&
// selectedPaymentNetwork == ReceivePaymentNetwork.bitcoin) {
// emit(state.copyWith(switchToSecure: true));
// return;
// }

// if (walletType == BBWalletType.instant &&
// currentPayNetwork != ReceivePaymentNetwork.liquid &&
// selectedPaymentNetwork == ReceivePaymentNetwork.liquid) {
// return;
// }

// if (walletType == BBWalletType.secure &&
// currentPayNetwork != ReceivePaymentNetwork.lightning &&
// selectedPaymentNetwork == ReceivePaymentNetwork.lightning) {
// // Allow LN -> BTC swap
// return;
// }

// if (walletType == BBWalletType.secure &&
// currentPayNetwork != ReceivePaymentNetwork.liquid &&
// selectedPaymentNetwork == ReceivePaymentNetwork.liquid) {
// // Allow LBTC -> BTC swap
// return;
// }
}

void clearSwitch() {
Expand Down Expand Up @@ -255,26 +216,19 @@ class ReceiveCubit extends Cubit<ReceiveState> {
if (address == null) return;

if (!isLiq && state.defaultAddress != null) {
emit(
state.copyWith(description: address.label ?? ''),
);
emit(state.copyWith(description: address.label ?? ''));
}

if (isLiq && state.defaultLiquidAddress != null) {
emit(
state.copyWith(description: address.label ?? ''),
);
emit(state.copyWith(description: address.label ?? ''));
}
}

Future<void> generateNewAddress() async {
if (state.paymentNetwork == PaymentNetwork.lightning) return;

emit(
state.copyWith(
errLoadingAddress: '',
savedInvoiceAmount: 0,
),
state.copyWith(errLoadingAddress: '', savedInvoiceAmount: 0),
);

if (state.walletBloc == null) return;
Expand Down Expand Up @@ -315,7 +269,6 @@ class ReceiveCubit extends Cubit<ReceiveState> {
'WARNING! Electrum stop gap has been increased to $addressGap. This will affect your wallet sync time.\nGoto WalletSettings->Addresses to see all generated addresses.',
),
);
// _networkCubit.updateStopGapAndSave(addressGap + 1);
emit(state.copyWith(updateAddressGap: addressGap + 1));
Future.delayed(const Duration(milliseconds: 100));
}
Expand All @@ -337,7 +290,6 @@ class ReceiveCubit extends Cubit<ReceiveState> {
state.copyWith(
defaultLiquidAddress: updatedWallet.lastGeneratedAddress,
defaultAddress: updatedWallet.lastGeneratedAddress,
// privateLabel: '',
savedDescription: '',
description: '',
),
Expand Down Expand Up @@ -399,4 +351,16 @@ class ReceiveCubit extends Cubit<ReceiveState> {
wallet: state.walletBloc!.state.wallet!,
);
}

Future<void> isPayjoinEnabled() async {
final walletBloc = state.walletBloc;
final wallet = walletBloc?.state.wallet;
if (walletBloc == null || wallet == null) return;

if (wallet.utxos.isEmpty) {
emit(state.copyWith(isPayjoin: false));
} else {
emit(state.copyWith(isPayjoin: true));
}
}
}
1 change: 1 addition & 0 deletions lib/receive/bloc/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ part 'state.freezed.dart';
class ReceiveState with _$ReceiveState {
const factory ReceiveState({
@Default(true) bool loadingAddress,
@Default(true) bool isPayjoin,
@Default('') String errLoadingAddress,
Address? defaultAddress,
Address? defaultLiquidAddress,
Expand Down
23 changes: 22 additions & 1 deletion lib/receive/receive_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,10 @@ class _ReceiveDisplayAddressState extends State<ReceiveDisplayAddress> {
context.select((ReceiveCubit x) => x.state.paymentNetwork);
String receiveAddressLabel = 'Payment invoice';

final isPayjoin = context.select((ReceiveCubit _) => _.state.isPayjoin);

if (paymentNetwork == PaymentNetwork.bitcoin) {
receiveAddressLabel = 'Bitcoin address';
receiveAddressLabel = isPayjoin ? 'Payjoin address' : 'Bitcoin address';
} else if (paymentNetwork == PaymentNetwork.liquid) {
receiveAddressLabel = 'Liquid address';
}
Expand Down Expand Up @@ -1122,6 +1124,25 @@ class _ReceiveDisplayAddressState extends State<ReceiveDisplayAddress> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BBText.body(receiveAddressLabel),
if (isPayjoin == false && paymentNetwork == PaymentNetwork.bitcoin)
Card(
color: Colors.yellow[100],
margin: const EdgeInsets.all(10),
child: const ListTile(
leading: Icon(Icons.warning, color: Colors.orange),
title: Text(
'Payjoin transactions',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
subtitle: Text(
'Wallet does not meet the criteria',
style: TextStyle(color: Colors.black87),
),
),
),
Row(
children: [
Expanded(
Expand Down

0 comments on commit e9816ea

Please sign in to comment.