Skip to content

Commit

Permalink
bip32: fix FromStr implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 30, 2023
1 parent 64f2e1a commit d0e0e94
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
license = "Apache-2.0"

[workspace.dependencies]
amplify = "4.0.0"
amplify = "4.0.1"
bitcoin_hashes = "0.12.0"
bech32 = "0.9.1"
bp-primitives = "0.10.6"
Expand Down
19 changes: 11 additions & 8 deletions std/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,13 @@ impl Idx for HardenedIndex {
const MAX: Self = Self(u32::MAX);

#[inline]
fn from_child_number(index: impl Into<u16>) -> Self { Self(index.into() as u32) }
fn from_child_number(child_no: impl Into<u16>) -> Self { Self(child_no.into() as u32) }

#[inline]
fn try_from_child_number(index: impl Into<u32>) -> Result<Self, IndexError> {
let index = index.into();
fn try_from_child_number(child_no: impl Into<u32>) -> Result<Self, IndexError> {
let index = child_no.into();
if index < HARDENED_INDEX_BOUNDARY {
Ok(Self(index - HARDENED_INDEX_BOUNDARY))
Ok(Self(index))
} else {
Err(IndexError {
what: "child number",
Expand All @@ -386,10 +386,13 @@ impl Idx for HardenedIndex {
fn child_number(&self) -> u32 { self.0 }

#[inline]
fn try_from_index(value: u32) -> Result<Self, IndexError> {
Self::try_from_child_number(value - HARDENED_INDEX_BOUNDARY).map_err(|_| IndexError {
fn try_from_index(child_no: u32) -> Result<Self, IndexError> {
if child_no < HARDENED_INDEX_BOUNDARY {
return Ok(Self(child_no));
}
Self::try_from_child_number(child_no - HARDENED_INDEX_BOUNDARY).map_err(|_| IndexError {
what: "index",
invalid: value,
invalid: child_no,
start: HARDENED_INDEX_BOUNDARY,
end: u32::MAX,
})
Expand Down Expand Up @@ -448,7 +451,7 @@ impl DerivationIndex {
pub fn from_index(value: u32) -> Self {
match value {
0..=0x0FFFFFFF => NormalIndex(value).into(),
_ => HardenedIndex(value).into(),
_ => HardenedIndex(value - HARDENED_INDEX_BOUNDARY).into(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ where IndexParseError: From<<I as FromStr>::Err>
{
type Err = DerivationParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with('/') {
return Err(DerivationParseError::InvalidFormat(s.to_owned()));
fn from_str(mut s: &str) -> Result<Self, Self::Err> {
if s.starts_with('/') {
s = &s[1..];
}
let inner = s[1..]
let inner = s
.split('/')
.map(I::from_str)
.collect::<Result<Vec<_>, I::Err>>()
Expand Down
18 changes: 15 additions & 3 deletions std/src/xpub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,25 @@ impl FromStr for XpubDescriptor {
}
if !d.origin.derivation.is_empty() {
let network = if d.xpub.testnet { HardenedIndex::ONE } else { HardenedIndex::ZERO };
if d.origin.derivation.last() != Some(&network.into()) {
return Err(XpubParseError::DepthMismatch);
if d.origin.derivation.get(1) != Some(&network.into()) {
return Err(XpubParseError::NetworkMismatch);
}
if d.origin.derivation.last() != Some(&d.xpub.meta.child_number) {
return Err(XpubParseError::DepthMismatch);
return Err(XpubParseError::ParentMismatch);
}
}
Ok(d)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn display_from_str() {
let s = "[643a7adc/86h/1h/0h]tpubDCNiWHaiSkgnQjuhsg9kjwaUzaxQjUcmhagvYzqQ3TYJTgFGJstVaqnu4yhtFktBhCVFmBNLQ5sN53qKzZbMksm3XEyGJsEhQPfVZdWmTE2";
let xpub = XpubDescriptor::from_str(s).unwrap();
assert_eq!(s, xpub.to_string());
}
}

0 comments on commit d0e0e94

Please sign in to comment.