Skip to content

Commit 20022c0

Browse files
CW-994 mweb enhancements (cake-tech#2204)
* add mweb block explorer * potential fix for #2 * fix spending when mweb is disabled * Update cw_bitcoin/lib/litecoin_wallet.dart * fix resolving conflict missing brackets --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
1 parent 3e4409d commit 20022c0

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

cw_bitcoin/lib/electrum_wallet.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:cw_bitcoin/bitcoin_amount_format.dart';
1111
import 'package:cw_core/utils/print_verbose.dart';
1212
import 'package:cw_bitcoin/bitcoin_wallet.dart';
1313
import 'package:cw_bitcoin/litecoin_wallet.dart';
14-
import 'package:path_provider/path_provider.dart';
1514
import 'package:shared_preferences/shared_preferences.dart';
1615
import 'package:blockchain_utils/blockchain_utils.dart';
1716
import 'package:collection/collection.dart';
@@ -899,6 +898,13 @@ abstract class ElectrumWalletBase
899898
throw BitcoinTransactionNoDustException();
900899
}
901900

901+
// if mweb isn't enabled, don't consider spending mweb coins:
902+
if (this is LitecoinWallet) {
903+
var mwebEnabled = (this as LitecoinWallet).mwebEnabled;
904+
if (!mwebEnabled) {
905+
coinTypeToSpendFrom = UnspentCoinType.nonMweb;
906+
}
907+
}
902908

903909
// If there is only one output, and the amount to send is more than the max spendable amount
904910
// then it is actually a send all transaction

cw_bitcoin/lib/litecoin_wallet.dart

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
886886
if (unspent.vout == 0) {
887887
unspent.isChange = true;
888888
}
889+
890+
// printV("unspent: $unspent ${unspent.vout} ${utxo.value}");
889891
mwebUnspentCoins.add(unspent);
890892
});
891893

@@ -1140,13 +1142,27 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
11401142
@override
11411143
Future<PendingTransaction> createTransaction(Object credentials) async {
11421144
try {
1143-
var tx = await super.createTransaction(credentials) as PendingBitcoinTransaction;
1145+
var creds;
1146+
if (!mwebEnabled) {
1147+
BitcoinTransactionCredentials btcCreds = (credentials as BitcoinTransactionCredentials);
1148+
// sets unspent coin type to nonMweb:
1149+
creds = BitcoinTransactionCredentials(
1150+
btcCreds.outputs,
1151+
priority: btcCreds.priority,
1152+
feeRate: btcCreds.feeRate,
1153+
coinTypeToSpendFrom: UnspentCoinType.nonMweb,
1154+
);
1155+
} else {
1156+
creds = credentials;
1157+
}
1158+
var tx = await super.createTransaction(creds as Object) as PendingBitcoinTransaction;
11441159
tx.isMweb = mwebEnabled;
11451160

11461161
if (!mwebEnabled) {
11471162
tx.changeAddressOverride = (await (walletAddresses as LitecoinWalletAddresses)
11481163
.getChangeAddress(coinTypeToSpendFrom: UnspentCoinType.nonMweb))
11491164
.address;
1165+
11501166
if (tx.shouldCommitUR()) {
11511167
tx.unsignedPsbt = await buildPsbt(tx, false);
11521168
}
@@ -1165,17 +1181,17 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
11651181
final address = output.address.toLowerCase();
11661182
final extractedAddress = output.extractedAddress?.toLowerCase();
11671183

1168-
if (address.contains("mweb")) {
1184+
if (address.startsWith("ltcmweb")) {
11691185
hasMwebOutput = true;
11701186
}
1171-
if (!address.contains("mweb")) {
1187+
if (!address.startsWith("ltcmweb")) {
11721188
hasRegularOutput = true;
11731189
}
11741190
if (extractedAddress != null && extractedAddress.isNotEmpty) {
1175-
if (extractedAddress.contains("mweb")) {
1191+
if (extractedAddress.startsWith("ltcmweb")) {
11761192
hasMwebOutput = true;
11771193
}
1178-
if (!extractedAddress.contains("mweb")) {
1194+
if (!extractedAddress.startsWith("ltcmweb")) {
11791195
hasRegularOutput = true;
11801196
}
11811197
}

cw_bitcoin/lib/litecoin_wallet_addresses.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
188188
if (!element.isSending || element.isFrozen) {
189189
return;
190190
}
191-
if (element.address.contains("mweb")) {
191+
if (element.address.startsWith("ltcmweb")) {
192192
comesFromMweb = true;
193193
}
194194
});

lib/view_model/transaction_details_view_model.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ abstract class TransactionDetailsViewModelBase with Store {
5959
if (!canReplaceByFee) _checkForRBF(tx);
6060
break;
6161
case WalletType.litecoin:
62+
_addLitecoinListItems(tx, dateFormat);
6263
case WalletType.bitcoinCash:
6364
_addElectrumListItems(tx, dateFormat);
6465
break;
@@ -388,6 +389,30 @@ abstract class TransactionDetailsViewModelBase with Store {
388389
items.addAll(_items);
389390
}
390391

392+
void _addLitecoinListItems(TransactionInfo tx, DateFormat dateFormat) {
393+
_addElectrumListItems(tx, dateFormat);
394+
395+
bool isMweb = bitcoin!.txIsMweb(tx);
396+
397+
final _items = [
398+
if (isMweb)
399+
BlockExplorerListItem(
400+
title: S.current.view_in_block_explorer,
401+
value: S.current.view_transaction_on + 'mwebexplorer.com',
402+
onTap: () async {
403+
try {
404+
final uri = Uri.parse('https://www.mwebexplorer.com/blocks/block/${tx.height}');
405+
if (await canLaunchUrl(uri))
406+
await launchUrl(uri, mode: LaunchMode.externalApplication);
407+
} catch (e) {}
408+
},
409+
key: ValueKey('block_explorer_list_item_mweb_wallet_type_key'),
410+
),
411+
];
412+
413+
items.addAll(_items);
414+
}
415+
391416
void _addHavenListItems(TransactionInfo tx, DateFormat dateFormat) {
392417
items.addAll([
393418
StandartListItem(

0 commit comments

Comments
 (0)