Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

expose ecdsa_sign_prehashed in sp-io #10119

Merged
merged 4 commits into from
Dec 11, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ pub trait Misc {
err,
);
None
},
}
}
}
}
Expand Down Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding ecdsa_verify_prehashed for completeness of the API.

AFAICT recover already takes a hash, so that one should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added in latest commit.

&mut self,
id: KeyTypeId,
pub_key: &ecdsa::Public,
msg: &[u8; 32],
) -> Option<ecdsa::Signature> {
let keystore = &***self
.extension::<KeystoreExt>()
.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.
Expand All @@ -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`].
Expand Down