Skip to content

Commit

Permalink
[Cleanup] Bip21 address creation moved to ReceiveState
Browse files Browse the repository at this point in the history
  • Loading branch information
saiy2k committed May 6, 2024
1 parent a7b9f8e commit 9855b5f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 117 deletions.
33 changes: 21 additions & 12 deletions lib/receive/bloc/state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:bb_mobile/_model/address.dart';
import 'package:bb_mobile/_model/transaction.dart';
import 'package:bb_mobile/_model/wallet.dart';
import 'package:bb_mobile/_pkg/consts/configs.dart';
import 'package:bb_mobile/wallet/bloc/wallet_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand Down Expand Up @@ -33,6 +34,20 @@ class ReceiveState with _$ReceiveState {
}) = _ReceiveState;
const ReceiveState._();

String getAddressWithAmountAndLabel(double amount, bool isLiquid, {SwapTx? swapTx}) {
final String address = getQRStr(swapTx: swapTx);

String finalAddress = '';
if (paymentNetwork == PaymentNetwork.lightning || (amount == 0 && description.isEmpty)) {
finalAddress = address;
} else {
finalAddress = isLiquid
? 'liquidnetwork:$address&amount=${amount.toStringAsFixed(8)}${description.isNotEmpty ? '&label=$description' : ''}&assetid=$liquidAssetId'
: 'bitcoin:$address&amount=${amount.toStringAsFixed(8)}${description.isNotEmpty ? '&label=$description' : ''}';
}
return finalAddress;
}

String getQRStr({SwapTx? swapTx}) {
if (paymentNetwork == PaymentNetwork.lightning) {
if (swapTx == null) return '';
Expand All @@ -45,8 +60,7 @@ class ReceiveState with _$ReceiveState {
final btcAmt = (savedInvoiceAmount / 100000000).toStringAsFixed(8);

var invoice = 'bitcoin:' + defaultAddress!.address + '?amount=' + btcAmt;
if (savedDescription.isNotEmpty)
invoice = invoice + '&label=' + savedDescription;
if (savedDescription.isNotEmpty) invoice = invoice + '&label=' + savedDescription;

return invoice;
}
Expand All @@ -59,29 +73,24 @@ class ReceiveState with _$ReceiveState {
return defaultAddress?.address ?? '';
}

bool showNewRequestButton() =>
savedDescription.isEmpty && savedInvoiceAmount == 0;
bool showNewRequestButton() => savedDescription.isEmpty && savedInvoiceAmount == 0;

bool isSupported() {
if (paymentNetwork == PaymentNetwork.bitcoin &&
walletBloc!.state.wallet?.baseWalletType == BaseWalletType.Liquid)
if (paymentNetwork == PaymentNetwork.bitcoin && walletBloc!.state.wallet?.baseWalletType == BaseWalletType.Liquid)
return false;
if (paymentNetwork == PaymentNetwork.liquid &&
walletBloc!.state.wallet?.baseWalletType == BaseWalletType.Bitcoin)
if (paymentNetwork == PaymentNetwork.liquid && walletBloc!.state.wallet?.baseWalletType == BaseWalletType.Bitcoin)
return false;
return true;
}

bool showQR(SwapTx? swapTx) {
return (swapTx != null && paymentNetwork == PaymentNetwork.lightning) ||
(paymentNetwork == PaymentNetwork.bitcoin ||
paymentNetwork == PaymentNetwork.liquid);
(paymentNetwork == PaymentNetwork.bitcoin || paymentNetwork == PaymentNetwork.liquid);
}

bool isLn() => paymentNetwork == PaymentNetwork.lightning;

bool checkIfMainWalletSelected() =>
walletBloc?.state.wallet?.mainWallet ?? false;
bool checkIfMainWalletSelected() => walletBloc?.state.wallet?.mainWallet ?? false;

// bool _swapTxIsNotNull() => swapBloc.state.swapTx != null;

Expand Down
Loading

0 comments on commit 9855b5f

Please sign in to comment.