Introduce AppVerify (#3343)

This trait, which works just like `Verify`, except on AppKey types.
I'd like for it all just to be `Verify`, but Rust's trait rules
concerning upstream changes mean it can't happen. This is a simple
workaround needed for some stuff in Polkadot.
This commit is contained in:
Gavin Wood
2019-08-09 13:43:32 +02:00
committed by GitHub
parent 8f3a68db25
commit cc02a7424d
@@ -35,6 +35,7 @@ use rstd::ops::{
Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign,
RemAssign, Shl, Shr
};
use crate::AppKey;
/// A lazy value.
pub trait Lazy<T: ?Sized> {
@@ -70,6 +71,28 @@ impl Verify for primitives::sr25519::Signature {
}
}
/// Means of signature verification of an application key.
pub trait AppVerify {
/// Type of the signer.
type Signer;
/// Verify a signature. Return `true` if signature is valid for the value.
fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &Self::Signer) -> bool;
}
impl<
S: Verify<Signer=<<T as AppKey>::Public as app_crypto::AppPublic>::Generic> + From<T>,
T: app_crypto::Wraps<Inner=S> + app_crypto::AppKey + app_crypto::AppSignature +
AsRef<S> + AsMut<S> + From<S>,
> AppVerify for T {
type Signer = <T as AppKey>::Public;
fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &Self::Signer) -> bool {
use app_crypto::IsWrappedBy;
let inner: &S = self.as_ref();
let inner_pubkey = <Self::Signer as app_crypto::AppPublic>::Generic::from_ref(&signer);
Verify::verify(inner, msg, inner_pubkey)
}
}
/// Some sort of check on the origin is performed by this object.
pub trait EnsureOrigin<OuterOrigin> {
/// A return type.