Keyring Accounts

Accounts contain methods for deriving keys, etc. Besides the default dag_account and eth_account modules, the accounts sub-package also contains an EcdsaAccount abstract class (Pydantic model). All accounts are based on the abstract class EcdsaAccount. New custom account classes must inherit from this base model.

Add Custom Account to Registry

from pypergraph.keyring import account_registry, MultiAccountWallet()
import CustomAccount # Your custom account that inherits from pypergraph.keyring.accounts import EcdsaAccount

 account_registry.add_account("Custom", CustomAccount)

 wallet = MultiAccountWallet()
 wallet.create(network="Custom", label="New Custom", mnemonic="abandon abandon abandon ...", num_of_accounts=3)

 accounts = wallet.get_accounts() # Returns a list of EcdsaAccounts

The accounts sub-package contains the asset libraries DagAssetLibrary and EthAssetLibrary (not fully implemented yet). These classes inherit from the abstract class AssetLibrary, which can be used to construct custom asset libraries, for additional token support.

Add Custom Asset Library to Custom Account

from pypergraph.keyring import KeyringAssetInfo

import CustomAccount
import CustomAssetLibrary

custom_asset_library = CustomAssetLibrary()

token = KeyringAssetInfo(
    id='0xa000000000000000000000000000000000000000',
    address='0xa000000000000000000000000000000000000000',
    label='Custom Token',
    symbol='CUS',
    network='testnet',
    decimals=8
)

custom_asset_library.import_token(token)

custom_account = CustomAccount()

# Add the imported custom token to the custom account
custom_account.set_tokens(custom_asset_library.serialize())

The above methods are not restricted to custom object, see below.


class pypergraph.keyring.accounts.AssetLibrary[source]

Bases: ABC

abstract property default_assets: List[str]

Returns a list of default asset symbols.

abstract property default_assets_map: Dict[str, KeyringAssetInfo]

Returns the default asset map (i.e., a dict mapping symbols to asset info).

deserialize(assets: Dict[str, KeyringAssetInfo]) None[source]

Loads a previously saved asset map.

get_asset_by_symbol(symbol: str) KeyringAssetInfo | None[source]

Looks up an asset by symbol. First checks the default assets, then any imported tokens.

get_default_assets() List[str][source]

Returns a copy of the default asset symbols.

import_token(token: KeyringAssetInfo) bool[source]

Imports a new token. Returns True if the token was added (i.e. did not exist before), otherwise returns False.

serialize() Dict[str, dict][source]

Serializes (or exports) the imported assets for saving state.

class pypergraph.keyring.accounts.DagAccount(*, tokens: ~typing.Dict[str, dict] = <factory>, wallet: ~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey | None = None, assets: ~typing.List[~typing.Any] = <factory>, bip44_index: int | None = None, provider: ~typing.Any = None, label: str | None = None)[source]

Bases: EcdsaAccount

property decimals: int
get_address() str[source]
get_address_from_public_key(public_key_hex: str) str[source]
Parameters:

public_key_hex – The private key as a hexadecimal string.

Returns:

The DAG address corresponding to the public key (node ID).

get_public_key() str[source]
property has_token_support: bool
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

property network_id: str
static sha256(data: bytes) str[source]
property supported_assets: List[str]
static validate_address(address: str) bool[source]
verify_message(msg: str, signature: str, says_address: str) bool[source]
class pypergraph.keyring.accounts.DagAssetLibrary[source]

Bases: AssetLibrary

property default_assets: List[str]

Returns a list of default asset symbols.

property default_assets_map: Dict[str, KeyringAssetInfo]

Returns the default asset map (i.e., a dict mapping symbols to asset info).

class pypergraph.keyring.accounts.EcdsaAccount(*, tokens: ~typing.Dict[str, dict] = <factory>, wallet: ~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey | None = None, assets: ~typing.List[~typing.Any] = <factory>, bip44_index: int | None = None, provider: ~typing.Any = None, label: str | None = None)[source]

Bases: BaseModel, ABC

assets: List[Any]
bip44_index: int | None
create(private_key: str | None)[source]
abstract property decimals: int
deserialize(bip44_index: int | None = None, label: str | None = None, private_key: str | None = None, public_key: str | None = None, tokens: List[str] | None = None)[source]
get_address() str[source]
get_bip44_index() int | None[source]
get_decimals() int[source]
get_label() str[source]
get_network_id()[source]
get_private_key() str[source]
get_private_key_buffer()[source]
get_public_key() str[source]
get_state() Dict[str, Any][source]
get_tokens() List[str] | None[source]
get_web3_provider()[source]
abstract property has_token_support: bool
label: str | None
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

abstract property network_id: str
provider: Any
recover_signed_msg_public_key(msg: str, signature: str) str[source]
save_token_info(address: str)[source]
serialize(include_private_key: bool = True) Dict[str, Any][source]
set_tokens(tokens: Dict[str, dict])[source]
set_web3_provider(provider)[source]
abstract property supported_assets: List[str]
tokens: Dict[str, dict]
abstractmethod verify_message(msg: str, signature: str, says_address: str) bool[source]
wallet: EllipticCurvePrivateKey | None
class pypergraph.keyring.accounts.EthAccount(*, tokens: ~typing.List[str] = ['0xa393473d64d2F9F026B60b6Df7859A689715d092'], wallet: ~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey | None = None, assets: ~typing.List[~typing.Any] = <factory>, bip44_index: int | None = None, provider: ~typing.Any = None, label: str | None = None)[source]

Bases: EcdsaAccount

property decimals: int
get_address_from_public_key(public_key: str) str[source]

Derive the Ethereum address from the public key.

get_encryption_public_key() str[source]

Get the public key for encryption.

property has_token_support: bool
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

property network_id: str
save_token_info(address: str)[source]

Save the token info if not already present in the tokens list.

sign_transaction(tx)[source]

Sign an Ethereum transaction with the account’s private key.

tx is an instance of the transaction object from a library like web3.eth.account.

property supported_assets: List[str]
tokens: List[str]
static validate_address(address: str) bool[source]

Validate an Ethereum address.

verify_message(msg: str, signature: str, says_address: str) bool[source]

Verify if a signed message matches the provided address.

class pypergraph.keyring.accounts.EthAssetLibrary[source]

Bases: AssetLibrary

property default_assets: List[str]

Returns a list of default asset symbols.

property default_assets_map: Dict[str, KeyringAssetInfo]

Returns the default asset map (i.e., a dict mapping symbols to asset info).