From cc02a7424d56b95fcb0dbcba2ee7859a12e6a253 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 9 Aug 2019 13:43:32 +0200 Subject: [PATCH] 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. --- substrate/core/sr-primitives/src/traits.rs | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/substrate/core/sr-primitives/src/traits.rs b/substrate/core/sr-primitives/src/traits.rs index 9c6954db28..7370a5074f 100644 --- a/substrate/core/sr-primitives/src/traits.rs +++ b/substrate/core/sr-primitives/src/traits.rs @@ -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 { @@ -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>(&self, msg: L, signer: &Self::Signer) -> bool; +} + +impl< + S: Verify::Public as app_crypto::AppPublic>::Generic> + From, + T: app_crypto::Wraps + app_crypto::AppKey + app_crypto::AppSignature + + AsRef + AsMut + From, +> AppVerify for T { + type Signer = ::Public; + fn verify>(&self, msg: L, signer: &Self::Signer) -> bool { + use app_crypto::IsWrappedBy; + let inner: &S = self.as_ref(); + let inner_pubkey = ::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 { /// A return type.