129 lines
6.4 KiB
Rust
129 lines
6.4 KiB
Rust
use crate::{request::*, *};
|
|
use hex_literal::hex;
|
|
use std::str::FromStr;
|
|
|
|
// Pulled from: https://github.com/satoshilabs/slips/blob/master/slip-0010.md
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
#[test]
|
|
fn secp256k1() {
|
|
use k256::SecretKey;
|
|
|
|
// seed, chain, chain code, private, public
|
|
let tests = [(
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m").unwrap(),
|
|
hex!("873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508"),
|
|
hex!("e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35"),
|
|
hex!("0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"),
|
|
), (
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'").unwrap(),
|
|
hex!("47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141"),
|
|
hex!("edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea"),
|
|
hex!("035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"),
|
|
), (
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'/1").unwrap(),
|
|
hex!("2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19"),
|
|
hex!("3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368"),
|
|
hex!("03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"),
|
|
), (
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'/1/2'").unwrap(),
|
|
hex!("04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f"),
|
|
hex!("cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca"),
|
|
hex!("0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"),
|
|
), (
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'/1/2'/2").unwrap(),
|
|
hex!("cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd"),
|
|
hex!("0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4"),
|
|
hex!("02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"),
|
|
), (
|
|
&hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")[..],
|
|
DerivationPath::from_str("m").unwrap(),
|
|
hex!("60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689"),
|
|
hex!("4b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e"),
|
|
hex!("03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7"),
|
|
), (
|
|
&hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")[..],
|
|
DerivationPath::from_str("m/0").unwrap(),
|
|
hex!("f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c"),
|
|
hex!("abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e"),
|
|
hex!("02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"),
|
|
), (
|
|
&hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")[..],
|
|
DerivationPath::from_str("m/0/2147483647'/1/2147483646'/2").unwrap(),
|
|
hex!("9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271"),
|
|
hex!("bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23"),
|
|
hex!("024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"),
|
|
)];
|
|
for (seed, chain, chain_code, private_key, public_key) in tests {
|
|
let xkey = ExtendedPrivateKey::<SecretKey>::new(seed).unwrap();
|
|
let derived_key = xkey.derive_path(&chain).unwrap();
|
|
assert_eq!(derived_key.chain_code(), chain_code);
|
|
assert_eq!(derived_key.private_key().to_bytes().as_slice(), private_key);
|
|
assert_eq!(derived_key.public_key().to_bytes(), public_key);
|
|
let request = DerivationRequest::new(DerivationAlgorithm::Secp256k1, chain);
|
|
let response = request.derive_with_master_seed(seed.to_vec()).unwrap();
|
|
assert_eq!(response.data, private_key);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
#[test]
|
|
fn ed25519() {
|
|
use ed25519_dalek::SigningKey;
|
|
|
|
// seed, chain, chain code, private, public
|
|
let tests = [
|
|
(
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m").unwrap(),
|
|
hex!("90046a93de5380a72b5e45010748567d5ea02bbf6522f979e05c0d8d8ca9fffb"),
|
|
hex!("2b4be7f19ee27bbf30c667b642d5f4aa69fd169872f8fc3059c08ebae2eb19e7"),
|
|
hex!("00a4b2856bfec510abab89753fac1ac0e1112364e7d250545963f135f2a33188ed"),
|
|
),
|
|
(
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'").unwrap(),
|
|
hex!("8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69"),
|
|
hex!("68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3"),
|
|
hex!("008c8a13df77a28f3445213a0f432fde644acaa215fc72dcdf300d5efaa85d350c"),
|
|
),
|
|
(
|
|
&hex!("000102030405060708090a0b0c0d0e0f")[..],
|
|
DerivationPath::from_str("m/0'/1'/2'/2'/1000000000'").unwrap(),
|
|
hex!("68789923a0cac2cd5a29172a475fe9e0fb14cd6adb5ad98a3fa70333e7afa230"),
|
|
hex!("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793"),
|
|
hex!("003c24da049451555d51a7014a37337aa4e12d41e485abccfa46b47dfb2af54b7a"),
|
|
),
|
|
];
|
|
for (seed, chain, chain_code, private_key, public_key) in tests {
|
|
let xkey = ExtendedPrivateKey::<SigningKey>::new(seed).unwrap();
|
|
let derived_key = xkey.derive_path(&chain).unwrap();
|
|
assert_eq!(derived_key.chain_code(), chain_code);
|
|
assert_eq!(PrivateKey::to_bytes(derived_key.private_key()), private_key);
|
|
assert_eq!(PublicKey::to_bytes(&derived_key.public_key()), public_key);
|
|
let request = DerivationRequest::new(DerivationAlgorithm::Ed25519, chain);
|
|
let response = request.derive_with_master_seed(seed.to_vec()).unwrap();
|
|
assert_eq!(response.data, private_key);
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
#[test]
|
|
#[should_panic]
|
|
fn panics_at_depth() {
|
|
use ed25519_dalek::SigningKey;
|
|
|
|
let seed = hex!("000102030405060708090a0b0c0d0e0f");
|
|
let mut xkey = ExtendedPrivateKey::<SigningKey>::new(seed).unwrap();
|
|
for i in 0..u32::from(u8::MAX) + 1 {
|
|
xkey = xkey
|
|
.derive_child(&DerivationIndex::new(i, true).unwrap())
|
|
.unwrap();
|
|
}
|
|
}
|