mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 16:21:06 +00:00
Add CryptoStore::ecdsa_sign_prehashed() (#8838)
* Pair::sign_prehashed() * add CryptoStore::ecdsa_sign_prehashed() * add test for testing keystore * address review comments
This commit is contained in:
@@ -194,6 +194,20 @@ pub trait CryptoStore: Send + Sync {
|
||||
public: &sr25519::Public,
|
||||
transcript_data: VRFTranscriptData,
|
||||
) -> Result<Option<VRFSignature>, Error>;
|
||||
|
||||
/// Sign pre-hashed
|
||||
///
|
||||
/// Signs a pre-hashed message with the private key that matches
|
||||
/// the ECDSA public key passed.
|
||||
///
|
||||
/// Returns the SCALE encoded signature if key is found and supported,
|
||||
/// `None` if the key doesn't exist or an error when something failed.
|
||||
async fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error>;
|
||||
}
|
||||
|
||||
/// Sync version of the CryptoStore
|
||||
@@ -353,6 +367,20 @@ pub trait SyncCryptoStore: CryptoStore + Send + Sync {
|
||||
public: &sr25519::Public,
|
||||
transcript_data: VRFTranscriptData,
|
||||
) -> Result<Option<VRFSignature>, Error>;
|
||||
|
||||
/// Sign pre-hashed
|
||||
///
|
||||
/// Signs a pre-hashed message with the private key that matches
|
||||
/// the ECDSA public key passed.
|
||||
///
|
||||
/// Returns the SCALE encoded signature if key is found and supported,
|
||||
/// `None` if the key doesn't exist or an error when something failed.
|
||||
fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error>;
|
||||
}
|
||||
|
||||
/// A pointer to a keystore.
|
||||
|
||||
@@ -22,6 +22,7 @@ use sp_core::{
|
||||
crypto::{Pair, Public, CryptoTypePublicPair},
|
||||
ed25519, sr25519, ecdsa,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
{CryptoStore, SyncCryptoStorePtr, Error, SyncCryptoStore},
|
||||
vrf::{VRFTranscriptData, VRFSignature, make_transcript},
|
||||
@@ -144,6 +145,15 @@ impl CryptoStore for KeyStore {
|
||||
) -> Result<Option<VRFSignature>, Error> {
|
||||
SyncCryptoStore::sr25519_vrf_sign(self, key_type, public, transcript_data)
|
||||
}
|
||||
|
||||
async fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
SyncCryptoStore::ecdsa_sign_prehashed(self, id, public, msg)
|
||||
}
|
||||
}
|
||||
|
||||
impl SyncCryptoStore for KeyStore {
|
||||
@@ -325,6 +335,16 @@ impl SyncCryptoStore for KeyStore {
|
||||
proof,
|
||||
}))
|
||||
}
|
||||
|
||||
fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
let pair = self.ecdsa_key_pair(id, public);
|
||||
pair.map(|k| k.sign_prehashed(msg)).map(Ok).transpose()
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<SyncCryptoStorePtr> for KeyStore {
|
||||
@@ -342,7 +362,7 @@ impl Into<Arc<dyn CryptoStore>> for KeyStore {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_core::{sr25519, testing::{ED25519, SR25519}};
|
||||
use sp_core::{sr25519, testing::{ED25519, SR25519, ECDSA}};
|
||||
use crate::{SyncCryptoStore, vrf::VRFTranscriptValue};
|
||||
|
||||
#[test]
|
||||
@@ -416,4 +436,25 @@ mod tests {
|
||||
|
||||
assert!(result.unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ecdsa_sign_prehashed_works() {
|
||||
let store = KeyStore::new();
|
||||
|
||||
let suri = "//Alice";
|
||||
let pair = ecdsa::Pair::from_string(suri, None).unwrap();
|
||||
|
||||
let msg = sp_core::keccak_256(b"this should be a hashed message");
|
||||
|
||||
// no key in key store
|
||||
let res = SyncCryptoStore::ecdsa_sign_prehashed(&store, ECDSA, &pair.public(), &msg).unwrap();
|
||||
assert!(res.is_none());
|
||||
|
||||
// insert key, sign again
|
||||
let res = SyncCryptoStore::insert_unknown(&store, ECDSA, suri, pair.public().as_ref()).unwrap();
|
||||
assert_eq!((), res);
|
||||
|
||||
let res = SyncCryptoStore::ecdsa_sign_prehashed(&store, ECDSA, &pair.public(), &msg).unwrap();
|
||||
assert!(res.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user