Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ongoing BP wallet improvements #15

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ toml = { workspace = true, optional = true }
cfg_eval = { workspace = true, optional = true }

[features]
default = ["esplora"]
default = []
all = ["esplora", "fs"]
esplora = ["bp-esplora"]
fs = ["serde"]
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/bin/bp.rs"
[dependencies]
amplify = { workspace = true, features = ["serde"] }
strict_encoding = { workspace = true }
bp-wallet = { version = "0.11.0-beta.2", path = "..", features = ["serde", "fs"] }
bp-wallet = { version = "0.11.0-beta.2", path = "..", features = ["serde", "fs", "esplora"] }
bp-std = { workspace = true, features = ["serde"] }
descriptors = { workspace = true, features = ["serde"] }
psbt = { workspace = true, features = ["serde"] }
Expand Down
3 changes: 2 additions & 1 deletion cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ impl<O: DescriptorOpts> Exec for Args<Command, O> {
);
exit(1);
}
let index = index.unwrap_or_else(|| runtime.next_index(keychain, !*no_shift));
let index =
index.unwrap_or_else(|| runtime.next_derivation_index(keychain, !*no_shift));
println!("\nTerm.\tAddress");
for derived_addr in
runtime.addresses(keychain).skip(index.index() as usize).take(*no as usize)
Expand Down
3 changes: 2 additions & 1 deletion src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@
let (change_vout, change_terminal) = if remaining_value
> self.descr.generator.class().dust_limit()
{
let change_index = self.next_index(params.change_keychain, params.change_shift);
let change_index =
self.next_derivation_index(params.change_keychain, params.change_shift);

Check warning on line 262 in src/payments.rs

View check run for this annotation

Codecov / codecov/patch

src/payments.rs#L261-L262

Added lines #L261 - L262 were not covered by tests
let change_terminal = Terminal::new(params.change_keychain, change_index);
let change_vout = psbt
.construct_change_expect(&self.descr.generator, change_terminal, remaining_value)
Expand Down
32 changes: 27 additions & 5 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,39 @@
WalletCache::with::<_, K, _, L2>(&self.descr, blockchain).map(|cache| self.cache = cache)
}

pub fn next_index(&mut self, keychain: impl Into<Keychain>, shift: bool) -> NormalIndex {
pub fn to_deriver(&self) -> D
where
D: Clone,
K: Clone,
{
self.descr.clone()
}

Check warning on line 335 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L329-L335

Added lines #L329 - L335 were not covered by tests

fn last_published_derivation_index(&self, keychain: impl Into<Keychain>) -> NormalIndex {

Check warning on line 337 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L337

Added line #L337 was not covered by tests
let keychain = keychain.into();
let mut idx = self
.address_coins()
self.address_coins()

Check warning on line 339 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L339

Added line #L339 was not covered by tests
.keys()
.filter(|ad| ad.terminal.keychain == keychain)
.map(|ad| ad.terminal.index)
.max()
.as_ref()
.map(NormalIndex::saturating_inc)
.unwrap_or_default();
.unwrap_or_default()
}

Check warning on line 347 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L346-L347

Added lines #L346 - L347 were not covered by tests

pub fn last_derivation_index(&self, keychain: impl Into<Keychain>) -> NormalIndex {
let keychain = keychain.into();
let last_index = self.data.last_used.get(&keychain).copied().unwrap_or_default();
cmp::max(last_index, self.last_published_derivation_index(keychain))
}

Check warning on line 353 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L349-L353

Added lines #L349 - L353 were not covered by tests

pub fn next_derivation_index(
&mut self,
keychain: impl Into<Keychain>,
shift: bool,
) -> NormalIndex {
let keychain = keychain.into();
let mut idx = self.last_published_derivation_index(keychain);

Check warning on line 361 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L355-L361

Added lines #L355 - L361 were not covered by tests
let last_index = self.data.last_used.entry(keychain).or_default();
idx = cmp::max(*last_index, idx);
if shift {
Expand All @@ -347,7 +369,7 @@

pub fn next_address(&mut self, keychain: impl Into<Keychain>, shift: bool) -> Address {
let keychain = keychain.into();
let index = self.next_index(keychain, shift);
let index = self.next_derivation_index(keychain, shift);

Check warning on line 372 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L372

Added line #L372 was not covered by tests
self.addresses(keychain)
.skip(index.index() as usize)
.next()
Expand Down
Loading