MCPcopy Create free account
hub / github.com/ddimaria/rust-blockchain-tutorial / recover_public_key

Function recover_public_key

utils/src/crypto.rs:209–219  ·  view source on GitHub ↗

Recover a public key using a recoverable signature and signed message ```rust use utils::crypto::{keypair, recover_public_key, sign_recovery}; let (private_key, public_key) = keypair(); let message = b"The message"; let signature = sign_recovery(message, &private_key).unwrap(); let (recovery_id, serialized_signature) = signature.serialize_compact(); let recovered_public_key = recover_public_key(

(message: &[u8], signature: &[u8], recovery_id: i32)

Source from the content-addressed store, hash-verified

207/// assert_eq!(recovered_public_key, public_key);
208/// ```
209pub fn recover_public_key(message: &[u8], signature: &[u8], recovery_id: i32) -> Result<PublicKey> {
210 let message = hash_message(message)?;
211 let recovery_id = RecoveryId::from_i32(recovery_id)
212 .map_err(|e| UtilsError::ConversionError(e.to_string()))?;
213 let signature = RecoverableSignature::from_compact(signature, recovery_id)
214 .map_err(|e| UtilsError::VerifyError(e.to_string()))?;
215
216 CONTEXT
217 .recover_ecdsa(&message, &signature)
218 .map_err(|e| UtilsError::RecoverError(e.to_string()))
219}
220
221/// Recover the address of the public key using a recoverable signature and signed message
222///

Callers 5

verifyMethod · 0.85
recover_addressMethod · 0.85
recover_public_keyMethod · 0.85
recover_addressFunction · 0.85
it_recoversFunction · 0.85

Calls 1

hash_messageFunction · 0.85

Tested by 1

it_recoversFunction · 0.68