Skip to content

Commit 2727e3c

Browse files
authored
Merge pull request #2 from aleksuss/tx-hash
Added hash method in transaction
2 parents 819f532 + f9930db commit 2727e3c

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ __pycache__
55
Pipfile.lock
66
/*bin
77
*log
8-
*pyc
9-
.tox
8+
*.pyc
9+
.vscode
10+
.idea/
11+
.tox

exonum/transactions.py

Lines changed: 16 additions & 5 deletions
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
@@ -24,7 +24,7 @@ def tx(self, secret_key, hex=False):
2424
header_fmt,
2525
buf,
2626
0,
27-
kwargs["network_id"],
27+
0, # network_id field doesn't use anymore but place is reserved with value 0
2828
kwargs["protocol_version"],
2929
kwargs["message_id"],
3030
kwargs["service_id"],
@@ -43,13 +43,12 @@ def tx(self, secret_key, hex=False):
4343

4444

4545
class transactions(object):
46-
def __init__(self, service_id=-1, protocol_version=0, network_id=0):
46+
def __init__(self, service_id=-1, protocol_version=0):
4747
if service_id < 0:
4848
raise IllegalServiceId()
4949

5050
self.service_id = service_id
5151
self.protocol_version = protocol_version
52-
self.network_id = network_id
5352
self.tx = []
5453

5554
@staticmethod
@@ -69,7 +68,11 @@ def __call__(self, cls):
6968
class Tx(tx_cls):
7069
def __init__(tx_self, *args, **kwargs):
7170
if "message_id" not in kwargs:
72-
kwargs["network_id"] = self.network_id
71+
kwargs[
72+
"network_id"
73+
] = (
74+
0
75+
) # network_id field doesn't use anymore but place is reserved with value 0
7376
kwargs["protocol_version"] = self.protocol_version
7477
kwargs["message_id"] = message_id
7578
kwargs["service_id"] = self.service_id
@@ -96,7 +99,15 @@ def tx(self, secret_key, hex=False):
9699
k: v for k, v in plain.items() if k not in meta_fields
97100
}
98101
del message["payload_sz"]
102+
del message[
103+
"network_id"
104+
] # network_id field doesn't use anymore in JSON
99105
return message
100106

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

tests/test_serde.py

Lines changed: 7 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 (
75
Decimal,
86
EncodingStruct,
@@ -76,14 +74,14 @@ class X(with_metaclass(EncodingStruct)):
7674
assert raw == z
7775

7876

79-
# def test_segment_vector():
80-
# class X(with_metaclass(EncodingStruct)):
81-
# f = Vec(Str)
77+
def test_segment_vector():
78+
class X(with_metaclass(EncodingStruct)):
79+
f = Vec(Str)
8280

83-
# x = X(f=[u"am", u"i", u"working", u"or", u"what?"])
84-
# raw = x.to_bytes()
85-
# xx = X.read_buffer(raw)
86-
# assert xx.f.val == x.f.val
81+
x = X(f=[u"am", u"i", u"working", u"or", u"what?"])
82+
raw = x.to_bytes()
83+
xx = X.read_buffer(raw)
84+
assert xx.f.val == x.f.val
8785

8886

8987
def test_inner():

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
# ...

work.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from uuid import uuid4
2-
1+
import json
32
import exonum.transactions as tx
43
import exonum.datatypes as exonum
54

5+
from uuid import uuid4
66
from pysodium import crypto_sign_keypair
77

88
from importlib import reload
@@ -33,12 +33,11 @@
3333

3434
@transactions
3535
class CreateUser(metaclass=exonum.EncodingStruct):
36-
public_key = exonum.PublicKey
37-
name = exonum.Str
36+
public_key = exonum.PublicKey()
37+
name = exonum.Str()
3838

3939

4040
a = CreateUser(public_key=public_key, name="Me")
4141

42-
import json
43-
4442
print(json.dumps(a.tx(secret_key), indent=2))
43+
print("tx hash:", a.hash(secret_key))

0 commit comments

Comments
 (0)