Skip to content

Commit b051bba

Browse files
committed
v6.7.0
- Update dependencies.
1 parent 4109102 commit b051bba

7 files changed

Lines changed: 92 additions & 87 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.7.0
2+
3+
- Update dependencies.
4+
15
## 6.6.0
26

37
- Update dependencies.

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ packages:
1515
path: ".."
1616
relative: true
1717
source: path
18-
version: "6.5.0"
18+
version: "6.6.0"
1919
blockchain_utils:
2020
dependency: "direct main"
2121
description:

lib/src/provider/models/utxo_details.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ abstract class BitcoinBaseOutput {
9494

9595
/// Convert the output to a TxOutput, a generic representation of a transaction output.
9696
TxOutput get toOutput;
97+
98+
BigInt get value;
9799
}
98100

99101
abstract class BCHBaseOutput extends BitcoinOutput {
@@ -106,6 +108,7 @@ abstract class BitcoinSpendableBaseOutput implements BitcoinBaseOutput {
106108
abstract final BitcoinBaseAddress address;
107109

108110
/// The value (amount) of the Bitcoin output.
111+
@override
109112
abstract final BigInt value;
110113
}
111114

@@ -133,6 +136,7 @@ class BitcoinScriptOutput implements BitcoinBaseOutput {
133136
final Script script;
134137

135138
/// The value (amount) of the Bitcoin output.
139+
@override
136140
final BigInt value;
137141

138142
final CashToken? token;
@@ -180,9 +184,12 @@ class BitcoinBurnableOutput extends BitcoinBaseOutput {
180184
final String categoryID;
181185

182186
/// The value (amount) of the burnable output (optional only for token with hasAmount flags).
183-
final BigInt? value;
187+
@override
188+
final BigInt value;
184189

185-
BitcoinBurnableOutput({required this.categoryID, this.utxoHash, this.value});
190+
BitcoinBurnableOutput(
191+
{required this.categoryID, this.utxoHash, BigInt? value})
192+
: value = value ?? BigInt.zero;
186193

187194
@override
188195
TxOutput get toOutput => throw UnimplementedError();
@@ -297,14 +304,10 @@ extension Calculate on List<UtxoWithAddress> {
297304
Map<String, BigInt> sumOfTokenUtxos() {
298305
final tokens = <String, BigInt>{};
299306
for (final utxo in this) {
300-
if (utxo.utxo.token == null) continue;
301-
final token = utxo.utxo.token!;
302-
if (!token.hasAmount) continue;
303-
if (tokens.containsKey(token.category)) {
304-
tokens[token.category] = tokens[token.category]! + token.amount;
305-
} else {
306-
tokens[token.category] = token.amount;
307-
}
307+
final token = utxo.utxo.token;
308+
if (token == null || !token.hasAmount) continue;
309+
final amount = tokens[token.category] ?? BigInt.zero;
310+
tokens[token.category] = amount + token.amount;
308311
}
309312
return tokens;
310313
}

lib/src/transaction_builder/core.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import 'package:bitcoin_base/src/bitcoin/script/scripts.dart';
2+
import 'package:bitcoin_base/src/models/network.dart';
23
import 'package:bitcoin_base/src/provider/models/utxo_details.dart';
4+
import 'package:blockchain_utils/helper/extensions/extensions.dart';
35

46
typedef BitcoinSignerCallBack = String Function(
57
List<int> trDigest, UtxoWithAddress utxo, String publicKey, int sighash);
68
typedef BitcoinSignerCallBackAsync = Future<String> Function(
79
List<int> trDigest, UtxoWithAddress utxo, String publicKey, int sighash);
810

911
abstract class BasedBitcoinTransacationBuilder {
12+
final List<BitcoinBaseOutput> outPuts;
13+
final List<UtxoWithAddress> utxos;
14+
final BitcoinOrdering inputOrdering;
15+
final BitcoinOrdering outputOrdering;
16+
final BigInt fee;
17+
final BasedUtxoNetwork network;
18+
BasedBitcoinTransacationBuilder(
19+
{required List<BitcoinBaseOutput> outPuts,
20+
required List<UtxoWithAddress> utxos,
21+
required this.inputOrdering,
22+
required this.outputOrdering,
23+
required this.fee,
24+
required this.network})
25+
: outPuts = outPuts.immutable,
26+
utxos = utxos.immutable;
27+
1028
BtcTransaction buildTransaction(BitcoinSignerCallBack sign);
1129
Future<BtcTransaction> buildTransactionAsync(BitcoinSignerCallBackAsync sign);
1230

lib/src/transaction_builder/forked_transaction_builder.dart

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,21 @@ import 'package:blockchain_utils/blockchain_utils.dart';
2424
/// - [outputOrdering]: Ordering preference for transaction outputs. Default is BIP-69.
2525
///
2626
/// Note: The constructor automatically validates the builder by calling the [_validateBuilder] method.
27-
class ForkedTransactionBuilder implements BasedBitcoinTransacationBuilder {
28-
final List<BitcoinBaseOutput> outPuts;
29-
final BigInt fee;
30-
final BasedUtxoNetwork network;
31-
final List<UtxoWithAddress> utxosInfo;
27+
class ForkedTransactionBuilder extends BasedBitcoinTransacationBuilder {
3228
final String? memo;
3329
final bool enableRBF;
3430
final bool isFakeTransaction;
35-
final BitcoinOrdering inputOrdering;
36-
final BitcoinOrdering outputOrdering;
31+
3732
ForkedTransactionBuilder(
38-
{required this.outPuts,
39-
required this.fee,
40-
required this.network,
41-
required List<UtxoWithAddress> utxos,
42-
this.inputOrdering = BitcoinOrdering.bip69,
43-
this.outputOrdering = BitcoinOrdering.bip69,
33+
{required super.outPuts,
34+
required super.fee,
35+
required super.network,
36+
required super.utxos,
37+
super.inputOrdering = BitcoinOrdering.bip69,
38+
super.outputOrdering = BitcoinOrdering.bip69,
4439
this.memo,
4540
this.enableRBF = false,
46-
this.isFakeTransaction = false})
47-
: utxosInfo = utxos {
41+
this.isFakeTransaction = false}) {
4842
_validateBuilder();
4943
}
5044

@@ -56,7 +50,7 @@ class ForkedTransactionBuilder implements BasedBitcoinTransacationBuilder {
5650

5751
/// validate every address is related to network
5852
/// exception if failed.
59-
for (final i in utxosInfo) {
53+
for (final i in utxos) {
6054
i.ownerDetails.address.toAddress(network);
6155
}
6256
for (final i in outPuts) {
@@ -242,21 +236,20 @@ that demonstrate the right to spend the bitcoins associated with the correspondi
242236
}
243237

244238
Tuple<List<TxInput>, List<UtxoWithAddress>> _buildInputs() {
245-
var sortedUtxos = List<UtxoWithAddress>.from(utxosInfo);
239+
final sortedUtxos = List<UtxoWithAddress>.from(utxos);
246240

247241
if (inputOrdering == BitcoinOrdering.shuffle) {
248-
sortedUtxos = sortedUtxos..shuffle();
242+
sortedUtxos.shuffle();
249243
} else if (inputOrdering == BitcoinOrdering.bip69) {
250-
sortedUtxos = sortedUtxos
251-
..sort(
252-
(a, b) {
253-
final txidComparison = a.utxo.txHash.compareTo(b.utxo.txHash);
254-
if (txidComparison == 0) {
255-
return a.utxo.vout - b.utxo.vout;
256-
}
257-
return txidComparison;
258-
},
259-
);
244+
sortedUtxos.sort(
245+
(a, b) {
246+
final txidComparison = a.utxo.txHash.compareTo(b.utxo.txHash);
247+
if (txidComparison == 0) {
248+
return a.utxo.vout - b.utxo.vout;
249+
}
250+
return txidComparison;
251+
},
252+
);
260253
}
261254
final inputs = sortedUtxos.map((e) => e.utxo.toInput()).toList();
262255
if (enableRBF && inputs.isNotEmpty) {
@@ -320,14 +313,10 @@ be retrieved by anyone who examines the blockchain's history.
320313
Map<String, BigInt> _sumTokenOutputAmounts(List<TxOutput> outputs) {
321314
final tokens = <String, BigInt>{};
322315
for (final out in outputs) {
323-
if (out.cashToken == null) continue;
324-
final token = out.cashToken!;
325-
if (!token.hasAmount) continue;
326-
if (tokens.containsKey(token.category)) {
327-
tokens[token.category] = tokens[token.category]! + token.amount;
328-
} else {
329-
tokens[token.category] = token.amount;
330-
}
316+
final token = out.cashToken;
317+
if (token == null || !token.hasAmount) continue;
318+
final amount = tokens[token.category] ?? BigInt.zero;
319+
tokens[token.category] = amount + token.amount;
331320
}
332321
return tokens;
333322
}
@@ -355,14 +344,12 @@ be retrieved by anyone who examines the blockchain's history.
355344
final sumTokenOutputAmouts = _sumTokenOutputAmounts(outputs);
356345
for (final i in sumOfTokenUtxos.entries) {
357346
if (sumTokenOutputAmouts[i.key] != i.value) {
358-
var amount = sumTokenOutputAmouts[i.key] ?? BigInt.zero;
347+
BigInt amount = sumTokenOutputAmouts[i.key] ?? BigInt.zero;
359348
amount += outPuts
360349
.whereType<BitcoinBurnableOutput>()
361350
.where((element) => element.categoryID == i.key)
362-
.fold(
363-
BigInt.zero,
364-
(previousValue, element) =>
365-
previousValue + (element.value ?? BigInt.zero));
351+
.fold(BigInt.zero,
352+
(previousValue, element) => previousValue + (element.value));
366353

367354
if (amount != i.value) {
368355
throw DartBitcoinPluginException(

lib/src/transaction_builder/transaction_builder.dart

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,29 @@ import 'package:blockchain_utils/blockchain_utils.dart';
1717
/// - [outPuts]: List of Bitcoin outputs to be included in the transaction.
1818
/// - [fee]: Transaction fee (BigInt) for processing the transaction.
1919
/// - [network]: The target Bitcoin network.
20-
/// - [utxosInfo]: List of UtxoWithAddress objects providing information about Unspent Transaction Outputs (UTXOs).
2120
/// - [memo]: Optional memo or additional information associated with the transaction.
2221
/// - [enableRBF]: Flag indicating whether Replace-By-Fee (RBF) is enabled. Default is false.
2322
/// - [isFakeTransaction]: Flag indicating whether the transaction is a fake/mock transaction. Default is false.
2423
/// - [inputOrdering]: Ordering preference for transaction inputs. Default is BIP-69.
2524
/// - [outputOrdering]: Ordering preference for transaction outputs. Default is BIP-69.
2625
///
2726
/// Note: The constructor automatically validates the builder by calling the [_validateBuilder] method.
28-
class BitcoinTransactionBuilder implements BasedBitcoinTransacationBuilder {
29-
final List<BitcoinBaseOutput> outPuts;
30-
final BigInt fee;
31-
final BasedUtxoNetwork network;
32-
final List<UtxoWithAddress> utxosInfo;
27+
class BitcoinTransactionBuilder extends BasedBitcoinTransacationBuilder {
3328
final String? memo;
3429
final bool enableRBF;
3530
final bool isFakeTransaction;
36-
final BitcoinOrdering inputOrdering;
37-
final BitcoinOrdering outputOrdering;
31+
3832
BitcoinTransactionBuilder({
39-
required this.outPuts,
40-
required this.fee,
41-
required this.network,
42-
required List<UtxoWithAddress> utxos,
43-
this.inputOrdering = BitcoinOrdering.bip69,
44-
this.outputOrdering = BitcoinOrdering.bip69,
33+
required super.outPuts,
34+
required super.fee,
35+
required super.network,
36+
required super.utxos,
37+
super.inputOrdering = BitcoinOrdering.bip69,
38+
super.outputOrdering = BitcoinOrdering.bip69,
4539
this.memo,
4640
this.enableRBF = false,
4741
this.isFakeTransaction = false,
48-
}) : utxosInfo = utxos {
42+
}) {
4943
_validateBuilder();
5044
}
5145

@@ -55,14 +49,14 @@ class BitcoinTransactionBuilder implements BasedBitcoinTransacationBuilder {
5549
throw const DartBitcoinPluginException(
5650
'invalid network for BitcoinCashNetwork and BSVNetwork use ForkedTransactionBuilder');
5751
}
58-
final token = utxosInfo.any((element) => element.utxo.token != null);
52+
final token = utxos.any((element) => element.utxo.token != null);
5953
final tokenInput = outPuts.whereType<BitcoinTokenOutput>();
6054
final burn = outPuts.whereType<BitcoinBurnableOutput>();
6155
if (token || tokenInput.isNotEmpty || burn.isNotEmpty) {
6256
throw const DartBitcoinPluginException(
6357
'Cash Token only work on Bitcoin cash network');
6458
}
65-
for (final i in utxosInfo) {
59+
for (final i in utxos) {
6660
/// Verify each input for its association with this network's address. Raise an exception if the address is incorrect.
6761
i.ownerDetails.address.toAddress(network);
6862
}
@@ -151,7 +145,7 @@ class BitcoinTransactionBuilder implements BasedBitcoinTransacationBuilder {
151145
/// Returns:
152146
/// - bool: True if at least one UTXO in the list is a SegWit UTXO, false otherwise.
153147
bool _hasSegwit() {
154-
for (final element in utxosInfo) {
148+
for (final element in utxos) {
155149
if (element.utxo.isSegwit) {
156150
return true;
157151
}
@@ -166,7 +160,7 @@ class BitcoinTransactionBuilder implements BasedBitcoinTransacationBuilder {
166160
/// Returns:
167161
/// - bool: True if at least one UTXO in the list is a P2TR UTXO, false otherwise.
168162
bool _hasTaproot() {
169-
for (final element in utxosInfo) {
163+
for (final element in utxos) {
170164
if (element.utxo.isP2tr) {
171165
return true;
172166
}
@@ -383,21 +377,20 @@ that demonstrate the right to spend the bitcoins associated with the correspondi
383377
}
384378

385379
Tuple<List<TxInput>, List<UtxoWithAddress>> _buildInputs() {
386-
var sortedUtxos = List<UtxoWithAddress>.from(utxosInfo);
380+
final sortedUtxos = List<UtxoWithAddress>.from(utxos);
387381

388382
if (inputOrdering == BitcoinOrdering.shuffle) {
389-
sortedUtxos = sortedUtxos..shuffle();
383+
sortedUtxos.shuffle();
390384
} else if (inputOrdering == BitcoinOrdering.bip69) {
391-
sortedUtxos = sortedUtxos
392-
..sort(
393-
(a, b) {
394-
final txidComparison = a.utxo.txHash.compareTo(b.utxo.txHash);
395-
if (txidComparison == 0) {
396-
return a.utxo.vout - b.utxo.vout;
397-
}
398-
return txidComparison;
399-
},
400-
);
385+
sortedUtxos.sort(
386+
(a, b) {
387+
final txidComparison = a.utxo.txHash.compareTo(b.utxo.txHash);
388+
if (txidComparison == 0) {
389+
return a.utxo.vout - b.utxo.vout;
390+
}
391+
return txidComparison;
392+
},
393+
);
401394
}
402395
final inputs = sortedUtxos.map((e) => e.utxo.toInput()).toList();
403396
if (enableRBF && inputs.isNotEmpty) {

pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: bitcoin_base
22
description: A versatile library for Bitcoin, Dogecoin, Litecoin, Dash, BSV, and BCH. Supports P2PKH, P2SH, P2WPKH, P2WSH, P2TR, with advanced creation, signing, and spending capabilities.
3-
version: 6.6.0
3+
version: 6.7.0
44
homepage: "https://github.com/mrtnetwork/bitcoin_base"
55
repository: "https://github.com/mrtnetwork/bitcoin_base"
66
Author: mrhaydari.t@gmail.com
@@ -23,9 +23,9 @@ dependencies:
2323

2424

2525
dev_dependencies:
26-
lints: ^5.1.1
27-
test: ^1.25.9
28-
flutter_lints: ^5.0.0
26+
lints: ^6.0.0
27+
test: ^1.26.3
28+
flutter_lints: ^6.0.0
2929

3030

3131
# For information on the generic Dart part of this file, see the

0 commit comments

Comments
 (0)