planetmint/tests/tendermint/test_utils.py
Jürgen Eckel d971709a79
250 migrate zenroom script usage within planetmint so that tx schema 21 is used (#253)
* * **Changed** adjusted to zenroom calling convention of PRP #13 (breaking change)
* **Changed** zenroom test cases to comply to the new calling convention
* **Fixed** zenroom signing bug (call of wrong function)
* **Changed** using cryptoconditions 0.10.0
* **Deprecated** usage of ripde160md as a address generation algorithm, isn't available from python 3.9.14 on, skipping these tests from now on.
* **Changed** script/ouptut tag to be of type array or object for schema v3.0 and v2.0
* **Changed** added 'script' handling to the common/transactions.py class
* **Fixed** data input handling to the transaction fullfillment methods

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* connected the version string in the banner of 'planetmint start' to the planetmint/version.py variables.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* added input validation to the transaction script parsing and passing

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* added backend support for the scripts

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* adjusted tests to the new zenroom calling convention

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* blackified the code

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* increased version to 1.1.0

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* fixed docs building issues of dependency inheritance

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2022-09-08 21:41:10 +02:00

68 lines
2.1 KiB
Python

# Copyright © 2020 Interplanetary Database Association e.V.,
# Planetmint and IPDB software contributors.
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0
import base64
import json
from pytest import mark
try:
from hashlib import sha3_256
except ImportError:
from sha3 import sha3_256
def test_encode_decode_transaction(b):
from planetmint.tendermint_utils import encode_transaction, decode_transaction
asset = {"value": "key"}
encode_tx = encode_transaction(asset)
new_encode_tx = base64.b64encode(json.dumps(asset).encode("utf8")).decode("utf8")
assert encode_tx == new_encode_tx
de64 = base64.b64decode(encode_tx)
assert asset == decode_transaction(de64)
def test_calculate_hash_no_key(b):
from planetmint.tendermint_utils import calculate_hash
# pass an empty list
assert calculate_hash([]) == ""
# TODO test for the case of an empty list of hashes, and possibly other cases.
def test_merkleroot():
from planetmint.tendermint_utils import merkleroot
hashes = [sha3_256(i.encode()).digest() for i in "abc"]
assert merkleroot(hashes) == ("78c7c394d3158c218916b7ae0ebdea502e0f4e85c08e3b371e3dfd824d389fa3")
SAMPLE_PUBLIC_KEY = {
"address": "53DC09497A6ED73B342C78AB1E916076A03A8B95",
"pub_key": {"type": "AC26791624DE60", "value": "7S+T/do70jvneAq0M1so2X3M1iWTSuwtuSAr3nVpfEw="},
}
@mark.skip(
reason="ripemd160, the core of pulbic_key64_to_address is no longer supported by hashlib (from python 3.9.13 on)"
)
def test_convert_base64_public_key_to_address():
from planetmint.tendermint_utils import public_key64_to_address
address = public_key64_to_address(SAMPLE_PUBLIC_KEY["pub_key"]["value"])
assert address == SAMPLE_PUBLIC_KEY["address"]
def test_public_key_encoding_decoding():
from planetmint.tendermint_utils import public_key_from_base64, public_key_to_base64
public_key = public_key_from_base64(SAMPLE_PUBLIC_KEY["pub_key"]["value"])
base64_public_key = public_key_to_base64(public_key)
assert base64_public_key == SAMPLE_PUBLIC_KEY["pub_key"]["value"]