Skip to content

Commit

Permalink
Minor clean up (#5284)
Browse files Browse the repository at this point in the history
This PR performs minor code cleanup to reduce verbosity. Since the
compiler has already optimized out indirect calls in the existing code,
these changes improve readability but do not affect performance.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
  • Loading branch information
conr2d and bkchr authored Aug 13, 2024
1 parent 42eb4ec commit 0cd577b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
7 changes: 7 additions & 0 deletions prdoc/pr_5284.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Minor clean up
author: conr2d
topic: runtime

crates:
- name: sp-runtime
bump: none
31 changes: 11 additions & 20 deletions substrate/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,34 +438,25 @@ impl TryFrom<MultiSigner> for ecdsa::Public {
#[cfg(feature = "std")]
impl std::fmt::Display for MultiSigner {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Self::Ed25519(ref who) => write!(fmt, "ed25519: {}", who),
Self::Sr25519(ref who) => write!(fmt, "sr25519: {}", who),
Self::Ecdsa(ref who) => write!(fmt, "ecdsa: {}", who),
match self {
Self::Ed25519(who) => write!(fmt, "ed25519: {}", who),
Self::Sr25519(who) => write!(fmt, "sr25519: {}", who),
Self::Ecdsa(who) => write!(fmt, "ecdsa: {}", who),
}
}
}

impl Verify for MultiSignature {
type Signer = MultiSigner;
fn verify<L: Lazy<[u8]>>(&self, mut msg: L, signer: &AccountId32) -> bool {
match (self, signer) {
(Self::Ed25519(ref sig), who) => match ed25519::Public::from_slice(who.as_ref()) {
Ok(signer) => sig.verify(msg, &signer),
Err(()) => false,
},
(Self::Sr25519(ref sig), who) => match sr25519::Public::from_slice(who.as_ref()) {
Ok(signer) => sig.verify(msg, &signer),
Err(()) => false,
},
(Self::Ecdsa(ref sig), who) => {
let who: [u8; 32] = *signer.as_ref();
match self {
Self::Ed25519(sig) => sig.verify(msg, &who.into()),
Self::Sr25519(sig) => sig.verify(msg, &who.into()),
Self::Ecdsa(sig) => {
let m = sp_io::hashing::blake2_256(msg.get());
match sp_io::crypto::secp256k1_ecdsa_recover_compressed(sig.as_ref(), &m) {
Ok(pubkey) =>
&sp_io::hashing::blake2_256(pubkey.as_ref()) ==
<dyn AsRef<[u8; 32]>>::as_ref(who),
_ => false,
}
sp_io::crypto::secp256k1_ecdsa_recover_compressed(sig.as_ref(), &m)
.map_or(false, |pubkey| sp_io::hashing::blake2_256(&pubkey) == who)
},
}
}
Expand Down

0 comments on commit 0cd577b

Please sign in to comment.