BP-WG / bp-std

Modern & lightweight implementation of bitcoin standards without rust-bitcoin/miniscript dependencies
Apache License 2.0
16 stars 17 forks source link

bug: DerivationPath from_str error with path = "86'/1'/0'" #14

Closed shuimuliang closed 7 months ago

shuimuliang commented 8 months ago

Cargo.toml

[dependencies]
bp-derive = "0.11.0-beta.3"

[patch.crates-io]
bp-derive = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
use core::str::FromStr;
use derive::DerivationPath;
use derive::DerivationParseError;
use derive::HardenedIndex;

fn main() {
    let path_h = "86h/1h/0h";
    let derivation: Result<DerivationPath<HardenedIndex>, DerivationParseError> =
        DerivationPath::from_str(path_h);
    assert!(derivation.is_ok());

    let path_apostrophe = "86'/1'/0'";
    let derivation: Result<DerivationPath<HardenedIndex>, DerivationParseError> =
        DerivationPath::from_str(path_apostrophe);
    assert!(derivation.is_err());
    dbg!(derivation.err());

    /*
    derivation.err() = Some(
        InvalidIndex(
            "86'/1'/0'",
            HardenedRequired(
                "86'",
            ),
        ),
    )
    */
}
shuimuliang commented 8 months ago

Result<_, Err> location: bp-std/derive/src/path.rs ln175

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

    fn from_str(mut s: &str) -> Result<Self, Self::Err> {
        if s.starts_with('/') {
            s = &s[1..];
        }
        let inner = s
            .split('/')
            .map(I::from_str)
            .collect::<Result<Vec<_>, I::Err>>()
            .map_err(|err| DerivationParseError::InvalidIndex(s.to_owned(), err.into()))?;
        if inner.is_empty() {
            return Err(DerivationParseError::InvalidFormat(s.to_owned()));
        }
        Ok(Self(inner))
    }
}