Skip to main content

Secp256k1Signature

Struct Secp256k1Signature 

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

Wrapper for secp256k1 ECDSA signatures.

Contains the 64-byte signature (r || s) and the recovery ID for Ethereum compatibility.

§Signature Formats

  • Standard: 64 bytes (r: 32 bytes || s: 32 bytes)
  • Recoverable: 65 bytes (r || s || v) where v is the recovery ID

§Security

The S value is normalized to the lower half of the curve order to prevent signature malleability attacks.

§Example

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

let keypair = Secp256k1KeyPair::generate();
let hash = [0u8; 32];
let signature = keypair.sign(&hash).expect("signing failed");

// Get standard 64-byte signature
assert_eq!(signature.as_ref().len(), 64);

// Get recoverable signature with recovery ID
let recoverable = signature.to_recoverable_bytes();
assert_eq!(recoverable.len(), 65);

// Get recovery ID for ecrecover
let v = signature.recovery_id();
assert!(v == 0 || v == 1);

Implementations§

Source§

impl Secp256k1Signature

Source

pub const fn from_bytes_and_recovery_id( bytes: [u8; 64], recovery_id: u8, ) -> Self

Create a signature from raw bytes and a recovery ID.

This is useful for reconstructing a signature from its components.

§Arguments
  • bytes - The 64-byte signature (r || s)
  • recovery_id - The recovery ID (0 or 1)
§Example
use txgate_crypto::keypair::{KeyPair, Secp256k1KeyPair, Secp256k1Signature};

let keypair = Secp256k1KeyPair::generate();
let hash = [0u8; 32];
let signature = keypair.sign(&hash).expect("signing failed");

// Get the recoverable bytes
let recoverable = signature.to_recoverable_bytes();

// Reconstruct the signature
let bytes: [u8; 64] = recoverable[..64].try_into().unwrap();
let recovery_id = recoverable[64];
let reconstructed = Secp256k1Signature::from_bytes_and_recovery_id(bytes, recovery_id);

assert_eq!(signature.as_ref(), reconstructed.as_ref());
Source

pub const fn recovery_id(&self) -> u8

Get the recovery ID.

This is 0 or 1, which can be used with Ethereum’s ecrecover:

  • For EIP-155 transactions: v = recovery_id + 27
  • For EIP-2930/EIP-1559: v = recovery_id
Source

pub fn to_recoverable_bytes(&self) -> [u8; 65]

Return signature as 65 bytes: r (32) || s (32) || v (1).

The v byte is the raw recovery ID (0 or 1). For Ethereum transactions, you may need to add 27 or use chain-specific calculations for EIP-155.

Source

pub fn r(&self) -> &[u8; 32]

Get the r component of the signature (first 32 bytes).

This always succeeds because the signature is always exactly 64 bytes.

§Panics

This method cannot panic in practice because the internal storage is always exactly 64 bytes, but the conversion is technically fallible.

Source

pub fn s(&self) -> &[u8; 32]

Get the s component of the signature (last 32 bytes).

This always succeeds because the signature is always exactly 64 bytes.

§Panics

This method cannot panic in practice because the internal storage is always exactly 64 bytes, but the conversion is technically fallible.

Trait Implementations§

Source§

impl AsRef<[u8]> for Secp256k1Signature

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Secp256k1Signature

Source§

fn clone(&self) -> Secp256k1Signature

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Secp256k1Signature

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

👎Deprecated: use ToHexExt instead
Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca).
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

👎Deprecated: use ToHexExt instead
Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA).
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
Source§

impl<T> ToHexExt for T
where T: AsRef<[u8]>,

Source§

fn encode_hex(&self) -> String

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca).
Source§

fn encode_hex_upper(&self) -> String

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA).
Source§

fn encode_hex_with_prefix(&self) -> String

Encode the hex strict representing self into the result with prefix 0x. Lower case letters are used (e.g. 0xf9b4ca).
Source§

fn encode_hex_upper_with_prefix(&self) -> String

Encode the hex strict representing self into the result with prefix 0X. Upper case letters are used (e.g. 0xF9B4CA).
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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