From 753b7937ddd74391ec8a4c454493b3535059397d Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Tue, 7 Aug 2018 12:26:45 +0300 Subject: [PATCH 1/5] Added hash method in transaction --- .gitignore | 5 ++++- exonum/transactions.py | 8 +++++++- tests/test_serde.py | 17 ++++++++--------- tests/test_tx.py | 1 + 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 8eb8712..1d8c13f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ __pycache__ .pytest_cache Pipfile.lock /*bin -*log \ No newline at end of file +*log +*.pyc +.vscode +.idea/ diff --git a/exonum/transactions.py b/exonum/transactions.py index adb6646..6489512 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 @@ -100,7 +100,13 @@ def tx(self, secret_key, hex=False): in plain.items() if k not in meta_fields} del message["payload_sz"] + del message['network_id'] # Redundant field 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 0700945..d756736 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, SocketAddr, Str, Uuid, Vec, i64, u8, u16) @@ -71,14 +69,15 @@ 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(): class X(with_metaclass(EncodingStruct)): diff --git a/tests/test_tx.py b/tests/test_tx.py index b5bfade..70cf0c2 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): # ... From 21f718dc2d464d01e05003b62c6bf8f5b39a22c1 Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Tue, 7 Aug 2018 12:43:29 +0300 Subject: [PATCH 2/5] Added example how to use tx hash --- work.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/work.py b/work.py index 9f08f24..ffaebe7 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 @@ -24,21 +24,18 @@ # a.tx(secret_key) public_key = bytes.fromhex( - "0f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9" -) + "0f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9") secret_key = bytes.fromhex( - "5520c351b7760aedeef32687918eb2587ab515e4ae0eeef271a0f0a99f1df3710f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9" -) + "5520c351b7760aedeef32687918eb2587ab515e4ae0eeef271a0f0a99f1df3710f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9") @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)) From 20fc01a87a751f2e0975cccc191302e53ba28d42 Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Tue, 7 Aug 2018 12:57:52 +0300 Subject: [PATCH 3/5] Applied black formatter --- exonum/transactions.py | 2 +- tests/test_serde.py | 14 +++++++++++--- work.py | 8 +++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/exonum/transactions.py b/exonum/transactions.py index 30cbf53..c861eb7 100644 --- a/exonum/transactions.py +++ b/exonum/transactions.py @@ -96,7 +96,7 @@ 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'] # Redundant field in JSON + del message["network_id"] # Redundant field in JSON return message def hash(self, secret_key): diff --git a/tests/test_serde.py b/tests/test_serde.py index bd161b3..67b4baa 100644 --- a/tests/test_serde.py +++ b/tests/test_serde.py @@ -1,8 +1,17 @@ # coding: utf-8 from uuid import uuid4 from six import with_metaclass -from exonum.datatypes import (Decimal, EncodingStruct, SocketAddr, Str, Uuid, - Vec, i64, u8, u16) +from exonum.datatypes import ( + Decimal, + EncodingStruct, + SocketAddr, + Str, + Uuid, + Vec, + i64, + u8, + u16, +) def test_simple(): @@ -75,7 +84,6 @@ class X(with_metaclass(EncodingStruct)): assert xx.f.val == x.f.val - def test_inner(): class X(with_metaclass(EncodingStruct)): first = Vec(u16) diff --git a/work.py b/work.py index ffaebe7..5026fbf 100644 --- a/work.py +++ b/work.py @@ -24,9 +24,11 @@ # a.tx(secret_key) public_key = bytes.fromhex( - "0f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9") + "0f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9" +) secret_key = bytes.fromhex( - "5520c351b7760aedeef32687918eb2587ab515e4ae0eeef271a0f0a99f1df3710f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9") + "5520c351b7760aedeef32687918eb2587ab515e4ae0eeef271a0f0a99f1df3710f17189c062e7f3fbb47a21834d41e4d5c5388dd7db38c4de1ce732971a38ef9" +) @transactions @@ -38,4 +40,4 @@ class CreateUser(metaclass=exonum.EncodingStruct): a = CreateUser(public_key=public_key, name="Me") print(json.dumps(a.tx(secret_key), indent=2)) -print('tx hash:', a.hash(secret_key)) +print("tx hash:", a.hash(secret_key)) From cbf8bed8dd3a3172dd61518e8754921bd25fcfb8 Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Tue, 7 Aug 2018 13:37:27 +0300 Subject: [PATCH 4/5] Removed network_id field from JSON --- exonum/transactions.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/exonum/transactions.py b/exonum/transactions.py index c861eb7..df6c833 100644 --- a/exonum/transactions.py +++ b/exonum/transactions.py @@ -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,7 @@ 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 +95,7 @@ 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"] # Redundant field in JSON + del message["network_id"] # network_id field doesn't use anymore in JSON return message def hash(self, secret_key): From f9930db57577fbf118c2db42e842859fe5d46fcb Mon Sep 17 00:00:00 2001 From: Oleksandr Anyshchenko Date: Tue, 7 Aug 2018 13:41:07 +0300 Subject: [PATCH 5/5] Applied formatting --- exonum/transactions.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/exonum/transactions.py b/exonum/transactions.py index df6c833..18c6ee3 100644 --- a/exonum/transactions.py +++ b/exonum/transactions.py @@ -68,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"] = 0 # network_id field doesn't use anymore but place is reserved with value 0 + 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 @@ -95,7 +99,9 @@ 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 + del message[ + "network_id" + ] # network_id field doesn't use anymore in JSON return message def hash(self, secret_key):