Skip to content

Commit

Permalink
derive: add XprivAccount::derive method
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Aug 21, 2024
1 parent fb425db commit 02e64ef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
9 changes: 6 additions & 3 deletions derive/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<I: IdxBase> From<[I; 4]> for DerivationSeg<I> {
impl<I: IdxBase> DerivationSeg<I> {
pub fn new(index: I) -> Self { DerivationSeg(NonEmptyOrdSet::with(index)) }

pub fn with(iter: impl IntoIterator<Item = I>) -> Result<Self, confinement::Error> {
pub fn with(iter: impl IntoIterator<Item=I>) -> Result<Self, confinement::Error> {

Check warning on line 67 in derive/src/path.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/path.rs#L67

Added line #L67 was not covered by tests
Confined::try_from_iter(iter).map(DerivationSeg)
}

Expand Down Expand Up @@ -197,7 +197,8 @@ impl<I: Display> Display for DerivationPath<I> {
}

impl<I: FromStr> FromStr for DerivationPath<I>
where IndexParseError: From<<I as FromStr>::Err>
where
IndexParseError: From<<I as FromStr>::Err>,
{
type Err = DerivationParseError;

Expand Down Expand Up @@ -232,13 +233,15 @@ impl<'path, I: Copy> IntoIterator for &'path DerivationPath<I> {
}

impl<I> FromIterator<I> for DerivationPath<I> {
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self { Self(iter.into_iter().collect()) }
fn from_iter<T: IntoIterator<Item=I>>(iter: T) -> Self { Self(iter.into_iter().collect()) }

Check warning on line 236 in derive/src/path.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/path.rs#L236

Added line #L236 was not covered by tests
}

impl<I: Idx> DerivationPath<I> {
/// Constructs empty derivation path.
pub fn new() -> Self { Self(vec![]) }

pub fn with_capacity(capacity: usize) -> Self { Self(Vec::with_capacity(capacity)) }

Check warning on line 243 in derive/src/path.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/path.rs#L243

Added line #L243 was not covered by tests

pub fn terminal(&self) -> Option<Terminal> {
let mut iter = self.iter().rev();
let index = iter.next()?;
Expand Down
42 changes: 35 additions & 7 deletions derive/src/xkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,22 @@ impl XprivAccount {

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

#[must_use]
pub fn derive(&self, path: impl AsRef<[HardenedIndex]>) -> Self {
let path = path.as_ref();
let xpriv = self.xpriv.derive_priv(path);
let mut prev = DerivationPath::with_capacity(self.origin.derivation.len() + path.len());
prev.extend(&self.origin.derivation);
prev.extend(path);
XprivAccount {
origin: XkeyOrigin {
master_fp: self.origin.master_fp,
derivation: prev,
},
xpriv,
}
}

Check warning on line 857 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L844-L857

Added lines #L844 - L857 were not covered by tests
}

impl Display for XprivAccount {
Expand Down Expand Up @@ -911,7 +927,7 @@ impl XpubDerivable {
pub fn try_custom(
xpub: Xpub,
origin: XkeyOrigin,
keychains: impl IntoIterator<Item = Keychain>,
keychains: impl IntoIterator<Item=Keychain>,
) -> Result<Self, XkeyAccountError> {

Check warning on line 931 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L930-L931

Added lines #L930 - L931 were not covered by tests
Ok(XpubDerivable {
spec: XpubAccount::new(xpub, origin)?,

Check warning on line 933 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L933

Added line #L933 was not covered by tests
Expand Down Expand Up @@ -979,7 +995,9 @@ mod _serde {

impl Serialize for Xpub {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
where
S: Serializer,
{

Check warning on line 1000 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L998-L1000

Added lines #L998 - L1000 were not covered by tests
if serializer.is_human_readable() {
serializer.serialize_str(&self.to_string())
} else {
Expand All @@ -990,7 +1008,9 @@ mod _serde {

impl<'de> Deserialize<'de> for Xpub {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
where
D: Deserializer<'de>,
{

Check warning on line 1013 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L1011-L1013

Added lines #L1011 - L1013 were not covered by tests
if deserializer.is_human_readable() {
let s = String::deserialize(deserializer)?;
Xpub::from_str(&s).map_err(|err| {
Expand All @@ -1006,14 +1026,18 @@ mod _serde {

impl Serialize for XpubAccount {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
where
S: Serializer,
{

Check warning on line 1031 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L1029-L1031

Added lines #L1029 - L1031 were not covered by tests
serializer.serialize_str(&self.to_string())
}
}

impl<'de> Deserialize<'de> for XpubAccount {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
where
D: Deserializer<'de>,
{

Check warning on line 1040 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L1038-L1040

Added lines #L1038 - L1040 were not covered by tests
let s = String::deserialize(deserializer)?;
XpubAccount::from_str(&s).map_err(|err| {
de::Error::custom(format!(
Expand All @@ -1025,14 +1049,18 @@ mod _serde {

impl Serialize for XpubDerivable {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
where
S: Serializer,
{

Check warning on line 1054 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L1052-L1054

Added lines #L1052 - L1054 were not covered by tests
serializer.serialize_str(&self.to_string())
}
}

impl<'de> Deserialize<'de> for XpubDerivable {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
where
D: Deserializer<'de>,
{

Check warning on line 1063 in derive/src/xkey.rs

View check run for this annotation

Codecov / codecov/patch

derive/src/xkey.rs#L1061-L1063

Added lines #L1061 - L1063 were not covered by tests
let s = String::deserialize(deserializer)?;
XpubDerivable::from_str(&s).map_err(|err| {
de::Error::custom(format!("invalid xpub derivation string representation; {err}"))
Expand Down

0 comments on commit 02e64ef

Please sign in to comment.