Skip to content
Draft
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
9 changes: 6 additions & 3 deletions cw_bitcoin/lib/bitcoin_transaction_credentials.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
import 'package:cw_core/output_info.dart';
import 'package:cw_core/unspent_coin_type.dart';
import "package:cw_bitcoin/bitcoin_transaction_priority.dart";
import "package:cw_core/coin_control/coin_selection.dart";
import "package:cw_core/output_info.dart";
import "package:cw_core/unspent_coin_type.dart";

class BitcoinTransactionCredentials {
BitcoinTransactionCredentials(
this.outputs, {
required this.priority,
this.feeRate,
this.coinTypeToSpendFrom = UnspentCoinType.any,
this.coinSelection = const AllCoinSelection(),
this.payjoinUri,
});

final List<OutputInfo> outputs;
final BitcoinTransactionPriority? priority;
final int? feeRate;
final CoinSelection coinSelection;
final UnspentCoinType coinTypeToSpendFrom;
final String? payjoinUri;
}
11 changes: 9 additions & 2 deletions cw_bitcoin/lib/bitcoin_unspent.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_core/unspent_transaction_output.dart';
import "package:bitcoin_base/bitcoin_base.dart";
import "package:cw_bitcoin/bitcoin_address_record.dart";
import "package:cw_core/unspent_transaction_output.dart";

class BitcoinUnspent extends Unspent {
BitcoinUnspent(BaseBitcoinAddressRecord addressRecord, String hash, int value, int vout)
Expand All @@ -26,6 +27,9 @@ class BitcoinUnspent extends Unspent {

final BaseBitcoinAddressRecord bitcoinAddressRecord;
bool? isPegOut;

@override
String get id => bitcoinAddressRecord.type == SegwitAddresType.mweb ? hash : "$hash:$vout";
}

class BitcoinSilentPaymentsUnspent extends BitcoinUnspent {
Expand Down Expand Up @@ -63,6 +67,9 @@ class BitcoinSilentPaymentsUnspent extends BitcoinUnspent {
return json;
}

@override
bool get isSilentPayment => true;

String? silentPaymentTweak;
String? silentPaymentLabel;
}
28 changes: 13 additions & 15 deletions cw_bitcoin/lib/bitcoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
import "package:cw_core/receive_page_option.dart";
import 'package:cw_core/unspent_coin_type.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/utils/zpub.dart';
import 'package:cw_core/wallet_info.dart';
Expand All @@ -58,7 +58,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
required String password,
required WalletInfo walletInfo,
required DerivationInfo derivationInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
required Box<PayjoinSession> payjoinBox,
required EncryptionFileUtils encryptionFileUtils,
Uint8List? seedBytes,
Expand All @@ -84,7 +83,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
password: password,
walletInfo: walletInfo,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfo,
network: networkParam == null
? BitcoinNetwork.mainnet
: networkParam == BitcoinNetwork.mainnet
Expand Down Expand Up @@ -178,7 +176,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
required String mnemonic,
required String password,
required WalletInfo walletInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
required Box<PayjoinSession> payjoinBox,
required EncryptionFileUtils encryptionFileUtils,
String? passphrase,
Expand Down Expand Up @@ -214,7 +211,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
password: password,
walletInfo: walletInfo,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfo,
initialAddresses: initialAddresses,
initialSilentAddresses: initialSilentAddresses,
initialSilentAddressIndex: initialSilentAddressIndex,
Expand All @@ -233,7 +229,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
static Future<BitcoinWallet> open({
required String name,
required WalletInfo walletInfo,
required Box<UnspentCoinsInfo> unspentCoinsInfo,
required Box<PayjoinSession> payjoinBox,
required String password,
required EncryptionFileUtils encryptionFileUtils,
Expand Down Expand Up @@ -317,7 +312,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
passphrase: passphrase,
walletInfo: walletInfo,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfo,
initialAddresses: snp?.addresses,
initialSilentAddresses: snp?.silentAddresses,
initialSilentAddressIndex: snp?.silentAddressIndex ?? 0,
Expand Down Expand Up @@ -425,9 +419,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
@override
bool get hasLightningSupport => lightningWallet?.sdk != null;

bool get isPayjoinAvailable => unspentCoinsInfo.values
.where((element) => element.walletId == id && element.isSending && !element.isFrozen)
.isNotEmpty;
Future<bool> get isPayjoinAvailable async =>
(await spendableCoins()).isNotEmpty;

Future<PsbtV2> buildPsbt({
required List<BitcoinOutput> outputs,
Expand Down Expand Up @@ -601,7 +594,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
}

final originalPsbt =
await signPsbt(base64.encode(transaction.asPsbtV0()), getUtxoWithPrivateKeys());
await signPsbt(base64.encode(transaction.asPsbtV0()), await getUtxoWithPrivateKeys());

tx.commitOverride = () async {
final sender =
Expand All @@ -612,10 +605,12 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
return tx;
}

List<UtxoWithPrivateKey> getUtxoWithPrivateKeys({bool confirmedOnly = false}) => unspentCoins
.where((e) => e.isSending && !e.isFrozen && (!confirmedOnly || (e.confirmations ?? 0) > 0))
.map((unspent) => UtxoWithPrivateKey.fromUnspent(unspent, this))
.toList();
Future<List<UtxoWithPrivateKey>> getUtxoWithPrivateKeys({bool confirmedOnly = false}) async =>
(await spendableCoins())
.cast<BitcoinUnspent>()
.where((e) => !confirmedOnly || (e.confirmations ?? 0) > 0)
.map((unspent) => UtxoWithPrivateKey.fromUnspent(unspent, this))
.toList();

Future<void> commitPsbt(String finalizedPsbt) {
final psbt = PsbtV2()..deserializeV0(base64.decode(finalizedPsbt));
Expand Down Expand Up @@ -726,4 +721,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {

return true;
}

@override
Uri coinControlUrl(String txId) => Uri.https("ordinals.com", "/tx/${txId}");
}
23 changes: 5 additions & 18 deletions cw_bitcoin/lib/bitcoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_mnemonics_bip39.dart';
import 'package:cw_bitcoin/mnemonic_is_incorrect_exception.dart';
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
import "package:cw_core/coin_control/coin_notes_store.dart";
import "package:cw_core/coin_control/frozen_coins_store.dart";
import 'package:cw_core/encryption_file_utils.dart';
import 'package:cw_core/payjoin_session.dart';
import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/utils/zpub.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
Expand All @@ -21,9 +22,8 @@ class BitcoinWalletService extends WalletService<
BitcoinRestoreWalletFromSeedCredentials,
BitcoinWalletFromKeysCredentials,
BitcoinRestoreWalletFromHardware> {
BitcoinWalletService(this.unspentCoinsInfoSource, this.payjoinSessionSource, this.isDirect);
BitcoinWalletService(this.payjoinSessionSource, this.isDirect);

final Box<UnspentCoinsInfo> unspentCoinsInfoSource;
final Box<PayjoinSession> payjoinSessionSource;
final bool isDirect;

Expand Down Expand Up @@ -63,7 +63,6 @@ class BitcoinWalletService extends WalletService<
password: credentials.password!,
passphrase: credentials.passphrase,
walletInfo: credentials.walletInfo!,
unspentCoinsInfo: unspentCoinsInfoSource,
payjoinBox: payjoinSessionSource,
network: network,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
Expand All @@ -90,7 +89,6 @@ class BitcoinWalletService extends WalletService<
password: password,
name: name,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
payjoinBox: payjoinSessionSource,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand All @@ -103,7 +101,6 @@ class BitcoinWalletService extends WalletService<
password: password,
name: name,
walletInfo: walletInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
payjoinBox: payjoinSessionSource,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand All @@ -121,15 +118,8 @@ class BitcoinWalletService extends WalletService<
}
await WalletInfo.delete(walletInfo);

final unspentCoinsToDelete = unspentCoinsInfoSource.values
.where((unspentCoin) => unspentCoin.walletId == walletInfo.id)
.toList();

final keysToDelete = unspentCoinsToDelete.map((unspentCoin) => unspentCoin.key).toList();

if (keysToDelete.isNotEmpty) {
await unspentCoinsInfoSource.deleteAll(keysToDelete);
}
await FrozenCoinsStore.instance.deleteWallet(walletInfo.id);
await CoinNotesStore.instance.deleteWallet(walletInfo.id);
}

@override
Expand All @@ -148,7 +138,6 @@ class BitcoinWalletService extends WalletService<
xpub: xpub,
walletInfo: credentials.walletInfo!,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfoSource,
networkParam: network,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
payjoinBox: payjoinSessionSource,
Expand All @@ -172,7 +161,6 @@ class BitcoinWalletService extends WalletService<
xpub: xpub,
walletInfo: credentials.walletInfo!,
derivationInfo: await credentials.walletInfo!.getDerivationInfo(),
unspentCoinsInfo: unspentCoinsInfoSource,
networkParam: network,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
payjoinBox: payjoinSessionSource,
Expand All @@ -199,7 +187,6 @@ class BitcoinWalletService extends WalletService<
passphrase: credentials.passphrase,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
unspentCoinsInfo: unspentCoinsInfoSource,
payjoinBox: payjoinSessionSource,
network: network,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
Expand Down
Loading
Loading