forked from bitcoindevkit/bdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
938 lines (853 loc) · 32.1 KB
/
lib.rs
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
use serde_json::json;
use std::cmp;
use std::collections::HashMap;
use std::env;
use std::fmt;
use std::str::FromStr;
use std::sync::Mutex;
use anyhow::bail;
use anyhow::Context;
use bdk_chain::bitcoin::{
absolute, address::NetworkUnchecked, bip32, consensus, constants, hex::DisplayHex, relative,
secp256k1::Secp256k1, transaction, Address, Amount, Network, NetworkKind, PrivateKey, Psbt,
PublicKey, Sequence, Transaction, TxIn, TxOut,
};
use bdk_chain::miniscript::{
descriptor::{DescriptorSecretKey, SinglePubKey},
plan::{Assets, Plan},
psbt::PsbtExt,
Descriptor, DescriptorPublicKey,
};
use bdk_chain::ConfirmationBlockTime;
use bdk_chain::{
indexed_tx_graph,
indexer::keychain_txout::{self, KeychainTxOutIndex},
local_chain::{self, LocalChain},
tx_graph, ChainOracle, DescriptorExt, FullTxOut, IndexedTxGraph, Merge,
};
use bdk_coin_select::{
metrics::LowestFee, Candidate, ChangePolicy, CoinSelector, DrainWeights, FeeRate, Target,
TargetFee, TargetOutputs,
};
use bdk_file_store::Store;
use clap::{Parser, Subcommand};
use rand::prelude::*;
pub use anyhow;
pub use clap;
/// Alias for a `IndexedTxGraph` with specific `Anchor` and `Indexer`.
pub type KeychainTxGraph = IndexedTxGraph<ConfirmationBlockTime, KeychainTxOutIndex<Keychain>>;
/// ChangeSet
#[derive(Default, Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ChangeSet {
/// Descriptor for recipient addresses.
pub descriptor: Option<Descriptor<DescriptorPublicKey>>,
/// Descriptor for change addresses.
pub change_descriptor: Option<Descriptor<DescriptorPublicKey>>,
/// Stores the network type of the transaction data.
pub network: Option<Network>,
/// Changes to the [`LocalChain`].
pub local_chain: local_chain::ChangeSet,
/// Changes to [`TxGraph`](tx_graph::TxGraph).
pub tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>,
/// Changes to [`KeychainTxOutIndex`].
pub indexer: keychain_txout::ChangeSet,
}
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Args<CS: clap::Subcommand, S: clap::Args> {
#[clap(subcommand)]
pub command: Commands<CS, S>,
}
#[derive(Subcommand, Debug, Clone)]
pub enum Commands<CS: clap::Subcommand, S: clap::Args> {
/// Initialize a new data store.
Init {
/// Network
#[clap(long, short, default_value = "signet")]
network: Network,
/// Descriptor
#[clap(env = "DESCRIPTOR")]
descriptor: String,
/// Change descriptor
#[clap(long, short, env = "CHANGE_DESCRIPTOR")]
change_descriptor: Option<String>,
},
#[clap(flatten)]
ChainSpecific(CS),
/// Address generation and inspection.
Address {
#[clap(subcommand)]
addr_cmd: AddressCmd,
},
/// Get the wallet balance.
Balance,
/// TxOut related commands.
#[clap(name = "txout")]
TxOut {
#[clap(subcommand)]
txout_cmd: TxOutCmd,
},
/// PSBT operations
Psbt {
#[clap(subcommand)]
psbt_cmd: PsbtCmd<S>,
},
/// Generate new BIP86 descriptors.
Generate {
/// Network
#[clap(long, short, default_value = "signet")]
network: Network,
},
}
#[derive(Subcommand, Debug, Clone)]
pub enum AddressCmd {
/// Get the next unused address.
Next,
/// Get a new address regardless of the existing unused addresses.
New,
/// List all addresses
List {
/// List change addresses
#[clap(long)]
change: bool,
},
/// Get last revealed address index for each keychain.
Index,
}
#[derive(Subcommand, Debug, Clone)]
pub enum TxOutCmd {
/// List transaction outputs.
List {
/// Return only spent outputs.
#[clap(short, long)]
spent: bool,
/// Return only unspent outputs.
#[clap(short, long)]
unspent: bool,
/// Return only confirmed outputs.
#[clap(long)]
confirmed: bool,
/// Return only unconfirmed outputs.
#[clap(long)]
unconfirmed: bool,
},
}
#[derive(Subcommand, Debug, Clone)]
pub enum PsbtCmd<S: clap::Args> {
/// Create a new PSBT.
New {
/// Amount to send in satoshis
value: u64,
/// Recipient address
address: Address<NetworkUnchecked>,
/// Set max absolute timelock (from consensus value)
#[clap(long, short)]
after: Option<u32>,
/// Set max relative timelock (from consensus value)
#[clap(long, short)]
older: Option<u32>,
/// Coin selection algorithm
#[clap(long, short, default_value = "bnb")]
coin_select: CoinSelectionAlgo,
/// Debug print the PSBT
#[clap(long, short)]
debug: bool,
},
/// Sign with a hot signer
Sign {
/// PSBT
#[clap(long)]
psbt: Option<String>,
/// Private descriptor
#[clap(long, short = 'd')]
descriptor: Option<String>,
},
/// Extract transaction
Extract {
/// PSBT
psbt: String,
/// Whether to try broadcasting the tx
#[clap(long, short = 'b')]
try_broadcast: bool,
#[clap(flatten)]
chain_specific: S,
},
}
#[derive(
Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize, serde::Serialize,
)]
pub enum Keychain {
External,
Internal,
}
impl fmt::Display for Keychain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Keychain::External => write!(f, "external"),
Keychain::Internal => write!(f, "internal"),
}
}
}
#[derive(Clone, Debug, Default)]
pub enum CoinSelectionAlgo {
LargestFirst,
SmallestFirst,
OldestFirst,
NewestFirst,
#[default]
BranchAndBound,
}
impl FromStr for CoinSelectionAlgo {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use CoinSelectionAlgo::*;
Ok(match s {
"largest-first" => LargestFirst,
"smallest-first" => SmallestFirst,
"oldest-first" => OldestFirst,
"newest-first" => NewestFirst,
"bnb" => BranchAndBound,
unknown => bail!("unknown coin selection algorithm '{}'", unknown),
})
}
}
impl fmt::Display for CoinSelectionAlgo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use CoinSelectionAlgo::*;
write!(
f,
"{}",
match self {
LargestFirst => "largest-first",
SmallestFirst => "smallest-first",
OldestFirst => "oldest-first",
NewestFirst => "newest-first",
BranchAndBound => "bnb",
}
)
}
}
// Records changes to the internal keychain when we
// have to include a change output during tx creation.
#[derive(Debug)]
pub struct ChangeInfo {
pub change_keychain: Keychain,
pub indexer: keychain_txout::ChangeSet,
pub index: u32,
}
pub fn create_tx<O: ChainOracle>(
graph: &mut KeychainTxGraph,
chain: &O,
assets: &Assets,
cs_algorithm: CoinSelectionAlgo,
address: Address,
value: u64,
) -> anyhow::Result<(Psbt, Option<ChangeInfo>)>
where
O::Error: std::error::Error + Send + Sync + 'static,
{
let mut changeset = keychain_txout::ChangeSet::default();
// get planned utxos
let mut plan_utxos = planned_utxos(graph, chain, assets)?;
// sort utxos if cs-algo requires it
match cs_algorithm {
CoinSelectionAlgo::LargestFirst => {
plan_utxos.sort_by_key(|(_, utxo)| cmp::Reverse(utxo.txout.value))
}
CoinSelectionAlgo::SmallestFirst => plan_utxos.sort_by_key(|(_, utxo)| utxo.txout.value),
CoinSelectionAlgo::OldestFirst => plan_utxos.sort_by_key(|(_, utxo)| utxo.chain_position),
CoinSelectionAlgo::NewestFirst => {
plan_utxos.sort_by_key(|(_, utxo)| cmp::Reverse(utxo.chain_position))
}
CoinSelectionAlgo::BranchAndBound => plan_utxos.shuffle(&mut rand::rng()),
}
// build candidate set
let candidates: Vec<Candidate> = plan_utxos
.iter()
.map(|(plan, utxo)| {
Candidate::new(
utxo.txout.value.to_sat(),
plan.satisfaction_weight() as u32,
plan.witness_version().is_some(),
)
})
.collect();
// create recipient output(s)
let mut outputs = vec![TxOut {
value: Amount::from_sat(value),
script_pubkey: address.script_pubkey(),
}];
let (change_keychain, _) = graph
.index
.keychains()
.last()
.expect("must have a keychain");
let ((change_index, change_script), index_changeset) = graph
.index
.next_unused_spk(change_keychain)
.expect("Must exist");
changeset.merge(index_changeset);
let mut change_output = TxOut {
value: Amount::ZERO,
script_pubkey: change_script,
};
let change_desc = graph
.index
.keychains()
.find(|(k, _)| k == &change_keychain)
.expect("must exist")
.1;
let min_drain_value = change_desc.dust_value().to_sat();
let target = Target {
outputs: TargetOutputs::fund_outputs(
outputs
.iter()
.map(|output| (output.weight().to_wu() as u32, output.value.to_sat())),
),
fee: TargetFee::default(),
};
let change_policy = ChangePolicy {
min_value: min_drain_value,
drain_weights: DrainWeights::TR_KEYSPEND,
};
// run coin selection
let mut selector = CoinSelector::new(&candidates);
match cs_algorithm {
CoinSelectionAlgo::BranchAndBound => {
let metric = LowestFee {
target,
long_term_feerate: FeeRate::from_sat_per_vb(10.0),
change_policy,
};
match selector.run_bnb(metric, 10_000) {
Ok(_) => {}
Err(_) => selector
.select_until_target_met(target)
.context("selecting coins")?,
}
}
_ => selector
.select_until_target_met(target)
.context("selecting coins")?,
}
// get the selected plan utxos
let selected: Vec<_> = selector.apply_selection(&plan_utxos).collect();
// if the selection tells us to use change and the change value is sufficient, we add it as an output
let mut change_info = Option::<ChangeInfo>::None;
let drain = selector.drain(target, change_policy);
if drain.value > min_drain_value {
change_output.value = Amount::from_sat(drain.value);
outputs.push(change_output);
change_info = Some(ChangeInfo {
change_keychain,
indexer: changeset,
index: change_index,
});
outputs.shuffle(&mut rand::rng());
}
let unsigned_tx = Transaction {
version: transaction::Version::TWO,
lock_time: assets
.absolute_timelock
.unwrap_or(absolute::LockTime::from_height(
chain.get_chain_tip()?.height,
)?),
input: selected
.iter()
.map(|(plan, utxo)| TxIn {
previous_output: utxo.outpoint,
sequence: plan
.relative_timelock
.map_or(Sequence::ENABLE_RBF_NO_LOCKTIME, Sequence::from),
..Default::default()
})
.collect(),
output: outputs,
};
// update psbt with plan
let mut psbt = Psbt::from_unsigned_tx(unsigned_tx)?;
for (i, (plan, utxo)) in selected.iter().enumerate() {
let psbt_input = &mut psbt.inputs[i];
plan.update_psbt_input(psbt_input);
psbt_input.witness_utxo = Some(utxo.txout.clone());
}
Ok((psbt, change_info))
}
// Alias the elements of `planned_utxos`
pub type PlanUtxo = (Plan, FullTxOut<ConfirmationBlockTime>);
pub fn planned_utxos<O: ChainOracle>(
graph: &KeychainTxGraph,
chain: &O,
assets: &Assets,
) -> Result<Vec<PlanUtxo>, O::Error> {
let chain_tip = chain.get_chain_tip()?;
let outpoints = graph.index.outpoints();
graph
.graph()
.try_filter_chain_unspents(chain, chain_tip, outpoints.iter().cloned())?
.filter_map(|((k, i), full_txo)| -> Option<Result<PlanUtxo, _>> {
let desc = graph
.index
.keychains()
.find(|(keychain, _)| *keychain == k)
.expect("keychain must exist")
.1
.at_derivation_index(i)
.expect("i can't be hardened");
let plan = desc.plan(assets).ok()?;
Some(Ok((plan, full_txo)))
})
.collect()
}
pub fn handle_commands<CS: clap::Subcommand, S: clap::Args>(
graph: &Mutex<KeychainTxGraph>,
chain: &Mutex<LocalChain>,
db: &Mutex<Store<ChangeSet>>,
network: Network,
broadcast: impl FnOnce(S, &Transaction) -> anyhow::Result<()>,
cmd: Commands<CS, S>,
) -> anyhow::Result<()> {
match cmd {
Commands::Init { .. } => unreachable!("handled by init command"),
Commands::Generate { .. } => unreachable!("handled by generate command"),
Commands::ChainSpecific(_) => unreachable!("example code should handle this!"),
Commands::Address { addr_cmd } => {
let graph = &mut *graph.lock().unwrap();
let index = &mut graph.index;
match addr_cmd {
AddressCmd::Next | AddressCmd::New => {
let spk_chooser = match addr_cmd {
AddressCmd::Next => KeychainTxOutIndex::next_unused_spk,
AddressCmd::New => KeychainTxOutIndex::reveal_next_spk,
_ => unreachable!("only these two variants exist in match arm"),
};
let ((spk_i, spk), index_changeset) =
spk_chooser(index, Keychain::External).expect("Must exist");
let db = &mut *db.lock().unwrap();
db.append_changeset(&ChangeSet {
indexer: index_changeset,
..Default::default()
})?;
let addr = Address::from_script(spk.as_script(), network)?;
println!("[address @ {}] {}", spk_i, addr);
Ok(())
}
AddressCmd::Index => {
for (keychain, derivation_index) in index.last_revealed_indices() {
println!("{:?}: {}", keychain, derivation_index);
}
Ok(())
}
AddressCmd::List { change } => {
let target_keychain = match change {
true => Keychain::Internal,
false => Keychain::External,
};
for (spk_i, spk) in index.revealed_keychain_spks(target_keychain) {
let address = Address::from_script(spk.as_script(), network)
.expect("should always be able to derive address");
println!(
"{:?} {} used:{}",
spk_i,
address,
index.is_used(target_keychain, spk_i)
);
}
Ok(())
}
}
}
Commands::Balance => {
let graph = &*graph.lock().unwrap();
let chain = &*chain.lock().unwrap();
fn print_balances<'a>(
title_str: &'a str,
items: impl IntoIterator<Item = (&'a str, Amount)>,
) {
println!("{}:", title_str);
for (name, amount) in items.into_iter() {
println!(" {:<10} {:>12} sats", name, amount.to_sat())
}
}
let balance = graph.graph().try_balance(
chain,
chain.get_chain_tip()?,
graph.index.outpoints().iter().cloned(),
|(k, _), _| k == &Keychain::Internal,
)?;
let confirmed_total = balance.confirmed + balance.immature;
let unconfirmed_total = balance.untrusted_pending + balance.trusted_pending;
print_balances(
"confirmed",
[
("total", confirmed_total),
("spendable", balance.confirmed),
("immature", balance.immature),
],
);
print_balances(
"unconfirmed",
[
("total", unconfirmed_total),
("trusted", balance.trusted_pending),
("untrusted", balance.untrusted_pending),
],
);
Ok(())
}
Commands::TxOut { txout_cmd } => {
let graph = &*graph.lock().unwrap();
let chain = &*chain.lock().unwrap();
let chain_tip = chain.get_chain_tip()?;
let outpoints = graph.index.outpoints();
match txout_cmd {
TxOutCmd::List {
spent,
unspent,
confirmed,
unconfirmed,
} => {
let txouts = graph
.graph()
.try_filter_chain_txouts(chain, chain_tip, outpoints.iter().cloned())?
.filter(|(_, full_txo)| match (spent, unspent) {
(true, false) => full_txo.spent_by.is_some(),
(false, true) => full_txo.spent_by.is_none(),
_ => true,
})
.filter(|(_, full_txo)| match (confirmed, unconfirmed) {
(true, false) => full_txo.chain_position.is_confirmed(),
(false, true) => !full_txo.chain_position.is_confirmed(),
_ => true,
})
.collect::<Vec<_>>();
for (spk_i, full_txo) in txouts {
let addr = Address::from_script(&full_txo.txout.script_pubkey, network)?;
println!(
"{:?} {} {} {} spent:{:?}",
spk_i, full_txo.txout.value, full_txo.outpoint, addr, full_txo.spent_by
)
}
Ok(())
}
}
}
Commands::Psbt { psbt_cmd } => match psbt_cmd {
PsbtCmd::New {
value,
address,
after,
older,
coin_select,
debug,
} => {
let address = address.require_network(network)?;
let (psbt, change_info) = {
let mut graph = graph.lock().unwrap();
let chain = chain.lock().unwrap();
// collect assets we can sign for
let mut assets = Assets::new();
if let Some(n) = after {
assets = assets.after(absolute::LockTime::from_consensus(n));
}
if let Some(n) = older {
assets = assets.older(relative::LockTime::from_consensus(n)?);
}
for (_, desc) in graph.index.keychains() {
match desc {
Descriptor::Wpkh(wpkh) => {
assets = assets.add(wpkh.clone().into_inner());
}
Descriptor::Tr(tr) => {
assets = assets.add(tr.internal_key().clone());
}
_ => bail!("unsupported descriptor type"),
}
}
create_tx(&mut graph, &*chain, &assets, coin_select, address, value)?
};
if let Some(ChangeInfo {
change_keychain,
indexer,
index,
}) = change_info
{
// We must first persist to disk the fact that we've got a new address from the
// change keychain so future scans will find the tx we're about to broadcast.
// If we're unable to persist this, then we don't want to broadcast.
{
let db = &mut *db.lock().unwrap();
db.append_changeset(&ChangeSet {
indexer,
..Default::default()
})?;
}
// We don't want other callers/threads to use this address while we're using it
// but we also don't want to scan the tx we just created because it's not
// technically in the blockchain yet.
graph
.lock()
.unwrap()
.index
.mark_used(change_keychain, index);
}
if debug {
dbg!(psbt);
} else {
// print base64 encoded psbt
let fee = psbt.fee()?.to_sat();
let mut obj = serde_json::Map::new();
obj.insert("psbt".to_string(), json!(psbt.to_string()));
obj.insert("fee".to_string(), json!(fee));
println!("{}", serde_json::to_string_pretty(&obj)?);
};
Ok(())
}
PsbtCmd::Sign { psbt, descriptor } => {
let mut psbt = Psbt::from_str(psbt.unwrap_or_default().as_str())?;
let desc_str = match descriptor {
Some(s) => s,
None => env::var("DESCRIPTOR").context("unable to sign")?,
};
let secp = Secp256k1::new();
let (_, keymap) = Descriptor::parse_descriptor(&secp, &desc_str)?;
if keymap.is_empty() {
bail!("unable to sign")
}
// note: we're only looking at the first entry in the keymap
// the idea is to find something that impls `GetKey`
let sign_res = match keymap.iter().next().expect("not empty") {
(DescriptorPublicKey::Single(single_pub), DescriptorSecretKey::Single(prv)) => {
let pk = match single_pub.key {
SinglePubKey::FullKey(pk) => pk,
SinglePubKey::XOnly(_) => unimplemented!("single xonly pubkey"),
};
let keys: HashMap<PublicKey, PrivateKey> = [(pk, prv.key)].into();
psbt.sign(&keys, &secp)
}
(_, DescriptorSecretKey::XPrv(k)) => psbt.sign(&k.xkey, &secp),
_ => unimplemented!("multi xkey signer"),
};
let _ = sign_res
.map_err(|errors| anyhow::anyhow!("failed to sign PSBT {:?}", errors))?;
let mut obj = serde_json::Map::new();
obj.insert("psbt".to_string(), json!(psbt.to_string()));
println!("{}", serde_json::to_string_pretty(&obj)?);
Ok(())
}
PsbtCmd::Extract {
try_broadcast,
chain_specific,
psbt,
} => {
let mut psbt = Psbt::from_str(psbt.as_str())?;
psbt.finalize_mut(&Secp256k1::new())
.map_err(|errors| anyhow::anyhow!("failed to finalize PSBT {errors:?}"))?;
let tx = psbt.extract_tx()?;
if try_broadcast {
let mut graph = graph.lock().unwrap();
match broadcast(chain_specific, &tx) {
Ok(_) => {
println!("Broadcasted Tx: {}", tx.compute_txid());
let changeset = graph.insert_tx(tx);
// We know the tx is at least unconfirmed now. Note if persisting here fails,
// it's not a big deal since we can always find it again from the
// blockchain.
db.lock().unwrap().append_changeset(&ChangeSet {
tx_graph: changeset.tx_graph,
indexer: changeset.indexer,
..Default::default()
})?;
}
Err(e) => {
// We failed to broadcast, so allow our change address to be used in the future
let (change_keychain, _) = graph
.index
.keychains()
.last()
.expect("must have a keychain");
let change_index = tx.output.iter().find_map(|txout| {
let spk = txout.script_pubkey.clone();
match graph.index.index_of_spk(spk) {
Some(&(keychain, index)) if keychain == change_keychain => {
Some((keychain, index))
}
_ => None,
}
});
if let Some((keychain, index)) = change_index {
graph.index.unmark_used(keychain, index);
}
bail!(e);
}
}
} else {
// encode raw tx hex
let hex = consensus::serialize(&tx).to_lower_hex_string();
let mut obj = serde_json::Map::new();
obj.insert("tx".to_string(), json!(hex));
println!("{}", serde_json::to_string_pretty(&obj)?);
}
Ok(())
}
},
}
}
/// The initial state returned by [`init_or_load`].
pub struct Init<CS: clap::Subcommand, S: clap::Args> {
/// CLI args
pub args: Args<CS, S>,
/// Indexed graph
pub graph: Mutex<KeychainTxGraph>,
/// Local chain
pub chain: Mutex<LocalChain>,
/// Database
pub db: Mutex<Store<ChangeSet>>,
/// Network
pub network: Network,
}
/// Loads from persistence or creates new
pub fn init_or_load<CS: clap::Subcommand, S: clap::Args>(
db_magic: &[u8],
db_path: &str,
) -> anyhow::Result<Option<Init<CS, S>>> {
let args = Args::<CS, S>::parse();
match args.command {
// initialize new db
Commands::Init { .. } => initialize::<CS, S>(args, db_magic, db_path).map(|_| None),
// generate keys
Commands::Generate { network } => generate_bip86_helper(network).map(|_| None),
// try load
_ => {
let mut db =
Store::<ChangeSet>::open(db_magic, db_path).context("could not open file store")?;
let changeset = db.aggregate_changesets()?.expect("db must not be empty");
let network = changeset.network.expect("changeset network");
let chain = Mutex::new({
let (mut chain, _) =
LocalChain::from_genesis_hash(constants::genesis_block(network).block_hash());
chain.apply_changeset(&changeset.local_chain)?;
chain
});
let graph = Mutex::new({
// insert descriptors and apply loaded changeset
let mut index = KeychainTxOutIndex::default();
if let Some(desc) = changeset.descriptor {
index.insert_descriptor(Keychain::External, desc)?;
}
if let Some(change_desc) = changeset.change_descriptor {
index.insert_descriptor(Keychain::Internal, change_desc)?;
}
let mut graph = KeychainTxGraph::new(index);
graph.apply_changeset(indexed_tx_graph::ChangeSet {
tx_graph: changeset.tx_graph,
indexer: changeset.indexer,
});
graph
});
let db = Mutex::new(db);
Ok(Some(Init {
args,
graph,
chain,
db,
network,
}))
}
}
}
/// Initialize db backend.
fn initialize<CS, S>(args: Args<CS, S>, db_magic: &[u8], db_path: &str) -> anyhow::Result<()>
where
CS: clap::Subcommand,
S: clap::Args,
{
if let Commands::Init {
network,
descriptor,
change_descriptor,
} = args.command
{
let mut changeset = ChangeSet::default();
// parse descriptors
let secp = Secp256k1::new();
let mut index = KeychainTxOutIndex::default();
let (descriptor, _) =
Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, &descriptor)?;
let _ = index.insert_descriptor(Keychain::External, descriptor.clone())?;
changeset.descriptor = Some(descriptor);
if let Some(desc) = change_descriptor {
let (change_descriptor, _) =
Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, &desc)?;
let _ = index.insert_descriptor(Keychain::Internal, change_descriptor.clone())?;
changeset.change_descriptor = Some(change_descriptor);
}
// create new
let (_, chain_changeset) =
LocalChain::from_genesis_hash(constants::genesis_block(network).block_hash());
changeset.network = Some(network);
changeset.local_chain = chain_changeset;
let mut db = Store::<ChangeSet>::create_new(db_magic, db_path)?;
db.append_changeset(&changeset)?;
println!("New database {db_path}");
}
Ok(())
}
/// Generate BIP86 descriptors.
fn generate_bip86_helper(network: impl Into<NetworkKind>) -> anyhow::Result<()> {
let secp = Secp256k1::new();
let mut seed = [0x00; 32];
rand::rng().fill_bytes(&mut seed);
let m = bip32::Xpriv::new_master(network, &seed)?;
let fp = m.fingerprint(&secp);
let path = if m.network.is_mainnet() {
"86h/0h/0h"
} else {
"86h/1h/0h"
};
let descriptors: Vec<String> = [0, 1]
.iter()
.map(|i| format!("tr([{fp}]{m}/{path}/{i}/*)"))
.collect();
let external_desc = &descriptors[0];
let internal_desc = &descriptors[1];
let (descriptor, keymap) =
<Descriptor<DescriptorPublicKey>>::parse_descriptor(&secp, external_desc)?;
let (internal_descriptor, internal_keymap) =
<Descriptor<DescriptorPublicKey>>::parse_descriptor(&secp, internal_desc)?;
println!("Public");
println!("{}", descriptor);
println!("{}", internal_descriptor);
println!("\nPrivate");
println!("{}", descriptor.to_string_with_secret(&keymap));
println!(
"{}",
internal_descriptor.to_string_with_secret(&internal_keymap)
);
Ok(())
}
impl Merge for ChangeSet {
fn merge(&mut self, other: Self) {
if other.descriptor.is_some() {
self.descriptor = other.descriptor;
}
if other.change_descriptor.is_some() {
self.change_descriptor = other.change_descriptor;
}
if other.network.is_some() {
self.network = other.network;
}
Merge::merge(&mut self.local_chain, other.local_chain);
Merge::merge(&mut self.tx_graph, other.tx_graph);
Merge::merge(&mut self.indexer, other.indexer);
}
fn is_empty(&self) -> bool {
self.descriptor.is_none()
&& self.change_descriptor.is_none()
&& self.network.is_none()
&& self.local_chain.is_empty()
&& self.tx_graph.is_empty()
&& self.indexer.is_empty()
}
}