Skip to main content

Secp256k1KeyPair

Struct Secp256k1KeyPair 

Source
pub struct Secp256k1KeyPair { /* private fields */ }
Expand description

secp256k1 key pair for Ethereum, Bitcoin, Tron, and Ripple.

This key pair uses the secp256k1 elliptic curve, which is the standard for most major blockchain networks.

§Security

  • The signing key is stored securely and is ZeroizeOnDrop
  • Signatures are normalized to prevent malleability
  • Recovery IDs are computed for Ethereum compatibility

§Example

use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair};

// Generate a new key pair
let keypair = Secp256k1KeyPair::generate();

// Or create from existing bytes
let secret_bytes = [0x42u8; 32]; // Use real secret in production!
let keypair = Secp256k1KeyPair::from_bytes(secret_bytes)
    .expect("valid secret key");

// Sign a message hash
let hash = [0u8; 32]; // Use real hash in production!
let signature = keypair.sign(&hash).expect("signing succeeded");

// Get Ethereum address
let address = keypair.public_key().ethereum_address();
println!("Address: 0x{}", hex::encode(address));

Implementations§

Source§

impl Secp256k1KeyPair

Source

pub fn from_secret_key(secret: &SecretKey) -> Result<Self, SignError>

Create a key pair from a SecretKey.

This consumes the SecretKey to ensure the key material exists in only one place.

§Errors

Returns an error if the secret key bytes are not a valid secp256k1 scalar (e.g., zero or greater than the curve order).

§Example
use txgate_crypto::keys::SecretKey;
use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair};

let secret = SecretKey::generate();
let keypair = Secp256k1KeyPair::from_secret_key(&secret)
    .expect("valid secret key");
Source

pub fn verify(&self, hash: &[u8; 32], signature: &Secp256k1Signature) -> bool

Verify a signature against a hash using this key pair’s public key.

This is primarily useful for testing. In production, verification is typically done using the public key alone.

§Arguments
  • hash - The 32-byte hash that was signed
  • signature - The signature to verify
§Returns

true if the signature is valid, false otherwise.

Trait Implementations§

Source§

impl Debug for Secp256k1KeyPair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl KeyPair for Secp256k1KeyPair

Source§

type Signature = Secp256k1Signature

The signature type produced by this key pair.
Source§

type PublicKey = Secp256k1PublicKey

The public key type for this key pair.
Source§

fn generate() -> Self

Generate a new random key pair. Read more
Source§

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

Create a key pair from raw secret key bytes. Read more
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. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V