1- from typing import Union
1+ from typing import TYPE_CHECKING , Union
22
33import click
4- from ape import accounts
5- from ape .cli import (
6- ape_cli_context ,
7- existing_alias_argument ,
8- network_option ,
9- non_existing_alias_argument ,
10- skip_confirmation_option ,
11- )
12- from eth_account import Account
13- from eth_account .messages import encode_defunct
4+ from ape .cli .arguments import existing_alias_argument , non_existing_alias_argument
5+ from ape .cli .options import ape_cli_context , network_option , skip_confirmation_option
6+
7+ if TYPE_CHECKING :
8+ # NOTE: Type-checking only imports so CLI help loads faster.
9+ from ape .api import AccountAPI
10+ from ape_ledger .accounts import LedgerAccount
11+ from ape_ledger .hdpath import HDAccountPath , HDBasePath
1412
15- from ape_ledger .accounts import LedgerAccount
16- from ape_ledger .choices import AddressPromptChoice
1713from ape_ledger .exceptions import LedgerSigningError
18- from ape_ledger .hdpath import HDAccountPath , HDBasePath
1914
2015
21- def _select_account (hd_path : Union [HDBasePath , str ]) -> tuple [str , HDAccountPath ]:
16+ def _select_account (hd_path : Union ["HDBasePath" , str ]) -> tuple [str , "HDAccountPath" ]:
17+ # NOTE: Lazy import so CLI help loads faster.
18+ from ape_ledger .choices import AddressPromptChoice
19+
2220 choices = AddressPromptChoice (hd_path )
2321 return choices .get_user_selected_account ()
2422
@@ -52,8 +50,18 @@ def _list(cli_ctx):
5250 click .echo (f" { account .address } { alias_display } { hd_path_display } " )
5351
5452
55- def _get_ledger_accounts () -> list [LedgerAccount ]:
56- return [a for a in accounts if isinstance (a , LedgerAccount )]
53+ def _get_ledger_accounts () -> list ["LedgerAccount" ]:
54+ from ape .utils .basemodel import ManagerAccessMixin
55+
56+ from ape_ledger .accounts import LedgerAccount
57+
58+ return [a for a in ManagerAccessMixin .account_manager if isinstance (a , LedgerAccount )]
59+
60+
61+ def _hdpath_callback (ctx , param , val ) -> "HDBasePath" :
62+ from ape_ledger .hdpath import HDBasePath
63+
64+ return HDBasePath (val )
5765
5866
5967@cli .command ()
@@ -66,24 +74,30 @@ def _get_ledger_accounts() -> list[LedgerAccount]:
6674 "Defaults to m/44'/60'/{x}'/0/0 where {{x}} is the account ID. "
6775 "Exclude {x} to append the account ID to the end of the base path."
6876 ),
69- callback = lambda ctx , param , arg : HDBasePath ( arg ) ,
77+ callback = _hdpath_callback ,
7078)
7179def add (cli_ctx , alias , hd_path ):
7280 """Add an account from your Ledger hardware wallet"""
7381
7482 address , account_hd_path = _select_account (hd_path )
75- container = accounts .containers ["ledger" ]
83+ container = cli_ctx . account_manager .containers ["ledger" ]
7684 container .save_account (alias , address , str (account_hd_path ))
7785 cli_ctx .logger .success (f"Account '{ address } ' successfully added with alias '{ alias } '." )
7886
7987
88+ def _filter_accounts (acct : "AccountAPI" ) -> bool :
89+ from ape_ledger .accounts import LedgerAccount
90+
91+ return isinstance (acct , LedgerAccount )
92+
93+
8094@cli .command ()
8195@ape_cli_context ()
82- @existing_alias_argument (account_type = LedgerAccount )
96+ @existing_alias_argument (account_type = _filter_accounts )
8397def delete (cli_ctx , alias ):
8498 """Remove a Ledger account from ape"""
8599
86- container = accounts .containers ["ledger" ]
100+ container = cli_ctx . account_manager .containers ["ledger" ]
87101 container .delete_account (alias )
88102 cli_ctx .logger .success (f"Account '{ alias } ' has been removed." )
89103
@@ -94,7 +108,7 @@ def delete(cli_ctx, alias):
94108def delete_all (cli_ctx , skip_confirmation ):
95109 """Remove all Ledger accounts from ape"""
96110
97- container = accounts .containers ["ledger" ]
111+ container = cli_ctx . account_manager .containers ["ledger" ]
98112 ledger_accounts = _get_ledger_accounts ()
99113 if len (ledger_accounts ) == 0 :
100114 cli_ctx .logger .warning ("No accounts found." )
@@ -131,11 +145,14 @@ def sign_message(cli_ctx, alias, message, network):
131145
132146
133147def _sign_message (cli_ctx , alias , message ):
134- if alias not in accounts .aliases :
148+ from eth_account .account import Account
149+ from eth_account .messages import encode_defunct
150+
151+ if alias not in cli_ctx .account_manager .aliases :
135152 cli_ctx .abort (f"Account with alias '{ alias } ' does not exist." )
136153
137154 eip191message = encode_defunct (text = message )
138- account = accounts .load (alias )
155+ account = cli_ctx . account_manager .load (alias )
139156 signature = account .sign_message (eip191message )
140157
141158 if not signature :
@@ -153,9 +170,13 @@ def _sign_message(cli_ctx, alias, message):
153170
154171
155172@cli .command (short_help = "Verify a message with your Trezor device" )
173+ @ape_cli_context ()
156174@click .argument ("message" )
157175@click .argument ("signature" )
158- def verify_message (message , signature ):
176+ def verify_message (cli_ctx , message , signature ):
177+ from eth_account .account import Account
178+ from eth_account .messages import encode_defunct
179+
159180 eip191message = encode_defunct (text = message )
160181
161182 try :
@@ -164,5 +185,9 @@ def verify_message(message, signature):
164185 message = "Message cannot be verified. Check the signature and try again."
165186 raise LedgerSigningError (message ) from exc
166187
167- alias = accounts [signer_address ].alias if signer_address in accounts else ""
188+ alias = (
189+ cli_ctx .account_manager [signer_address ].alias
190+ if signer_address in cli_ctx .account_manager
191+ else ""
192+ )
168193 click .echo (f"Signer: { signer_address } { alias } " )
0 commit comments