Skip to content

Commit

Permalink
feat: support for creating accounts using mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade committed Mar 26, 2024
1 parent 5767044 commit 23ccfd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 11 additions & 5 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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
Expand All @@ -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"))
Expand Down
16 changes: 12 additions & 4 deletions operate/wallet/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23ccfd4

Please sign in to comment.