Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use commons convert method #30

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 27 additions & 38 deletions nevermined_sdk_py/nevermined/provenance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from common_utils_py.did import did_to_id, id_to_did
from common_utils_py.did import id_to_did, convert_to_bytes
from contracts_lib_py.web3_provider import Web3Provider
from eth_utils import add_0x_prefix

Expand Down Expand Up @@ -29,10 +29,10 @@ def used(self, provenance_id, did, agent_id, activity_id, signature, account, at
"""
try:
receipt = self._keeper.did_registry.used(
self._convert_to_hex(provenance_id),
self._convert_to_hex(did),
self._convert_to_hex(agent_id),
self._convert_to_hex(activity_id),
convert_to_bytes(provenance_id),
convert_to_bytes(did),
convert_to_bytes(agent_id),
convert_to_bytes(activity_id),
signature, account, attributes)
return bool(receipt and receipt.status == 1)
except Exception as e:
Expand All @@ -56,11 +56,11 @@ def was_derived_from(self, provenance_id, new_entity_did, used_entity_did, agent
"""
try:
receipt = self._keeper.did_registry.was_derived_from(
self._convert_to_hex(provenance_id),
self._convert_to_hex(new_entity_did),
self._convert_to_hex(used_entity_did),
self._convert_to_hex(agent_id),
self._convert_to_hex(activity_id),
convert_to_bytes(provenance_id),
convert_to_bytes(new_entity_did),
convert_to_bytes(used_entity_did),
convert_to_bytes(agent_id),
convert_to_bytes(activity_id),
account, attributes)
return bool(receipt and receipt.status == 1)
except Exception as e:
Expand All @@ -82,10 +82,10 @@ def was_associated_with(self, provenance_id, did, agent_id, activity_id, account
"""
try:
receipt = self._keeper.did_registry.was_associated_with(
self._convert_to_hex(provenance_id),
self._convert_to_hex(did),
self._convert_to_hex(agent_id),
self._convert_to_hex(activity_id),
convert_to_bytes(provenance_id),
convert_to_bytes(did),
convert_to_bytes(agent_id),
convert_to_bytes(activity_id),
account,
attributes)
return bool(receipt and receipt.status == 1)
Expand All @@ -111,13 +111,13 @@ def acted_on_behalf(self, provenance_id, did, delegate_agent_id, responsible_age
"""
try:

receipt = self._keeper.did_registry.acted_on_behalf(self._convert_to_hex(provenance_id),
self._convert_to_hex(did),
self._convert_to_hex(
receipt = self._keeper.did_registry.acted_on_behalf(convert_to_bytes(provenance_id),
convert_to_bytes(did),
convert_to_bytes(
delegate_agent_id),
self._convert_to_hex(
convert_to_bytes(
responsible_agent_id),
self._convert_to_hex(activity_id),
convert_to_bytes(activity_id),
signature,
account, attributes)
return bool(receipt and receipt.status == 1)
Expand All @@ -133,7 +133,7 @@ def get_provenance_entry(self, provenance_id):
:return: information on-chain related with the provenance
"""
provenance_entry = self._keeper.did_registry.get_provenance_entry(
self._convert_to_hex(provenance_id))
convert_to_bytes(provenance_id))
provenance_entry['did'] = id_to_did(provenance_entry['did'])
provenance_entry['related_did'] = id_to_did(provenance_entry['related_did'])
return provenance_entry
Expand All @@ -145,7 +145,7 @@ def is_provenance_delegate(self, did, delegate):
:param delegate: delegate address of the delegate
:return: true if the address is a provenance delegate
"""
return self._keeper.did_registry.is_provenance_delegate(self._convert_to_hex(did), delegate)
return self._keeper.did_registry.is_provenance_delegate(convert_to_bytes(did), delegate)

def add_did_provenance_delegate(self, did, delegated_address, account):
"""
Expand All @@ -155,7 +155,7 @@ def add_did_provenance_delegate(self, did, delegated_address, account):
:param account: Account making the call
:return: true if the address is a provenance delegate
"""
return self._keeper.did_registry.add_did_provenance_delegate(self._convert_to_hex(did),
return self._keeper.did_registry.add_did_provenance_delegate(convert_to_bytes(did),
delegated_address, account)

def remove_did_provenance_delegate(self, did, delegated_address, account):
Expand All @@ -166,7 +166,7 @@ def remove_did_provenance_delegate(self, did, delegated_address, account):
:param account: Account making the call
:return: true if the address is a provenance delegate
"""
return self._keeper.did_registry.remove_did_provenance_delegate(self._convert_to_hex(did),
return self._keeper.did_registry.remove_did_provenance_delegate(convert_to_bytes(did),
delegated_address, account)

def get_provenance_owner(self, provenance_id):
Expand All @@ -176,7 +176,7 @@ def get_provenance_owner(self, provenance_id):

:return: String with the address owning the provenance entry
"""
return self._keeper.did_registry.get_provenance_owner(self._convert_to_hex(provenance_id))
return self._keeper.did_registry.get_provenance_owner(convert_to_bytes(provenance_id))

def get_did_provenance_events(self, did):
"""
Expand All @@ -186,7 +186,7 @@ def get_did_provenance_events(self, did):
:return: list of provenance events.
"""
return self._keeper.did_registry.get_did_provenance_events(
self._convert_to_hex(did))
convert_to_bytes(did))

def get_did_provenance_methods_events(self, method, did):
"""
Expand All @@ -199,16 +199,5 @@ def get_did_provenance_methods_events(self, method, did):

"""
return self._keeper.did_registry.get_provenance_method_events(method,
self._convert_to_hex(did))

@staticmethod
def _convert_to_hex(i):
if isinstance(i, str):
if i.startswith('did:nv'):
return Web3Provider.get_web3().toBytes(hexstr=add_0x_prefix(did_to_id(i)))
else:
return Web3Provider.get_web3().toBytes(hexstr=add_0x_prefix(i))
elif isinstance(i, bytes):
return i
else:
raise ValueError(f'The id {i} is not in a valid format.')
convert_to_bytes(did))

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'pyopenssl',
'PyJWT', # not jwt
'PyYAML==4.2b4',
'common-utils-py==0.4.1',
'common-utils-py==0.4.2',
'contracts-lib-py==0.5.2',
'nevermined-secret-store==0.1.0',
'requests~=2.21.0',
Expand Down