From fc95c0ae33e562ce799800b0a212bf78b11de344 Mon Sep 17 00:00:00 2001 From: Gabriel Levcovitz Date: Mon, 23 Jun 2025 11:55:00 -0300 Subject: [PATCH] feat(nano): update OCB serialization --- hathorlib/nanocontracts/on_chain_blueprint.py | 21 +++++++++---------- pyproject.toml | 2 +- tests/test_on_chain_blueprint.py | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/hathorlib/nanocontracts/on_chain_blueprint.py b/hathorlib/nanocontracts/on_chain_blueprint.py index 67a91d2..2dea5fe 100644 --- a/hathorlib/nanocontracts/on_chain_blueprint.py +++ b/hathorlib/nanocontracts/on_chain_blueprint.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from enum import Enum +from enum import IntEnum, unique from typing import NamedTuple from hathorlib.conf import HathorSettings @@ -25,14 +25,15 @@ ON_CHAIN_BLUEPRINT_VERSION: int = 1 -class CodeKind(Enum): +@unique +class CodeKind(IntEnum): """ Represents what type of code and format is being used, to allow new code/compression types in the future. """ - PYTHON_GZIP = 'python+gzip' + PYTHON_ZLIB = 1 def __bytes__(self) -> bytes: - return self.value.encode() + return int_to_bytes(number=self.value, size=1) class Code(NamedTuple): @@ -47,11 +48,10 @@ class Code(NamedTuple): def __bytes__(self) -> bytes: # Code serialization format: [kind:variable bytes][null byte][data:variable bytes] - if self.kind is not CodeKind.PYTHON_GZIP: + if self.kind is not CodeKind.PYTHON_ZLIB: raise ValueError('Invalid code kind value') buf = bytearray() buf.extend(bytes(self.kind)) - buf.append(0) buf.extend(self.data) return bytes(buf) @@ -63,11 +63,10 @@ def from_bytes(cls, data: bytes) -> 'Code': check that. """ data = bytearray(data) - cut_at = data.index(0) - kind = CodeKind(data[0:cut_at].decode()) - if kind is not CodeKind.PYTHON_GZIP: + kind = CodeKind(data[0]) + if kind is not CodeKind.PYTHON_ZLIB: raise ValueError('Code kind not supported') - compressed_code = data[cut_at + 1:] + compressed_code = data[1:] return cls(kind, compressed_code) @@ -83,7 +82,7 @@ def __init__(self) -> None: self.nc_pubkey: bytes = b'' self.nc_signature: bytes = b'' - self.code: Code = Code(CodeKind.PYTHON_GZIP, b'') + self.code: Code = Code(CodeKind.PYTHON_ZLIB, b'') def serialize_code(self) -> bytes: """Serialization of self.code, to be used for the serialization of this transaction type.""" diff --git a/pyproject.toml b/pyproject.toml index 471710a..b9c78e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ [tool.poetry] name = "hathorlib" -version = "0.10.0" +version = "0.11.0" description = "Hathor Network base objects library" authors = ["Hathor Team "] license = "Apache-2.0" diff --git a/tests/test_on_chain_blueprint.py b/tests/test_on_chain_blueprint.py index 69e3dd4..d70838b 100644 --- a/tests/test_on_chain_blueprint.py +++ b/tests/test_on_chain_blueprint.py @@ -19,7 +19,7 @@ def _get_ocb(self): b'\xf6\xf0`g\x1b0\xb6\xca\x1b\xed\x83:N\xa0\x98\xd2' \ b'\xdf\x02!\x00\xbe\xf85\xf6O`\xfed`Ip\xe2a\xc4\x03vv' \ b'\xec\x94\ny?\xde\x90\xc3\x12\x9c\xd8\xdd\xd8\xe5\r' - code = Code(CodeKind.PYTHON_GZIP, b'') + code = Code(CodeKind.PYTHON_ZLIB, b'') ocb.code = code return ocb