From 23ccfd415278a7e49c3a9bd77a07490c03555e41 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Tue, 26 Mar 2024 17:57:34 +0530 Subject: [PATCH] feat: support for creating accounts using mnemonic --- operate/cli.py | 16 +++++++++++----- operate/wallet/master.py | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/operate/cli.py b/operate/cli.py index 6394fef3e..a350cf35c 100644 --- a/operate/cli.py +++ b/operate/cli.py @@ -95,7 +95,7 @@ def master_wallet_manager(self) -> MasterWalletManager: password=self.password, ) manager.setup() - return JSONResponse(content=manager) + return manager def setup(self) -> None: """Make the root directory.""" @@ -230,7 +230,7 @@ async def _validate_password(request: Request) -> t.Dict: operate.password = data["password"] return JSONResponse( - content={"error": "Login successful"}, + content={"message": "Login successful"}, status_code=200, ) @@ -264,8 +264,14 @@ async def _create_wallet(request: Request) -> t.List[t.Dict]: ledger_type = get_ledger_type_from_chain_type(chain=chain_type) manager = operate.master_wallet_manager if manager.exists(ledger_type=ledger_type): - return JSONResponse(content=manager.load(ledger_type=ledger_type).json) - return JSONResponse(content=manager.create(ledger_type=ledger_type).json) + return JSONResponse( + content={ + "wallet": manager.load(ledger_type=ledger_type).json, + "mnemonic": None, + } + ) + wallet, mnemonic = manager.create(ledger_type=ledger_type) + return JSONResponse(content={"wallet": wallet.json, "mnemonic": mnemonic}) @app.put("/api/wallet") @with_retries @@ -288,7 +294,7 @@ async def _create_wallet(request: Request) -> t.List[t.Dict]: ledger_type = get_ledger_type_from_chain_type(chain=chain_type) manager = operate.master_wallet_manager if not manager.exists(ledger_type=ledger_type): - return JSONResponse(content=wallet) + return JSONResponse(content={"error": "Wallet does not exist"}) wallet = manager.load(ledger_type=ledger_type) wallet.create_safe(chain_type=chain_type, owner=data.get("owner")) diff --git a/operate/wallet/master.py b/operate/wallet/master.py index f4f067ac4..5be72559e 100644 --- a/operate/wallet/master.py +++ b/operate/wallet/master.py @@ -6,6 +6,8 @@ from aea.crypto.registries import make_crypto, make_ledger_api from aea_ledger_ethereum.ethereum import EthereumCrypto from autonomy.chain.base import registry_contracts +from mnemonic import Mnemonic +from web3 import Account from operate.ledger import get_default_rpc, get_ledger_type_from_chain_type from operate.resource import LocalResource @@ -56,7 +58,7 @@ def ledger_api( ) @staticmethod - def new(password: str, path: Path) -> "MasterWallet": + def new(password: str, path: Path) -> t.Tuple["MasterWallet", t.List[str]]: """Create a new master wallet.""" raise NotImplementedError() @@ -94,16 +96,22 @@ def get_crypto_obj(self, password: str) -> EthereumCrypto: ) @classmethod - def new(cls, password: str, path: Path) -> "EthereumMasterWallet": + def new( + cls, password: str, path: Path + ) -> t.Tuple["EthereumMasterWallet", t.List[str]]: """Create a new master wallet.""" + # Enable support for creating account using mnemonics + Account.enable_unaudited_hdwallet_features() + # Create crypto object and store using password crypto = make_crypto("ethereum") + crypto._entity, mnemonic = Account.create_with_mnemonic() crypto.dump(private_key_file=path / cls._key, password=password) # Create wallet wallet = EthereumMasterWallet(path=path, address=crypto.address, safe_chains=[]) wallet.store() - return wallet + return wallet, mnemonic.split() def create_safe( self, @@ -164,7 +172,7 @@ def setup(self) -> "MasterWalletManager": self.path.mkdir(exist_ok=True) return self - def create(self, ledger_type: LedgerType) -> MasterWallet: + def create(self, ledger_type: LedgerType) -> t.Tuple[MasterWallet, t.List[str]]: """ Create a master wallet