2024-01-07 04:18:52 +00:00
|
|
|
#![allow(clippy::implicit_clone)]
|
|
|
|
|
2023-09-07 18:24:07 +00:00
|
|
|
use crate::{request::*, *};
|
2023-09-01 04:10:56 +00:00
|
|
|
use hex_literal::hex;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
// Pulled from: https://github.com/satoshilabs/slips/blob/master/slip-0010.md
|
|
|
|
|
2023-09-25 22:05:20 +00:00
|
|
|
use keyfork_slip10_test_data::{test_data, Test};
|
|
|
|
|
2023-09-06 15:21:47 +00:00
|
|
|
#[cfg(feature = "secp256k1")]
|
2023-09-01 04:10:56 +00:00
|
|
|
#[test]
|
2023-09-06 15:21:47 +00:00
|
|
|
fn secp256k1() {
|
|
|
|
use k256::SecretKey;
|
|
|
|
|
2023-09-25 22:05:20 +00:00
|
|
|
let tests = test_data()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&"secp256k1".to_string())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for per_seed in tests {
|
|
|
|
let seed = &per_seed.seed;
|
|
|
|
for test in &per_seed.tests {
|
|
|
|
let chain = DerivationPath::from_str(test.chain).unwrap();
|
|
|
|
let Test {
|
|
|
|
chain_code,
|
|
|
|
private_key,
|
|
|
|
public_key,
|
|
|
|
..
|
|
|
|
} = test;
|
|
|
|
|
|
|
|
// Tests for ExtendedPrivateKey
|
2024-02-12 05:02:59 +00:00
|
|
|
let varlen_seed = VariableLengthSeed::new(&seed);
|
|
|
|
let xkey = ExtendedPrivateKey::<SecretKey>::new(varlen_seed);
|
2023-09-25 22:05:20 +00:00
|
|
|
let derived_key = xkey.derive_path(&chain).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
derived_key.chain_code().as_slice(),
|
|
|
|
chain_code,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
PrivateKey::to_bytes(derived_key.private_key()).as_slice(),
|
|
|
|
private_key,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
PublicKey::to_bytes(&derived_key.public_key()).as_slice(),
|
|
|
|
public_key,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests for DerivationRequest
|
|
|
|
let request = DerivationRequest::new(DerivationAlgorithm::Secp256k1, &chain);
|
2024-02-12 05:02:59 +00:00
|
|
|
let response = request.derive_with_master_seed(&seed).unwrap();
|
2024-02-12 01:35:26 +00:00
|
|
|
assert_eq!(&response.data, private_key.as_slice(), "test: {chain}");
|
2023-09-25 22:05:20 +00:00
|
|
|
}
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 15:21:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
#[test]
|
|
|
|
fn ed25519() {
|
|
|
|
use ed25519_dalek::SigningKey;
|
|
|
|
|
2023-11-05 06:29:10 +00:00
|
|
|
let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap();
|
2023-09-25 22:05:20 +00:00
|
|
|
|
|
|
|
for per_seed in tests {
|
|
|
|
let seed = &per_seed.seed;
|
|
|
|
for test in &per_seed.tests {
|
|
|
|
let chain = DerivationPath::from_str(test.chain).unwrap();
|
|
|
|
let Test {
|
|
|
|
chain_code,
|
|
|
|
private_key,
|
|
|
|
public_key,
|
|
|
|
..
|
|
|
|
} = test;
|
|
|
|
|
|
|
|
// Tests for ExtendedPrivateKey
|
2024-02-12 05:02:59 +00:00
|
|
|
let varlen_seed = VariableLengthSeed::new(&seed);
|
|
|
|
let xkey = ExtendedPrivateKey::<SigningKey>::new(varlen_seed);
|
2023-09-25 22:05:20 +00:00
|
|
|
let derived_key = xkey.derive_path(&chain).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
derived_key.chain_code().as_slice(),
|
|
|
|
chain_code,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
PrivateKey::to_bytes(derived_key.private_key()).as_slice(),
|
|
|
|
private_key,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
PublicKey::to_bytes(&derived_key.public_key()).as_slice(),
|
|
|
|
public_key,
|
|
|
|
"test: {chain}",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Tests for DerivationRequest
|
|
|
|
let request = DerivationRequest::new(DerivationAlgorithm::Ed25519, &chain);
|
2024-02-12 05:02:59 +00:00
|
|
|
let response = request.derive_with_master_seed(&seed).unwrap();
|
2024-02-12 01:35:26 +00:00
|
|
|
assert_eq!(&response.data, private_key.as_slice(), "test: {chain}");
|
2023-09-25 22:05:20 +00:00
|
|
|
}
|
2023-09-06 15:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 06:36:05 +00:00
|
|
|
|
2023-09-25 22:11:29 +00:00
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn panics_with_unhardened_derivation() {
|
|
|
|
use ed25519_dalek::SigningKey;
|
|
|
|
|
|
|
|
let seed = hex!("000102030405060708090a0b0c0d0e0f");
|
2024-02-12 01:35:26 +00:00
|
|
|
let xkey = ExtendedPrivateKey::<SigningKey>::new(seed);
|
2023-11-05 06:29:10 +00:00
|
|
|
xkey.derive_path(&DerivationPath::from_str("m/0").unwrap())
|
|
|
|
.unwrap();
|
2023-09-25 22:11:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 06:36:05 +00:00
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn panics_at_depth() {
|
|
|
|
use ed25519_dalek::SigningKey;
|
|
|
|
|
|
|
|
let seed = hex!("000102030405060708090a0b0c0d0e0f");
|
2024-02-12 01:35:26 +00:00
|
|
|
let mut xkey = ExtendedPrivateKey::<SigningKey>::new(seed);
|
2024-01-07 04:18:52 +00:00
|
|
|
for i in 0..=u32::from(u8::MAX) {
|
2023-09-21 22:30:48 +00:00
|
|
|
xkey = xkey
|
|
|
|
.derive_child(&DerivationIndex::new(i, true).unwrap())
|
|
|
|
.unwrap();
|
2023-09-12 06:36:05 +00:00
|
|
|
}
|
|
|
|
}
|