diff --git a/Cargo.lock b/Cargo.lock index fa6188ad3..e865d5d1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,7 +325,6 @@ dependencies = [ "rstest", "sha2", "thiserror", - "tiny-bip39", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a29fe2b7f..a3ad6fa37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,7 +107,6 @@ syn = "2.0.27" quote = "1.0.32" proc-macro2 = "1.0.66" proc-macro-crate = "1.3.1" -tiny-bip39 = "1.0.0" anyhow = "1.0.71" sha2 = "0.10.8" hkdf = "0.12.0" diff --git a/crates/chia-bls/Cargo.toml b/crates/chia-bls/Cargo.toml index 367108017..70e434c6b 100644 --- a/crates/chia-bls/Cargo.toml +++ b/crates/chia-bls/Cargo.toml @@ -18,7 +18,6 @@ arbitrary = ["dep:arbitrary"] [dependencies] chia-traits = { workspace = true } chia_py_streamable_macro = { workspace = true, optional = true } -tiny-bip39 = { workspace = true } anyhow = { workspace = true } sha2 = { workspace = true } hkdf = { workspace = true } diff --git a/crates/chia-bls/README.md b/crates/chia-bls/README.md deleted file mode 100644 index 96e1e2d8a..000000000 --- a/crates/chia-bls/README.md +++ /dev/null @@ -1,76 +0,0 @@ -Library providing building blocks for a Chia wallet. - -BIP39 mnemonic handling: - -``` -fn entropy_to_mnemonic(entropy: &[u8; 32]) -> String -fn mnemonic_to_entropy(mnemonic: &str) -> Result<[u8; 32], Error> -fn entropy_to_seed(entropy: &[u8; 32]) -> [u8; 64] -``` - -SecretKey - -``` -impl SecretKey { - pub fn from_seed(seed: &[u8; 64]) -> SecretKey - pub fn from_bytes(bytes: &[u8; 32]) -> Option - pub fn to_bytes(&self) -> [u8; 32] - - pub fn public_key(&self) -> PublicKey - - pub fn derive_unhardened(&self, idx: u32) -> SecretKey - pub fn derive_hardened(&self, idx: u32) -> SecretKey -} -``` - -PublicKey - -``` -impl PublicKey { - pub fn from_bytes(bytes: &[u8; 48]) -> Option - pub fn to_bytes(&self) -> [u8; 48] - pub fn derive_unhardened(&self, idx: u32) -> PublicKey -} -``` - -Unhardened Key derivation (`Key` can be both a secret- or public key) - -``` -fn master_to_wallet_unhardened_intermediate(key: &Key) -> Key -fn master_to_wallet_unhardened(key: &Key, idx: u32) -> Key - -``` - -Hardened key derivation (only SecretKey) - -``` -fn master_to_wallet_hardened_intermediate(key: &SecretKey) -> SecretKey -fn master_to_wallet_hardened(key: &SecretKey, idx: u32) -> SecretKey -fn master_to_pool_singleton(key: &SecretKey, pool_wallet_idx: u32) -> SecretKey -fn master_to_pool_authentication(key: &SecretKey, pool_wallet_idx: u32, idx: u32) -> SecretKey -``` - -Signature - -``` -impl Signature { - pub fn from_bytes(buf: &[u8; 96]) -> Option - pub fn to_bytes(&self) -> [u8; 96] - pub fn aggregate(&mut self, sig: &Signature) -} - -impl Default for Signature { - fn default() -> Self -} -``` - -sign and verify (using the Augmented scheme) - -``` -pub fn sign>(sk: &SecretKey, msg: Msg) -> Signature -pub fn aggregate, I>(sigs: I) -> Signature - where I: IntoIterator -pub fn verify>(sig: &Signature, key: &PublicKey, msg: Msg) -> bool -pub fn aggregate_verify, Msg: Borrow<[u8]>, I>(sig: &Signature, data: I) -> bool - where I: IntoIterator -``` diff --git a/crates/chia-bls/src/lib.rs b/crates/chia-bls/src/lib.rs index 96658a542..8fd08f823 100644 --- a/crates/chia-bls/src/lib.rs +++ b/crates/chia-bls/src/lib.rs @@ -4,7 +4,6 @@ mod bls_cache; mod derive_keys; mod error; mod gtelement; -mod mnemonic; mod public_key; mod secret_key; mod signature; @@ -16,7 +15,6 @@ pub use bls_cache::BlsCache; pub use derive_keys::*; pub use error::{Error, Result}; pub use gtelement::GTElement; -pub use mnemonic::*; pub use public_key::{hash_to_g1, hash_to_g1_with_dst, PublicKey}; pub use secret_key::SecretKey; pub use signature::{ diff --git a/crates/chia-bls/src/mnemonic.rs b/crates/chia-bls/src/mnemonic.rs deleted file mode 100644 index b1b7494d1..000000000 --- a/crates/chia-bls/src/mnemonic.rs +++ /dev/null @@ -1,93 +0,0 @@ -use bip39::{Language, Mnemonic, Seed}; -use std::array::TryFromSliceError; - -pub fn entropy_to_mnemonic(entropy: &[u8; 32]) -> anyhow::Result { - Ok(Mnemonic::from_entropy(entropy, Language::English)?.into_phrase()) -} - -pub fn mnemonic_to_entropy(mnemonic: &str) -> anyhow::Result<[u8; 32]> { - let m = Mnemonic::from_phrase(mnemonic, Language::English)?; - let ent = m.entropy(); - ent.try_into().map_err(|e: TryFromSliceError| { - anyhow::Error::from(e).context("incorrect number of words in mnemonic") - }) -} - -pub fn entropy_to_seed(entropy: &[u8; 32]) -> anyhow::Result<[u8; 64]> { - let m = Mnemonic::from_entropy(entropy, Language::English)?; - Ok(Seed::new(&m, "").as_bytes().try_into()?) -} - -#[cfg(test)] -use hex::FromHex; - -#[test] -fn test_parse_mnemonic() { - // test vectors from BIP39 - // https://github.com/trezor/python-mnemonic/blob/master/vectors.json - // The seeds are changed to account for chia using an empty passphrase - // (whereas the trezor test vectors use "TREZOR") - - // phrase, entropy, seed - let test_cases = [ - ("all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform", - "066dca1a2bb7e8a1db2832148ce9933eea0f3ac9548d793112d9a95c9407efad", - "fc795be0c3f18c50dddb34e72179dc597d64055497ecc1e69e2e56a5409651bc139aae8070d4df0ea14d8d2a518a9a00bb1cc6e92e053fe34051f6821df9164c" - ), - ("void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold", - "f585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f", - "b873212f885ccffbf4692afcb84bc2e55886de2dfa07d90f5c3c239abc31c0a6ce047e30fd8bf6a281e71389aa82d73df74c7bbfb3b06b4639a5cee775cccd3c" - ), - ("panda eyebrow bullet gorilla call smoke muffin taste mesh discover soft ostrich alcohol speed nation flash devote level hobby quick inner drive ghost inside", - "9f6a2878b2520799a44ef18bc7df394e7061a224d2c33cd015b157d746869863", - "3e066d7dee2dbf8fcd3fe240a3975658ca118a8f6f4ca81cf99104944604b05a5090a79d99e545704b914ca0397fedb82fd00fd6a72098703709c891a065ee49") - ]; - - for (phrase, entropy, seed) in test_cases { - println!("{phrase}"); - assert_eq!( - hex::encode(mnemonic_to_entropy(phrase).unwrap()), - entropy.to_string() - ); - assert_eq!( - entropy_to_mnemonic(&<[u8; 32]>::from_hex(entropy).unwrap()) - .unwrap() - .as_str(), - phrase - ); - assert_eq!( - hex::encode(entropy_to_seed(&<[u8; 32]>::from_hex(entropy).unwrap()).unwrap()), - seed.to_string() - ); - } -} - -#[test] -fn test_invalid_mnemonic() { - assert_eq!( - format!( - "{}", - mnemonic_to_entropy("camp survey warrior").unwrap_err() - ), - "invalid number of words in phrase: 3" - ); - assert_eq!( - format!( - "{}", - mnemonic_to_entropy( - "panda eyebrow bullet gorilla call smoke muffin taste mesh discover soft ostrich" - ) - .unwrap_err() - ), - "invalid checksum" - ); - assert_eq!(format!("{}", mnemonic_to_entropy("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap_err()), "incorrect number of words in mnemonic"); - assert_eq!(mnemonic_to_entropy("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art").unwrap(), <[u8; 32]>::from_hex("0000000000000000000000000000000000000000000000000000000000000000").unwrap()); - - assert_eq!(mnemonic_to_entropy("letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless").unwrap(), - <[u8; 32]>::from_hex("8080808080808080808080808080808080808080808080808080808080808080").unwrap()); - - // make sure all whitespace is ignored - assert_eq!(mnemonic_to_entropy("letter advice \t cage\t absurd \tamount doctor acoustic \n avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless").unwrap(), - <[u8; 32]>::from_hex("8080808080808080808080808080808080808080808080808080808080808080").unwrap()); -}