Skip to content

Commit

Permalink
Nonce fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Oct 25, 2023
1 parent 9419ffe commit 1527bc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
39 changes: 19 additions & 20 deletions ocean_lib/data_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import logging
import os
import re
from datetime import datetime, timezone
from json import JSONDecodeError
from math import ceil
from typing import Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -46,26 +45,26 @@ def set_http_client(http_client: Session) -> None:

@staticmethod
@enforce_types
def sign_message(
wallet, msg: str, provider_uri: Optional[str] = None
) -> Tuple[str, str]:
if provider_uri:
method, nonce_endpoint = DataServiceProviderBase.build_endpoint(
"nonce", provider_uri
)

nonce_response = DataServiceProviderBase._http_method(
method, url=nonce_endpoint, params={"userAddress": wallet.address}
).json()

nonce = (
int(ceil(float(nonce_response["nonce"])))
if nonce_response["nonce"]
else 0
)
nonce = nonce + 1
def sign_message(wallet, msg: str, provider_uri: str) -> Tuple[str, str]:
method, nonce_endpoint = DataServiceProviderBase.build_endpoint(
"nonce", provider_uri
)

nonce_response = DataServiceProviderBase._http_method(
method, url=nonce_endpoint, params={"userAddress": wallet.address}
)

if (
not nonce_response
or not hasattr(nonce_response, "status_code")
or nonce_response.status_code != 200
or "nonce" not in nonce_response.json()
):
current_nonce = 0
else:
nonce = str(datetime.now(timezone.utc).timestamp() * 1000)
current_nonce = int(ceil(float(nonce_response.json()["nonce"])))

nonce = current_nonce + 1

print(f"signing message with nonce {nonce}: {msg}, account={wallet.address}")

Expand Down
9 changes: 7 additions & 2 deletions ocean_lib/data_provider/data_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""Provider module."""
import json
import logging
from datetime import datetime, timezone
from json import JSONDecodeError
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from unittest.mock import Mock

from enforce_typing import enforce_types
from requests.models import PreparedRequest, Response
Expand Down Expand Up @@ -219,6 +217,7 @@ def start_compute_job(
consumer=consumer,
dataset=dataset,
compute_environment=compute_environment,
dataset_compute_service=dataset_compute_service,
algorithm=algorithm,
algorithm_meta=algorithm_meta,
algorithm_custom_data=algorithm_custom_data,
Expand Down Expand Up @@ -345,6 +344,7 @@ def compute_job_result(
nonce, signature = DataServiceProvider.sign_message(
consumer,
f"{consumer.address}{job_id}{str(index)}",
provider_uri=dataset_compute_service.service_endpoint,
)

req = PreparedRequest()
Expand Down Expand Up @@ -415,6 +415,7 @@ def _send_compute_request(
nonce, signature = DataServiceProvider.sign_message(
consumer,
f"{consumer.address}{job_id}{did}",
provider_uri=service_endpoint,
)

req = PreparedRequest()
Expand Down Expand Up @@ -449,6 +450,7 @@ def _send_compute_request(
def _prepare_compute_payload(
consumer,
dataset: ComputeInput,
dataset_compute_service: Any, # Can not add Service typing due to enforce_type errors.
compute_environment: str,
algorithm: Optional[ComputeInput] = None,
algorithm_meta: Optional[AlgorithmMetadata] = None,
Expand All @@ -473,9 +475,12 @@ def _prepare_compute_payload(
_input, req_key
), f"The received dataset does not have a {req_key}."

# TODO: is the nonce correct here?
# Should it be the one from the compute service or a dataset?
nonce, signature = DataServiceProvider.sign_message(
consumer,
f"{consumer.address}{dataset.did}",
provider_uri=dataset_compute_service.service_endpoint,
)

payload = {
Expand Down
2 changes: 1 addition & 1 deletion tests/readmes/test_readmes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_script_execution(self, script_name):
"publish-flow-credentials",
"publish-flow-restapi", # TODO: fix and restore
"gas-strategy-remote",
# "using-clef", # TODO: removed original clef readme, to reinstate in #1461
"using-clef", # TODO: removed original clef readme, to reinstate in #1461
]

if script_name.replace("test_", "").replace(".py", "") in skippable:
Expand Down

0 comments on commit 1527bc2

Please sign in to comment.