Skip to content

Commit

Permalink
adding now the ability to generate two seperate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
coax1d committed Jan 6, 2025
1 parent 44305ee commit 72548d7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
43 changes: 42 additions & 1 deletion substrate/primitives/application-crypto/src/ecdsa_bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ impl RuntimePublic for Public {
}

fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> 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`.
Expand Down Expand Up @@ -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");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
16 changes: 12 additions & 4 deletions substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,20 @@ pub trait Crypto {
&mut self,
id: KeyTypeId,
seed: Option<Vec<u8>>,
) -> 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::<KeystoreExt>()

let ecdsa_pub = self.extension::<KeystoreExt>()
.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::<KeystoreExt>()
.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
Expand Down

0 comments on commit 72548d7

Please sign in to comment.