2023-05-13 06:02:47 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use zeroize::Zeroizing;
|
|
|
|
use rand_core::OsRng;
|
|
|
|
|
|
|
|
use sp_core::{Pair, sr25519::Signature};
|
|
|
|
|
|
|
|
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
|
2023-05-13 08:20:13 +00:00
|
|
|
use frost::dkg::musig::musig;
|
2023-05-13 06:02:47 +00:00
|
|
|
use schnorrkel::Schnorrkel;
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
use serai_client::{
|
2023-05-13 06:02:47 +00:00
|
|
|
primitives::insecure_pair_from_name,
|
2023-03-31 10:34:09 +00:00
|
|
|
validator_sets::{
|
2023-05-13 08:20:13 +00:00
|
|
|
primitives::{ValidatorSet, KeyPair, musig_context, musig_key, set_keys_message},
|
2023-03-31 10:34:09 +00:00
|
|
|
ValidatorSetsEvent,
|
|
|
|
},
|
2023-10-23 04:33:38 +00:00
|
|
|
SeraiValidatorSets, Serai,
|
2023-03-31 10:34:09 +00:00
|
|
|
};
|
|
|
|
|
2023-10-23 04:33:38 +00:00
|
|
|
use crate::common::tx::publish_tx;
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2023-10-23 04:33:38 +00:00
|
|
|
pub async fn set_keys(serai: &Serai, set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
2023-03-31 10:34:09 +00:00
|
|
|
let pair = insecure_pair_from_name("Alice");
|
|
|
|
let public = pair.public();
|
|
|
|
|
2023-05-13 06:02:47 +00:00
|
|
|
let public_key = <Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut public.0.as_ref()).unwrap();
|
|
|
|
assert_eq!(
|
2023-10-10 10:53:24 +00:00
|
|
|
serai
|
2023-10-14 06:47:58 +00:00
|
|
|
.with_current_latest_block()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.validator_sets()
|
|
|
|
.musig_key(set)
|
2023-10-10 10:53:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
2023-05-13 08:20:13 +00:00
|
|
|
musig_key(set, &[public]).0
|
2023-05-13 06:02:47 +00:00
|
|
|
);
|
2023-03-31 10:34:09 +00:00
|
|
|
|
2023-05-13 06:02:47 +00:00
|
|
|
let secret_key = <Ristretto as Ciphersuite>::read_F::<&[u8]>(
|
|
|
|
&mut pair.as_ref().secret.to_bytes()[.. 32].as_ref(),
|
2023-03-31 10:34:09 +00:00
|
|
|
)
|
2023-05-13 06:02:47 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(Ristretto::generator() * secret_key, public_key);
|
2023-05-13 08:20:13 +00:00
|
|
|
let threshold_keys =
|
|
|
|
musig::<Ristretto>(&musig_context(set), &Zeroizing::new(secret_key), &[public_key]).unwrap();
|
2023-03-31 10:34:09 +00:00
|
|
|
assert_eq!(
|
2023-10-10 10:53:24 +00:00
|
|
|
serai
|
2023-10-14 06:47:58 +00:00
|
|
|
.with_current_latest_block()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.validator_sets()
|
|
|
|
.musig_key(set)
|
2023-10-10 10:53:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
2023-05-13 06:02:47 +00:00
|
|
|
threshold_keys.group_key().to_bytes()
|
|
|
|
);
|
|
|
|
|
|
|
|
let sig = frost::tests::sign_without_caching(
|
|
|
|
&mut OsRng,
|
|
|
|
frost::tests::algorithm_machines(
|
|
|
|
&mut OsRng,
|
|
|
|
Schnorrkel::new(b"substrate"),
|
|
|
|
&HashMap::from([(threshold_keys.params().i(), threshold_keys.into())]),
|
|
|
|
),
|
2023-05-13 08:20:13 +00:00
|
|
|
&set_keys_message(&set, &key_pair),
|
2023-03-31 10:34:09 +00:00
|
|
|
);
|
2023-05-13 06:02:47 +00:00
|
|
|
|
|
|
|
// Vote in a key pair
|
2023-10-23 04:33:38 +00:00
|
|
|
let block = publish_tx(
|
|
|
|
serai,
|
|
|
|
&SeraiValidatorSets::set_keys(set.network, key_pair.clone(), Signature(sig.to_bytes())),
|
|
|
|
)
|
2023-05-13 06:02:47 +00:00
|
|
|
.await;
|
|
|
|
|
2023-03-31 10:34:09 +00:00
|
|
|
assert_eq!(
|
2023-10-14 06:47:58 +00:00
|
|
|
serai.as_of(block).validator_sets().key_gen_events().await.unwrap(),
|
2023-03-31 10:34:09 +00:00
|
|
|
vec![ValidatorSetsEvent::KeyGen { set, key_pair: key_pair.clone() }]
|
|
|
|
);
|
2023-10-14 06:47:58 +00:00
|
|
|
assert_eq!(serai.as_of(block).validator_sets().keys(set).await.unwrap(), Some(key_pair));
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
block
|
|
|
|
}
|