Skip to content

Commit

Permalink
Nonce fixes. (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c authored Nov 1, 2023
1 parent 8e07749 commit 495f9eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 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 @@ -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}")

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

0 comments on commit 495f9eb

Please sign in to comment.