cargo fmt

This commit is contained in:
Ryan Heywood 2023-11-05 01:29:10 -05:00
parent c206800ad2
commit 4b4b85931f
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
13 changed files with 138 additions and 66 deletions

View File

@ -1,4 +1,4 @@
use std::{env, str::FromStr, process::ExitCode}; use std::{env, process::ExitCode, str::FromStr};
use keyfork_derive_util::{ use keyfork_derive_util::{
request::{DerivationAlgorithm, DerivationRequest}, request::{DerivationAlgorithm, DerivationRequest},

View File

@ -1,4 +1,4 @@
use keyfork_derive_util::{DerivationPath, DerivationIndex}; use keyfork_derive_util::{DerivationIndex, DerivationPath};
pub static OPENPGP: DerivationIndex = DerivationIndex::new_unchecked(7366512, true); pub static OPENPGP: DerivationIndex = DerivationIndex::new_unchecked(7366512, true);

View File

@ -1,4 +1,4 @@
use crate::{DerivationIndex, DerivationPath, PrivateKey, PublicKey, ExtendedPublicKey}; use crate::{DerivationIndex, DerivationPath, ExtendedPublicKey, PrivateKey, PublicKey};
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -122,11 +122,14 @@ mod tests {
Ok(()) Ok(())
} }
#[test] #[test]
fn add_vec() -> Result<(), Box<dyn std::error::Error>> { fn add_vec() -> Result<(), Box<dyn std::error::Error>> {
let path = DerivationPath::from_str("m")?; let path = DerivationPath::from_str("m")?;
let other_path = [DerivationIndex::new(72, true)?, DerivationIndex::new(47, false)?, DerivationIndex::new((i32::MAX) as u32, false)?]; let other_path = [
DerivationIndex::new(72, true)?,
DerivationIndex::new(47, false)?,
DerivationIndex::new((i32::MAX) as u32, false)?,
];
let path = path + &other_path[..]; let path = path + &other_path[..];
assert_eq!(path, DerivationPath::from_str("m/72'/47/2147483647")?); assert_eq!(path, DerivationPath::from_str("m/72'/47/2147483647")?);

View File

@ -35,7 +35,9 @@ pub trait PublicKey: Sized {
let hash = Sha256::new().chain_update(self.to_bytes()).finalize(); let hash = Sha256::new().chain_update(self.to_bytes()).finalize();
let hash = Ripemd160::new().chain_update(hash).finalize(); let hash = Ripemd160::new().chain_update(hash).finalize();
// Note: Safety assured by type returned from Ripemd160 // Note: Safety assured by type returned from Ripemd160
hash[..4].try_into().expect("Ripemd160 returned too little data") hash[..4]
.try_into()
.expect("Ripemd160 returned too little data")
} }
} }

View File

@ -59,10 +59,7 @@ fn secp256k1() {
fn ed25519() { fn ed25519() {
use ed25519_dalek::SigningKey; use ed25519_dalek::SigningKey;
let tests = test_data() let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap();
.unwrap()
.remove(&"ed25519".to_string())
.unwrap();
for per_seed in tests { for per_seed in tests {
let seed = &per_seed.seed; let seed = &per_seed.seed;
@ -110,7 +107,8 @@ fn panics_with_unhardened_derivation() {
let seed = hex!("000102030405060708090a0b0c0d0e0f"); let seed = hex!("000102030405060708090a0b0c0d0e0f");
let xkey = ExtendedPrivateKey::<SigningKey>::new(seed).unwrap(); let xkey = ExtendedPrivateKey::<SigningKey>::new(seed).unwrap();
xkey.derive_path(&DerivationPath::from_str("m/0").unwrap()).unwrap(); xkey.derive_path(&DerivationPath::from_str("m/0").unwrap())
.unwrap();
} }
#[cfg(feature = "ed25519")] #[cfg(feature = "ed25519")]

View File

@ -1,8 +1,8 @@
use std::{collections::HashMap, str::FromStr, sync::Arc, error::Error, fmt::Display}; use std::{collections::HashMap, error::Error, fmt::Display, str::FromStr, sync::Arc};
use sha2::{Digest, Sha256, Sha512};
use pbkdf2::pbkdf2;
use hmac::Hmac; use hmac::Hmac;
use pbkdf2::pbkdf2;
use sha2::{Digest, Sha256, Sha512};
/// The error type representing a failure to create a [`Mnemonic`]. These errors only occur during /// The error type representing a failure to create a [`Mnemonic`]. These errors only occur during
/// [`Mnemonic`] creation. /// [`Mnemonic`] creation.
@ -30,7 +30,7 @@ impl Display for MnemonicGenerationError {
} }
MnemonicGenerationError::InvalidPbkdf2Length => { MnemonicGenerationError::InvalidPbkdf2Length => {
f.write_str("Invalid length from PBKDF2") f.write_str("Invalid length from PBKDF2")
}, }
} }
} }
} }
@ -222,7 +222,10 @@ impl Mnemonic {
.collect() .collect()
} }
pub fn seed<'a>(&self, passphrase: impl Into<Option<&'a str>>) -> Result<Vec<u8>, MnemonicGenerationError> { pub fn seed<'a>(
&self,
passphrase: impl Into<Option<&'a str>>,
) -> Result<Vec<u8>, MnemonicGenerationError> {
let passphrase = passphrase.into(); let passphrase = passphrase.into();
let mut seed = [0u8; 64]; let mut seed = [0u8; 64];
@ -293,8 +296,14 @@ mod tests {
let their_mnemonic = bip39::Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap(); let their_mnemonic = bip39::Mnemonic::from_entropy(&entropy[..256 / 8]).unwrap();
assert_eq!(my_mnemonic.to_string(), their_mnemonic.to_string()); assert_eq!(my_mnemonic.to_string(), their_mnemonic.to_string());
assert_eq!(my_mnemonic.seed(None).unwrap(), their_mnemonic.to_seed("")); assert_eq!(my_mnemonic.seed(None).unwrap(), their_mnemonic.to_seed(""));
assert_eq!(my_mnemonic.seed("testing").unwrap(), their_mnemonic.to_seed("testing")); assert_eq!(
assert_ne!(my_mnemonic.seed("test1").unwrap(), their_mnemonic.to_seed("test2")); my_mnemonic.seed("testing").unwrap(),
their_mnemonic.to_seed("testing")
);
assert_ne!(
my_mnemonic.seed("test1").unwrap(),
their_mnemonic.to_seed("test2")
);
} }
#[test] #[test]

View File

@ -390,7 +390,7 @@ impl<'a> MessageDialog<'a> {
binary, binary,
title: None, title: None,
ok: None, ok: None,
timeout: None timeout: None,
} }
} }

View File

@ -219,37 +219,67 @@ pub fn test_data() -> Result<HashMap<String, Vec<TestData>>, Box<dyn std::error:
Test { Test {
chain: "m/0'", chain: "m/0'",
fingerprint: hex::decode("ddebc675")?, fingerprint: hex::decode("ddebc675")?,
chain_code: hex::decode("8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69")?, chain_code: hex::decode(
private_key: hex::decode("68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3")?, "8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69",
public_key: hex::decode("008c8a13df77a28f3445213a0f432fde644acaa215fc72dcdf300d5efaa85d350c")?, )?,
private_key: hex::decode(
"68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3",
)?,
public_key: hex::decode(
"008c8a13df77a28f3445213a0f432fde644acaa215fc72dcdf300d5efaa85d350c",
)?,
}, },
Test { Test {
chain: "m/0'/1'", chain: "m/0'/1'",
fingerprint: hex::decode("13dab143")?, fingerprint: hex::decode("13dab143")?,
chain_code: hex::decode("a320425f77d1b5c2505a6b1b27382b37368ee640e3557c315416801243552f14")?, chain_code: hex::decode(
private_key: hex::decode("b1d0bad404bf35da785a64ca1ac54b2617211d2777696fbffaf208f746ae84f2")?, "a320425f77d1b5c2505a6b1b27382b37368ee640e3557c315416801243552f14",
public_key: hex::decode("001932a5270f335bed617d5b935c80aedb1a35bd9fc1e31acafd5372c30f5c1187")?, )?,
private_key: hex::decode(
"b1d0bad404bf35da785a64ca1ac54b2617211d2777696fbffaf208f746ae84f2",
)?,
public_key: hex::decode(
"001932a5270f335bed617d5b935c80aedb1a35bd9fc1e31acafd5372c30f5c1187",
)?,
}, },
Test { Test {
chain: "m/0'/1'/2'", chain: "m/0'/1'/2'",
fingerprint: hex::decode("ebe4cb29")?, fingerprint: hex::decode("ebe4cb29")?,
chain_code: hex::decode("2e69929e00b5ab250f49c3fb1c12f252de4fed2c1db88387094a0f8c4c9ccd6c")?, chain_code: hex::decode(
private_key: hex::decode("92a5b23c0b8a99e37d07df3fb9966917f5d06e02ddbd909c7e184371463e9fc9")?, "2e69929e00b5ab250f49c3fb1c12f252de4fed2c1db88387094a0f8c4c9ccd6c",
public_key: hex::decode("00ae98736566d30ed0e9d2f4486a64bc95740d89c7db33f52121f8ea8f76ff0fc1")?, )?,
private_key: hex::decode(
"92a5b23c0b8a99e37d07df3fb9966917f5d06e02ddbd909c7e184371463e9fc9",
)?,
public_key: hex::decode(
"00ae98736566d30ed0e9d2f4486a64bc95740d89c7db33f52121f8ea8f76ff0fc1",
)?,
}, },
Test { Test {
chain: "m/0'/1'/2'/2'", chain: "m/0'/1'/2'/2'",
fingerprint: hex::decode("316ec1c6")?, fingerprint: hex::decode("316ec1c6")?,
chain_code: hex::decode("8f6d87f93d750e0efccda017d662a1b31a266e4a6f5993b15f5c1f07f74dd5cc")?, chain_code: hex::decode(
private_key: hex::decode("30d1dc7e5fc04c31219ab25a27ae00b50f6fd66622f6e9c913253d6511d1e662")?, "8f6d87f93d750e0efccda017d662a1b31a266e4a6f5993b15f5c1f07f74dd5cc",
public_key: hex::decode("008abae2d66361c879b900d204ad2cc4984fa2aa344dd7ddc46007329ac76c429c")?, )?,
private_key: hex::decode(
"30d1dc7e5fc04c31219ab25a27ae00b50f6fd66622f6e9c913253d6511d1e662",
)?,
public_key: hex::decode(
"008abae2d66361c879b900d204ad2cc4984fa2aa344dd7ddc46007329ac76c429c",
)?,
}, },
Test { Test {
chain: "m/0'/1'/2'/2'/1000000000'", chain: "m/0'/1'/2'/2'/1000000000'",
fingerprint: hex::decode("d6322ccd")?, fingerprint: hex::decode("d6322ccd")?,
chain_code: hex::decode("68789923a0cac2cd5a29172a475fe9e0fb14cd6adb5ad98a3fa70333e7afa230")?, chain_code: hex::decode(
private_key: hex::decode("8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793")?, "68789923a0cac2cd5a29172a475fe9e0fb14cd6adb5ad98a3fa70333e7afa230",
public_key: hex::decode("003c24da049451555d51a7014a37337aa4e12d41e485abccfa46b47dfb2af54b7a")?, )?,
private_key: hex::decode(
"8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793",
)?,
public_key: hex::decode(
"003c24da049451555d51a7014a37337aa4e12d41e485abccfa46b47dfb2af54b7a",
)?,
}, },
], ],
}, },
@ -259,44 +289,80 @@ pub fn test_data() -> Result<HashMap<String, Vec<TestData>>, Box<dyn std::error:
Test { Test {
chain: "m", chain: "m",
fingerprint: hex::decode("00000000")?, fingerprint: hex::decode("00000000")?,
chain_code: hex::decode("ef70a74db9c3a5af931b5fe73ed8e1a53464133654fd55e7a66f8570b8e33c3b")?, chain_code: hex::decode(
private_key: hex::decode("171cb88b1b3c1db25add599712e36245d75bc65a1a5c9e18d76f9f2b1eab4012")?, "ef70a74db9c3a5af931b5fe73ed8e1a53464133654fd55e7a66f8570b8e33c3b",
public_key: hex::decode("008fe9693f8fa62a4305a140b9764c5ee01e455963744fe18204b4fb948249308a")?, )?,
private_key: hex::decode(
"171cb88b1b3c1db25add599712e36245d75bc65a1a5c9e18d76f9f2b1eab4012",
)?,
public_key: hex::decode(
"008fe9693f8fa62a4305a140b9764c5ee01e455963744fe18204b4fb948249308a",
)?,
}, },
Test { Test {
chain: "m/0'", chain: "m/0'",
fingerprint: hex::decode("31981b50")?, fingerprint: hex::decode("31981b50")?,
chain_code: hex::decode("0b78a3226f915c082bf118f83618a618ab6dec793752624cbeb622acb562862d")?, chain_code: hex::decode(
private_key: hex::decode("1559eb2bbec5790b0c65d8693e4d0875b1747f4970ae8b650486ed7470845635")?, "0b78a3226f915c082bf118f83618a618ab6dec793752624cbeb622acb562862d",
public_key: hex::decode("0086fab68dcb57aa196c77c5f264f215a112c22a912c10d123b0d03c3c28ef1037")?, )?,
private_key: hex::decode(
"1559eb2bbec5790b0c65d8693e4d0875b1747f4970ae8b650486ed7470845635",
)?,
public_key: hex::decode(
"0086fab68dcb57aa196c77c5f264f215a112c22a912c10d123b0d03c3c28ef1037",
)?,
}, },
Test { Test {
chain: "m/0'/2147483647'", chain: "m/0'/2147483647'",
fingerprint: hex::decode("1e9411b1")?, fingerprint: hex::decode("1e9411b1")?,
chain_code: hex::decode("138f0b2551bcafeca6ff2aa88ba8ed0ed8de070841f0c4ef0165df8181eaad7f")?, chain_code: hex::decode(
private_key: hex::decode("ea4f5bfe8694d8bb74b7b59404632fd5968b774ed545e810de9c32a4fb4192f4")?, "138f0b2551bcafeca6ff2aa88ba8ed0ed8de070841f0c4ef0165df8181eaad7f",
public_key: hex::decode("005ba3b9ac6e90e83effcd25ac4e58a1365a9e35a3d3ae5eb07b9e4d90bcf7506d")?, )?,
private_key: hex::decode(
"ea4f5bfe8694d8bb74b7b59404632fd5968b774ed545e810de9c32a4fb4192f4",
)?,
public_key: hex::decode(
"005ba3b9ac6e90e83effcd25ac4e58a1365a9e35a3d3ae5eb07b9e4d90bcf7506d",
)?,
}, },
Test { Test {
chain: "m/0'/2147483647'/1'", chain: "m/0'/2147483647'/1'",
fingerprint: hex::decode("fcadf38c")?, fingerprint: hex::decode("fcadf38c")?,
chain_code: hex::decode("73bd9fff1cfbde33a1b846c27085f711c0fe2d66fd32e139d3ebc28e5a4a6b90")?, chain_code: hex::decode(
private_key: hex::decode("3757c7577170179c7868353ada796c839135b3d30554bbb74a4b1e4a5a58505c")?, "73bd9fff1cfbde33a1b846c27085f711c0fe2d66fd32e139d3ebc28e5a4a6b90",
public_key: hex::decode("002e66aa57069c86cc18249aecf5cb5a9cebbfd6fadeab056254763874a9352b45")?, )?,
private_key: hex::decode(
"3757c7577170179c7868353ada796c839135b3d30554bbb74a4b1e4a5a58505c",
)?,
public_key: hex::decode(
"002e66aa57069c86cc18249aecf5cb5a9cebbfd6fadeab056254763874a9352b45",
)?,
}, },
Test { Test {
chain: "m/0'/2147483647'/1'/2147483646'", chain: "m/0'/2147483647'/1'/2147483646'",
fingerprint: hex::decode("aca70953")?, fingerprint: hex::decode("aca70953")?,
chain_code: hex::decode("0902fe8a29f9140480a00ef244bd183e8a13288e4412d8389d140aac1794825a")?, chain_code: hex::decode(
private_key: hex::decode("5837736c89570de861ebc173b1086da4f505d4adb387c6a1b1342d5e4ac9ec72")?, "0902fe8a29f9140480a00ef244bd183e8a13288e4412d8389d140aac1794825a",
public_key: hex::decode("00e33c0f7d81d843c572275f287498e8d408654fdf0d1e065b84e2e6f157aab09b")?, )?,
private_key: hex::decode(
"5837736c89570de861ebc173b1086da4f505d4adb387c6a1b1342d5e4ac9ec72",
)?,
public_key: hex::decode(
"00e33c0f7d81d843c572275f287498e8d408654fdf0d1e065b84e2e6f157aab09b",
)?,
}, },
Test { Test {
chain: "m/0'/2147483647'/1'/2147483646'/2'", chain: "m/0'/2147483647'/1'/2147483646'/2'",
fingerprint: hex::decode("422c654b")?, fingerprint: hex::decode("422c654b")?,
chain_code: hex::decode("5d70af781f3a37b829f0d060924d5e960bdc02e85423494afc0b1a41bbe196d4")?, chain_code: hex::decode(
private_key: hex::decode("551d333177df541ad876a60ea71f00447931c0a9da16f227c11ea080d7391b8d")?, "5d70af781f3a37b829f0d060924d5e960bdc02e85423494afc0b1a41bbe196d4",
public_key: hex::decode("0047150c75db263559a70d5778bf36abbab30fb061ad69f69ece61a72b0cfa4fc0")?, )?,
private_key: hex::decode(
"551d333177df541ad876a60ea71f00447931c0a9da16f227c11ea080d7391b8d",
)?,
public_key: hex::decode(
"0047150c75db263559a70d5778bf36abbab30fb061ad69f69ece61a72b0cfa4fc0",
)?,
}, },
], ],
}, },

View File

@ -1,5 +1,5 @@
use super::Keyfork; use super::Keyfork;
use clap::{Parser, Subcommand, ValueEnum, builder::PossibleValue}; use clap::{builder::PossibleValue, Parser, Subcommand, ValueEnum};
use std::fmt::Display; use std::fmt::Display;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]

View File

@ -7,5 +7,7 @@ mod cli;
fn main() { fn main() {
let opts = cli::Keyfork::parse(); let opts = cli::Keyfork::parse();
opts.command.handle(&opts).expect("Unable to handle command"); opts.command
.handle(&opts)
.expect("Unable to handle command");
} }

View File

@ -17,7 +17,6 @@ fn secp256k1() {
let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let rt = Builder::new_multi_thread().enable_io().build().unwrap();
let tempdir = TempDir::new("keyfork-seed").unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap();
for (i, per_seed) in tests.into_iter().enumerate() { for (i, per_seed) in tests.into_iter().enumerate() {
let mut socket_name = i.to_string(); let mut socket_name = i.to_string();
socket_name.push_str("-keyforkd.sock"); socket_name.push_str("-keyforkd.sock");
let socket_path = tempdir.path().join(socket_name); let socket_path = tempdir.path().join(socket_name);
@ -57,15 +56,11 @@ fn secp256k1() {
#[test] #[test]
fn ed25519() { fn ed25519() {
let tests = test_data() let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap();
.unwrap()
.remove(&"ed25519".to_string())
.unwrap();
let rt = Builder::new_multi_thread().enable_io().build().unwrap(); let rt = Builder::new_multi_thread().enable_io().build().unwrap();
let tempdir = TempDir::new("keyfork-seed").unwrap(); let tempdir = TempDir::new("keyfork-seed").unwrap();
for (i, per_seed) in tests.into_iter().enumerate() { for (i, per_seed) in tests.into_iter().enumerate() {
let mut socket_name = i.to_string(); let mut socket_name = i.to_string();
socket_name.push_str("-keyforkd.sock"); socket_name.push_str("-keyforkd.sock");
let socket_path = tempdir.path().join(socket_name); let socket_path = tempdir.path().join(socket_name);

View File

@ -1,7 +1,7 @@
use std::{future::Future, pin::Pin, sync::Arc, task::Poll}; use std::{future::Future, pin::Pin, sync::Arc, task::Poll};
use keyfork_derive_util::request::{DerivationError, DerivationRequest, DerivationResponse};
use keyfork_derive_path_data::guess_target; use keyfork_derive_path_data::guess_target;
use keyfork_derive_util::request::{DerivationError, DerivationRequest, DerivationResponse};
use tower::Service; use tower::Service;
use tracing::info; use tracing::info;
@ -101,10 +101,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn properly_derives_ed25519() { async fn properly_derives_ed25519() {
let tests = test_data() let tests = test_data().unwrap().remove(&"ed25519".to_string()).unwrap();
.unwrap()
.remove(&"ed25519".to_string())
.unwrap();
for per_seed in tests { for per_seed in tests {
let seed = &per_seed.seed; let seed = &per_seed.seed;