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
impl Secp256k1Signature
Sourcepub const fn from_bytes_and_recovery_id(
bytes: [u8; 64],
recovery_id: u8,
) -> Self
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());Sourcepub const fn recovery_id(&self) -> u8
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
Sourcepub fn to_recoverable_bytes(&self) -> [u8; 65]
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.
Sourcepub fn r(&self) -> &[u8; 32]
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.
Sourcepub fn s(&self) -> &[u8; 32]
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
impl AsRef<[u8]> for Secp256k1Signature
Source§impl Clone for Secp256k1Signature
impl Clone for Secp256k1Signature
Source§fn clone(&self) -> Secp256k1Signature
fn clone(&self) -> Secp256k1Signature
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for Secp256k1Signature
impl RefUnwindSafe for Secp256k1Signature
impl Send for Secp256k1Signature
impl Sync for Secp256k1Signature
impl Unpin for Secp256k1Signature
impl UnwindSafe for Secp256k1Signature
Blanket Implementations§
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
ToHexExt insteadself into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
ToHexExt insteadself into the result.
Upper case letters are used (e.g. F9B4CA).Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)Source§impl<T> ToHexExt for T
impl<T> ToHexExt for T
Source§fn encode_hex(&self) -> String
fn encode_hex(&self) -> String
self into the result.
Lower case letters are used (e.g. f9b4ca).Source§fn encode_hex_upper(&self) -> String
fn encode_hex_upper(&self) -> String
self into the result.
Upper case letters are used (e.g. F9B4CA).Source§fn encode_hex_with_prefix(&self) -> String
fn encode_hex_with_prefix(&self) -> String
self into the result with prefix 0x.
Lower case letters are used (e.g. 0xf9b4ca).Source§fn encode_hex_upper_with_prefix(&self) -> String
fn encode_hex_upper_with_prefix(&self) -> String
self into the result with prefix 0X.
Upper case letters are used (e.g. 0xF9B4CA).