Skip to main content

EthereumSigner

Struct EthereumSigner 

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

Ethereum ECDSA signer.

Wraps a secp256k1 SigningKey and applies Keccak-256 hashing, EIP-2 Low-S normalization, and recovery ID calculation.

Implementations§

Source§

impl EthereumSigner

Source

pub fn address(&self) -> [u8; 20]

Derive the Ethereum address from this signer’s public key. Returns the 20-byte address (last 20 bytes of keccak256(uncompressed_pubkey[1..])).

Source

pub fn sign_typed_data( &self, domain_separator: &[u8; 32], struct_hash: &[u8; 32], ) -> Result<EthereumSignature, SignerError>

EIP-712: Sign typed structured data.

Computes keccak256("\x19\x01" || domain_separator || struct_hash) and signs it.

  • domain_separator: 32-byte keccak256 hash of the EIP-712 domain (see Eip712Domain::separator).
  • struct_hash: 32-byte keccak256 hash of the typed struct (computed by the caller).
Source

pub fn personal_sign( &self, message: &[u8], ) -> Result<EthereumSignature, SignerError>

EIP-191: Sign a personal message (as used by MetaMask personal_sign).

Computes keccak256("\x19Ethereum Signed Message:\n{len}{message}") and signs it. This is the standard for off-chain message signing in Ethereum wallets.

Source

pub fn sign_with_chain_id( &self, message: &[u8], chain_id: u64, ) -> Result<EthereumSignature, SignerError>

EIP-155: Sign a message with chain-specific replay protection.

Produces v = {0,1} + chain_id * 2 + 35 instead of v = 27/28. This is required for mainnet Ethereum transactions since the Spurious Dragon fork.

Common chain IDs: 1 (mainnet), 5 (Goerli), 11155111 (Sepolia), 137 (Polygon).

Source

pub fn sign_digest_with_chain_id( &self, digest: &[u8; 32], chain_id: u64, ) -> Result<EthereumSignature, SignerError>

EIP-155: Sign a pre-hashed digest with chain-specific replay protection.

Source

pub fn personal_sign_with_chain_id( &self, message: &[u8], chain_id: u64, ) -> Result<EthereumSignature, SignerError>

EIP-191: Sign a personal message with chain-specific v value.

Source

pub fn address_checksum(&self) -> String

Return the EIP-55 checksummed hex address string (e.g., 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B).

Source

pub fn from_mnemonic( phrase: &str, passphrase: &str, index: u32, ) -> Result<Self, SignerError>

Create an EthereumSigner from a BIP-39 mnemonic phrase.

Uses the standard Ethereum HD derivation path m/44'/60'/0'/0/{index}.

§Arguments
  • phrase — BIP-39 mnemonic words (12 or 24 words)
  • passphrase — Optional BIP-39 passphrase (empty string for none)
  • index — Account index (0 for the first account)
§Example
use chains_sdk::ethereum::EthereumSigner;

let signer = EthereumSigner::from_mnemonic(
    "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
    "",
    0,
).unwrap();
println!("Address: {}", signer.address_checksum());

Trait Implementations§

Source§

impl Drop for EthereumSigner

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl KeyPair for EthereumSigner

Source§

fn generate() -> Result<Self, SignerError>

Generate a new random key pair using OS entropy (CSPRNG).
Source§

fn from_bytes(private_key: &[u8]) -> Result<Self, SignerError>

Reconstruct a key pair from raw private key bytes (32 bytes).
Source§

fn private_key_bytes(&self) -> Zeroizing<Vec<u8>>

Export the private key as auto-zeroizing bytes. The returned Zeroizing<Vec<u8>> will scrub the memory on drop.
Source§

fn from_keypair_bytes(keypair: &[u8]) -> Result<Self, <Self as Signer>::Error>
where Self: Sized,

Reconstruct a key pair from a 64-byte expanded keypair (seed ∥ pubkey). Default impl uses only the first 32 bytes (seed).
Source§

fn keypair_bytes(&self) -> Zeroizing<Vec<u8>>

Export the full keypair as private_key ∥ public_key. Default: 32B seed + compressed pubkey.
Source§

impl Signer for EthereumSigner

Source§

type Signature = EthereumSignature

The signature type produced by this signer.
Source§

type Error = SignerError

The error type returned on failure.
Source§

fn sign(&self, message: &[u8]) -> Result<EthereumSignature, SignerError>

Sign a raw message. The implementation applies chain-specific hashing internally (e.g., Keccak-256 for Ethereum, double-SHA-256 for Bitcoin).
Source§

fn sign_prehashed( &self, digest: &[u8], ) -> Result<EthereumSignature, SignerError>

Sign a pre-hashed digest directly. The caller is responsible for applying the correct hash function. Returns InvalidHashLength if the digest length doesn’t match the expected hash output size.
Source§

fn public_key_bytes(&self) -> Vec<u8>

Return the public key as bytes (compressed format where applicable). Read more
Source§

fn public_key_bytes_uncompressed(&self) -> Vec<u8>

Return the public key in uncompressed format. 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.