From 033e2cdee12f1aa996cee188b5a01f233f7e9d69 Mon Sep 17 00:00:00 2001 From: Jan Segre Date: Tue, 21 Oct 2025 19:26:53 +0200 Subject: [PATCH] feat(nano): include header-id when generating a sighash --- hathor/transaction/headers/nano_header.py | 12 +++++------- tests/nanocontracts/test_nanocontract.py | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/hathor/transaction/headers/nano_header.py b/hathor/transaction/headers/nano_header.py index c80e5b91c..589e7488e 100644 --- a/hathor/transaction/headers/nano_header.py +++ b/hathor/transaction/headers/nano_header.py @@ -208,7 +208,7 @@ def _serialize_action(self, action: NanoHeaderAction) -> bytes: ] return b''.join(ret) - def _serialize_without_header_id(self, *, skip_signature: bool) -> deque[bytes]: + def _serialize(self, *, skip_signature: bool) -> bytes: """Serialize the header with the option to skip the signature.""" encoded_method = self.nc_method.encode('ascii') @@ -230,16 +230,14 @@ def _serialize_without_header_id(self, *, skip_signature: bool) -> deque[bytes]: ret.append(self.nc_script) else: ret.append(leb128.encode_unsigned(0, max_bytes=_NC_SCRIPT_LEN_MAX_BYTES)) - return ret - - def serialize(self) -> bytes: - ret = self._serialize_without_header_id(skip_signature=False) ret.appendleft(VertexHeaderId.NANO_HEADER.value) return b''.join(ret) + def serialize(self) -> bytes: + return self._serialize(skip_signature=False) + def get_sighash_bytes(self) -> bytes: - ret = self._serialize_without_header_id(skip_signature=True) - return b''.join(ret) + return self._serialize(skip_signature=True) def is_creating_a_new_contract(self) -> bool: """Return true if this transaction is creating a new contract.""" diff --git a/tests/nanocontracts/test_nanocontract.py b/tests/nanocontracts/test_nanocontract.py index 1d4a5da8e..51560fec0 100644 --- a/tests/nanocontracts/test_nanocontract.py +++ b/tests/nanocontracts/test_nanocontract.py @@ -34,7 +34,7 @@ MissingStackItems, TooManySigOps, ) -from hathor.transaction.headers import NanoHeader, VertexHeaderId +from hathor.transaction.headers import NanoHeader from hathor.transaction.headers.nano_header import NanoHeaderAction from hathor.transaction.scripts import P2PKH, HathorScript, Opcode from hathor.transaction.validation_state import ValidationState @@ -158,7 +158,7 @@ def test_serialization_skip_signature(self) -> None: nc = self._get_nc() nano_header = nc.get_nano_header() sighash_bytes = nano_header.get_sighash_bytes() - deserialized, buf = NanoHeader.deserialize(Transaction(), VertexHeaderId.NANO_HEADER.value + sighash_bytes) + deserialized, buf = NanoHeader.deserialize(Transaction(), sighash_bytes) assert len(buf) == 0 assert deserialized.nc_seqnum == nano_header.nc_seqnum