Skip to content

Commit

Permalink
Merge pull request #221 from valory-xyz/feat/cid-v1
Browse files Browse the repository at this point in the history
Add support for CID v1
  • Loading branch information
DavidMinarsch authored Jul 14, 2022
2 parents e9f9ac5 + 3fe10c8 commit 154f952
Show file tree
Hide file tree
Showing 93 changed files with 1,237 additions and 478 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ isort = "==5.7.0"
web3 = "==5.25.0"
cosmpy = ">=0.4.1,<0.5.0"
semver = "<3.0.0,>=2.9.1"
py-multibase = ">=1.0.0"
py-multicodec = ">=0.2.0"

[packages]
# we don't specify dependencies for the library here for intallation as per: https://pipenv-fork.readthedocs.io/en/latest/advanced.html#pipfile-vs-setuppy
Expand Down
25 changes: 25 additions & 0 deletions aea/cli/ipfs_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
)
from aea.configurations.data_types import PackageId, PublicId
from aea.configurations.loader import load_configuration_object
from aea.helpers.cid import DEFAULT_ENCODING, to_v0, to_v1
from aea.helpers.dependency_tree import DependecyTree, dump_yaml, load_yaml, to_plural
from aea.helpers.fingerprint import check_fingerprint, update_fingerprint
from aea.helpers.io import from_csv, to_csv
Expand Down Expand Up @@ -146,6 +147,7 @@ def hash_package(
package_hash = IPFSHashOnly.hash_directory(
str(configuration.directory), wrap=(not no_wrap)
)

return key, package_hash


Expand Down Expand Up @@ -365,3 +367,26 @@ def hash_file(path: str, no_wrap: bool) -> None:

click.echo(f"Path : {path}")
click.echo(f"Hash : {IPFSHashOnly.get(path, wrap=not no_wrap)}")


@hash_group.command(name="to-v0")
@click.argument("hash_string")
def to_v0_string(hash_string: str) -> None:
"""Convert hash to CID v0."""
try:
v0_hash = to_v0(hash_string)
click.echo(v0_hash)
except Exception as e: # pylint: disable=broad-except
raise click.ClickException(str(e)) from e


@hash_group.command(name="to-v1")
@click.argument("hash_string")
@click.option("--encoding", type=str, default=DEFAULT_ENCODING)
def to_v1_string(hash_string: str, encoding: str) -> None:
"""Convert hash to CID v1."""
try:
v1_hash = to_v1(hash_string, encoding)
click.echo(v1_hash)
except Exception as e: # pylint: disable=broad-except
raise click.ClickException(str(e)) from e
6 changes: 5 additions & 1 deletion aea/cli/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
PROTOCOLS,
SKILLS,
)
from aea.helpers.cid import to_v0, to_v1


try:
Expand Down Expand Up @@ -200,7 +201,7 @@ def __init__(self, ctx: Context) -> None:

def check_item_present(self, item_type_plural: str, public_id: PublicId) -> None:
"""Check if item is pinned on the node."""
if not self.ipfs_tool.is_a_package(public_id.hash):
if not self.ipfs_tool.is_a_package(to_v0(public_id.hash)):
raise click.ClickException(
f"Dependency {public_id} is missing from registry.\nPlease push it first and then retry or use {PUSH_ITEMS_FLAG} flag to push automatically."
)
Expand All @@ -221,6 +222,8 @@ def push_item(self, item_type_plural: str, public_id: PublicId) -> None:
)

_, package_hash, _ = self.ipfs_tool.add(component_path)
package_hash = to_v1(package_hash)

click.echo("Pushed missing package with:")
click.echo(f"\tPublicId: {public_id}")
click.echo(f"\tPackage hash: {package_hash}")
Expand Down Expand Up @@ -412,6 +415,7 @@ def _publish_agent_ipfs(ctx: Context, push_missing: bool) -> None:
readme_file_target_path = os.path.join(package_dir, DEFAULT_README_FILE)
shutil.copy(readme_source_path, readme_file_target_path)
_, package_hash, _ = registry.ipfs_tool.add(package_dir)
package_hash = to_v1(package_hash)

click.echo(
f"Successfully published agent {name} to the Registry with.\n\tPublic ID: {ctx.agent_config.public_id}\n\tPackage hash: {package_hash}"
Expand Down
3 changes: 3 additions & 0 deletions aea/cli/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
try_get_item_target_path,
)
from aea.configurations.data_types import PublicId
from aea.helpers.cid import to_v1


try:
Expand Down Expand Up @@ -208,6 +209,8 @@ def push_item_ipfs(component_path: Path, public_id: PublicId) -> None:

ipfs_tool = IPFSTool(get_ipfs_node_multiaddr())
_, package_hash, _ = ipfs_tool.add(component_path)
package_hash = to_v1(package_hash)

click.echo("Pushed component with:")
click.echo(f"\tPublicId: {public_id}")
click.echo(f"\tPackage hash: {package_hash}")
2 changes: 1 addition & 1 deletion aea/configurations/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
_COSMOS_IDENTIFIER = "cosmos"
SIGNING_PROTOCOL = "open_aea/signing:latest"
SIGNING_PROTOCOL_WITH_HASH = (
"open_aea/signing:1.0.0:QmX5gZL9j8y1z8HYkNJmMkcdTBH4ENiJBAFfYYuJgYcHSH"
"open_aea/signing:1.0.0:bafybeievkw5bnmrssxwckf7leszpw2eyull6b5avithgrhd2sdd5izfjku"
)
DEFAULT_LEDGER = _ETHEREUM_IDENTIFIER
PRIVATE_KEY_PATH_SCHEMA = "{}_private_key.txt"
Expand Down
4 changes: 2 additions & 2 deletions aea/configurations/schemas/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@
},
"public_id": {
"type": "string",
"pattern": "^[a-zA-Z_][a-zA-Z0-9_]{0,127}/[a-zA-Z_][a-zA-Z0-9_]{0,127}(:(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?)?(:[a-zA-Z_][a-zA-Z0-9]{45})?$"
"pattern": "^[a-zA-Z_][a-zA-Z0-9_]{0,127}/[a-zA-Z_][a-zA-Z0-9_]{0,127}(:(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?)?(:((Qm[a-zA-Z0-9]{44})|(ba[a-zA-Z0-9]{57})))?$"
},
"ipfs_hash": {
"type": "string",
"pattern": "[a-zA-Z_][a-zA-Z0-9]{45}"
"pattern": "(Qm[a-zA-Z0-9]{44})|(ba[a-zA-Z0-9]{57})"
},
"version_specifiers": {
"type": "string",
Expand Down
6 changes: 3 additions & 3 deletions aea/connections/scaffold/connection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ description: The scaffold connection provides a scaffold for a connection to be
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: QmVdEbfzEyGcS59ypmWaMNHGv9N3CDV7s2tdwnH7Xhmq4q
connection.py: QmcMDwByumudUqC8PRHgRvKLt8gRDr1VePcAa2P1567pc8
readme.md: Qmdt71SaCCwAG1c24VktXDm4pxgUBiPMg4bWfUTiqorypf
__init__.py: bafybeidmifvya6yjc6h7xppntnwqji2crm5o74xrwhmkutycdo4cqdryhy
connection.py: bafybeigqfexwzamkgssmesfglbtnd3fstlwbsaveerzfezoipmkkk7ceke
readme.md: bafybeihg5yfzgqvg5ngy7r2o5tfeqnelx2ffxw4po5hmheqjfhumpmxpoq
fingerprint_ignore_patterns: []
connections: []
protocols: []
Expand Down
4 changes: 2 additions & 2 deletions aea/contracts/scaffold/contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ description: The scaffold contract scaffolds a contract to be implemented by the
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: Qmex1ch5KzYop5CxhVa3zhVgjRgRG3tNjc1Nq4vyRyaod6
contract.py: QmS2qAfZV399CpT1uz4DL2sWDTnhovMaq2vQpfBznHgwqi
__init__.py: bafybeihwzh2vnfnzgnfmfgmuhkcn4dzjgnd6fzkksydrygb75odcjzexzu
contract.py: bafybeibw3ypddawa5tk3qinnytkxhffjhsqf5b6fcnddgarj7hodfmxyde
fingerprint_ignore_patterns: []
class_name: MyScaffoldContract
contract_interface_paths: {}
Expand Down
9 changes: 7 additions & 2 deletions aea/helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@


STRING_LENGTH_LIMIT = 128
IPFS_HASH_LENGTH_LIMIT = 46
SIMPLE_ID_REGEX = rf"[a-zA-Z_][a-zA-Z0-9_]{{0,{STRING_LENGTH_LIMIT - 1}}}"
IPFS_HASH_REGEX = rf"[a-zA-Z_][a-zA-Z0-9]{{{IPFS_HASH_LENGTH_LIMIT - 1}}}"
ISO_8601_DATE_FORMAT = "%Y-%m-%d"

IPFS_HASH_LENGTH_LIMIT_V0 = 46
IPFS_HASH_LENGTH_LIMIT_V1 = 59
IPFS_HASH_REGEX_V0 = rf"Qm[a-zA-Z0-9]{{{IPFS_HASH_LENGTH_LIMIT_V0 - 2}}}"
IPFS_HASH_REGEX_v1 = rf"ba[a-zA-Z0-9]{{{IPFS_HASH_LENGTH_LIMIT_V1 - 2}}}"
IPFS_HASH_REGEX = f"(({IPFS_HASH_REGEX_V0})|({IPFS_HASH_REGEX_v1}))"


_default_logger = logging.getLogger(__name__)


Expand Down
Loading

0 comments on commit 154f952

Please sign in to comment.