|
| 1 | +# Copyright 2023 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from collections import deque |
| 18 | +from dataclasses import dataclass |
| 19 | +from typing import TYPE_CHECKING |
| 20 | + |
| 21 | +from hathorlib.headers.base import VertexBaseHeader |
| 22 | +from hathorlib.headers.types import VertexHeaderId |
| 23 | +from hathorlib.nanocontracts import DeprecatedNanoContract |
| 24 | +from hathorlib.nanocontracts.types import NCActionType |
| 25 | +from hathorlib.utils import int_to_bytes, unpack, unpack_len |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from hathorlib.base_transaction import BaseTransaction |
| 29 | + |
| 30 | +NC_VERSION = 1 |
| 31 | +NC_INITIALIZE_METHOD = 'initialize' |
| 32 | + |
| 33 | + |
| 34 | +@dataclass(frozen=True) |
| 35 | +class NanoHeaderAction: |
| 36 | + type: NCActionType |
| 37 | + token_index: int |
| 38 | + amount: int |
| 39 | + |
| 40 | + |
| 41 | +@dataclass(frozen=True) |
| 42 | +class NanoHeader(VertexBaseHeader): |
| 43 | + tx: BaseTransaction |
| 44 | + |
| 45 | + # nc_id equals to the blueprint_id when a Nano Contract is being created. |
| 46 | + # nc_id equals to the nanocontract_id when a method is being called. |
| 47 | + nc_id: bytes |
| 48 | + |
| 49 | + # Name of the method to be called. When creating a new Nano Contract, it must be equal to 'initialize'. |
| 50 | + nc_method: str |
| 51 | + |
| 52 | + # Serialized arguments to nc_method. |
| 53 | + nc_args_bytes: bytes |
| 54 | + |
| 55 | + nc_actions: list[NanoHeaderAction] |
| 56 | + |
| 57 | + # Pubkey and signature of the transaction owner / caller. |
| 58 | + nc_pubkey: bytes |
| 59 | + nc_signature: bytes |
| 60 | + |
| 61 | + nc_version: int = NC_VERSION |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def _deserialize_action(cls, buf: bytes) -> tuple[NanoHeaderAction, bytes]: |
| 65 | + from hathorlib.base_transaction import bytes_to_output_value |
| 66 | + type_bytes, buf = buf[:1], buf[1:] |
| 67 | + action_type = NCActionType.from_bytes(type_bytes) |
| 68 | + (token_index,), buf = unpack('!B', buf) |
| 69 | + amount, buf = bytes_to_output_value(buf) |
| 70 | + return NanoHeaderAction( |
| 71 | + type=action_type, |
| 72 | + token_index=token_index, |
| 73 | + amount=amount, |
| 74 | + ), buf |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def deserialize(cls, tx: BaseTransaction, buf: bytes) -> tuple[NanoHeader, bytes]: |
| 78 | + header_id, buf = buf[:1], buf[1:] |
| 79 | + assert header_id == VertexHeaderId.NANO_HEADER.value |
| 80 | + (nc_version,), buf = unpack('!B', buf) |
| 81 | + if nc_version != NC_VERSION: |
| 82 | + raise ValueError('unknown nanocontract version: {}'.format(nc_version)) |
| 83 | + |
| 84 | + nc_id, buf = unpack_len(32, buf) |
| 85 | + (nc_method_len,), buf = unpack('!B', buf) |
| 86 | + nc_method, buf = unpack_len(nc_method_len, buf) |
| 87 | + (nc_args_bytes_len,), buf = unpack('!H', buf) |
| 88 | + nc_args_bytes, buf = unpack_len(nc_args_bytes_len, buf) |
| 89 | + |
| 90 | + nc_actions: list[NanoHeaderAction] = [] |
| 91 | + if not isinstance(tx, DeprecatedNanoContract): |
| 92 | + (nc_actions_len,), buf = unpack('!B', buf) |
| 93 | + for _ in range(nc_actions_len): |
| 94 | + action, buf = cls._deserialize_action(buf) |
| 95 | + nc_actions.append(action) |
| 96 | + |
| 97 | + (nc_pubkey_len,), buf = unpack('!B', buf) |
| 98 | + nc_pubkey, buf = unpack_len(nc_pubkey_len, buf) |
| 99 | + (nc_signature_len,), buf = unpack('!B', buf) |
| 100 | + nc_signature, buf = unpack_len(nc_signature_len, buf) |
| 101 | + |
| 102 | + decoded_nc_method = nc_method.decode('ascii') |
| 103 | + |
| 104 | + return cls( |
| 105 | + tx=tx, |
| 106 | + nc_version=nc_version, |
| 107 | + nc_id=nc_id, |
| 108 | + nc_method=decoded_nc_method, |
| 109 | + nc_args_bytes=nc_args_bytes, |
| 110 | + nc_actions=nc_actions, |
| 111 | + nc_pubkey=nc_pubkey, |
| 112 | + nc_signature=nc_signature, |
| 113 | + ), bytes(buf) |
| 114 | + |
| 115 | + def _serialize_action(self, action: NanoHeaderAction) -> bytes: |
| 116 | + from hathorlib.base_transaction import output_value_to_bytes |
| 117 | + ret = [ |
| 118 | + action.type.to_bytes(), |
| 119 | + int_to_bytes(action.token_index, 1), |
| 120 | + output_value_to_bytes(action.amount), |
| 121 | + ] |
| 122 | + return b''.join(ret) |
| 123 | + |
| 124 | + def _serialize_without_header_id(self, *, skip_signature: bool) -> deque[bytes]: |
| 125 | + """Serialize the header with the option to skip the signature.""" |
| 126 | + encoded_method = self.nc_method.encode('ascii') |
| 127 | + |
| 128 | + ret: deque[bytes] = deque() |
| 129 | + ret.append(int_to_bytes(NC_VERSION, 1)) |
| 130 | + ret.append(self.nc_id) |
| 131 | + ret.append(int_to_bytes(len(encoded_method), 1)) |
| 132 | + ret.append(encoded_method) |
| 133 | + ret.append(int_to_bytes(len(self.nc_args_bytes), 2)) |
| 134 | + ret.append(self.nc_args_bytes) |
| 135 | + |
| 136 | + if not isinstance(self.tx, DeprecatedNanoContract): |
| 137 | + ret.append(int_to_bytes(len(self.nc_actions), 1)) |
| 138 | + for action in self.nc_actions: |
| 139 | + ret.append(self._serialize_action(action)) |
| 140 | + |
| 141 | + ret.append(int_to_bytes(len(self.nc_pubkey), 1)) |
| 142 | + ret.append(self.nc_pubkey) |
| 143 | + if not skip_signature: |
| 144 | + ret.append(int_to_bytes(len(self.nc_signature), 1)) |
| 145 | + ret.append(self.nc_signature) |
| 146 | + else: |
| 147 | + ret.append(int_to_bytes(0, 1)) |
| 148 | + return ret |
| 149 | + |
| 150 | + def serialize(self) -> bytes: |
| 151 | + ret = self._serialize_without_header_id(skip_signature=False) |
| 152 | + ret.appendleft(VertexHeaderId.NANO_HEADER.value) |
| 153 | + return b''.join(ret) |
| 154 | + |
| 155 | + def get_sighash_bytes(self) -> bytes: |
| 156 | + ret = self._serialize_without_header_id(skip_signature=True) |
| 157 | + return b''.join(ret) |
0 commit comments