|
7 | 7 | from urllib.parse import urljoin |
8 | 8 |
|
9 | 9 | import ipfshttpclient |
| 10 | +from lighthouseweb3 import Lighthouse |
10 | 11 | import yaml |
11 | 12 | from rfc3986 import urlparse |
12 | 13 | import web3 |
|
18 | 19 | from snet.cli.metadata.organization import OrganizationMetadata, PaymentStorageClient, Payment, Group |
19 | 20 | from snet.cli.utils.config import get_contract_address, get_field_from_args_or_session, \ |
20 | 21 | read_default_contract_address |
21 | | -from snet.cli.utils.ipfs_utils import bytesuri_to_hash, get_from_ipfs_and_checkhash, \ |
22 | | - hash_to_bytesuri, publish_file_in_ipfs |
| 22 | +from snet.cli.utils.ipfs_utils import get_from_ipfs_and_checkhash, \ |
| 23 | + hash_to_bytesuri, publish_file_in_ipfs, publish_file_in_filecoin |
23 | 24 | from snet.cli.utils.utils import DefaultAttributeObject, get_web3, is_valid_url, serializable, type_converter, \ |
24 | | - get_cli_version, bytes32_to_str |
| 25 | + get_cli_version, bytes32_to_str, bytesuri_to_hash, get_file_from_filecoin |
25 | 26 |
|
26 | 27 |
|
27 | 28 | class Command(object): |
@@ -79,6 +80,10 @@ def _get_ipfs_client(self): |
79 | 80 | ipfs_endpoint = self.config.get_ipfs_endpoint() |
80 | 81 | return ipfshttpclient.connect(ipfs_endpoint) |
81 | 82 |
|
| 83 | + def _get_filecoin_client(self): |
| 84 | + lighthouse_token = self.config.get_filecoin_key() |
| 85 | + return Lighthouse(token=lighthouse_token) |
| 86 | + |
82 | 87 |
|
83 | 88 | class VersionCommand(Command): |
84 | 89 | def show(self): |
@@ -489,9 +494,11 @@ def _get_organization_registration(self, org_id): |
489 | 494 |
|
490 | 495 | def _get_organization_metadata_from_registry(self, org_id): |
491 | 496 | rez = self._get_organization_registration(org_id) |
492 | | - metadata_hash = bytesuri_to_hash(rez["orgMetadataURI"]) |
493 | | - metadata = get_from_ipfs_and_checkhash( |
494 | | - self._get_ipfs_client(), metadata_hash) |
| 497 | + storage_type, metadata_hash = bytesuri_to_hash(rez["orgMetadataURI"]) |
| 498 | + if storage_type == "ipfs": |
| 499 | + metadata = get_from_ipfs_and_checkhash(self._get_ipfs_client(), metadata_hash) |
| 500 | + else: |
| 501 | + metadata = get_file_from_filecoin(metadata_hash) |
495 | 502 | metadata = metadata.decode("utf-8") |
496 | 503 | return OrganizationMetadata.from_json(json.loads(metadata)) |
497 | 504 |
|
@@ -589,10 +596,17 @@ def create(self): |
589 | 596 |
|
590 | 597 | members = self.get_members_from_args() |
591 | 598 |
|
592 | | - ipfs_metadata_uri = publish_file_in_ipfs( |
593 | | - self._get_ipfs_client(), metadata_file, False) |
594 | | - params = [type_converter("bytes32")( |
595 | | - org_id), hash_to_bytesuri(ipfs_metadata_uri), members] |
| 599 | + storage = self.args.storage |
| 600 | + if not storage or storage == "ipfs": |
| 601 | + metadata_uri = publish_file_in_ipfs(self._get_ipfs_client(), metadata_file, False) |
| 602 | + elif storage == "filecoin": |
| 603 | + # upload to Filecoin via Lighthouse SDK |
| 604 | + metadata_uri = publish_file_in_filecoin(self._get_filecoin_client(), metadata_file) |
| 605 | + else: |
| 606 | + raise ValueError(f"Unsupported storage option: {storage}. Use --storage <ipfs or filecoin>") |
| 607 | + |
| 608 | + params = [type_converter("bytes32")(org_id), |
| 609 | + hash_to_bytesuri(metadata_uri, storage), members] |
596 | 610 | self._printout("Creating transaction to create organization name={} id={}\n".format( |
597 | 611 | org_metadata.org_name, org_id)) |
598 | 612 | self.transact_contract_command( |
@@ -622,31 +636,34 @@ def update_metadata(self): |
622 | 636 | with open(metadata_file, 'r') as f: |
623 | 637 | org_metadata = OrganizationMetadata.from_json(json.load(f)) |
624 | 638 | except Exception as e: |
625 | | - print( |
626 | | - "Organization metadata json file not found ,Please check --metadata-file path ") |
| 639 | + print("Organization metadata JSON file not found. Please check --metadata-file path.") |
627 | 640 | raise e |
628 | | - # validate the metadata before updating |
629 | 641 |
|
| 642 | + # Validate the metadata before updating |
630 | 643 | org_id = self.args.org_id |
631 | | - existing_registry_org_metadata = self._get_organization_metadata_from_registry( |
632 | | - org_id) |
| 644 | + existing_registry_org_metadata = self._get_organization_metadata_from_registry(org_id) |
633 | 645 | org_metadata.validate(existing_registry_org_metadata) |
634 | 646 |
|
635 | 647 | # Check if Organization already exists |
636 | 648 | found = self._get_organization_by_id(org_id)[0] |
637 | 649 | if not found: |
638 | | - raise Exception( |
639 | | - "\nOrganization with id={} does not exists!\n".format(org_id)) |
| 650 | + raise Exception("\nOrganization with id={} does not exist!\n".format(org_id)) |
| 651 | + |
| 652 | + storage = self.args.storage |
| 653 | + if not storage or storage == "ipfs": |
| 654 | + metadata_uri = publish_file_in_ipfs(self._get_ipfs_client(), metadata_file, False) |
| 655 | + elif storage == "filecoin": |
| 656 | + # upload to Filecoin via Lighthouse SDK |
| 657 | + metadata_uri = publish_file_in_filecoin(self._get_filecoin_client(), metadata_file) |
| 658 | + else: |
| 659 | + raise ValueError(f"Unsupported storage option: {storage}. Use --storage <ipfs or filecoin>") |
640 | 660 |
|
641 | | - ipfs_metadata_uri = publish_file_in_ipfs( |
642 | | - self._get_ipfs_client(), metadata_file, False) |
643 | | - params = [type_converter("bytes32")( |
644 | | - org_id), hash_to_bytesuri(ipfs_metadata_uri)] |
| 661 | + params = [type_converter("bytes32")(org_id), hash_to_bytesuri(metadata_uri, storage)] |
645 | 662 | self._printout( |
646 | | - "Creating transaction to create organization name={} id={}\n".format(org_metadata.org_name, org_id)) |
647 | | - self.transact_contract_command( |
648 | | - "Registry", "changeOrganizationMetadataURI", params) |
649 | | - self._printout("id:\n%s" % org_id) |
| 663 | + "Creating transaction to update organization metadata for org name={} id={}\n".format(org_metadata.org_name, |
| 664 | + org_id)) |
| 665 | + self.transact_contract_command("Registry", "changeOrganizationMetadataURI", params) |
| 666 | + self._printout("Organization updated successfully with id:\n%s" % org_id) |
650 | 667 |
|
651 | 668 | def list_services(self): |
652 | 669 | org_id = self.args.org_id |
|
0 commit comments