Skip to content

Commit bd5ba80

Browse files
authored
Merge pull request #31 from varovainen/va-2025-01-30-fix-derivations
fix: numerical derivations
2 parents a2d0db1 + ec31682 commit bd5ba80

File tree

5 files changed

+73
-24
lines changed

5 files changed

+73
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ zeroize = {version = "1.7.0", features = ["derive"]}
3232
[dev-dependencies]
3333
hex = "0.4.3"
3434
mnemonic-external = "0.1.0"
35-
sp-core = "34.0.0"
35+
sp-core = "35.0.0"
3636

3737
[features]
3838
default = ["ed25519", "ecdsa", "sr25519", "std"]

src/common.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,23 @@ pub fn cut_path(path_and_pwd: &str) -> Option<FullDerivation<'_>> {
140140
.captures_iter(path.as_str())
141141
.map(|caps| {
142142
let derivation = caps.name("derivation").expect("");
143-
if derivation.as_str().starts_with('/') {
144-
DeriveJunction::hard(&derivation.as_str()[1..])
143+
let (is_hard, derivation_body) = {
144+
if derivation.as_str().starts_with('/') {
145+
(true, &derivation.as_str()[1..])
146+
} else {
147+
(false, derivation.as_str())
148+
}
149+
};
150+
if let Ok(number) = str::parse::<u64>(derivation_body) {
151+
if is_hard {
152+
DeriveJunction::hard(number)
153+
} else {
154+
DeriveJunction::soft(number)
155+
}
156+
} else if is_hard {
157+
DeriveJunction::hard(derivation_body)
145158
} else {
146-
DeriveJunction::soft(derivation.as_str())
159+
DeriveJunction::soft(derivation_body)
147160
}
148161
})
149162
.collect()

src/ecdsa.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ mod tests {
124124
use crate::common::{cut_path, ALICE_WORDS};
125125
use crate::ecdsa::{Pair as EcdsaPair, Public as EcdsaPublic, Signature as EcdsaSignature};
126126

127-
#[test]
128-
fn identical_ecdsa() {
129-
let derivation = "//hard//alicealicealicealicealicealicealicealice";
130-
let password = "trickytrick";
131-
127+
fn identical_ecdsa(derivation: &str, password: &str) {
132128
// phrase and full derivation, for `sp-core` procedure
133129
let phrase_with_derivations = format!("{ALICE_WORDS}{derivation}");
134130

@@ -178,4 +174,17 @@ mod tests {
178174
let public_import = EcdsaPublic(public_from_core);
179175
assert!(public_import.verify(msg, &signature_import));
180176
}
177+
178+
#[test]
179+
fn test_identical_ecdsa_1() {
180+
identical_ecdsa(
181+
"//hard//alicealicealicealicealicealicealicealice",
182+
"trickytrick",
183+
)
184+
}
185+
186+
#[test]
187+
fn test_identical_ecdsa_2() {
188+
identical_ecdsa("//1", "pwd")
189+
}
181190
}

src/ed25519.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ mod tests {
9393
Pair as Ed25519Pair, Public as Ed25519Public, Signature as Ed25519Signature,
9494
};
9595

96-
#[test]
97-
fn identical_ed25519() {
98-
let derivation = "//hard//alicealicealicealicealicealicealicealice";
99-
let password = "trickytrick";
100-
96+
fn identical_ed25519(derivation: &str, password: &str) {
10197
// phrase and full derivation, for `sp-core` procedure
10298
let phrase_with_derivations = format!("{ALICE_WORDS}{derivation}");
10399

@@ -148,4 +144,17 @@ mod tests {
148144
let public_import = Ed25519Public(public_from_core);
149145
assert!(public_import.verify(msg, &signature_import));
150146
}
147+
148+
#[test]
149+
fn test_identical_ed25519_1() {
150+
identical_ed25519(
151+
"//hard//alicealicealicealicealicealicealicealice",
152+
"trickytrick",
153+
)
154+
}
155+
156+
#[test]
157+
fn test_identical_ed25519_2() {
158+
identical_ed25519("//1", "pwd")
159+
}
151160
}

src/sr25519.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,7 @@ mod tests {
147147
Pair as Sr25529Pair, Public as Sr25519Public, Signature as Sr25519Signature,
148148
};
149149

150-
#[test]
151-
fn identical_sr25519() {
152-
let derivation = "//hard/soft//alicealicealicealicealicealicealicealice";
153-
let password = "trickytrick";
154-
150+
fn identical_sr25519(derivation: &str, password: &str) {
155151
// phrase and full derivation, for `sp-core` procedure
156152
let phrase_with_derivations = format!("{ALICE_WORDS}{derivation}");
157153

@@ -223,11 +219,7 @@ mod tests {
223219

224220
impl CryptoRng for DummyRng {}
225221

226-
#[test]
227-
fn identical_sr25519_external_rng() {
228-
let derivation = "//hard/soft//alicealicealicealicealicealicealicealice";
229-
let password = "trickytrick";
230-
222+
fn identical_sr25519_external_rng(derivation: &str, password: &str) {
231223
// phrase and full derivation, for `sp-core` procedure
232224
let phrase_with_derivations = format!("{ALICE_WORDS}{derivation}");
233225

@@ -276,4 +268,30 @@ mod tests {
276268
&public_import_into_core
277269
));
278270
}
271+
272+
#[test]
273+
fn test_identical_sr25519_1() {
274+
identical_sr25519(
275+
"//hard/soft//alicealicealicealicealicealicealicealice",
276+
"trickytrick",
277+
)
278+
}
279+
280+
#[test]
281+
fn test_identical_sr25519_2() {
282+
identical_sr25519("//1", "pwd")
283+
}
284+
285+
#[test]
286+
fn test_identical_sr25519_1_external() {
287+
identical_sr25519_external_rng(
288+
"//hard/soft//alicealicealicealicealicealicealicealice",
289+
"trickytrick",
290+
)
291+
}
292+
293+
#[test]
294+
fn test_identical_sr25519_2_external() {
295+
identical_sr25519_external_rng("//1", "pwd")
296+
}
279297
}

0 commit comments

Comments
 (0)