eth-account

repository·main·Indexed 43 days ago

https://github.com/apeworx/eth-account

A Python library for signing Ethereum transactions and messages locally using private keys. It provides primitives for account management, cryptographic signing, and support for Typed Transactions including EIP-1559 (Dynamic Fee), Access List, and EIP-4844 (Blob) transactions. Features include private key encryption/decryption, EIP-712 typed data signing, EIP-7702 authorization signing, and experimental BIP39 mnemonic support.

Tokens
8.1K
Snippets
31
Records
41
Agent score
55%

What's inside eth-account

  1. Overview of eth-account

    main
    eth-account is a Python library designed to sign Ethereum transactions and messages using local private keys. It provides the necessary primitives for managing accounts and cryptographic signing operations locally without requiring a connection to a live node for the signing process itself.
  2. Use Local Signers for private key abstraction

    main
    A Local Signer is currently the primary implementation for abstracting a private key. It allows you to perform signing operations without manually handling the raw private key in every function call. While currently limited to local key management, the architecture is designed to support future implementations like Hierarchical Deterministic (HD) wallets and hardware wallets.
  3. Understand the difference between Account and Signer

    main

    In eth-account, there is a distinction between the Account class and Signer classes:

    • Account: Requires the private key to be explicitly provided on each usage.
    • Signers: Abstract away the private key. They manage the key internally, so you don't have to pass it around manually for every operation.

    All signer classes in this package implement the interface defined by BaseAccount.

  4. Use Typed Transactions in eth-account

    main
    The eth-account library provides specialized classes for different Ethereum transaction types, known as Typed Transactions. Instead of manually constructing raw transaction dictionaries, you can use these classes to ensure the correct structure and parameters for specific EIPs (Ethereum Improvement Proposals).
  5. What is a SignableMessage?

    main

    A SignableMessage is a NamedTuple representing a message compatible with the EIP-191 standard, prepared for signing. It consists of three components:

    • version: A single byte representing the message version.
    • header: Version-specific data (e.g., a validator address or a domain hash).
    • body: The actual data to be signed.

    In typical usage, you should not create these manually. Instead, use the encoding utility functions like encode_intended_validator, encode_defunct, or encode_typed_data to generate them.

    from eth_account.messages import SignableMessage
    
    # SignableMessage is a NamedTuple with:
    # version: bytes
    # header: bytes
    # body: bytes
  6. Supported Private Key Types for signing methods

    main

    The signing methods in Account (such as sign_typed_data and sign_authorization) accept several types for the private_key parameter:

    • hex str (e.g., "0x...")
    • bytes
    • int
    • eth_keys.datatypes.PrivateKey objects

    If a hex string or bytes are provided, they are parsed into a PrivateKey object. The key must be exactly 32 bytes long.

  7. Create a new account with Account.create()

    main

    Use Account.create() to generate a new private key and return a LocalAccount object. You can provide extra_entropy (str, bytes, or int) to add additional randomness to the key generation process.

    from eth_account import Account
    
    # Create a new account with optional extra entropy
    acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
    print(acct.address)
    print(acct.key)