11import pathlib
2+ from typing import Literal
23
34from pydantic import model_validator , HttpUrl
45from pydantic_settings import BaseSettings , SettingsConfigDict
78__all__ = ["Identity" , "NetworkConfig" ]
89
910
11+ def _load_configuration (id : str | pathlib .Path , kind : Literal ["identity" , "network" ]):
12+ id = pathlib .Path (id )
13+ global_config = pathlib .Path .home () / ".config" / "soroban" / kind / id / ".toml"
14+ local_config = pathlib .Path (".soroban" ) / kind / id / ".toml"
15+
16+ id = pathlib .Path (id )
17+
18+ if id .is_file ():
19+ return id
20+ elif local_config .is_file ():
21+ return local_config
22+ elif global_config .is_file ():
23+ return global_config
24+
25+
1026class Identity (BaseSettings ):
1127 secret_key : str | None = None
1228 public_key : str | None = None
@@ -32,11 +48,7 @@ def from_source_account(
3248 if account is None :
3349 identity = Identity ()
3450 elif isinstance (account , (str , pathlib .Path )):
35- fname = (
36- account
37- if pathlib .Path (account ).is_file ()
38- else pathlib .Path (".soroban/identity" ) / account / ".toml"
39- )
51+ fname = _load_configuration (account , "identity" )
4052 identity = Identity (_env_file = fname )
4153 else :
4254 identity = Identity (keypair = account )
@@ -49,3 +61,12 @@ class NetworkConfig(BaseSettings):
4961 base_fee : int = 100
5062
5163 model_config = SettingsConfigDict (env_file = "network.toml" )
64+
65+ @classmethod
66+ def from_network (cls , network : str | pathlib .Path | None = None ) -> "NetworkConfig" :
67+ if network is None :
68+ network = NetworkConfig ()
69+ elif isinstance (network , (str , pathlib .Path )):
70+ fname = _load_configuration (network , "network" )
71+ network = NetworkConfig (_env_file = fname )
72+ return network
0 commit comments