diff --git a/.gitignore b/.gitignore index b836702..af6d8c8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,7 @@ __pycache__ Pipfile.lock /*bin *log -*pyc -.tox \ No newline at end of file +*.pyc +.vscode +.idea/ +.tox diff --git a/exonum/transactions.py b/exonum/transactions.py index 4f73bea..18c6ee3 100644 --- a/exonum/transactions.py +++ b/exonum/transactions.py @@ -5,7 +5,7 @@ from itertools import chain from pysodium import crypto_sign_BYTES as SIGNATURE_SZ -from pysodium import crypto_sign_detached +from pysodium import crypto_sign_detached, crypto_hash_sha256 from .datatypes import EncodingStruct, ExonumBase, TxHeader from .error import IllegalServiceId, NotEncodingStruct @@ -24,7 +24,7 @@ def tx(self, secret_key, hex=False): header_fmt, buf, 0, - kwargs["network_id"], + 0, # network_id field doesn't use anymore but place is reserved with value 0 kwargs["protocol_version"], kwargs["message_id"], kwargs["service_id"], @@ -43,13 +43,12 @@ def tx(self, secret_key, hex=False): class transactions(object): - def __init__(self, service_id=-1, protocol_version=0, network_id=0): + def __init__(self, service_id=-1, protocol_version=0): if service_id < 0: raise IllegalServiceId() self.service_id = service_id self.protocol_version = protocol_version - self.network_id = network_id self.tx = [] @staticmethod @@ -69,7 +68,11 @@ def __call__(self, cls): class Tx(tx_cls): def __init__(tx_self, *args, **kwargs): if "message_id" not in kwargs: - kwargs["network_id"] = self.network_id + kwargs[ + "network_id" + ] = ( + 0 + ) # network_id field doesn't use anymore but place is reserved with value 0 kwargs["protocol_version"] = self.protocol_version kwargs["message_id"] = message_id kwargs["service_id"] = self.service_id @@ -96,7 +99,15 @@ def tx(self, secret_key, hex=False): k: v for k, v in plain.items() if k not in meta_fields } del message["payload_sz"] + del message[ + "network_id" + ] # network_id field doesn't use anymore in JSON return message + def hash(self, secret_key): + tx_bytes = self.tx(secret_key, hex=True) + tx_hash = crypto_hash_sha256(tx_bytes) + return codecs.encode(tx_hash, "hex").decode("utf-8") + self.tx.append(cls.__name__) return Tx diff --git a/tests/test_serde.py b/tests/test_serde.py index c8ff4a4..67b4baa 100644 --- a/tests/test_serde.py +++ b/tests/test_serde.py @@ -1,8 +1,6 @@ # coding: utf-8 from uuid import uuid4 - from six import with_metaclass - from exonum.datatypes import ( Decimal, EncodingStruct, @@ -76,14 +74,14 @@ class X(with_metaclass(EncodingStruct)): assert raw == z -# def test_segment_vector(): -# class X(with_metaclass(EncodingStruct)): -# f = Vec(Str) +def test_segment_vector(): + class X(with_metaclass(EncodingStruct)): + f = Vec(Str) -# x = X(f=[u"am", u"i", u"working", u"or", u"what?"]) -# raw = x.to_bytes() -# xx = X.read_buffer(raw) -# assert xx.f.val == x.f.val + x = X(f=[u"am", u"i", u"working", u"or", u"what?"]) + raw = x.to_bytes() + xx = X.read_buffer(raw) + assert xx.f.val == x.f.val def test_inner(): diff --git a/tests/test_tx.py b/tests/test_tx.py index e13de65..612f267 100644 --- a/tests/test_tx.py +++ b/tests/test_tx.py @@ -11,6 +11,7 @@ transactions = tx.transactions(service_id=250) + # py3 # class Policy(metaclass=exonum.EncodingStruct): # ... diff --git a/work.py b/work.py index 9f08f24..5026fbf 100644 --- a/work.py +++ b/work.py @@ -1,8 +1,8 @@ -from uuid import uuid4 - +import json import exonum.transactions as tx import exonum.datatypes as exonum +from uuid import uuid4 from pysodium import crypto_sign_keypair from importlib import reload @@ -33,12 +33,11 @@ @transactions class CreateUser(metaclass=exonum.EncodingStruct): - public_key = exonum.PublicKey - name = exonum.Str + public_key = exonum.PublicKey() + name = exonum.Str() a = CreateUser(public_key=public_key, name="Me") -import json - print(json.dumps(a.tx(secret_key), indent=2)) +print("tx hash:", a.hash(secret_key))