From 7728dd7db06121b34b7a3b6780783d3d0476e938 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 --- substrate/primitives/io/src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/substrate/primitives/io/src/lib.rs b/substrate/primitives/io/src/lib.rs index 94ae1a8f70..44649abd28 100644 --- a/substrate/primitives/io/src/lib.rs +++ b/substrate/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`].