Skip to content

CW-994 mweb enhancements #2204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,14 @@ abstract class ElectrumWalletBase
throw BitcoinTransactionNoDustException();
}

// if mweb isn't enabled, don't consider spending mweb coins:
if (this is LitecoinWallet) {
var mwebEnabled = (this as LitecoinWallet).mwebEnabled;
if (!mwebEnabled) {
coinTypeToSpendFrom = UnspentCoinType.nonMweb;
}
}

final utxoDetails = _createUTXOS(
sendAll: false,
credentialsAmount: credentialsAmount,
Expand Down
39 changes: 28 additions & 11 deletions cw_bitcoin/lib/litecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
if (unspent.vout == 0) {
unspent.isChange = true;
}

// printV("unspent: $unspent ${unspent.vout} ${utxo.value}");
mwebUnspentCoins.add(unspent);
});

Expand Down Expand Up @@ -1058,13 +1060,26 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
try {
var tx = await super.createTransaction(credentials) as PendingBitcoinTransaction;
tx.isMweb = mwebEnabled;
var creds;
if (!mwebEnabled) {
BitcoinTransactionCredentials btcCreds = (credentials as BitcoinTransactionCredentials);
// sets unspent coin type to nonMweb:
creds = BitcoinTransactionCredentials(
btcCreds.outputs,
priority: btcCreds.priority,
feeRate: btcCreds.feeRate,
coinTypeToSpendFrom: UnspentCoinType.nonMweb,
);
} else {
creds = credentials;
}
var tx = await super.createTransaction(creds as Object) as PendingBitcoinTransaction;
tx.useMwebToSubmit = mwebEnabled;

if (!mwebEnabled) {
tx.changeAddressOverride =
(await (walletAddresses as LitecoinWalletAddresses).getChangeAddress(coinTypeToSpendFrom: UnspentCoinType.nonMweb))
.address;
tx.changeAddressOverride = (await (walletAddresses as LitecoinWalletAddresses)
.getChangeAddress(coinTypeToSpendFrom: UnspentCoinType.nonMweb))
.address;
return tx;
}
await waitForMwebAddresses();
Expand All @@ -1088,17 +1103,17 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final address = output.address.toLowerCase();
final extractedAddress = output.extractedAddress?.toLowerCase();

if (address.contains("mweb")) {
if (address.startsWith("ltcmweb")) {
hasMwebOutput = true;
}
if (!address.contains("mweb")) {
if (!address.startsWith("ltcmweb")) {
hasRegularOutput = true;
}
if (extractedAddress != null && extractedAddress.isNotEmpty) {
if (extractedAddress.contains("mweb")) {
if (extractedAddress.startsWith("ltcmweb")) {
hasMwebOutput = true;
}
if (!extractedAddress.contains("mweb")) {
if (!extractedAddress.startsWith("ltcmweb")) {
hasRegularOutput = true;
}
}
Expand All @@ -1117,10 +1132,12 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
bool isRegular = !hasMwebInput && !hasMwebOutput;
bool shouldNotUseMwebChange = isPegIn || isRegular || !hasMwebInput;
tx.changeAddressOverride = (await (walletAddresses as LitecoinWalletAddresses)
.getChangeAddress(coinTypeToSpendFrom: shouldNotUseMwebChange ? UnspentCoinType.nonMweb : UnspentCoinType.any))
.getChangeAddress(
coinTypeToSpendFrom:
shouldNotUseMwebChange ? UnspentCoinType.nonMweb : UnspentCoinType.any))
.address;
if (isRegular) {
tx.isMweb = false;
tx.useMwebToSubmit = false;
return tx;
}

Expand Down
2 changes: 1 addition & 1 deletion cw_bitcoin/lib/litecoin_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
if (!element.isSending || element.isFrozen) {
return;
}
if (element.address.contains("mweb")) {
if (element.address.startsWith("ltcmweb")) {
comesFromMweb = true;
}
});
Expand Down
6 changes: 3 additions & 3 deletions cw_bitcoin/lib/pending_bitcoin_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PendingBitcoinTransaction with PendingTransaction {
required this.hasChange,
this.isSendAll = false,
this.hasTaprootInputs = false,
this.isMweb = false,
this.useMwebToSubmit = false,
this.utxos = const [],
}) : _listeners = <void Function(ElectrumTransactionInfo transaction)>[];

Expand All @@ -38,7 +38,7 @@ class PendingBitcoinTransaction with PendingTransaction {
final bool hasChange;
final bool hasTaprootInputs;
List<UtxoWithAddress> utxos;
bool isMweb;
bool useMwebToSubmit;
String? changeAddressOverride;
String? idOverride;
String? hexOverride;
Expand Down Expand Up @@ -129,7 +129,7 @@ class PendingBitcoinTransaction with PendingTransaction {

@override
Future<void> commit() async {
if (isMweb) {
if (useMwebToSubmit) {
await _ltcCommit();
} else {
await _commit();
Expand Down
25 changes: 25 additions & 0 deletions lib/view_model/transaction_details_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ abstract class TransactionDetailsViewModelBase with Store {
if (!canReplaceByFee) _checkForRBF(tx);
break;
case WalletType.litecoin:
_addLitecoinListItems(tx, dateFormat);
case WalletType.bitcoinCash:
_addElectrumListItems(tx, dateFormat);
break;
Expand Down Expand Up @@ -341,6 +342,30 @@ abstract class TransactionDetailsViewModelBase with Store {
items.addAll(_items);
}

void _addLitecoinListItems(TransactionInfo tx, DateFormat dateFormat) {
_addElectrumListItems(tx, dateFormat);

bool isMweb = bitcoin!.txIsMweb(tx);

final _items = [
if (isMweb)
BlockExplorerListItem(
title: S.current.view_in_block_explorer,
value: S.current.view_transaction_on + 'mwebexplorer.com',
onTap: () async {
try {
final uri = Uri.parse('https://www.mwebexplorer.com/blocks/block/${tx.height}');
if (await canLaunchUrl(uri))
await launchUrl(uri, mode: LaunchMode.externalApplication);
} catch (e) {}
},
key: ValueKey('block_explorer_list_item_mweb_wallet_type_key'),
),
];

items.addAll(_items);
}

void _addHavenListItems(TransactionInfo tx, DateFormat dateFormat) {
items.addAll([
StandartListItem(
Expand Down
Loading