From af8e60f72fdbb25d4dd46152d9bf0c7eff3db206 Mon Sep 17 00:00:00 2001 From: Denis Tsai Date: Sat, 11 Dec 2021 23:20:18 +0800 Subject: [PATCH] expose ecdsa_sign_prehashed in sp-io (#10119) * expose ecdsa_sign_prehashed in sp-io * add ecdsa_verify_prehashed to host functions for completeness * cargo fmt --- primitives/io/src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/primitives/io/src/lib.rs b/primitives/io/src/lib.rs index 94ae1a8f70838..44649abd28911 100644 --- a/primitives/io/src/lib.rs +++ b/primitives/io/src/lib.rs @@ -709,6 +709,22 @@ pub trait Crypto { .map(|sig| ecdsa::Signature::from_slice(sig.as_slice())) } + /// Sign the given a pre-hashed `msg` with the `ecdsa` key that corresponds to the given public + /// key and key type in the keystore. + /// + /// Returns the signature. + fn ecdsa_sign_prehashed( + &mut self, + id: KeyTypeId, + pub_key: &ecdsa::Public, + msg: &[u8; 32], + ) -> Option { + let keystore = &***self + .extension::() + .expect("No `keystore` associated for the current context!"); + SyncCryptoStore::ecdsa_sign_prehashed(keystore, id, pub_key, msg).ok().flatten() + } + /// Verify `ecdsa` signature. /// /// Returns `true` when the verification was successful. @@ -724,6 +740,17 @@ pub trait Crypto { ecdsa::Pair::verify(sig, msg, pub_key) } + /// Verify `ecdsa` signature with pre-hashed `msg`. + /// + /// Returns `true` when the verification was successful. + fn ecdsa_verify_prehashed( + sig: &ecdsa::Signature, + msg: &[u8; 32], + pub_key: &ecdsa::Public, + ) -> bool { + ecdsa::Pair::verify_prehashed(sig, msg, pub_key) + } + /// Register a `ecdsa` signature for batch verification. /// /// Batch verification must be enabled by calling [`start_batch_verify`].