1010from aleph .sdk .chains .remote import RemoteAccount
1111from aleph .sdk .chains .solana import SOLAccount
1212from aleph .sdk .conf import load_main_configuration , settings
13+ from aleph .sdk .evm_utils import get_chains_with_super_token
1314from aleph .sdk .types import AccountFromPrivateKey
1415
1516logger = logging .getLogger (__name__ )
1617
1718T = TypeVar ("T" , bound = AccountFromPrivateKey )
1819
20+ chain_account_map : Dict [Chain , Type [T ]] = { # type: ignore
21+ Chain .ETH : ETHAccount ,
22+ Chain .AVAX : ETHAccount ,
23+ Chain .BASE : ETHAccount ,
24+ Chain .SOL : SOLAccount ,
25+ }
26+
1927
2028def load_chain_account_type (chain : Chain ) -> Type [AccountFromPrivateKey ]:
21- chain_account_map : Dict [Chain , Type [AccountFromPrivateKey ]] = {
22- Chain .ETH : ETHAccount ,
23- Chain .AVAX : ETHAccount ,
24- Chain .SOL : SOLAccount ,
25- Chain .BASE : ETHAccount ,
26- }
27- return chain_account_map .get (chain ) or ETHAccount
29+ return chain_account_map .get (chain ) or ETHAccount # type: ignore
2830
2931
30- def account_from_hex_string (private_key_str : str , account_type : Type [T ]) -> T :
32+ def account_from_hex_string (
33+ private_key_str : str ,
34+ account_type : Optional [Type [T ]],
35+ chain : Optional [Chain ] = None ,
36+ ) -> AccountFromPrivateKey :
3137 if private_key_str .startswith ("0x" ):
3238 private_key_str = private_key_str [2 :]
33- return account_type (bytes .fromhex (private_key_str ))
3439
40+ if not chain :
41+ if not account_type :
42+ account_type = load_chain_account_type (Chain .ETH ) # type: ignore
43+ return account_type (bytes .fromhex (private_key_str )) # type: ignore
44+
45+ account_type = load_chain_account_type (chain )
46+ account = account_type (bytes .fromhex (private_key_str ))
47+ if chain in get_chains_with_super_token ():
48+ account .switch_chain (chain )
49+ return account # type: ignore
3550
36- def account_from_file (private_key_path : Path , account_type : Type [T ]) -> T :
51+
52+ def account_from_file (
53+ private_key_path : Path ,
54+ account_type : Optional [Type [T ]],
55+ chain : Optional [Chain ] = None ,
56+ ) -> AccountFromPrivateKey :
3757 private_key = private_key_path .read_bytes ()
38- return account_type (private_key )
58+
59+ if not chain :
60+ if not account_type :
61+ account_type = load_chain_account_type (Chain .ETH ) # type: ignore
62+ return account_type (private_key ) # type: ignore
63+
64+ account_type = load_chain_account_type (chain )
65+ account = account_type (private_key )
66+ if chain in get_chains_with_super_token ():
67+ account .switch_chain (chain )
68+ return account
3969
4070
4171def _load_account (
4272 private_key_str : Optional [str ] = None ,
4373 private_key_path : Optional [Path ] = None ,
4474 account_type : Optional [Type [AccountFromPrivateKey ]] = None ,
75+ chain : Optional [Chain ] = None ,
4576) -> AccountFromPrivateKey :
46- """Load private key from a string or a file. takes the string argument in priority"""
47- if private_key_str or (private_key_path and private_key_path .is_file ()):
48- if account_type :
49- if private_key_path and private_key_path .is_file ():
50- return account_from_file (private_key_path , account_type )
51- elif private_key_str :
52- return account_from_hex_string (private_key_str , account_type )
53- else :
54- raise ValueError ("Any private key specified" )
77+ """Load an account from a private key string or file, or from the configuration file."""
78+
79+ # Loads configuration if no account_type is specified
80+ if not account_type :
81+ config = load_main_configuration (settings .CONFIG_FILE )
82+ if config and hasattr (config , "chain" ):
83+ account_type = load_chain_account_type (config .chain )
84+ logger .debug (
85+ f"Detected { config .chain } account for path { settings .CONFIG_FILE } "
86+ )
5587 else :
56- main_configuration = load_main_configuration (settings .CONFIG_FILE )
57- if main_configuration :
58- account_type = load_chain_account_type (main_configuration .chain )
59- logger .debug (
60- f"Detected { main_configuration .chain } account for path { settings .CONFIG_FILE } "
61- )
62- else :
63- account_type = ETHAccount # Defaults to ETHAccount
64- logger .warning (
65- f"No main configuration data found in { settings .CONFIG_FILE } , defaulting to { account_type .__name__ } "
66- )
67- if private_key_path and private_key_path .is_file ():
68- return account_from_file (private_key_path , account_type )
69- elif private_key_str :
70- return account_from_hex_string (private_key_str , account_type )
71- else :
72- raise ValueError ("Any private key specified" )
88+ account_type = account_type = load_chain_account_type (
89+ Chain .ETH
90+ ) # Defaults to ETHAccount
91+ logger .warning (
92+ f"No main configuration data found in { settings .CONFIG_FILE } , defaulting to { account_type and account_type .__name__ } "
93+ )
7394
95+ # Loads private key from a string
96+ if private_key_str :
97+ return account_from_hex_string (private_key_str , account_type , chain )
98+ # Loads private key from a file
99+ elif private_key_path and private_key_path .is_file ():
100+ return account_from_file (private_key_path , account_type , chain )
101+ # For ledger keys
74102 elif settings .REMOTE_CRYPTO_HOST :
75103 logger .debug ("Using remote account" )
76104 loop = asyncio .get_event_loop ()
@@ -80,10 +108,12 @@ def _load_account(
80108 unix_socket = settings .REMOTE_CRYPTO_UNIX_SOCKET ,
81109 )
82110 )
111+ # Fallback: config.path if set, else generate a new private key
83112 else :
84- account_type = ETHAccount # Defaults to ETHAccount
85113 new_private_key = get_fallback_private_key ()
86- account = account_type (private_key = new_private_key )
114+ account = account_from_hex_string (
115+ bytes .hex (new_private_key ), account_type , chain
116+ )
87117 logger .info (
88118 f"Generated fallback private key with address { account .get_address ()} "
89119 )
0 commit comments