diff --git a/ocean_lib/data_provider/base.py b/ocean_lib/data_provider/base.py index 68f3e029c..d5b4bb6cf 100644 --- a/ocean_lib/data_provider/base.py +++ b/ocean_lib/data_provider/base.py @@ -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 @@ -47,26 +46,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}") diff --git a/ocean_lib/data_provider/data_service_provider.py b/ocean_lib/data_provider/data_service_provider.py index e27807751..317c95a4d 100644 --- a/ocean_lib/data_provider/data_service_provider.py +++ b/ocean_lib/data_provider/data_service_provider.py @@ -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 @@ -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, @@ -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() @@ -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() @@ -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, @@ -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 = {