Skip to main content

KeyPair

Trait KeyPair 

Source
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§

Source

type Signature: AsRef<[u8]>

The signature type produced by this key pair.

Source

type PublicKey: AsRef<[u8]>

The public key type for this key pair.

Required Methods§

Source

fn generate() -> Self
where Self: Sized,

Generate a new random key pair.

Uses a cryptographically secure random number generator.

Source

fn from_bytes(bytes: [u8; 32]) -> Result<Self, SignError>
where Self: Sized,

Create a key pair from raw secret key bytes.

§Arguments
  • bytes - The 32-byte secret key material
§Errors

Returns an error if the bytes don’t represent a valid secret key for this curve.

Source

fn public_key(&self) -> &Self::PublicKey

Get the public key.

Source

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.

Implementors§