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.
- 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.
- 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_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).
- 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¶
- property supported_assets: List[str]¶
- 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¶
- 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]¶
- 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¶
- abstract property supported_assets: List[str]¶
- tokens: Dict[str, dict]¶
- 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.
- 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]¶
- 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).