gleeth/crypto/secp256k1

Types

Represents an Ethereum address derived from a public key

pub type EthereumAddress {
  EthereumAddress(address: String)
}

Constructors

  • EthereumAddress(address: String)

Represents a private key for signing transactions

pub type PrivateKey {
  PrivateKey(key: BitArray)
}

Constructors

  • PrivateKey(key: BitArray)

Represents a public key derived from a private key

pub type PublicKey {
  PublicKey(key: BitArray)
}

Constructors

  • PublicKey(key: BitArray)

Represents an ECDSA signature with recovery information

pub type Signature {
  Signature(r: BitArray, s: BitArray, recovery_id: Int)
}

Constructors

  • Signature(r: BitArray, s: BitArray, recovery_id: Int)

Values

pub fn address_to_string(address: EthereumAddress) -> String

Extract the address string from an EthereumAddress

pub fn create_public_key(
  private_key: PrivateKey,
) -> Result(PublicKey, String)

Create a public key from a private key

pub fn find_recovery_id(
  message_hash: BitArray,
  r: BitArray,
  s: BitArray,
  expected_address: String,
) -> Result(Int, String)

Find the correct recovery ID for a given signature and expected address This is useful when you have r,s components but need to determine the recovery ID

pub fn generate_private_key() -> Result(PrivateKey, String)

Generate a random private key (placeholder - would need proper randomness) This is a placeholder implementation - in production, use proper cryptographic randomness

pub fn is_valid_private_key(private_key: PrivateKey) -> Bool

Check if a private key is valid (non-zero and within secp256k1 curve order)

pub fn private_key_from_bytes(
  bytes: BitArray,
) -> Result(PrivateKey, String)

Create a private key from a 32-byte BitArray The private key must be exactly 32 bytes (256 bits)

pub fn private_key_from_hex(
  hex_string: String,
) -> Result(PrivateKey, String)

Create a private key from a hex string (with or without 0x prefix) The hex string must represent exactly 32 bytes (64 hex characters)

pub fn private_key_to_address(
  private_key: PrivateKey,
) -> Result(EthereumAddress, String)

Generate an Ethereum address directly from a private key

pub fn private_key_to_bytes(private_key: PrivateKey) -> BitArray

Extract the raw bytes from a private key

pub fn private_key_to_hex(private_key: PrivateKey) -> String

Convert a private key to hex string with 0x prefix

pub fn public_key_to_address(
  public_key: PublicKey,
) -> Result(EthereumAddress, String)

Generate an Ethereum address from a public key Uses the last 20 bytes of keccak256(public_key) as the address

pub fn public_key_to_bytes(public_key: PublicKey) -> BitArray

Extract the raw bytes from a public key

pub fn public_key_to_hex(public_key: PublicKey) -> String

Convert a public key to hex string with 0x prefix

pub fn recover_address(
  message_hash: BitArray,
  signature: Signature,
) -> Result(EthereumAddress, String)

Recover Ethereum address directly from signature and message hash

pub fn recover_address_candidates(
  message_hash: BitArray,
  r: BitArray,
  s: BitArray,
) -> Result(List(EthereumAddress), String)

Recover multiple address candidates (all possible recovery IDs)

pub fn recover_address_compact(
  message_hash: BitArray,
  compact_signature: BitArray,
  recovery_id: Int,
) -> Result(EthereumAddress, String)

Recover address from compact signature format

pub fn recover_public_key(
  message_hash: BitArray,
  signature: Signature,
) -> Result(PublicKey, String)

Recover the public key from a signature and message hash

pub fn recover_public_key_candidates(
  message_hash: BitArray,
  r: BitArray,
  s: BitArray,
) -> Result(List(PublicKey), String)

Recover multiple public key candidates (all 4 possible recovery IDs) This is useful when the recovery ID is unknown or needs to be determined

pub fn recover_public_key_compact(
  message_hash: BitArray,
  compact_signature: BitArray,
  recovery_id: Int,
) -> Result(PublicKey, String)

Recover public key from compact signature format

pub fn sign_message(
  message: BitArray,
  private_key: PrivateKey,
) -> Result(Signature, String)

Sign raw message bytes (will be hashed with keccak256)

pub fn sign_message_hash(
  message_hash: BitArray,
  private_key: PrivateKey,
) -> Result(Signature, String)

Sign a message hash with a private key The message should already be hashed (e.g., with keccak256)

pub fn sign_personal_message(
  message: String,
  private_key: PrivateKey,
) -> Result(Signature, String)

Sign an Ethereum personal message (prefixed with “\x19Ethereum Signed Message:\n”)

pub fn signature_from_hex(
  hex_string: String,
) -> Result(Signature, String)

Parse a 65-byte hex signature string (r[32] + s[32] + v[1]) into a Signature. Handles v=0/1 and v=27/28 (normalizes to recovery_id 0/1).

pub fn signature_from_vrs(
  v: Int,
  r: String,
  s: String,
) -> Result(Signature, String)

Create signature from v, r, s components

pub fn signature_to_compact(signature: Signature) -> BitArray

Convert signature to compact format (r + s + v) Used for Ethereum transaction signatures

pub fn signature_to_hex(signature: Signature) -> String

Convert signature to hex string with 0x prefix

pub fn signature_to_vrs(
  signature: Signature,
) -> #(Int, String, String)

Extract v, r, s components for Ethereum transactions

pub fn verify_signature(
  message_hash: BitArray,
  signature: Signature,
  public_key: PublicKey,
) -> Result(Bool, String)

Verify a signature against a message hash and public key

pub fn verify_signature_recovery(
  message_hash: BitArray,
  signature: Signature,
  expected_address: String,
) -> Result(Bool, String)

Verify signature recovery by checking if recovered address matches expected

Search Document