mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-24 11:36:18 +00:00
Map TM SignatureScheme to Substrate's sr25519
This commit is contained in:
parent
9db42f7d83
commit
975c9d7456
1 changed files with 41 additions and 0 deletions
41
substrate/consensus/src/signature_scheme.rs
Normal file
41
substrate/consensus/src/signature_scheme.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use sp_application_crypto::{
|
||||
RuntimePublic as PublicTrait, Pair as PairTrait,
|
||||
sr25519::{Public, Pair, Signature},
|
||||
};
|
||||
|
||||
use tendermint_machine::ext::SignatureScheme;
|
||||
|
||||
pub(crate) struct TendermintSigner {
|
||||
keys: Pair,
|
||||
lookup: Vec<Public>,
|
||||
}
|
||||
|
||||
impl SignatureScheme for TendermintSigner {
|
||||
type ValidatorId = u16;
|
||||
type Signature = Signature;
|
||||
type AggregateSignature = Vec<Signature>;
|
||||
|
||||
fn sign(&self, msg: &[u8]) -> Signature {
|
||||
self.keys.sign(msg)
|
||||
}
|
||||
|
||||
fn verify(&self, validator: u16, msg: &[u8], sig: &Signature) -> bool {
|
||||
self.lookup[usize::try_from(validator).unwrap()].verify(&msg, sig)
|
||||
}
|
||||
|
||||
fn aggregate(sigs: &[Signature]) -> Vec<Signature> {
|
||||
sigs.to_vec()
|
||||
}
|
||||
|
||||
fn verify_aggregate(&self, validators: &[u16], msg: &[u8], sigs: &Vec<Signature>) -> bool {
|
||||
if validators.len() != sigs.len() {
|
||||
return false;
|
||||
}
|
||||
for (v, sig) in validators.iter().zip(sigs.iter()) {
|
||||
if !self.verify(*v, msg, sig) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue