Map TM SignatureScheme to Substrate's sr25519

This commit is contained in:
Luke Parker 2022-10-20 03:46:09 -04:00
parent 9db42f7d83
commit 975c9d7456
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6

View 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
}
}