10
10
from aleph .sdk .chains .remote import RemoteAccount
11
11
from aleph .sdk .chains .solana import SOLAccount
12
12
from aleph .sdk .conf import load_main_configuration , settings
13
+ from aleph .sdk .evm_utils import get_chains_with_super_token
13
14
from aleph .sdk .types import AccountFromPrivateKey
14
15
15
16
logger = logging .getLogger (__name__ )
16
17
17
18
T = TypeVar ("T" , bound = AccountFromPrivateKey )
18
19
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
+
19
27
20
28
def 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
28
30
29
31
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 :
31
37
if private_key_str .startswith ("0x" ):
32
38
private_key_str = private_key_str [2 :]
33
- return account_type (bytes .fromhex (private_key_str ))
34
39
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
35
50
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 :
37
57
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
39
69
40
70
41
71
def _load_account (
42
72
private_key_str : Optional [str ] = None ,
43
73
private_key_path : Optional [Path ] = None ,
44
74
account_type : Optional [Type [AccountFromPrivateKey ]] = None ,
75
+ chain : Optional [Chain ] = None ,
45
76
) -> 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
+ )
55
87
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
+ )
73
94
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
74
102
elif settings .REMOTE_CRYPTO_HOST :
75
103
logger .debug ("Using remote account" )
76
104
loop = asyncio .get_event_loop ()
@@ -80,10 +108,12 @@ def _load_account(
80
108
unix_socket = settings .REMOTE_CRYPTO_UNIX_SOCKET ,
81
109
)
82
110
)
111
+ # Fallback: config.path if set, else generate a new private key
83
112
else :
84
- account_type = ETHAccount # Defaults to ETHAccount
85
113
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
+ )
87
117
logger .info (
88
118
f"Generated fallback private key with address { account .get_address ()} "
89
119
)
0 commit comments