[example-cli] simplify new address logic
This commit is contained in:
parent
6a1ac7f80a
commit
a78967e51b
@ -10,7 +10,7 @@ use bdk_chain::{
|
|||||||
psbt::Prevouts,
|
psbt::Prevouts,
|
||||||
secp256k1::{self, Secp256k1},
|
secp256k1::{self, Secp256k1},
|
||||||
util::sighash::SighashCache,
|
util::sighash::SighashCache,
|
||||||
Address, LockTime, Network, Script, Sequence, Transaction, TxIn, TxOut,
|
Address, LockTime, Network, Sequence, Transaction, TxIn, TxOut,
|
||||||
},
|
},
|
||||||
indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
|
indexed_tx_graph::{IndexedAdditions, IndexedTxGraph},
|
||||||
keychain::{DerivationAdditions, KeychainTxOutIndex},
|
keychain::{DerivationAdditions, KeychainTxOutIndex},
|
||||||
@ -219,8 +219,17 @@ pub fn run_address_cmd<A, X>(
|
|||||||
where
|
where
|
||||||
ChangeSet<A, X>: Default + Append + DeserializeOwned + Serialize,
|
ChangeSet<A, X>: Default + Append + DeserializeOwned + Serialize,
|
||||||
{
|
{
|
||||||
let process_spk = |spk_i: u32, spk: &Script, index_additions: DerivationAdditions<Keychain>| {
|
let index = &mut graph.index;
|
||||||
if !index_additions.is_empty() {
|
|
||||||
|
match cmd {
|
||||||
|
AddressCmd::Next | AddressCmd::New => {
|
||||||
|
let spk_chooser = match 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_additions) = spk_chooser(index, &Keychain::External);
|
||||||
let db = &mut *db.lock().unwrap();
|
let db = &mut *db.lock().unwrap();
|
||||||
db.stage(ChangeSet {
|
db.stage(ChangeSet {
|
||||||
indexed_additions: IndexedAdditions {
|
indexed_additions: IndexedAdditions {
|
||||||
@ -230,22 +239,9 @@ where
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
db.commit()?;
|
db.commit()?;
|
||||||
}
|
|
||||||
let addr = Address::from_script(spk, network).context("failed to derive address")?;
|
let addr = Address::from_script(spk, network).context("failed to derive address")?;
|
||||||
println!("[address @ {}] {}", spk_i, addr);
|
println!("[address @ {}] {}", spk_i, addr);
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
|
||||||
|
|
||||||
let index = &mut graph.index;
|
|
||||||
|
|
||||||
match cmd {
|
|
||||||
AddressCmd::Next => {
|
|
||||||
let ((spk_i, spk), index_additions) = index.next_unused_spk(&Keychain::External);
|
|
||||||
process_spk(spk_i, spk, index_additions)
|
|
||||||
}
|
|
||||||
AddressCmd::New => {
|
|
||||||
let ((spk_i, spk), index_additions) = index.reveal_next_spk(&Keychain::External);
|
|
||||||
process_spk(spk_i, spk, index_additions)
|
|
||||||
}
|
}
|
||||||
AddressCmd::Index => {
|
AddressCmd::Index => {
|
||||||
for (keychain, derivation_index) in index.last_revealed_indices() {
|
for (keychain, derivation_index) in index.last_revealed_indices() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user