[docs]defcreate(self,mnemonic:str,hd_path:str,network:str,number_of_accounts:int=1)->Self:""" Create a hierarchical deterministic keyring. :param mnemonic: Mnemonic phrase. :param hd_path: The derivation path for the coin chain (without index). :param network: The network associated with the coin. :param number_of_accounts: How many accounts (indexes) to create. :return: Hierarchical deterministic keyring. """bip39=Bip39Helper()bip32=Bip32Helper()self.network=networkinst=HdKeyring()inst.mnemonic=mnemonicinst.hd_path=hd_path# Init from mnemonicseed_bytes=bip39.get_seed_bytes_from_mnemonic(mnemonic=inst.mnemonic)inst.root_key=bip32.get_hd_root_key_from_seed(seed_bytes=seed_bytes,hd_path=inst.hd_path)# Needs to handle indexesaccounts=inst.create_accounts(number_of_accounts=number_of_accounts)inst.deserialize({"network":network,"accounts":accounts})returninst
[docs]defcreate_accounts(self,number_of_accounts:int=0)->List[Dict]:""" When adding an account (after accounts have been removed), it will add back the ones removed first. :param number_of_accounts: The number of accounts to create. :returns List[dict]: A list of dictionaries with bip44 index. """accounts=[]foriinrange(number_of_accounts):accounts.append({"bip44_index":i})returnaccounts
[docs]defdeserialize(self,data:dict):""" Deserialize then add account (bip44_index) to the keyring being constructed. :param data: """ifdata:self.network=data.get("network")self.accounts=[]fori,dinenumerate(data.get("accounts")):account=self.add_account_at(d.get("bip44_index"))account.set_tokens(d.get("tokens"))self.accounts.append(account)
[docs]defadd_account_at(self,index:int=0)->Union[DagAccount,EthAccount,EcdsaAccount]:""" Add account class object with a signing key to the keyring being constructed. :param index: Account number (bipIndex). :return: EcdsaAccount or DagAccount class object (dag_keyring.accounts) with signing key at self.wallet. """index=indexifindex>=0elselen(self.accounts)ifself.mnemonic:private_key=self.root_key.ChildKey(index).PrivateKey().hex()account=account_registry.create_account(self.network)account=account.deserialize(private_key=private_key,bip44_index=index)else:public_key=self.root_key.ChildKey(index).PublicKey()account=account_registry.create_account(self.network)account=account.deserialize(public_key=public_key,bip44_index=index)# self.accounts.append(account)returnaccount
[docs]defget_account_by_address(self,address:str)->Union[DagAccount,EthAccount]:# account is IKeyringAccountreturnnext((accforaccinself.accountsifacc.get_address().lower()==address.lower()),None,)
[docs]defremove_account(self,account):# account is IKeyringAccountself.accounts=[accforaccinself.accountsifacc!=account]# orig. == account