Skip to content

Commit

Permalink
Refactor compute signature logic. (#658)
Browse files Browse the repository at this point in the history
* Refactor compute signature logic.

* black.

* Convert to ns format nonce for c2d.

* Revert logs.

* Conditional cast for nonce before signature validation.
  • Loading branch information
mariacarmina authored Sep 1, 2023
1 parent a5e69ae commit fd2c30c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 0 additions & 1 deletion ocean_provider/routes/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ def computeStatus():
description: Service Unavailable
"""
data = get_request_data(request)
logger.info(f"computeStatus called. arguments = {data}")

body = process_compute_request(data)

Expand Down
9 changes: 8 additions & 1 deletion ocean_provider/utils/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def verify_nonce(signer_address, nonce):
db_nonce = get_nonce(signer_address)
if db_nonce and float(nonce) < float(db_nonce):
if db_nonce and _find_nonce_format(str(nonce)) < _find_nonce_format(str(db_nonce)):
msg = (
f"Invalid signature expected nonce ({db_nonce}) > current nonce ({nonce})."
)
Expand Down Expand Up @@ -112,3 +112,10 @@ def sign_message(message, wallet):
signature = "0x" + r[2:] + s[2:] + v[2:]

return signature


def _find_nonce_format(nonce: str):
if "." in nonce and nonce[-1] != ".":
return float(nonce)
else:
return int(nonce)
4 changes: 3 additions & 1 deletion ocean_provider/utils/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
import logging
import os
import time
from datetime import datetime, timezone
from urllib.parse import urljoin

Expand Down Expand Up @@ -47,7 +48,8 @@ def process_compute_request(data):


def sign_for_compute(wallet, owner, job_id=None):
nonce = datetime.now(timezone.utc).timestamp() * 1000
nonce = time.time_ns()
logger.info(f"nonce for user {owner} is {nonce}")

# prepare consumer signature on did
msg = f"{owner}{job_id}{nonce}" if job_id else f"{owner}{nonce}"
Expand Down
11 changes: 9 additions & 2 deletions ocean_provider/utils/test/test_accounts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from datetime import datetime, timedelta, timezone

import pytest
from ocean_provider.exceptions import InvalidSignatureError
from ocean_provider.user_nonce import update_nonce
from ocean_provider.utils.accounts import (
get_private_key,
sign_message,
verify_signature,
_find_nonce_format,
)
from tests.helpers.nonce import build_nonce

Expand All @@ -20,6 +19,14 @@ def test_get_private_key(publisher_wallet):
)


@pytest.mark.unit
def test_find_nonce_format():
nonce = "1"
assert isinstance(_find_nonce_format(nonce), int)
nonce = "1.1"
assert isinstance(_find_nonce_format(nonce), float)


@pytest.mark.unit
def test_verify_signature(consumer_wallet, publisher_wallet):
nonce = build_nonce(consumer_wallet.address)
Expand Down

0 comments on commit fd2c30c

Please sign in to comment.