Skip to content

Commit eba8266

Browse files
kiruxaspbPavel YagunovmabredinDeralden
authored
Release 2.1.3 (#506)
* Added simple entry point tests * Suppressed the eth-typing package warnings related to some new networks ChainIds not being present in networks.py * Add functionality for SDK * Add a message when passing an unavailable identity_type parameter * Update version to 2.1.3 * Add URL validator --------- Co-authored-by: Pavel Yagunov <pyagunov@naint.ru> Co-authored-by: Maxim Bredin <62469376+mabredin@users.noreply.github.com> Co-authored-by: Deralden <121487413+Deralden@users.noreply.github.com>
1 parent 38e1705 commit eba8266

16 files changed

+196
-340
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The package is published in PyPI at the following link:
66

77
|Package |Description |
88
|----------------------------------------------|---------------------------------------------------------------------|
9-
|[snet-cli](https://pypi.org/project/snet.cli/)|Command line interface to interact with the SingularityNET platform |
9+
|[snet.cli](https://pypi.org/project/snet.cli/)|Command line interface to interact with the SingularityNET platform |
1010

1111
## License
1212

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ jsonschema==4.0.0
1919
eth-account==0.9.0
2020
trezor==0.13.8
2121
ledgerblue==0.1.48
22-
validators==0.28.1
2322
snet.contracts==0.1.1

snet/cli/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
# PYTHON_ARGCOMPLETE_OK
33

44
import sys
5+
import warnings
56

67
import argcomplete
78

8-
from snet.cli import arguments
9+
with warnings.catch_warnings():
10+
# Suppress the eth-typing package`s warnings related to some new networks
11+
warnings.filterwarnings("ignore", "Network .* does not have a valid ChainId. eth-typing should be "
12+
"updated with the latest networks.", UserWarning)
13+
from snet.cli import arguments
14+
915
from snet.cli.config import Config
1016

1117

snet/cli/commands/commands.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
read_default_contract_address
2121
from snet.cli.utils.ipfs_utils import bytesuri_to_hash, get_from_ipfs_and_checkhash, \
2222
hash_to_bytesuri, publish_file_in_ipfs
23-
from snet.cli.utils.utils import DefaultAttributeObject, get_web3, serializable, type_converter, \
23+
from snet.cli.utils.utils import DefaultAttributeObject, get_web3, is_valid_url, serializable, type_converter, \
2424
get_cli_version, bytes32_to_str
2525

2626

@@ -392,6 +392,10 @@ def add_group(self):
392392
"Organization metadata json file not found ,Please check --metadata-file path ")
393393
raise e
394394

395+
for endpoint in self.args.endpoints:
396+
if not is_valid_url(endpoint):
397+
raise Exception(f"Invalid {endpoint} endpoint passed")
398+
395399
payment_storage_client = PaymentStorageClient(self.args.payment_channel_connection_timeout,
396400
self.args.payment_channel_request_timeout, self.args.endpoints)
397401
payment = Payment(self.args.payment_address, self.args.payment_expiration_threshold,

snet/cli/commands/mpe_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from snet.cli.metadata.organization import OrganizationMetadata
1313
from snet.cli.metadata.service import MPEServiceMetadata, load_mpe_service_metadata, mpe_service_metadata_from_json
1414
from snet.cli.utils import ipfs_utils
15-
from snet.cli.utils.utils import open_grpc_channel, type_converter
15+
from snet.cli.utils.utils import is_valid_url, open_grpc_channel, type_converter
1616

1717

1818
class MPEServiceCommand(BlockchainCommand):
@@ -120,6 +120,8 @@ def publish_proto_metadata_init(self):
120120
metadata.add_group(self.args.group_name)
121121
if self.args.endpoints:
122122
for endpoint in self.args.endpoints:
123+
if not is_valid_url(endpoint):
124+
raise Exception(f"Invalid {endpoint} endpoint passed")
123125
metadata.add_endpoint_to_group(
124126
self.args.group_name, endpoint)
125127
if self.args.fixed_price is not None:

snet/cli/config.py

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from configparser import ConfigParser, ExtendedInterpolation
22
from pathlib import Path
3+
import sys
34

45
default_snet_folder = Path("~").expanduser().joinpath(".snet")
6+
DEFAULT_NETWORK = "sepolia"
57

68

79
class Config(ConfigParser):
8-
def __init__(self, _snet_folder=default_snet_folder):
10+
def __init__(self, _snet_folder=default_snet_folder, sdk_config=None):
911
super(Config, self).__init__(interpolation=ExtendedInterpolation(), delimiters=("=",))
1012
self._config_file = _snet_folder.joinpath("config")
13+
self.sdk_config = sdk_config
14+
self.is_sdk = True if sdk_config else False
1115
if self._config_file.exists():
1216
with open(self._config_file) as f:
1317
self.read_file(f)
@@ -21,7 +25,7 @@ def get_session_network_name(self):
2125

2226
def safe_get_session_identity_network_names(self):
2327
if "identity" not in self["session"]:
24-
first_identity_message_and_exit()
28+
first_identity_message_and_exit(is_sdk=self.is_sdk)
2529

2630
session_identity = self["session"]["identity"]
2731
self._check_section("identity.%s" % session_identity)
@@ -128,7 +132,7 @@ def set_network_field(self, network, key, value):
128132
self._get_network_section(network)[key] = str(value)
129133
self._persist()
130134

131-
def add_identity(self, identity_name, identity, out_f):
135+
def add_identity(self, identity_name, identity, out_f=sys.stdout):
132136
identity_section = "identity.%s" % identity_name
133137
if identity_section in self:
134138
raise Exception("Identity section %s already exists in config" % identity_section)
@@ -190,7 +194,18 @@ def create_default_config(self):
190194
"default_eth_rpc_endpoint": "https://sepolia.infura.io/v3/09027f4a13e841d48dbfefc67e7685d5",
191195
}
192196
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)
194209
self._persist()
195210
print("We've created configuration file with default values in: %s\n" % str(self._config_file))
196211

@@ -203,19 +218,51 @@ def _persist(self):
203218
self.write(f)
204219
self._config_file.chmod(0o600)
205220

206-
207-
def first_identity_message_and_exit():
208-
print("\nPlease 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("\nThe 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("\nPlease 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("\nPlease 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")
219266
exit(1)
220267

221268

snet/cli/metadata/organization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from json import JSONEncoder
44
import json
55

6-
import validators
6+
from snet.cli.utils.utils import is_valid_url
77

88

99
class DefaultEncoder(JSONEncoder):
@@ -34,7 +34,7 @@ def from_json(cls, json_data: dict):
3434
endpoints = json_data["endpoints"]
3535
if endpoints:
3636
for endpoint in endpoints:
37-
if not validators.url(endpoint):
37+
if not is_valid_url(endpoint):
3838
raise Exception("Invalid endpoint passed in json file")
3939
return cls(**json_data)
4040

0 commit comments

Comments
 (0)