Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ __pycache__
Pipfile.lock
/*bin
*log
*pyc
.tox
*.pyc
.vscode
.idea/
.tox
21 changes: 16 additions & 5 deletions exonum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
16 changes: 7 additions & 9 deletions tests/test_serde.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding: utf-8
from uuid import uuid4

from six import with_metaclass

from exonum.datatypes import (
Decimal,
EncodingStruct,
Expand Down Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions tests/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

transactions = tx.transactions(service_id=250)


# py3
# class Policy(metaclass=exonum.EncodingStruct):
# ...
Expand Down
11 changes: 5 additions & 6 deletions work.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))