Skip to content

Commit

Permalink
Define CustomVerify trait
Browse files Browse the repository at this point in the history
Signed-off-by: Serban Iorga <serban@parity.io>
  • Loading branch information
serban300 committed Sep 19, 2022
1 parent c24431e commit adf91e9
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ impl IdentifyAccount for sp_core::ecdsa::Public {
}
}

/// Means of signature verification.
///
/// Accepts custom hashing fn for the message and custom convertor fn for the signer.
pub trait CustomVerify<
MsgHash: Hash,
AccountId: PartialEq,
SignerToAccountId: Convert<Self::Signer, AccountId>,
>
{
/// Type of the signer.
type Signer: IdentifyAccount;

/// Verify a signature.
///
/// Return `true` if signature is valid for the value.
fn custom_verify<L: Lazy<[u8]>>(&self, msg: L, signer: &AccountId) -> bool;
}

impl<
MsgHash: Hash,
AccountId: PartialEq,
SignerToAccountId: Convert<sp_core::ecdsa::Public, AccountId>,
> CustomVerify<MsgHash, AccountId, SignerToAccountId> for sp_core::ecdsa::Signature
where
<MsgHash as Hash>::Output: Into<[u8; 32]>,
{
type Signer = sp_core::ecdsa::Public;

fn custom_verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &AccountId) -> bool {
use sp_application_crypto::ByteArray;
let msg_hash = <MsgHash as Hash>::hash(msg.get()).into();
match sp_io::crypto::secp256k1_ecdsa_recover_compressed(self.as_ref(), &msg_hash)
.map(|raw_pubkey| sp_core::ecdsa::Public::from_slice(raw_pubkey.as_ref()))
{
Ok(Ok(pubkey)) => signer == &SignerToAccountId::convert(pubkey),
_ => false,
}
}
}

/// Means of signature verification.
pub trait Verify {
/// Type of the signer.
Expand Down Expand Up @@ -126,14 +166,8 @@ impl Verify for sp_core::sr25519::Signature {

impl Verify for sp_core::ecdsa::Signature {
type Signer = sp_core::ecdsa::Public;
fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &sp_core::ecdsa::Public) -> bool {
match sp_io::crypto::secp256k1_ecdsa_recover_compressed(
self.as_ref(),
&sp_io::hashing::blake2_256(msg.get()),
) {
Ok(pubkey) => signer.as_ref() == &pubkey[..],
_ => false,
}
fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &sp_core::ecdsa::Public) -> bool {
CustomVerify::<BlakeTwo256, _, Identity>::custom_verify(self, msg, signer)
}
}

Expand Down Expand Up @@ -2016,6 +2050,5 @@ mod tests {
assert!(ecdsa::Pair::verify(&signature, msg, &pair.public()));

assert!(signature.verify(msg, &pair.public()));
assert!(signature.verify(msg, &pair.public()));
}
}

0 comments on commit adf91e9

Please sign in to comment.