Skip to content

Commit

Permalink
derive: add derivation info API to XKey account types
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jun 30, 2024
1 parent db0ebdc commit bc4c99c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
5 changes: 3 additions & 2 deletions derive/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,17 @@ impl<I: Idx> DerivationPath<I> {
Some(Terminal::new(keychain, index))
}

pub fn shared_prefix<I2>(&self, master: &DerivationPath<I2>) -> usize
pub fn shared_prefix<I2>(&self, master: impl AsRef<[I2]>) -> usize
where
I: Into<DerivationIndex>,
I2: Idx + Into<DerivationIndex>,
{
let master = master.as_ref();
if master.len() <= self.len() {
let shared = self
.iter()
.zip(master)
.take_while(|(i1, i2)| (**i1).into() == (*i2).into())
.take_while(|(i1, i2)| (**i1).into() == (**i2).into())
.count();
if shared == master.len() {
return shared;
Expand Down
47 changes: 46 additions & 1 deletion derive/src/xkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@ impl XkeyOrigin {

pub const fn master_fp(&self) -> XpubFp { self.master_fp }

pub fn derivation(&self) -> &[HardenedIndex] { self.derivation.as_ref() }

pub fn as_derivation(&self) -> &DerivationPath<HardenedIndex> { &self.derivation }

pub fn to_derivation(&self) -> DerivationPath {
Expand All @@ -606,7 +608,7 @@ impl XkeyOrigin {
pub fn child_derivation<'a>(&'a self, child: &'a KeyOrigin) -> Option<&[DerivationIndex]> {
if self.master_fp() == child.master_fp() {
let d = child.derivation();
let shared = d.shared_prefix(self.as_derivation());
let shared = d.shared_prefix(self.derivation());
if shared > 0 {
return Some(&d[shared..]);
}
Expand Down Expand Up @@ -700,6 +702,24 @@ pub struct XpubAccount {

impl XpubAccount {
pub fn new(xpub: Xpub, origin: XkeyOrigin) -> Self { XpubAccount { xpub, origin } }

#[inline]
pub const fn master_fp(&self) -> XpubFp { self.origin.master_fp }

#[inline]
pub fn account_fp(&self) -> XpubFp { self.xpub.fingerprint() }

#[inline]
pub fn account_id(&self) -> XpubId { self.xpub.identifier() }

#[inline]
pub fn derivation(&self) -> &[HardenedIndex] { self.origin.derivation.as_ref() }

#[inline]
pub const fn as_derivation(&self) -> &DerivationPath<HardenedIndex> { &self.origin.derivation }

#[inline]
pub fn to_derivation(&self) -> DerivationPath { self.origin.to_derivation() }
}

impl Display for XpubAccount {
Expand Down Expand Up @@ -750,6 +770,31 @@ pub struct XprivAccount {

impl XprivAccount {
pub fn new(xpriv: Xpriv, origin: XkeyOrigin) -> Self { XprivAccount { xpriv, origin } }

pub fn to_xpub_account(&self) -> XpubAccount {
XpubAccount {
origin: self.origin.clone(),
xpub: self.xpriv.to_xpub(),
}
}

#[inline]
pub const fn master_fp(&self) -> XpubFp { self.origin.master_fp }

#[inline]
pub fn account_fp(&self) -> XpubFp { self.xpriv.fingerprint() }

#[inline]
pub fn account_id(&self) -> XpubId { self.xpriv.identifier() }

#[inline]
pub fn derivation(&self) -> &[HardenedIndex] { self.origin.derivation.as_ref() }

#[inline]
pub const fn as_derivation(&self) -> &DerivationPath<HardenedIndex> { &self.origin.derivation }

#[inline]
pub fn to_derivation(&self) -> DerivationPath { self.origin.to_derivation() }
}

impl Display for XprivAccount {
Expand Down

0 comments on commit bc4c99c

Please sign in to comment.