2023-05-13 06:02:47 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-02-24 19:51:06 +00:00
|
|
|
use serai_abi::primitives::NetworkId;
|
2023-05-13 06:02:47 +00:00
|
|
|
use zeroize::Zeroizing;
|
|
|
|
use rand_core::OsRng;
|
|
|
|
|
2024-02-24 19:51:06 +00:00
|
|
|
use sp_core::{
|
|
|
|
sr25519::{Pair, Signature},
|
|
|
|
Pair as PairTrait,
|
|
|
|
};
|
2023-05-13 06:02:47 +00:00
|
|
|
|
2023-12-15 04:45:15 +00:00
|
|
|
use ciphersuite::{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::{
|
|
|
|
validator_sets::{
|
2023-12-15 04:45:15 +00:00
|
|
|
primitives::{ValidatorSet, KeyPair, musig_context, set_keys_message},
|
2023-03-31 10:34:09 +00:00
|
|
|
ValidatorSetsEvent,
|
|
|
|
},
|
2024-02-24 19:51:06 +00:00
|
|
|
Amount, Serai, SeraiValidatorSets,
|
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)]
|
2024-06-21 12:39:17 +00:00
|
|
|
pub async fn set_keys(
|
|
|
|
serai: &Serai,
|
|
|
|
set: ValidatorSet,
|
|
|
|
key_pair: KeyPair,
|
|
|
|
pairs: &[Pair],
|
|
|
|
) -> [u8; 32] {
|
|
|
|
let mut pub_keys = vec![];
|
|
|
|
for pair in pairs {
|
|
|
|
let public_key =
|
|
|
|
<Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut pair.public().0.as_ref()).unwrap();
|
|
|
|
pub_keys.push(public_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut threshold_keys = vec![];
|
|
|
|
for i in 0 .. pairs.len() {
|
|
|
|
let secret_key = <Ristretto as Ciphersuite>::read_F::<&[u8]>(
|
|
|
|
&mut pairs[i].as_ref().secret.to_bytes()[.. 32].as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(Ristretto::generator() * secret_key, pub_keys[i]);
|
|
|
|
|
|
|
|
threshold_keys.push(
|
|
|
|
musig::<Ristretto>(&musig_context(set), &Zeroizing::new(secret_key), &pub_keys).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut musig_keys = HashMap::new();
|
|
|
|
for tk in threshold_keys {
|
|
|
|
musig_keys.insert(tk.params().i(), tk.into());
|
|
|
|
}
|
2023-05-13 06:02:47 +00:00
|
|
|
|
|
|
|
let sig = frost::tests::sign_without_caching(
|
|
|
|
&mut OsRng,
|
2024-06-21 12:39:17 +00:00
|
|
|
frost::tests::algorithm_machines(&mut OsRng, &Schnorrkel::new(b"substrate"), &musig_keys),
|
2023-12-15 04:45:15 +00:00
|
|
|
&set_keys_message(&set, &[], &key_pair),
|
2023-03-31 10:34:09 +00:00
|
|
|
);
|
2023-05-13 06:02:47 +00:00
|
|
|
|
2023-11-28 07:29:50 +00:00
|
|
|
// Set the key pair
|
2023-10-23 04:33:38 +00:00
|
|
|
let block = publish_tx(
|
|
|
|
serai,
|
2024-06-02 23:58:29 +00:00
|
|
|
&SeraiValidatorSets::set_keys(
|
|
|
|
set.network,
|
|
|
|
vec![].try_into().unwrap(),
|
|
|
|
key_pair.clone(),
|
|
|
|
Signature(sig.to_bytes()),
|
|
|
|
),
|
2023-10-23 04:33:38 +00:00
|
|
|
)
|
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
|
|
|
|
}
|
2024-02-24 19:51:06 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub async fn allocate_stake(
|
|
|
|
serai: &Serai,
|
|
|
|
network: NetworkId,
|
|
|
|
amount: Amount,
|
|
|
|
pair: &Pair,
|
|
|
|
nonce: u32,
|
|
|
|
) -> [u8; 32] {
|
|
|
|
// get the call
|
2024-02-25 23:37:15 +00:00
|
|
|
let tx = serai.sign(pair, SeraiValidatorSets::allocate(network, amount), nonce, 0);
|
2024-02-24 19:51:06 +00:00
|
|
|
publish_tx(serai, &tx).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub async fn deallocate_stake(
|
|
|
|
serai: &Serai,
|
|
|
|
network: NetworkId,
|
|
|
|
amount: Amount,
|
|
|
|
pair: &Pair,
|
|
|
|
nonce: u32,
|
|
|
|
) -> [u8; 32] {
|
|
|
|
// get the call
|
2024-02-25 23:37:15 +00:00
|
|
|
let tx = serai.sign(pair, SeraiValidatorSets::deallocate(network, amount), nonce, 0);
|
2024-02-24 19:51:06 +00:00
|
|
|
publish_tx(serai, &tx).await
|
|
|
|
}
|