pub trait KeyPair: Send + Sync {
type Signature: AsRef<[u8]>;
type PublicKey: AsRef<[u8]>;
// Required methods
fn generate() -> Self
where Self: Sized;
fn from_bytes(bytes: [u8; 32]) -> Result<Self, SignError>
where Self: Sized;
fn public_key(&self) -> &Self::PublicKey;
fn sign(&self, hash: &[u8; 32]) -> Result<Self::Signature, SignError>;
}Expand description
Trait for cryptographic key pairs.
This trait abstracts over different elliptic curve key pairs, allowing the signing service to work with multiple curves.
§Thread Safety
All implementations must be Send + Sync to support multi-threaded
signing operations.
§Example
use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair};
fn sign_message<K: KeyPair>(keypair: &K, hash: &[u8; 32]) -> Vec<u8> {
keypair.sign(hash)
.expect("signing failed")
.as_ref()
.to_vec()
}
let keypair = Secp256k1KeyPair::generate();
let hash = [0u8; 32];
let sig = sign_message(&keypair, &hash);Required Associated Types§
Required Methods§
Sourcefn generate() -> Selfwhere
Self: Sized,
fn generate() -> Selfwhere
Self: Sized,
Generate a new random key pair.
Uses a cryptographically secure random number generator.
Sourcefn public_key(&self) -> &Self::PublicKey
fn public_key(&self) -> &Self::PublicKey
Get the public key.
Sourcefn sign(&self, hash: &[u8; 32]) -> Result<Self::Signature, SignError>
fn sign(&self, hash: &[u8; 32]) -> Result<Self::Signature, SignError>
Sign a 32-byte message hash.
§Arguments
hash- The 32-byte hash to sign (NOT the raw message)
§Returns
The signature bytes.
§Errors
Returns an error if signing fails.
§Important
The hash parameter should be a cryptographic hash of the message
(e.g., SHA-256 or Keccak-256), NOT the raw message itself.