Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 5 additions & 10 deletions cw_bitcoin/lib/address_from_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@ BitcoinBaseAddress addressFromScript(Script script,

switch (addressType) {
case P2pkhAddressType.p2pkh:
return P2pkhAddress.fromScriptPubkey(
script: script, network: BitcoinNetwork.mainnet);
return P2pkhAddress.fromScriptPubkey(script: script, network: BitcoinNetwork.mainnet);
case P2shAddressType.p2pkhInP2sh:
case P2shAddressType.p2pkInP2sh:
return P2shAddress.fromScriptPubkey(
script: script, network: BitcoinNetwork.mainnet);
return P2shAddress.fromScriptPubkey(script: script, network: BitcoinNetwork.mainnet);
case SegwitAddresType.p2wpkh:
return P2wpkhAddress.fromScriptPubkey(
script: script, network: BitcoinNetwork.mainnet);
return P2wpkhAddress.fromScriptPubkey(script: script, network: BitcoinNetwork.mainnet);
case SegwitAddresType.p2wsh:
return P2wshAddress.fromScriptPubkey(
script: script, network: BitcoinNetwork.mainnet);
return P2wshAddress.fromScriptPubkey(script: script, network: BitcoinNetwork.mainnet);
case SegwitAddresType.p2tr:
return P2trAddress.fromScriptPubkey(
script: script, network: BitcoinNetwork.mainnet);
return P2trAddress.fromScriptPubkey(script: script, network: BitcoinNetwork.mainnet);
}

throw ArgumentError("Invalid script");
Expand Down
12 changes: 5 additions & 7 deletions cw_bitcoin/lib/bitcoin_address_record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@ class BitcoinAddressRecord extends BaseBitcoinAddressRecord {
required super.type,
String? scriptHash,
required super.network,
}) {
}) {
try {
this.scriptHash = scriptHash ??
(network != null ? BitcoinAddressUtils.scriptHash(address, network: network!) : null);
(network != null ? BitcoinAddressUtils.scriptHash(address, network: network!) : null);
} catch (e) {
printV(e);
}
}
}

static bool _legacyDefaultForType(BitcoinAddressType type) {

// Some address types (p2wpkh, p2wsh) were historically derived from the same account/path as our new standard
// but using legacy formats. For these, we default to legacy = false.
if (type == SegwitAddresType.p2wpkh || type == SegwitAddresType.p2wsh) return false;
Expand Down Expand Up @@ -154,6 +153,7 @@ class BitcoinAddressRecord extends BaseBitcoinAddressRecord {
}
return scriptHash!;
}

@override
String get derivationPath {
if (type == SegwitAddresType.mweb) {
Expand All @@ -162,9 +162,7 @@ class BitcoinAddressRecord extends BaseBitcoinAddressRecord {

final coinType = _coinTypeForNetwork();
final purpose = _purposeForType(type);
final accountPath = isLegacyDerivation
? electrum_path
: "m/$purpose'/$coinType'/0'";
final accountPath = isLegacyDerivation ? electrum_path : "m/$purpose'/$coinType'/0'";

final chain = isHidden ? 1 : 0;
return "$accountPath/$chain/$index";
Expand Down
4 changes: 2 additions & 2 deletions cw_bitcoin/lib/bitcoin_amount_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ final bitcoinAmountFormat = NumberFormat()
..maximumFractionDigits = bitcoinAmountLength
..minimumFractionDigits = 1;

String bitcoinAmountToString({required int amount}) => bitcoinAmountFormat.format(
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider));
String bitcoinAmountToString({required int amount}) =>
bitcoinAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider));

double bitcoinAmountToDouble({required int amount}) =>
cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider);
Expand Down
1 change: 0 additions & 1 deletion cw_bitcoin/lib/bitcoin_commit_transaction_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ class BitcoinCommitTransactionException implements Exception {
@override
String toString() => errorMessage;
}

18 changes: 9 additions & 9 deletions cw_bitcoin/lib/bitcoin_transaction_priority.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import 'package:cw_core/transaction_priority.dart';
import 'package:flutter/foundation.dart';

class BitcoinTransactionPriority extends TransactionPriority {
const BitcoinTransactionPriority({required String title, required int raw, String? description, String? hint})
const BitcoinTransactionPriority(
{required String title, required int raw, String? description, String? hint})
: super(title: title, raw: raw, description: description, hint: hint);

static const List<BitcoinTransactionPriority> all = [fast, medium, slow, custom];
static const BitcoinTransactionPriority slow =
BitcoinTransactionPriority(title: 'Slow', description: "2 sat/byte", hint: "~ 24 h", raw: 0);
static const BitcoinTransactionPriority medium =
BitcoinTransactionPriority(title: 'Medium', description: "3 sat/byte", hint: "~ 1 h", raw: 1);
static const BitcoinTransactionPriority fast =
BitcoinTransactionPriority(title: 'Fast', description: "4 sat/byte", hint: "~ 30 min", raw: 2);
static const BitcoinTransactionPriority fast = BitcoinTransactionPriority(
title: 'Fast', description: "4 sat/byte", hint: "~ 30 min", raw: 2);
static const BitcoinTransactionPriority custom =
BitcoinTransactionPriority(title: 'Custom', raw: 3);
BitcoinTransactionPriority(title: 'Custom', raw: 3);

static BitcoinTransactionPriority deserialize({required int raw}) {
switch (raw) {
Expand Down Expand Up @@ -116,19 +117,19 @@ class LitecoinTransactionPriority extends BitcoinTransactionPriority {

return label;
}

}

class BitcoinCashTransactionPriority extends BitcoinTransactionPriority {
const BitcoinCashTransactionPriority({required String title, required int raw})
: super(title: title, raw: raw);

static const List<BitcoinCashTransactionPriority> all = [fast, medium, slow];
static const BitcoinCashTransactionPriority slow =
BitcoinCashTransactionPriority(title: 'Slow', raw: 0);
BitcoinCashTransactionPriority(title: 'Slow', raw: 0);
static const BitcoinCashTransactionPriority medium =
BitcoinCashTransactionPriority(title: 'Medium', raw: 1);
BitcoinCashTransactionPriority(title: 'Medium', raw: 1);
static const BitcoinCashTransactionPriority fast =
BitcoinCashTransactionPriority(title: 'Fast', raw: 2);
BitcoinCashTransactionPriority(title: 'Fast', raw: 2);

static BitcoinCashTransactionPriority deserialize({required int raw}) {
switch (raw) {
Expand Down Expand Up @@ -170,4 +171,3 @@ class BitcoinCashTransactionPriority extends BitcoinTransactionPriority {
return label;
}
}

70 changes: 36 additions & 34 deletions cw_bitcoin/lib/bitcoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
autorun((_) {
this.walletAddresses.isEnabledAutoGenerateSubaddress = this.isEnabledAutoGenerateSubaddress;
});

reaction((_) => this.useLightning, (bool useLightning) {
if (useLightning && LightningWallet.isAvailable) {
if (mnemonic != null) {
Expand Down Expand Up @@ -306,28 +306,28 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
}

return BitcoinWallet(
mnemonic: mnemonic,
xpub: keysData.xPub != null ? convertZpubToXpub(keysData.xPub!) : null,
password: password,
passphrase: passphrase,
walletInfo: walletInfo,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfo,
initialAddresses: snp?.addresses,
initialSilentAddresses: snp?.silentAddresses,
initialSilentAddressIndex: snp?.silentAddressIndex ?? 0,
initialBalance: snp?.balance,
initialLightningBalance: snp?.lightningBalance,
encryptionFileUtils: encryptionFileUtils,
seedBytes: seedBytes,
initialRegularAddressIndex: snp?.regularAddressIndex,
initialChangeAddressIndex: snp?.changeAddressIndex,
addressPageType: snp?.addressPageType,
networkParam: network,
alwaysScan: snp?.alwaysScan,
useLightning: snp?.useLightning,
cachedLightningAddress: snp?.cachedLightningAddress,
payjoinBox: payjoinBox,
mnemonic: mnemonic,
xpub: keysData.xPub != null ? convertZpubToXpub(keysData.xPub!) : null,
password: password,
passphrase: passphrase,
walletInfo: walletInfo,
derivationInfo: derivationInfo,
unspentCoinsInfo: unspentCoinsInfo,
initialAddresses: snp?.addresses,
initialSilentAddresses: snp?.silentAddresses,
initialSilentAddressIndex: snp?.silentAddressIndex ?? 0,
initialBalance: snp?.balance,
initialLightningBalance: snp?.lightningBalance,
encryptionFileUtils: encryptionFileUtils,
seedBytes: seedBytes,
initialRegularAddressIndex: snp?.regularAddressIndex,
initialChangeAddressIndex: snp?.changeAddressIndex,
addressPageType: snp?.addressPageType,
networkParam: network,
alwaysScan: snp?.alwaysScan,
useLightning: snp?.useLightning,
cachedLightningAddress: snp?.cachedLightningAddress,
payjoinBox: payjoinBox,
);
}

Expand All @@ -346,12 +346,12 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
}

try {
final lBalance = await lightningWallet!.getBalance();
final lBalance = await lightningWallet!.getBalance();

this.balance[CryptoCurrency.btcln] = ElectrumBalance(
confirmed: lBalance,
unconfirmed: Money.zero(CryptoCurrency.btcln),
frozen: Money.zero(CryptoCurrency.btcln));
this.balance[CryptoCurrency.btcln] = ElectrumBalance(
confirmed: lBalance,
unconfirmed: Money.zero(CryptoCurrency.btcln),
frozen: Money.zero(CryptoCurrency.btcln));
} catch (e) {
printV("Error fetching lightning balance: $e");
}
Expand Down Expand Up @@ -494,7 +494,9 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
credentials = credentials as BitcoinTransactionCredentials;
final lnAddr = credentials.outputs.first.isParsedAddress ? credentials.outputs.first.extractedAddress! : credentials.outputs.first.address;
final lnAddr = credentials.outputs.first.isParsedAddress
? credentials.outputs.first.extractedAddress!
: credentials.outputs.first.address;

final isLNCompatible = await lightningWallet?.isCompatible(lnAddr);
if ((credentials.coinTypeToSpendFrom == UnspentCoinType.lightning && lightningWallet != null) ||
Expand All @@ -506,12 +508,12 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
amount = credentials.outputs.first.cryptoAmount;
}


return lightningWallet!.createTransaction(
lnAddr,
amount.amount > BigInt.zero ? amount.amount : null,
credentials.priority,
credentials.outputs.first.sendAll,);
lnAddr,
amount.amount > BigInt.zero ? amount.amount : null,
credentials.priority,
credentials.outputs.first.sendAll,
);
}

final tx = (await super.createTransaction(credentials)) as PendingBitcoinTransaction;
Expand Down
8 changes: 3 additions & 5 deletions cw_bitcoin/lib/bitcoin_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ abstract class BitcoinWalletAddressesBase extends ElectrumWalletAddresses with S
}

@override
bool containsAddress(String address) => super.containsAddress(address) || address == lightningAddress;
bool containsAddress(String address) =>
super.containsAddress(address) || address == lightningAddress;

@override
String get addressForBuy {
Expand All @@ -153,12 +154,9 @@ abstract class BitcoinWalletAddressesBase extends ElectrumWalletAddresses with S

@override
String get addressForExchange {

final current = getFreshAddress();
final availableReceiveAddresses = receiveAddresses.where((element) =>
!element.isUsed &&
!element.isHidden &&
!hiddenAddresses.contains(element.address));
!element.isUsed && !element.isHidden && !hiddenAddresses.contains(element.address));

final bool isSilentPaymentsPage = addressPageType == SilentPaymentsAddresType.p2sp;
final bool isLightningPage = addressPageType == LightningAddressType.p2l;
Expand Down
32 changes: 16 additions & 16 deletions cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ class BitcoinRestoreWalletFromWIFCredentials extends WalletCredentials {
}

class BitcoinWalletFromKeysCredentials extends WalletCredentials {
BitcoinWalletFromKeysCredentials({
required String name,
required String password,
required this.xpub,
WalletInfo? walletInfo,
super.hardwareWalletType
}) : super(name: name, password: password, walletInfo: walletInfo);
BitcoinWalletFromKeysCredentials(
{required String name,
required String password,
required this.xpub,
WalletInfo? walletInfo,
super.hardwareWalletType})
: super(name: name, password: password, walletInfo: walletInfo);

final String xpub;
}

class LitecoinWalletFromKeysCredentials extends WalletCredentials {
LitecoinWalletFromKeysCredentials({
required String name,
required String password,
required this.xpub,
required this.scanSecret,
required this.spendPubkey,
WalletInfo? walletInfo,
super.hardwareWalletType
}) : super(name: name, password: password, walletInfo: walletInfo);
LitecoinWalletFromKeysCredentials(
{required String name,
required String password,
required this.xpub,
required this.scanSecret,
required this.spendPubkey,
WalletInfo? walletInfo,
super.hardwareWalletType})
: super(name: name, password: password, walletInfo: walletInfo);

final String xpub;
final String scanSecret;
Expand Down
13 changes: 5 additions & 8 deletions cw_bitcoin/lib/bitcoin_wallet_keys.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
class BitcoinWalletKeys {
const BitcoinWalletKeys({required this.wif, required this.privateKey, required this.publicKey, required this.xpub});
const BitcoinWalletKeys(
{required this.wif, required this.privateKey, required this.publicKey, required this.xpub});

final String wif;
final String privateKey;
final String publicKey;
final String xpub;

Map<String, String> toJson() => {
'wif': wif,
'privateKey': privateKey,
'publicKey': publicKey,
'xpub': xpub
};
}
Map<String, String> toJson() =>
{'wif': wif, 'privateKey': privateKey, 'publicKey': publicKey, 'xpub': xpub};
}
24 changes: 13 additions & 11 deletions cw_bitcoin/lib/bitcoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class BitcoinWalletService extends WalletService<
BitcoinRestoreWalletFromSeedCredentials,
BitcoinWalletFromKeysCredentials,
BitcoinRestoreWalletFromHardware> {
BitcoinWalletService(this.unspentCoinsInfoSource,
this.payjoinSessionSource, this.isDirect);
BitcoinWalletService(this.unspentCoinsInfoSource, this.payjoinSessionSource, this.isDirect);

final Box<UnspentCoinsInfo> unspentCoinsInfoSource;
final Box<PayjoinSession> payjoinSessionSource;
Expand All @@ -38,9 +37,12 @@ class BitcoinWalletService extends WalletService<

final String mnemonic;
final derivationInfo = await credentials.walletInfo!.getDerivationInfo();
derivationInfo.derivationType = credentials.derivationInfo?.derivationType ?? derivationInfo.derivationType;
derivationInfo.derivationPath = credentials.derivationInfo?.derivationPath ?? derivationInfo.derivationPath;
derivationInfo.description = credentials.derivationInfo?.description ?? derivationInfo.description;
derivationInfo.derivationType =
credentials.derivationInfo?.derivationType ?? derivationInfo.derivationType;
derivationInfo.derivationPath =
credentials.derivationInfo?.derivationPath ?? derivationInfo.derivationPath;
derivationInfo.description =
credentials.derivationInfo?.description ?? derivationInfo.description;
derivationInfo.scriptType = credentials.derivationInfo?.scriptType ?? derivationInfo.scriptType;
await derivationInfo.save();
switch (derivationInfo.derivationType) {
Expand Down Expand Up @@ -119,8 +121,9 @@ class BitcoinWalletService extends WalletService<
}
await WalletInfo.delete(walletInfo);

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

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

Expand All @@ -135,11 +138,10 @@ class BitcoinWalletService extends WalletService<
final network = isTestnet == true ? BitcoinNetwork.testnet : BitcoinNetwork.mainnet;
credentials.walletInfo?.network = network.value;
final derivationInfo = await credentials.walletInfo!.getDerivationInfo();
derivationInfo.derivationPath =
credentials.hwAccountData.derivationPath;

derivationInfo.derivationPath = credentials.hwAccountData.derivationPath;

final xpub = convertAnyToXpub(credentials.hwAccountData.xpub!);

await credentials.walletInfo!.save();
final wallet = await BitcoinWallet(
password: credentials.password!,
Expand Down
Loading
Loading