-
Notifications
You must be signed in to change notification settings - Fork 27
/
root.dart
1125 lines (1018 loc) · 36.4 KB
/
root.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:typed_data';
import 'package:bdk_flutter/src/utils/utils.dart';
import 'generated/api/blockchain.dart';
import 'generated/api/descriptor.dart';
import 'generated/api/error.dart';
import 'generated/api/key.dart';
import 'generated/api/psbt.dart';
import 'generated/api/types.dart';
import 'generated/api/wallet.dart';
///A Bitcoin address.
class Address extends BdkAddress {
Address._({required super.ptr});
static Future<Address> fromScript(
{required ScriptBuf script, required Network network}) async {
try {
await Api.initialize();
final res = await BdkAddress.fromScript(script: script, network: network);
return Address._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
static Future<Address> fromString(
{required String s, required Network network}) async {
try {
await Api.initialize();
final res = await BdkAddress.fromString(address: s, network: network);
return Address._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Generates a script pubkey spending to this address
Future<ScriptBuf> scriptPubkey() async {
try {
final res = await BdkAddress.script(ptr: this);
return ScriptBuf(bytes: res.bytes);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
/// Blockchain backends module provides the implementation of a few commonly-used backends like Electrum, and Esplora.
class Blockchain extends BdkBlockchain {
Blockchain._({required super.ptr});
/// [Blockchain] constructor
static Future<Blockchain> create({required BlockchainConfig config}) async {
try {
await Api.initialize();
final res = await BdkBlockchain.newInstance(blockchainConfig: config);
return Blockchain._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Estimate the fee rate required to confirm a transaction in a given target of blocks
@override
Future<FeeRate> estimateFee({required int target, hint}) async {
try {
return super.estimateFee(target: target);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///The function for broadcasting a transaction
@override
Future<String> broadcast({required BdkTransaction transaction, hint}) async {
try {
return super.broadcast(transaction: transaction);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///The function for getting block hash by block height
@override
Future<String> getBlockHash({required int height, hint}) async {
try {
return super.getBlockHash(height: height);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///The function for getting the current height of the blockchain.
@override
Future<int> getHeight({hint}) {
try {
return super.getHeight();
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
/// The BumpFeeTxBuilder is used to bump the fee on a transaction that has been broadcast and has its RBF flag set to true.
class BumpFeeTxBuilder {
int? _nSequence;
Address? _allowShrinking;
bool _enableRbf = false;
final String txid;
final double feeRate;
BumpFeeTxBuilder({required this.txid, required this.feeRate});
///Explicitly tells the wallet that it is allowed to reduce the amount of the output matching this `address` in order to bump the transaction fee. Without specifying this the wallet will attempt to find a change output to shrink instead.
///
/// Note that the output may shrink to below the dust limit and therefore be removed. If it is preserved then it is currently not guaranteed to be in the same position as it was originally.
///
/// Throws and exception if address can’t be found among the recipients of the transaction we are bumping.
BumpFeeTxBuilder allowShrinking(Address address) {
_allowShrinking = address;
return this;
}
///Enable signaling RBF
///
/// This will use the default nSequence value of `0xFFFFFFFD`
BumpFeeTxBuilder enableRbf() {
_enableRbf = true;
return this;
}
///Enable signaling RBF with a specific nSequence value
///
/// This can cause conflicts if the wallet’s descriptors contain an “older” (OP_CSV) operator and the given nsequence is lower than the CSV value.
///
/// If the nsequence is higher than `0xFFFFFFFD` an error will be thrown, since it would not be a valid nSequence to signal RBF.
BumpFeeTxBuilder enableRbfWithSequence(int nSequence) {
_nSequence = nSequence;
return this;
}
/// Finish building the transaction. Returns the [PartiallySignedTransaction]& [TransactionDetails].
Future<(PartiallySignedTransaction, TransactionDetails)> finish(
Wallet wallet) async {
try {
final res = await finishBumpFeeTxBuilder(
txid: txid.toString(),
enableRbf: _enableRbf,
feeRate: feeRate,
wallet: wallet,
nSequence: _nSequence,
allowShrinking: _allowShrinking);
return (PartiallySignedTransaction._(ptr: res.$1.ptr), res.$2);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///A `BIP-32` derivation path
class DerivationPath extends BdkDerivationPath {
DerivationPath._({required super.ptr});
/// [DerivationPath] constructor
static Future<DerivationPath> create({required String path}) async {
try {
await Api.initialize();
final res = await BdkDerivationPath.fromString(path: path);
return DerivationPath._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///Script descriptor
class Descriptor extends BdkDescriptor {
Descriptor._({required super.extendedDescriptor, required super.keyMap});
/// [Descriptor] constructor
static Future<Descriptor> create(
{required String descriptor, required Network network}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newInstance(
descriptor: descriptor, network: network);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP44 template. Expands to pkh(key/44'/{0,1}'/0'/{0,1}/*)
///
/// Since there are hardened derivation steps, this template requires a private derivable key (generally a xprv/tprv).
static Future<Descriptor> newBip44(
{required DescriptorSecretKey secretKey,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip44(
secretKey: secretKey, network: network, keychainKind: keychain);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP44 public template. Expands to pkh(key/{0,1}/*)
///
/// This assumes that the key used has already been derived with m/44'/0'/0' for Mainnet or m/44'/1'/0' for Testnet.
///
/// This template requires the parent fingerprint to populate correctly the metadata of PSBTs.
static Future<Descriptor> newBip44Public(
{required DescriptorPublicKey publicKey,
required String fingerPrint,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip44Public(
network: network,
keychainKind: keychain,
publicKey: publicKey,
fingerprint: fingerPrint);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP49 template. Expands to sh(wpkh(key/49'/{0,1}'/0'/{0,1}/*))
///
///Since there are hardened derivation steps, this template requires a private derivable key (generally a xprv/tprv).
static Future<Descriptor> newBip49(
{required DescriptorSecretKey secretKey,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip49(
secretKey: secretKey, network: network, keychainKind: keychain);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP49 public template. Expands to sh(wpkh(key/{0,1}/*))
///
/// This assumes that the key used has already been derived with m/49'/0'/0'.
///
/// This template requires the parent fingerprint to populate correctly the metadata of PSBTs.
static Future<Descriptor> newBip49Public(
{required DescriptorPublicKey publicKey,
required String fingerPrint,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip49Public(
network: network,
keychainKind: keychain,
publicKey: publicKey,
fingerprint: fingerPrint);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP84 template. Expands to wpkh(key/84'/{0,1}'/0'/{0,1}/*)
///
///Since there are hardened derivation steps, this template requires a private derivable key (generally a xprv/tprv).
static Future<Descriptor> newBip84(
{required DescriptorSecretKey secretKey,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip84(
secretKey: secretKey, network: network, keychainKind: keychain);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP84 public template. Expands to wpkh(key/{0,1}/*)
///
/// This assumes that the key used has already been derived with m/84'/0'/0'.
///
/// This template requires the parent fingerprint to populate correctly the metadata of PSBTs.
static Future<Descriptor> newBip84Public(
{required DescriptorPublicKey publicKey,
required String fingerPrint,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip84Public(
network: network,
keychainKind: keychain,
publicKey: publicKey,
fingerprint: fingerPrint);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP86 template. Expands to tr(key/86'/{0,1}'/0'/{0,1}/*)
///
/// Since there are hardened derivation steps, this template requires a private derivable key (generally a xprv/tprv).
static Future<Descriptor> newBip86(
{required DescriptorSecretKey secretKey,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip86(
secretKey: secretKey, network: network, keychainKind: keychain);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///BIP86 public template. Expands to tr(key/{0,1}/*)
///
/// This assumes that the key used has already been derived with m/86'/0'/0' for Mainnet or m/86'/1'/0' for Testnet.
///
/// This template requires the parent fingerprint to populate correctly the metadata of PSBTs.
static Future<Descriptor> newBip86Public(
{required DescriptorPublicKey publicKey,
required String fingerPrint,
required Network network,
required KeychainKind keychain}) async {
try {
await Api.initialize();
final res = await BdkDescriptor.newBip86Public(
network: network,
keychainKind: keychain,
publicKey: publicKey,
fingerprint: fingerPrint);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return the public version of the output descriptor.
@override
Future<String> asString({hint}) async {
try {
return super.asString();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return the private version of the output descriptor if available, otherwise return the public version.
@override
Future<String> asStringPrivate({hint}) async {
try {
return super.asStringPrivate();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Computes an upper bound on the difference between a non-satisfied TxIn's segwit_weight and a satisfied TxIn's segwit_weight
@override
Future<int> maxSatisfactionWeight({hint}) async {
try {
return super.maxSatisfactionWeight();
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///An extended public key.
class DescriptorPublicKey extends BdkDescriptorPublicKey {
DescriptorPublicKey._({required super.ptr});
/// [DescriptorPublicKey] constructor
static Future<DescriptorPublicKey> fromString(String publicKey) async {
try {
await Api.initialize();
final res = await BdkDescriptorPublicKey.fromString(publicKey: publicKey);
return DescriptorPublicKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Get the public key as string.
@override
Future<String> asString({hint}) async {
try {
return super.asString();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Derive a public descriptor at a given path.
Future<DescriptorPublicKey> derive(
{required BdkDerivationPath path, hint}) async {
try {
final res = await BdkDescriptorPublicKey.derive(ptr: this, path: path);
return DescriptorPublicKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Extend the public descriptor with a custom path.
Future<DescriptorPublicKey> extend(
{required BdkDerivationPath path, hint}) async {
try {
final res = await BdkDescriptorPublicKey.extend(ptr: this, path: path);
return DescriptorPublicKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///Script descriptor
class DescriptorSecretKey extends BdkDescriptorSecretKey {
DescriptorSecretKey._({required super.ptr});
/// [DescriptorSecretKey] constructor
static Future<DescriptorSecretKey> fromString(String secretKey) async {
try {
await Api.initialize();
final res = await BdkDescriptorSecretKey.fromString(secretKey: secretKey);
return DescriptorSecretKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
/// [DescriptorSecretKey] constructor
static Future<DescriptorSecretKey> create(
{required Network network,
required Mnemonic mnemonic,
String? password}) async {
try {
await Api.initialize();
final res = await BdkDescriptorSecretKey.create(
network: network, mnemonic: mnemonic, password: password);
return DescriptorSecretKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Derived the XPrv using the derivation path
Future<DescriptorSecretKey> derive(BdkDerivationPath path) async {
try {
final res = await BdkDescriptorSecretKey.derive(ptr: this, path: path);
return DescriptorSecretKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Extends the XPrv using the derivation path
Future<DescriptorSecretKey> extend(BdkDerivationPath path) async {
try {
final res = await BdkDescriptorSecretKey.extend(ptr: this, path: path);
return DescriptorSecretKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Returns the public version of this key.
Future<DescriptorPublicKey> asPublic() async {
try {
final res = await BdkDescriptorSecretKey.asPublic(ptr: this);
return DescriptorPublicKey._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Get the private key as string.
@override
Future<String> asString({hint}) {
try {
return super.asString();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Get the private key as bytes.
@override
Future<Uint8List> secretBytes({hint}) async {
try {
return super.secretBytes();
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///Mnemonic phrases are a human-readable version of the private keys. Supported number of words are 12, 18, and 24.
class Mnemonic extends BdkMnemonic {
Mnemonic._({required super.ptr});
/// Generates [Mnemonic] with given [WordCount]
///
/// [Mnemonic] constructor
static Future<Mnemonic> create(WordCount wordCount) async {
try {
await Api.initialize();
final res = await BdkMnemonic.newInstance(wordCount: wordCount);
return Mnemonic._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
/// Create a new [Mnemonic] in the specified language from the given entropy.
/// Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
///
/// [Mnemonic] constructor
static Future<Mnemonic> fromEntropy(List<int> entropy) async {
try {
await Api.initialize();
final res = await BdkMnemonic.fromEntropy(entropy: entropy);
return Mnemonic._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
/// Parse a [Mnemonic] with given string
///
/// [Mnemonic] constructor
static Future<Mnemonic> fromString(String mnemonic) async {
try {
await Api.initialize();
final res = await BdkMnemonic.fromString(mnemonic: mnemonic);
return Mnemonic._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Returns Mnemonic as string
@override
Future<String> asString({hint}) async {
try {
return super.asString();
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///A Partially Signed Transaction
class PartiallySignedTransaction extends BdkPsbt {
PartiallySignedTransaction._({required super.ptr});
/// Parse a [PartiallySignedTransaction] with given Base64 string
///
/// [PartiallySignedTransaction] constructor
static Future<PartiallySignedTransaction> fromString(
String psbtBase64) async {
try {
await Api.initialize();
final res = await BdkPsbt.fromStr(psbtBase64: psbtBase64);
return PartiallySignedTransaction._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return fee amount
@override
Future<int?> feeAmount({hint}) {
try {
return super.feeAmount();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return fee rate
@override
Future<FeeRate?> feeRate({hint}) {
try {
return super.feeRate();
} on BdkError catch (e) {
throw mapToException(e);
}
}
@override
Future<String> jsonSerialize({hint}) {
try {
return super.jsonSerialize();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return psbt as string
@override
Future<String> serialize({hint}) {
try {
return super.serialize();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Return the transaction as bytes.
Future<Transaction> extractTx() async {
try {
final res = await BdkPsbt.extractTx(ptr: this);
return Transaction._(inner: res.inner);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Combines this [PartiallySignedTransaction] with other PSBT as described by BIP 174.
Future<PartiallySignedTransaction> combine(
PartiallySignedTransaction other) async {
try {
final res = await BdkPsbt.combine(ptr: this, other: other);
return PartiallySignedTransaction._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Returns the [PartiallySignedTransaction]'s transaction id
@override
Future<String> txid({hint}) {
try {
return super.txid();
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///Bitcoin script.
class ScriptBuf extends BdkScriptBuf {
/// [ScriptBuf] constructor
ScriptBuf({required super.bytes});
///Creates a new empty script.
static Future<ScriptBuf> empty() async {
try {
await Api.initialize();
final res = await BdkScriptBuf.empty();
return ScriptBuf(bytes: res.bytes);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Creates a new empty script with pre-allocated capacity.
static Future<ScriptBuf> withCapacity(int capacity) async {
try {
await Api.initialize();
final res = await BdkScriptBuf.withCapacity(capacity: capacity);
return ScriptBuf(bytes: res.bytes);
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Creates a ScriptBuf from a hex string.
static Future<ScriptBuf> fromHex(String s) async {
try {
await Api.initialize();
final res = await BdkScriptBuf.fromHex(s: s);
return ScriptBuf(bytes: res.bytes);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
///A bitcoin transaction.
class Transaction extends BdkTransaction {
Transaction._({required super.inner});
/// [Transaction] constructor
/// Decode an object with a well-defined format.
// This is the method that should be implemented for a typical, fixed sized type implementing this trait.
static Future<Transaction> fromBytes({
required List<int> transactionBytes,
}) async {
try {
await Api.initialize();
final res =
await BdkTransaction.fromBytes(transactionBytes: transactionBytes);
return Transaction._(inner: res.inner);
} on BdkError catch (e) {
throw mapToException(e);
}
}
@override
String toString() {
return inner;
}
}
///A transaction builder
///
/// A TxBuilder is created by calling TxBuilder or BumpFeeTxBuilder on a wallet.
/// After assigning it, you set options on it until finally calling finish to consume the builder and generate the transaction.
class TxBuilder {
final List<ScriptAmount> _recipients = [];
final List<OutPoint> _utxos = [];
final List<OutPoint> _unSpendable = [];
(OutPoint, Input, int)? _foreignUtxo;
bool _manuallySelectedOnly = false;
double? _feeRate;
ChangeSpendPolicy _changeSpendPolicy = ChangeSpendPolicy.changeAllowed;
int? _feeAbsolute;
bool _drainWallet = false;
ScriptBuf? _drainTo;
RbfValue? _rbfValue;
List<int> _data = [];
///Add data as an output, using OP_RETURN
TxBuilder addData({required List<int> data}) {
_data = data;
return this;
}
///Add a recipient to the internal list
TxBuilder addRecipient(ScriptBuf script, int amount) {
_recipients.add(ScriptAmount(script: script, amount: amount));
return this;
}
///Add a utxo to the internal list of unspendable utxos
///
/// It’s important to note that the “must-be-spent” utxos added with TxBuilder().addUtxo have priority over this.
/// See the docs of the two linked methods for more details.
TxBuilder unSpendable(List<OutPoint> outpoints) {
for (var e in outpoints) {
_unSpendable.add(e);
}
return this;
}
///Add a utxo to the internal list of utxos that must be spent
///
/// These have priority over the “unspendable” utxos, meaning that if a utxo is present both in the “utxos” and the “unspendable” list, it will be spent.
TxBuilder addUtxo(OutPoint outpoint) {
_utxos.add(outpoint);
return this;
}
///Add the list of outpoints to the internal list of UTXOs that must be spent.
///
///If an error occurs while adding any of the UTXOs then none of them are added and the error is returned.
///
/// These have priority over the “unspendable” utxos, meaning that if a utxo is present both in the “utxos” and the “unspendable” list, it will be spent.
TxBuilder addUtxos(List<OutPoint> outpoints) {
for (var e in outpoints) {
_utxos.add(e);
}
return this;
}
///Add a foreign UTXO i.e. a UTXO not owned by this wallet.
///At a minimum to add a foreign UTXO we need:
///
/// outpoint: To add it to the raw transaction.
/// psbt_input: To know the value.
/// satisfaction_weight: To know how much weight/vbytes the input will add to the transaction for fee calculation.
/// There are several security concerns about adding foreign UTXOs that application developers should consider. First, how do you know the value of the input is correct? If a non_witness_utxo is provided in the psbt_input then this method implicitly verifies the value by checking it against the transaction. If only a witness_utxo is provided then this method doesn’t verify the value but just takes it as a given – it is up to you to check that whoever sent you the input_psbt was not lying!
///
/// Secondly, you must somehow provide satisfaction_weight of the input. Depending on your application it may be important that this be known precisely.If not,
/// a malicious counterparty may fool you into putting in a value that is too low, giving the transaction a lower than expected feerate. They could also fool
/// you into putting a value that is too high causing you to pay a fee that is too high. The party who is broadcasting the transaction can of course check the
/// real input weight matches the expected weight prior to broadcasting.
TxBuilder addForeignUtxo(
Input psbtInput, OutPoint outPoint, int satisfactionWeight) {
_foreignUtxo = (outPoint, psbtInput, satisfactionWeight);
return this;
}
///Do not spend change outputs
///
/// This effectively adds all the change outputs to the “unspendable” list. See TxBuilder().addUtxos
TxBuilder doNotSpendChange() {
_changeSpendPolicy = ChangeSpendPolicy.changeForbidden;
return this;
}
///Spend all the available inputs. This respects filters like TxBuilder().unSpendable and the change policy.
TxBuilder drainWallet() {
_drainWallet = true;
return this;
}
///Sets the address to drain excess coins to.
///
/// Usually, when there are excess coins they are sent to a change address generated by the wallet.
/// This option replaces the usual change address with an arbitrary scriptPubkey of your choosing.
/// Just as with a change output, if the drain output is not needed (the excess coins are too small) it will not be included in the resulting transaction. T
/// he only difference is that it is valid to use drainTo without setting any ordinary recipients with add_recipient (but it is perfectly fine to add recipients as well).
///
/// If you choose not to set any recipients, you should either provide the utxos that the transaction should spend via add_utxos, or set drainWallet to spend all of them.
///
/// When bumping the fees of a transaction made with this option, you probably want to use allowShrinking to allow this output to be reduced to pay for the extra fees.
TxBuilder drainTo(ScriptBuf script) {
_drainTo = script;
return this;
}
///Enable signaling RBF with a specific nSequence value
///
/// This can cause conflicts if the wallet’s descriptors contain an “older” (OP_CSV) operator and the given nsequence is lower than the CSV value.
///
///If the nsequence is higher than 0xFFFFFFFD an error will be thrown, since it would not be a valid nSequence to signal RBF.
TxBuilder enableRbfWithSequence(int nSequence) {
_rbfValue = RbfValue.value(nSequence);
return this;
}
///Enable signaling RBF
///
/// This will use the default nSequence value of 0xFFFFFFFD.
TxBuilder enableRbf() {
_rbfValue = RbfValue.rbfDefault();
return this;
}
///Set an absolute fee
TxBuilder feeAbsolute(int feeAmount) {
_feeAbsolute = feeAmount;
return this;
}
///Set a custom fee rate
TxBuilder feeRate(double satPerVbyte) {
_feeRate = satPerVbyte;
return this;
}
///Replace the recipients already added with a new list
TxBuilder setRecipients(List<ScriptAmount> recipients) {
for (var e in _recipients) {
_recipients.add(e);
}
return this;
}
///Only spend utxos added by add_utxo.
///
/// The wallet will not add additional utxos to the transaction even if they are needed to make the transaction valid.
TxBuilder manuallySelectedOnly() {
_manuallySelectedOnly = true;
return this;
}
///Add a utxo to the internal list of unspendable utxos
///
/// It’s important to note that the “must-be-spent” utxos added with TxBuilder().addUtxo
/// have priority over this. See the docs of the two linked methods for more details.
TxBuilder addUnSpendable(OutPoint unSpendable) {
_unSpendable.add(unSpendable);
return this;
}
///Only spend change outputs
///
/// This effectively adds all the non-change outputs to the “unspendable” list.
TxBuilder onlySpendChange() {
_changeSpendPolicy = ChangeSpendPolicy.onlyChange;
return this;
}
///Finish building the transaction.
///
/// Returns a [PartiallySignedTransaction] & [TransactionDetails]
Future<(PartiallySignedTransaction, TransactionDetails)> finish(
Wallet wallet) async {
if (_recipients.isEmpty && _drainTo == null) {
throw NoRecipientsException();
}
try {
final res = await txBuilderFinish(
wallet: wallet,
recipients: _recipients,
utxos: _utxos,
foreignUtxo: _foreignUtxo,
unSpendable: _unSpendable,
manuallySelectedOnly: _manuallySelectedOnly,
drainWallet: _drainWallet,
rbf: _rbfValue,
drainTo: _drainTo,
feeAbsolute: _feeAbsolute,
feeRate: _feeRate,
data: _data,
changePolicy: _changeSpendPolicy);
return (PartiallySignedTransaction._(ptr: res.$1.ptr), res.$2);
} on BdkError catch (e) {
throw mapToException(e);
}
}
}
/// The Wallet acts as a way of coherently interfacing with output descriptors and related transactions. Its main components are:
///
/// 1. Output descriptors from which it can derive addresses.
/// 2. A Database where it tracks transactions and utxos related to the descriptors.
/// 3. Signers that can contribute signatures to addresses instantiated from the descriptors.
class Wallet extends BdkWallet {
Wallet._({required super.ptr});
/// [Wallet] constructor
///Create a wallet.
// The only way this can fail is if the descriptors passed in do not match the checksums in database.
static Future<Wallet> create({
required Descriptor descriptor,
Descriptor? changeDescriptor,
required Network network,
required DatabaseConfig databaseConfig,
}) async {
try {
await Api.initialize();
final res = await BdkWallet.newInstance(
descriptor: descriptor,
changeDescriptor: changeDescriptor,
network: network,
databaseConfig: databaseConfig,
);
return Wallet._(ptr: res.ptr);
} on BdkError catch (e) {
throw mapToException(e);
}
}
/// Return a derived address using the external descriptor, see AddressIndex for available address index selection
/// strategies. If none of the keys in the descriptor are derivable (i.e. the descriptor does not end with a * character)
/// then the same address will always be returned for any AddressIndex.
Future<AddressInfo> getAddress(
{required AddressIndex addressIndex, hint}) async {
try {
final res =
await BdkWallet.getAddress(ptr: this, addressIndex: addressIndex);
return AddressInfo(res.$2, Address._(ptr: res.$1.ptr));
} on BdkError catch (e) {
throw mapToException(e);
}
}
/// Return the balance, meaning the sum of this wallet’s unspent outputs’ values. Note that this method only operates
/// on the internal database, which first needs to be Wallet.sync manually.
@override
Future<Balance> getBalance({hint}) {
try {
return super.getBalance();
} on BdkError catch (e) {
throw mapToException(e);
}
}
///Returns the descriptor used to create addresses for a particular keychain.
Future<Descriptor> getDescriptorForKeychain(
{required KeychainKind keychain, hint}) async {
try {
final res = await BdkWallet.getDescriptorForKeychain(
ptr: this, keychain: keychain);
return Descriptor._(
extendedDescriptor: res.extendedDescriptor, keyMap: res.keyMap);