Skip to content

Commit

Permalink
descriptor: add default_keychain method
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 19, 2023
1 parent e3bac0a commit e3bf159
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions descriptors/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ pub enum StdDescr<S: DeriveSet = XpubDerivable> {
}

impl<S: DeriveSet> Derive<DerivedScript> for StdDescr<S> {
fn default_keychain(&self) -> Keychain {

Check warning on line 143 in descriptors/src/descriptor.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/descriptor.rs#L143

Added line #L143 was not covered by tests
match self {
StdDescr::Wpkh(d) => d.default_keychain(),
StdDescr::TrKey(d) => d.default_keychain(),

Check warning on line 146 in descriptors/src/descriptor.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/descriptor.rs#L145-L146

Added lines #L145 - L146 were not covered by tests
}
}

fn keychains(&self) -> &BTreeSet<Keychain> {

Check warning on line 150 in descriptors/src/descriptor.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/descriptor.rs#L150

Added line #L150 was not covered by tests
match self {
StdDescr::Wpkh(d) => d.keychains(),
Expand Down
3 changes: 3 additions & 0 deletions descriptors/src/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ impl<K: DeriveCompr> Wpkh<K> {
}

impl<K: DeriveCompr> Derive<DerivedScript> for Wpkh<K> {
#[inline]
fn default_keychain(&self) -> Keychain { self.0.default_keychain() }

Check warning on line 45 in descriptors/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/segwit.rs#L45

Added line #L45 was not covered by tests

#[inline]
fn keychains(&self) -> &BTreeSet<Keychain> { self.0.keychains() }

Check warning on line 48 in descriptors/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/segwit.rs#L48

Added line #L48 was not covered by tests

Expand Down
3 changes: 3 additions & 0 deletions descriptors/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ impl<K: DeriveXOnly> TrKey<K> {
}

impl<K: DeriveXOnly> Derive<DerivedScript> for TrKey<K> {
#[inline]
fn default_keychain(&self) -> Keychain { self.0.default_keychain() }

Check warning on line 45 in descriptors/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/taproot.rs#L45

Added line #L45 was not covered by tests

#[inline]
fn keychains(&self) -> &BTreeSet<Keychain> { self.0.keychains() }

Check warning on line 48 in descriptors/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/taproot.rs#L48

Added line #L48 was not covered by tests

Expand Down
11 changes: 11 additions & 0 deletions std/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ impl FromStr for DerivedAddr {
}

pub trait Derive<D> {
fn default_keychain(&self) -> Keychain;

fn keychains(&self) -> &BTreeSet<Keychain>;

fn derive(&self, keychain: impl Into<Keychain>, index: impl Into<NormalIndex>) -> D;
Expand Down Expand Up @@ -358,6 +360,9 @@ impl DeriveKey<XOnlyPk> for XpubDerivable {
}

impl Derive<LegacyPk> for XpubDerivable {
#[inline]
fn default_keychain(&self) -> Keychain { self.keychains.first() }

Check warning on line 364 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L364

Added line #L364 was not covered by tests

#[inline]
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 367 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L367

Added line #L367 was not covered by tests

Expand All @@ -367,6 +372,9 @@ impl Derive<LegacyPk> for XpubDerivable {
}

impl Derive<CompressedPk> for XpubDerivable {
#[inline]
fn default_keychain(&self) -> Keychain { self.keychains.first() }

Check warning on line 376 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L376

Added line #L376 was not covered by tests

#[inline]
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 379 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L379

Added line #L379 was not covered by tests

Expand All @@ -376,6 +384,9 @@ impl Derive<CompressedPk> for XpubDerivable {
}

impl Derive<XOnlyPk> for XpubDerivable {
#[inline]
fn default_keychain(&self) -> Keychain { self.keychains.first() }

Check warning on line 388 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L388

Added line #L388 was not covered by tests

#[inline]
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 391 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L391

Added line #L391 was not covered by tests

Expand Down
8 changes: 8 additions & 0 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ impl<I: IdxBase> DerivationSeg<I> {

#[inline]
pub fn at(&self, index: u8) -> Option<I> { self.0.iter().nth(index as usize).copied() }

#[inline]
pub fn first(&self) -> I {
*self
.0
.first()
.expect("confined type guarantees that there is at least one item in the collection")
}

Check warning on line 68 in std/src/path.rs

View check run for this annotation

Codecov / codecov/patch

std/src/path.rs#L63-L68

Added lines #L63 - L68 were not covered by tests
}

impl DerivationSeg<NormalIndex> {
Expand Down

0 comments on commit e3bf159

Please sign in to comment.