Skip to main content

A thin framework to help applications and users manage multiple ethereum accounts on multiple chains.

Project description

Chained Accounts

CI

A thin framework to help applications and users manage multiple ethereum accounts on multiple chains.

Motivation

Blockchain applications are becoming more cross-chain and multi-chain, therefore having a need to manage multiple user accounts on different chains. Users need an easy way to configure an application to use their private keys, including specifying which EVM chains they can be used on. Applications also need easy access to user private keys while providing a way to handle them securely, rather than storing them in plain text.

Overview

Each ChainedAccount:

  • has a user-friendly name
  • can be associated with one or more EVM chains.
  • is encrypted in a local keystore using the eth_keyfile package.

Applications can easily access the keystore and search for accounts by name, EVM chain, and address.

ChainedAccount objects remain encrypted by default, and can be lazily unlocked and cached when access is needed to the private key.

Note: This package does not directly perform any encryption/decryption, but relies on the https://github.com/ethereum/eth-account package.

** USE AT YOUR OWN RISK **

Installation

pip install chained_accounts

Examples

Create an account for use on either Ethereum Mainnet or Rinkeby testnet.

Python

from chained_accounts import ChainedAccount, find_accounts

key1 = "0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706"
ChainedAccount.add("my-eth-acct", [1, 4], key1, password="foo")

acc = find_accounts(chain_id=1)[0]
print(f"Address: {acc.address}")
print(f"Chains: {acc.chains}")

acc.unlock("foo")
print(f"Private key: {acc.key.hex()}")
ChainedAccount('my-eth-acct')
Address: 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0
Chains: [1, 4]
Private key: 0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706

Command Line

>> chained add my-eth-acct 0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706 1 4
Enter encryption password for my-eth-acct: 
Confirm password:
Added new account my-eth-acct (address= 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0) for use on chains (1, 4)
>> chained find
Found 1 accounts.
Account name: my-eth-acct, address: 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0, chain IDs: [1, 4]

User Guide

All ChainedAccount features are available through Python or the Command Line Interface (CLI).

Adding a new account

The following example demonstrates adding two accounts to the keystore. The first account is for use on either ethereum mainnet or Rinkeby testnet. The second account is for use on Polygon mainnet. For a list of valid chain identifiers, see www.chainlist.org.

from chained_accounts import ChainedAccount

key = '0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706'
ChainedAccount.add('my-eth-acct', chains=[1, 4], key=key, password='foo')

key = '0x7a3d4adc3b6fb4520893e9b486b67a730e0d879a421342f788dc3dc273543267'
ChainedAccount.add('my-matic-acct', chains=137, key=key, password='bar')

or, from the CLI:

>> chained add my-eth-acct 0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706 1 4
Enter encryption password for my-eth-acct: 
Confirm password:
Added new account my-eth-acct (address= 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0) for use on chains (1, 4)

>> chained add my-matic-acct 0x7a3d4adc3b6fb4520893e9b486b67a730e0d879a421342f788dc3dc273543267 137
Enter encryption password for my-matic-acct: 
Confirm password: 
Added new account my-matic-acct (address= 0xc1b6c5d803c45b8d1097d07df0c816157db6f00c) for use on chains (137,)

Getting accounts from the keystore

Accounts can be accessed by name using ChainedAccount.get(name), or can be found by searching the keystore using find_accounts(). The following example demonstrates how an application can search for a user account to use on Ethereum mainnet.

acc = find_accounts(chain_id=1)[0]
print(f"Address: {acc.address}")
print(f"Chains: {acc.chains}")
ChainedAccount('my-eth-acct')
Address: 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0
Chains: [1, 4]

Or, from the command line (find all accounts):

>> chained find
Found 2 accounts.
Account name: my-eth-acct, address: 0xcd19cf65af3a3aea1f44a7cb0257fc7455f245f0, chain IDs: [1, 4]
Account name: my-matic-acct, address: 0xc1b6c5d803c45b8d1097d07df0c816157db6f00c, chain IDs: [137]

Accessing private keys

Note that the ChainedAccount private key remains encrypted until the account is unlocked with a password.

assert not ChainedAccount.get('my-eth-acct').is_unlocked

To unlock and access the private key:

acc = ChainedAccount.get('my-eth-acct')
acc.unlock('foo')
print(f"Private key: {acc.key.hex()}")
Private key: 0x57fe7105302229455bcfd58a8b531b532d7a2bb3b50e1026afa455cd332bf706

Development

Developer Mode Installation

pip install -e ".[dev]"

Running tests

pytest

Code checks

All code should pass the following checks prior to submitting.

ruff check src/ tests/
ruff format --check src/ tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chained_accounts-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chained_accounts-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file chained_accounts-0.1.0.tar.gz.

File metadata

  • Download URL: chained_accounts-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for chained_accounts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 544355e47eee4ac4e4ffd2969da249b13d31b27697b1bce78606dcd0d5b7c16a
MD5 c41234d151ece183339873102ea6bb60
BLAKE2b-256 abe8e8218c07ece1d21ccb21f2f0723b0ef7cf0ef6ae1968d2538ab3baa51d76

See more details on using hashes here.

Provenance

The following attestation bundles were made for chained_accounts-0.1.0.tar.gz:

Publisher: release.yml on pydefi/chained-accounts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chained_accounts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for chained_accounts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e94c3119db86b867626d2a07cebac174bbe1056b5eee1be3d7d6294dc32a2850
MD5 862235d328478f810e670bf921fbd175
BLAKE2b-256 5a93cc4159b392a036c9a2c50265912341c0244984a5b61bf2ce12db208e393f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chained_accounts-0.1.0-py3-none-any.whl:

Publisher: release.yml on pydefi/chained-accounts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page