1
1
from configparser import ConfigParser , ExtendedInterpolation
2
2
from pathlib import Path
3
+ import sys
3
4
4
5
default_snet_folder = Path ("~" ).expanduser ().joinpath (".snet" )
6
+ DEFAULT_NETWORK = "sepolia"
5
7
6
8
7
9
class Config (ConfigParser ):
8
- def __init__ (self , _snet_folder = default_snet_folder ):
10
+ def __init__ (self , _snet_folder = default_snet_folder , sdk_config = None ):
9
11
super (Config , self ).__init__ (interpolation = ExtendedInterpolation (), delimiters = ("=" ,))
10
12
self ._config_file = _snet_folder .joinpath ("config" )
13
+ self .sdk_config = sdk_config
14
+ self .is_sdk = True if sdk_config else False
11
15
if self ._config_file .exists ():
12
16
with open (self ._config_file ) as f :
13
17
self .read_file (f )
@@ -21,7 +25,7 @@ def get_session_network_name(self):
21
25
22
26
def safe_get_session_identity_network_names (self ):
23
27
if "identity" not in self ["session" ]:
24
- first_identity_message_and_exit ()
28
+ first_identity_message_and_exit (is_sdk = self . is_sdk )
25
29
26
30
session_identity = self ["session" ]["identity" ]
27
31
self ._check_section ("identity.%s" % session_identity )
@@ -128,7 +132,7 @@ def set_network_field(self, network, key, value):
128
132
self ._get_network_section (network )[key ] = str (value )
129
133
self ._persist ()
130
134
131
- def add_identity (self , identity_name , identity , out_f ):
135
+ def add_identity (self , identity_name , identity , out_f = sys . stdout ):
132
136
identity_section = "identity.%s" % identity_name
133
137
if identity_section in self :
134
138
raise Exception ("Identity section %s already exists in config" % identity_section )
@@ -190,7 +194,18 @@ def create_default_config(self):
190
194
"default_eth_rpc_endpoint" : "https://sepolia.infura.io/v3/09027f4a13e841d48dbfefc67e7685d5" ,
191
195
}
192
196
self ["ipfs" ] = {"default_ipfs_endpoint" : "/dns/ipfs.singularitynet.io/tcp/80/" }
193
- self ["session" ] = {"network" : "sepolia" }
197
+ network = self .get_param_from_sdk_config ("network" )
198
+ if network :
199
+ if network not in self .get_all_networks_names ():
200
+ raise Exception ("Network '%s' is not in config" % network )
201
+ self ["session" ] = {"network" : network }
202
+ else :
203
+ self ["session" ] = {"network" : DEFAULT_NETWORK }
204
+ identity_name = self .get_param_from_sdk_config ("identity_name" )
205
+ identity_type = self .get_param_from_sdk_config ("identity_type" )
206
+ if identity_name and identity_type :
207
+ identity = self .setup_identity ()
208
+ self .add_identity (identity_name , identity )
194
209
self ._persist ()
195
210
print ("We've created configuration file with default values in: %s\n " % str (self ._config_file ))
196
211
@@ -203,19 +218,51 @@ def _persist(self):
203
218
self .write (f )
204
219
self ._config_file .chmod (0o600 )
205
220
206
-
207
- def first_identity_message_and_exit ():
208
- print ("\n Please create your first identity by running 'snet identity create'.\n \n "
209
- "The available identity types are:\n "
210
- " - 'rpc' (yields to a required ethereum json-rpc endpoint for signing using a given wallet\n "
211
- " index)\n "
212
- " - 'mnemonic' (uses a required bip39 mnemonic for HDWallet/account derivation and signing\n "
213
- " using a given wallet index)\n "
214
- " - 'key' (uses a required hex-encoded private key for signing)\n "
215
- " - 'ledger' (yields to a required ledger nano s device for signing using a given wallet\n "
216
- " index)\n "
217
- " - 'trezor' (yields to a required trezor device for signing using a given wallet index)\n "
218
- "\n " )
221
+ def get_param_from_sdk_config (self , param : str , alternative = None ):
222
+ if self .sdk_config :
223
+ return self .sdk_config .get (param , alternative )
224
+ return None
225
+
226
+ def setup_identity (self ):
227
+ identity_type = self .get_param_from_sdk_config ("identity_type" )
228
+ private_key = self .get_param_from_sdk_config ("private_key" )
229
+ default_wallet_index = self .get_param_from_sdk_config ("wallet_index" , 0 )
230
+ if not identity_type :
231
+ raise Exception ("identity_type not passed" )
232
+ if identity_type == "key" :
233
+ identity = {
234
+ "identity_type" : "key" ,
235
+ "private_key" : private_key ,
236
+ "default_wallet_index" : default_wallet_index
237
+ }
238
+ # TODO: logic for other identity_type
239
+ else :
240
+ print ("\n The identity_type parameter value you passed is not supported "
241
+ "by the sdk at this time.\n " )
242
+ print ("The available identity types are:\n "
243
+ " - 'key' (uses a required hex-encoded private key for signing)\n \n " )
244
+ exit (1 )
245
+ return identity
246
+
247
+
248
+ def first_identity_message_and_exit (is_sdk = False ):
249
+ if is_sdk :
250
+ print ("\n Please create your first identity by passing the 'identity_name' "
251
+ "and 'identity_type' parameters in SDK config.\n " )
252
+ print ("The available identity types are:\n "
253
+ " - 'key' (uses a required hex-encoded private key for signing)\n \n " )
254
+ else :
255
+ print ("\n Please create your first identity by running 'snet identity create'.\n \n " )
256
+ print ("The available identity types are:\n "
257
+ " - 'rpc' (yields to a required ethereum json-rpc endpoint for signing using a given wallet\n "
258
+ " index)\n "
259
+ " - 'mnemonic' (uses a required bip39 mnemonic for HDWallet/account derivation and signing\n "
260
+ " using a given wallet index)\n "
261
+ " - 'key' (uses a required hex-encoded private key for signing)\n "
262
+ " - 'ledger' (yields to a required ledger nano s device for signing using a given wallet\n "
263
+ " index)\n "
264
+ " - 'trezor' (yields to a required trezor device for signing using a given wallet index)\n "
265
+ "\n " )
219
266
exit (1 )
220
267
221
268
0 commit comments