API Reference

miniethers

Circuitpython module for ethereum wallet creation and signing

  • Author(s): Shamba Chowdhury

Implementation Notes

Software and Dependencies:

class circuitpython_miniethers.miniethers.Signature(r=None, s=None, v=None)

ECDSA signature representation compatible with ethers.js Signature class.

Represents an Elliptic Curve Digital Signature Algorithm (ECDSA) signature with components r, s, and v. Provides methods for parsing and serializing signatures in various formats.

Variables:
  • r (str) – The r component of the signature (hex string with 0x prefix)

  • s (str) – The s component of the signature (hex string with 0x prefix)

  • v (int) – The recovery parameter (27 or 28)

Note

Compatible with ethers.js v6 Signature format.

Initialize a Signature instance.

Parameters:
  • r (str or int or None) – The r value as hex string or integer (optional)

  • s (str or int or None) – The s value as hex string or integer (optional)

  • v (int or None) – The recovery parameter, typically 27 or 28 (optional)

# Create from components
sig = Signature(r="0x123...", s="0xabc...", v=27)

# Create empty signature
sig = Signature()
from()

Create a Signature from various input formats.

Parses and creates a Signature instance from compact signature strings, dictionaries, or existing Signature instances.

Parameters:

sig (str or dict or Signature) – Signature in various formats

Returns:

A new Signature instance

Return type:

Signature

Raises:

ValueError – If signature format is invalid

# From compact signature string
sig = Signature.from_sig("0xrrr...sss...vv")

# From dictionary
sig = Signature.from_sig({"r": "0x...", "s": "0x...", "v": 27})

# Via getattr (to handle 'from' keyword)
sig = getattr(Signature, 'from')("0xrrr...sss...vv")

Note

This method is also available as Signature.from() via setattr, but requires getattr(Signature, 'from') due to Python keyword restrictions.

static from_sig(sig)

Create a Signature from various input formats.

Parses and creates a Signature instance from compact signature strings, dictionaries, or existing Signature instances.

Parameters:

sig (str or dict or Signature) – Signature in various formats

Returns:

A new Signature instance

Return type:

Signature

Raises:

ValueError – If signature format is invalid

# From compact signature string
sig = Signature.from_sig("0xrrr...sss...vv")

# From dictionary
sig = Signature.from_sig({"r": "0x...", "s": "0x...", "v": 27})

# Via getattr (to handle 'from' keyword)
sig = getattr(Signature, 'from')("0xrrr...sss...vv")

Note

This method is also available as Signature.from() via setattr, but requires getattr(Signature, 'from') due to Python keyword restrictions.

property serialized

Compact serialized signature in hex format.

Returns the signature in compact format: r (64 chars) + s (64 chars) + v (2 chars) with 0x prefix, totaling 132 characters.

Returns:

Compact signature string (0xrrr…sss…vv)

Return type:

str

sig = Signature(r="0x123...", s="0xabc...", v=27)
print(sig.serialized)  # "0x123...abc...1b"
property yParity

The y-parity of the signature point.

Returns:

0 if v is 27, otherwise 1

Return type:

int

Note

yParity is used in EIP-2718 typed transactions.

class circuitpython_miniethers.miniethers.Wallet(private_key=None)

Ethereum wallet for signing messages and transactions.

Provides a high-level interface for Ethereum wallet operations including private key management, address derivation, and message signing compatible with ethers.js patterns. Optimized for CircuitPython and low-power devices.

Variables:
  • address (str) – Ethereum address (read-only property)

  • privateKey (str) – Private key as hex string (read-only property)

  • publicKey (str) – Uncompressed public key as hex string (read-only property)

# Create wallet from private key
wallet = Wallet("0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")

# Access properties
print(wallet.address)      # "0x..."
print(wallet.privateKey)   # "0x..."

# Sign a message
signature = wallet.signMessage("hello")

Note

Compatible with ethers.js v6 Wallet API patterns.

Initialize wallet with optional private key.

Parameters:

private_key (str or int or None) – Private key as hex string (with or without 0x prefix), integer, or None to generate random key

Raises:

ValueError – If private key is out of valid range

# From hex string (with 0x)
wallet = Wallet("0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")

# From hex string (without 0x)
wallet = Wallet("022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")

# From integer
wallet = Wallet(0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc)

# Generate random wallet
wallet = Wallet()
property address

Ethereum address derived from public key.

Returns the Ethereum address computed from the wallet’s public key using Keccak-256 hashing. The address is cached after first computation.

Returns:

Ethereum address with 0x prefix (42 characters)

Return type:

str

wallet = Wallet("0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")
print(wallet.address)  # "0x14791697260E4c9A71f18484C9f997B308e59325"
get_address()

Get Ethereum address (legacy method).

Deprecated since version Use: the address property instead.

Returns:

Ethereum address with 0x prefix

Return type:

str

get_private_key()

Get private key as integer (legacy method).

Deprecated since version Use: the privateKey property for hex string representation.

Returns:

Private key as integer

Return type:

int

get_public_key()

Get public key coordinates (legacy method).

Deprecated since version Use: the publicKey property for hex string representation.

Returns:

Tuple of (x, y) coordinates as integers

Return type:

tuple

property privateKey

Private key as hexadecimal string.

Returns:

Private key with 0x prefix (66 characters)

Return type:

str

Warning

Never expose private keys in production environments.

property publicKey

Uncompressed public key in hex format.

Returns the uncompressed public key (x and y coordinates concatenated) with 0x04 prefix following SEC1 encoding.

Returns:

Uncompressed public key with 0x04 prefix (132 characters)

Return type:

str

wallet = Wallet("0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")
print(wallet.publicKey)  # "0x04..."
signMessage(message)

Sign a personal message using ERC-191 standard.

Automatically prepends the Ethereum message prefix before signing. Compatible with ethers.js wallet.signMessage() method.

Parameters:

message (str or bytes) – Message to sign (string or bytes)

Returns:

Compact signature string (130 hex chars + 0x prefix)

Return type:

str

wallet = Wallet("0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc")
signature = wallet.signMessage("hello")
print(signature)  # "0xrrr...sss...vv"

# Parse signature
sig = getattr(Signature, 'from')(signature)
print(sig.r, sig.s, sig.v)

See also

ERC-191 specification.

verify_signature(message_hash, r, s)

Verify an ECDSA signature against the wallet’s public key.

Parameters:
  • message_hash (int) – Hash of the message that was signed

  • r (int) – Signature r component

  • s (int) – Signature s component

Returns:

True if signature is valid, False otherwise

Return type:

bool