X402.Signer.LocalKey (X402 v0.6.0)

Copy Markdown View Source

X402.Signer backed by a raw secp256k1 private key held in memory.

Requires the optional ex_secp256k1 and ex_keccak dependencies; new/1 returns {:error, :missing_dependency} when they are unavailable.

The struct redacts the private key when inspected, but the key still lives in process memory — prefer an external signer implementation (KMS, hardware wallet) for production payers holding significant funds.

Examples

{:ok, signer} = X402.Signer.LocalKey.new("0x" <> String.duplicate("11", 32))
{:ok, address} = X402.Signer.LocalKey.address(signer)

Summary

Types

t()

A local secp256k1 signing key with its derived EVM address.

Functions

Returns the EVM address derived from the private key.

Builds a signer from a 32-byte secp256k1 private key.

Signs the precomputed EIP-712 digest with the local key.

Types

t()

@type t() :: %X402.Signer.LocalKey{address: String.t(), private_key: binary()}

A local secp256k1 signing key with its derived EVM address.

Functions

address(local_key)

(since 0.6.0)
@spec address(t()) :: {:ok, String.t()}

Returns the EVM address derived from the private key.

new(private_key)

(since 0.6.0)
@spec new(binary()) ::
  {:ok, t()} | {:error, :invalid_private_key | :missing_dependency}

Builds a signer from a 32-byte secp256k1 private key.

Accepts the raw 32-byte binary or its hex encoding (with or without a leading 0x). The EVM address is derived once at construction.

Returns {:error, :invalid_private_key} for malformed keys and {:error, :missing_dependency} when the optional ex_secp256k1 / ex_keccak dependencies are unavailable.

Examples

iex> X402.Signer.LocalKey.new("not hex")
{:error, :invalid_private_key}

iex> {:ok, signer} = X402.Signer.LocalKey.new("0x" <> String.duplicate("11", 32))
iex> signer.address
"0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a"

sign_eip712(local_key, digest, typed_data)

(since 0.6.0)
@spec sign_eip712(t(), binary(), X402.Signer.typed_data()) ::
  {:ok, X402.Signer.signature()} | {:error, term()}

Signs the precomputed EIP-712 digest with the local key.

The typed data is ignored — a local key can sign the digest directly.