Skip to content

Commit 753b793

Browse files
author
Oleksandr Anyshchenko
committed
Added hash method in transaction
1 parent e9489b3 commit 753b793

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ __pycache__
33
.pytest_cache
44
Pipfile.lock
55
/*bin
6-
*log
6+
*log
7+
*.pyc
8+
.vscode
9+
.idea/

exonum/transactions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from itertools import chain
66

77
from pysodium import crypto_sign_BYTES as SIGNATURE_SZ
8-
from pysodium import crypto_sign_detached
8+
from pysodium import crypto_sign_detached, crypto_hash_sha256
99

1010
from .datatypes import EncodingStruct, ExonumBase, TxHeader
1111
from .error import IllegalServiceId, NotEncodingStruct
@@ -100,7 +100,13 @@ def tx(self, secret_key, hex=False):
100100
in plain.items()
101101
if k not in meta_fields}
102102
del message["payload_sz"]
103+
del message['network_id'] # Redundant field in JSON
103104
return message
104105

106+
def hash(self, secret_key):
107+
tx_bytes = self.tx(secret_key, hex=True)
108+
tx_hash = crypto_hash_sha256(tx_bytes)
109+
return codecs.encode(tx_hash, "hex").decode("utf-8")
110+
105111
self.tx.append(cls.__name__)
106112
return Tx

tests/test_serde.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# coding: utf-8
22
from uuid import uuid4
3-
43
from six import with_metaclass
5-
64
from exonum.datatypes import (Decimal, EncodingStruct, SocketAddr, Str, Uuid,
75
Vec, i64, u8, u16)
86

@@ -71,14 +69,15 @@ class X(with_metaclass(EncodingStruct)):
7169
assert raw == z
7270

7371

74-
# def test_segment_vector():
75-
# class X(with_metaclass(EncodingStruct)):
76-
# f = Vec(Str)
72+
def test_segment_vector():
73+
class X(with_metaclass(EncodingStruct)):
74+
f = Vec(Str)
75+
76+
x = X(f=[u"am", u"i", u"working", u"or", u"what?"])
77+
raw = x.to_bytes()
78+
xx = X.read_buffer(raw)
79+
assert xx.f.val == x.f.val
7780

78-
# x = X(f=[u"am", u"i", u"working", u"or", u"what?"])
79-
# raw = x.to_bytes()
80-
# xx = X.read_buffer(raw)
81-
# assert xx.f.val == x.f.val
8281

8382
def test_inner():
8483
class X(with_metaclass(EncodingStruct)):

tests/test_tx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
transactions = tx.transactions(service_id=250)
1313

14+
1415
# py3
1516
# class Policy(metaclass=exonum.EncodingStruct):
1617
# ...

0 commit comments

Comments
 (0)