Add embedded elliptic curve keys to Substrate

This commit is contained in:
Luke Parker
2024-08-03 01:54:57 -04:00
parent 9e716c07fc
commit fc51c9b71c
11 changed files with 210 additions and 20 deletions

View File

@@ -1,13 +1,14 @@
use scale::Encode;
use sp_core::sr25519::{Public, Signature};
use sp_runtime::BoundedVec;
use serai_abi::primitives::Amount;
pub use serai_abi::validator_sets::primitives;
use primitives::{Session, ValidatorSet, KeyPair};
use primitives::{MAX_KEY_LEN, Session, ValidatorSet, KeyPair};
use crate::{
primitives::{NetworkId, SeraiAddress},
primitives::{EmbeddedEllipticCurve, NetworkId, SeraiAddress},
Transaction, Serai, TemporalSerai, SeraiError,
};
@@ -195,6 +196,18 @@ impl<'a> SeraiValidatorSets<'a> {
}))
}
pub fn set_embedded_elliptic_curve_key(
embedded_elliptic_curve: EmbeddedEllipticCurve,
key: BoundedVec<u8, sp_core::ConstU32<{ MAX_KEY_LEN }>>,
) -> serai_abi::Call {
serai_abi::Call::ValidatorSets(
serai_abi::validator_sets::Call::set_embedded_elliptic_curve_key {
embedded_elliptic_curve,
key,
},
)
}
pub fn allocate(network: NetworkId, amount: Amount) -> serai_abi::Call {
serai_abi::Call::ValidatorSets(serai_abi::validator_sets::Call::allocate { network, amount })
}

View File

@@ -82,6 +82,24 @@ pub async fn set_keys(
block
}
#[allow(dead_code)]
pub async fn set_embedded_elliptic_curve_key(
serai: &Serai,
pair: &Pair,
embedded_elliptic_curve: EmbeddedEllipticCurve,
key: BoundedVec<u8, ConstU32<{ MAX_KEY_LEN }>>,
nonce: u32,
) -> [u8; 32] {
// get the call
let tx = serai.sign(
pair,
SeraiValidatorSets::set_embedded_elliptic_curve_key(embedded_elliptic_curve, key),
nonce,
0,
);
publish_tx(serai, &tx).await
}
#[allow(dead_code)]
pub async fn allocate_stake(
serai: &Serai,

View File

@@ -221,12 +221,31 @@ async fn validator_set_rotation() {
// add 1 participant
let last_participant = accounts[4].clone();
// If this is the first iteration, set embedded elliptic curve keys
if i == 0 {
for (i, embedded_elliptic_curve) in
[EmbeddedEllipticCurve::Embedwards25519, EmbeddedEllipticCurve::Secq256k1]
.into_iter()
.enumerate()
{
set_embedded_elliptic_curve_key(
&serai,
embedded_elliptic_curve,
vec![0; 32].try_into().unwrap(),
&last_participant,
i.try_into().unwrap(),
)
.await;
}
}
let hash = allocate_stake(
&serai,
network,
key_shares[&network],
&last_participant,
i.try_into().unwrap(),
(2 + i).try_into().unwrap(),
)
.await;
participants.push(last_participant.public());