Skip to content

Commit

Permalink
Allow canonical BIP32 string in FromStr (#354)
Browse files Browse the repository at this point in the history
* FromStr parsing of DerivationPaths and friends now use 'lenient' parsing - accepting CANONICAL BIP32 string repr.

* polish

* introduce concept of CAP43

* fix

* rename tests

* tests and comments

* fix Kotlin

* Release 1.1.119

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Generated by cargo-workspaces

---------

Co-authored-by: Matias Bzurovski <[email protected]>
  • Loading branch information
CyonAlexRDX and matiasbzurovski authored Jan 23, 2025
1 parent 4653813 commit 22a4d57
Show file tree
Hide file tree
Showing 94 changed files with 514 additions and 371 deletions.
168 changes: 84 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ extension DerivationPath {
derivationPathToHdPath(path: self)
}

/// Returns a CAP 43 String representation of this `DerivationPath`.
/// Useful when we need to log or debug, but should **never** be used when communicating with external APIs.
public func toString() -> String {
derivationPathToString(path: self)
}

/// Returns a BIP 32 String representation of this `DerivationPath`.
/// Needed to communicate with external APIs such as Arculus or Ledger.
public func toBip32String() -> String {
derivationPathToBip32String(path: self)
}

/// Attempts to build a DerivationPath from a CAP 43 or BIP 32 representation.
/// The initializer is lenient, so it will attempt with the latter if the first fails
public init(string: String) throws {
self = try newDerivationPathFromString(string: string)
}
Expand Down
20 changes: 20 additions & 0 deletions apple/Tests/TestCases/Crypto/DerivationPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ final class DerivationPathTests: HDPathProtocolTest<DerivationPath> {
"m/44H/1022H/2H/618H/1460H/42H"
)
}

func test_to_bip32() throws {
// Build from CAP43 syntax
var sut = try SUT(string: "m/44H/1022H/1H/525H/1460H/0S")
XCTAssertEqual(sut.toBip32String(), "m/44H/1022H/1H/525H/1460H/1073741824H")

// Build from BIP32 syntax
sut = try SUT(string: "m/44H/1022H/1H/525H/1460H/2H")
XCTAssertEqual(sut.toBip32String(), "m/44H/1022H/1H/525H/1460H/2H")
}

func test_to_string() throws {
// Build from CAP43 syntax
var sut = try SUT(string: "m/44H/1022H/1H/525H/1460H/0S")
XCTAssertEqual(sut.toString(), "m/44H/1022H/1H/525H/1460H/0S")

// Build from BIP32 syntax
sut = try SUT(string: "m/44H/1022H/1H/525H/1460H/2H")
XCTAssertEqual(sut.toString(), "m/44H/1022H/1H/525H/1460H/2H")
}
}
2 changes: 1 addition & 1 deletion crates/app/home-cards/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "home-cards"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/app/key-derivation-traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "key-derivation-traits"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/app/radix-connect-models/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "radix-connect-models"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/app/radix-connect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "radix-connect"
version = "1.1.118"
version = "1.1.119"
edition = "2021"


Expand Down
2 changes: 1 addition & 1 deletion crates/app/security-center/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "security-center"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/app/signing-traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "signing-traits"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/app/signing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "signing"
version = "1.1.118"
version = "1.1.119"
edition = "2021"


Expand Down
2 changes: 1 addition & 1 deletion crates/common/build-info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "build-info"
version = "1.1.118"
version = "1.1.119"
edition = "2021"
build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion crates/common/bytes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bytes"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/entity-foundation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "entity-foundation"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/host-info/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "host-info"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/identified-vec-of/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "identified-vec-of"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metadata"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/network/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "network"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/numeric/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "numeric"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/common/short-string/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "short-string"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/assert-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assert-json"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/collections/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-collections"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "error"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/has-sample-values/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "has-sample-values"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/misc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-misc"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/prelude/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prelude"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/time-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "time-utils"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/core/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-utils"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/addresses/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "addresses"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/cap26-models/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cap26-models"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/ecc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ecc"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/encryption/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "encryption"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/hash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hash"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/crypto/hd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hierarchical-deterministic"
version = "1.1.118"
version = "1.1.119"
edition = "2021"

[dependencies]
Expand Down
46 changes: 29 additions & 17 deletions crates/crypto/hd/src/bip32/hd_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::prelude::*;
DeserializeFromStr,
SerializeDisplay,
)]
#[display("{}", self.to_bip32_string())]
#[debug("{}", self.to_bip32_string_debug())]
#[display("{}", self.to_cap43_string())]
#[debug("{}", self.to_cap43_string_debug())]
pub struct HDPath {
pub components: Vec<HDPathComponent>,
}
Expand All @@ -29,8 +29,8 @@ impl HDPath {
}
}

impl FromBIP32Str for HDPath {
fn from_bip32_string(s: impl AsRef<str>) -> Result<Self> {
impl FromCAP43String for HDPath {
fn from_cap43_string(s: impl AsRef<str>) -> Result<Self> {
let s = s.as_ref();
let mut s = s;
if s.starts_with(&format!("m{}", Self::SEPARATOR)) {
Expand All @@ -45,7 +45,7 @@ impl FromBIP32Str for HDPath {
let components = s
.split(Self::SEPARATOR)
.filter(|s| !s.is_empty())
.map(HDPathComponent::from_bip32_string)
.map(HDPathComponent::from_cap43_string)
.collect::<Result<Vec<_>>>()?;
Ok(Self::new(components))
}
Expand All @@ -66,7 +66,7 @@ impl HDPath {
path.into_iter().join(Self::SEPARATOR)
}

pub fn to_bip32_string_with(
pub fn to_cap43_string_with(
&self,
include_head: bool,
canonicalize_entity_index: bool,
Expand All @@ -85,38 +85,38 @@ impl HDPath {
})
}

pub fn to_bip32_string_debug_with(&self, include_head: bool) -> String {
pub fn to_cap43_string_debug_with(&self, include_head: bool) -> String {
self.to_string_map_with(include_head, |(_, c)| format!("{:?}", c))
}

/// This method returns the canonical bip32 representation of the path.
/// String representation of the path using BIP32 notation.
/// In sargon, paths in the securified space are printed with the `S` notation after the index,
/// for readability purposes.
///
/// The notation `{i}S` means `{i + 2^30}H`, and since `H` means `+ 2^31` we can
/// verbosely express `{i}S` as `{i + 2^30 + 2^31} (without the H)
///
/// Such paths need to be canonicalized in bip32 notation meaning that
/// Such paths need to be on BIP32 notation meaning that
/// an index of `"{i}S"` => `"{i + 2^30}H"` when communication with other external APIs,
/// e.g. using Ledger hardware wallet or Arculus.
pub fn to_canonical_bip32_string(&self) -> String {
self.to_bip32_string_with(true, true)
pub fn to_bip32_string(&self) -> String {
self.to_cap43_string_with(true, true)
}
}
impl ToBIP32Str for HDPath {
fn to_bip32_string(&self) -> String {
self.to_bip32_string_with(true, false)
impl ToCAP43String for HDPath {
fn to_cap43_string(&self) -> String {
self.to_cap43_string_with(true, false)
}
fn to_bip32_string_debug(&self) -> String {
self.to_bip32_string_debug_with(true)
fn to_cap43_string_debug(&self) -> String {
self.to_cap43_string_debug_with(true)
}
}

impl FromStr for HDPath {
type Err = CommonError;

fn from_str(s: &str) -> Result<Self> {
Self::from_bip32_string(s)
Self::from_cap43_string(s)
}
}

Expand Down Expand Up @@ -235,4 +235,16 @@ mod tests {
assert_json_value_fails::<HDPath>(json!("m/44X"));
assert_json_value_fails::<HDPath>(json!("super invalid path"));
}

#[test]
fn from_bip32_str() {
let canonical = "m/44H/1022H/1H/525H/1460H/1073741824H";
let sut = SUT::from_str(canonical).unwrap();
assert_eq!(
DerivationPath::from(AccountPath::try_from(sut.clone()).unwrap())
.to_bip32_string(),
canonical
);
assert_eq!(sut.to_cap43_string(), "m/44H/1022H/1H/525H/1460H/0S");
}
}
Loading

0 comments on commit 22a4d57

Please sign in to comment.