diff --git a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs index c39624c87f5e..d3d56527a036 100644 --- a/substrate/primitives/application-crypto/src/ecdsa_bls381.rs +++ b/substrate/primitives/application-crypto/src/ecdsa_bls381.rs @@ -44,7 +44,13 @@ impl RuntimePublic for Public { } fn generate_pair(key_type: KeyTypeId, seed: Option>) -> Self { - sp_io::crypto::ecdsa_bls381_generate(key_type, seed) + let tuple = sp_io::crypto::ecdsa_bls381_generate(key_type, seed); + let ecdsa_pub = tuple.0; + let bls381_pub = tuple.1; + let mut combined_pub_raw = [0u8; ecdsa_bls381::PUBLIC_KEY_LEN]; + combined_pub_raw[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].copy_from_slice(ecdsa_pub.as_ref()); + combined_pub_raw[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].copy_from_slice(bls381_pub.as_ref()); + Self::from_raw(combined_pub_raw) } /// Dummy implementation. Returns `None`. @@ -124,3 +130,38 @@ fn combine_pop( combined_pop_raw[ecdsa::SIGNATURE_SERIALIZED_SIZE..].copy_from_slice(bls381_pop.as_ref()); Some(combined_pop_raw) } + +#[cfg(test)] +mod tests { + use super::*; + use sp_core::ecdsa; + use sp_core::bls381; + use sp_core::crypto::Pair; + + /// Helper function to generate test public keys for ECDSA and BLS381 + fn generate_test_keys() -> ([u8; ecdsa::PUBLIC_KEY_SERIALIZED_SIZE], [u8; bls381::PUBLIC_KEY_SERIALIZED_SIZE]) { + let ecdsa_pair = ecdsa::Pair::generate().0; + let bls381_pair = bls381::Pair::generate().0; + + let ecdsa_pub = ecdsa_pair.public(); + let bls381_pub = bls381_pair.public(); + + ( + ecdsa_pub.to_raw_vec().try_into().unwrap(), + bls381_pub.to_raw_vec().try_into().unwrap(), + ) + } + + #[test] + fn test_split_pub_key_bytes() { + let (ecdsa_pub, bls381_pub) = generate_test_keys(); + let mut combined_pub_key = Vec::new(); + combined_pub_key.extend_from_slice(&ecdsa_pub); + combined_pub_key.extend_from_slice(&bls381_pub); + + let result = split_pub_key_bytes(&combined_pub_key).unwrap(); + assert_eq!(result.0, ecdsa_pub, "ECDSA public key does not match"); + assert_eq!(result.1, bls381_pub, "BLS381 public key does not match"); + } +} + diff --git a/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs b/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs index e4c9ee8d2281..acabcbfd17c8 100644 --- a/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs +++ b/substrate/primitives/application-crypto/test/src/ecdsa_bls381.rs @@ -20,6 +20,7 @@ fn ecdsa_bls381_works_in_runtime() { let public = runtime_api.test_ecdsa_bls381_crypto(test_client.chain_info().genesis_hash).expect("things didnt fail"); + // let supported_keys = keystore.keys(ECDSA_BLS381).unwrap(); // assert!(supported_keys.contains(&public.expect("Things still no fail").to_raw_vec())); } diff --git a/substrate/primitives/io/src/lib.rs b/substrate/primitives/io/src/lib.rs index c0d959f90c68..8212344adf77 100644 --- a/substrate/primitives/io/src/lib.rs +++ b/substrate/primitives/io/src/lib.rs @@ -1247,12 +1247,20 @@ pub trait Crypto { &mut self, id: KeyTypeId, seed: Option>, - ) -> ecdsa_bls381::Public { + ) -> (ecdsa::Public, bls381::Public) { let seed = seed.as_ref().map(|s| std::str::from_utf8(s).expect("Seed is valid utf8!")); - self.extension::() + + let ecdsa_pub = self.extension::() .expect("No `keystore` associated for the current context!") - .ecdsa_bls381_generate_new(id, seed) - .expect("`ecdsa_bls381_generate` failed") + .ecdsa_generate_new(id, seed) + .expect("`ecdsa_bls381_generate` failed"); + + let bls_pub = self.extension::() + .expect("No `keystore` associated for the current context!") + .bls381_generate_new(id, seed) + .expect("`ecdsa_bls381_generate` failed"); + + (ecdsa_pub, bls_pub) } /// Generate a `bandersnatch` key pair for the given key type using an optional