Ejemplo n.º 1
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.invocation_script = reader.read_var_bytes(max=664)
        self.verification_script = reader.read_var_bytes(max=360)
Ejemplo n.º 2
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.config = MessageConfig(reader.read_uint8())
        x = reader.read_uint8()
        self.type = MessageType(x)
        # self.type = MessageType(reader.read_uint8())

        payload_data = reader.read_var_bytes(self.PAYLOAD_MAX_SIZE)
        if len(payload_data) > 0:
            if MessageConfig.COMPRESSED in self.config:
                # From the lz4 documentation:
                # "The uncompressed_size argument specifies an upper bound on the size of the uncompressed data size
                # rather than an absolute value"
                try:
                    payload_data = lz4.block.decompress(
                        payload_data, uncompressed_size=self.PAYLOAD_MAX_SIZE)
                except lz4.block.LZ4BlockError:
                    raise ValueError(
                        "Invalid payload data - decompress failed")

            self.payload = self._payload_from_data(self.type, payload_data)

        if self.payload is None:
            self.payload = payloads.EmptyPayload()
Ejemplo n.º 3
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.data = reader.read_var_bytes(520)
Ejemplo n.º 4
0
def deserialize_binary(reader: BinaryReader) -> Any:
    deserialized_items = []
    underserialized = 1

    while underserialized > 0:
        stack_type = reader.read_byte()
        if stack_type == StackItemType.Any:
            deserialized_items.append(None)
        elif stack_type == StackItemType.Boolean:
            deserialized_items.append(reader.read_bool())
        elif stack_type == StackItemType.Integer:
            value_in_bytes = reader.read_var_bytes()
            deserialized_items.append(Integer.from_bytes(value_in_bytes))
        elif stack_type in (StackItemType.ByteString, StackItemType.Buffer):
            deserialized_items.append(reader.read_var_bytes())
        elif stack_type in (StackItemType.Array, StackItemType.Struct):
            count = reader.read_var_int()
            deserialized_items.append(list(range(count)))
            underserialized += count
        elif stack_type == StackItemType.Map:
            count = reader.read_var_int()
            deserialized_items.append({key: None for key in range(count)})
            underserialized += count * 2
        else:
            raise ValueError

        underserialized -= 1

    stack_temp = []
    while len(deserialized_items) > 0:
        item = deserialized_items.pop()
        if isinstance(item, list):
            new_item = []
            for _ in range(len(item)):
                new_item.append(stack_temp.pop())
            item = new_item
        elif isinstance(item, dict):
            new_item = {}
            for _ in range(0, len(item), 2):
                key = stack_temp.pop()
                value = stack_temp.pop()
                new_item[key] = value
            item = new_item
        stack_temp.append(item)

    return stack_temp.pop()
Ejemplo n.º 5
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.usage = TransactionAttributeUsage(reader.read_uint8())
        self.data = reader.read_var_bytes(max=252)
Ejemplo n.º 6
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        super(MerkleBlockPayload, self).deserialize(reader)
        self.content_count = reader.read_var_int()
        self.hashes = reader.read_serializable_list(types.UInt256)
        self.flags = reader.read_var_bytes()
Ejemplo n.º 7
0
    def deserialize_unsigned(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream excluding the validation byte + witness.

        Args:
            reader: instance.
        """
        self.version = reader.read_uint32()
        self.prev_hash = reader.read_serializable(types.UInt256)
        self.block_index = reader.read_uint32()
        self.validator_index = reader.read_uint16()
        self.data = reader.read_var_bytes()
Ejemplo n.º 8
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        """
        Deserialize the object from a binary stream.

        Args:
            reader: instance.
        """
        self.filter = reader.read_var_bytes(max=36000)
        self.K = reader.read_uint8()
        if self.K > 50:
            raise ValueError("Deserialization error - K exceeds limit of 50")
        self.tweak = reader.read_uint32()
Ejemplo n.º 9
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        self.magic = reader.read_uint32()
        self.compiler = reader.read_bytes(32).decode('utf-8')
        self.version = Version.deserialize_from_bytes(reader.read_bytes(16))
        self.script_hash = reader.read_serializable(types.UInt160)
        self.checksum = reader.read_bytes(4)
        if self.checksum != self.compute_checksum():
            raise ValueError("Deserialization error - invalid checksum")

        self.script = reader.read_var_bytes()
        if self.script_hash != self.compute_script_hash():
            raise ValueError("Deserialization error - invalid script_hash")
Ejemplo n.º 10
0
    def deserialize(self, reader: serialization.BinaryReader) -> None:
        self.magic = reader.read_uint32()
        compiler_with_version = reader.read_bytes(64).decode('utf-8')
        self.compiler, version = compiler_with_version.rsplit('-', maxsplit=1)
        self.version = Version.deserialize_from_bytes(
            version[:32].encode('utf-8'))

        assert reader.read_uint16() == 0  # 2 reserved bytes
        reader.read_var_int(128)  # TODO: method tokens
        assert reader.read_uint16() == 0  # 2 reserved bytes

        self.script = reader.read_var_bytes()
        self.checksum = reader.read_bytes(4)
        if self.checksum != self.compute_checksum():
            raise ValueError("Deserialization error - invalid checksum")
Ejemplo n.º 11
0
 def deserialize_unsigned(self, reader: serialization.BinaryReader) -> None:
     self.version = reader.read_uint8()
     if self.version > 0:
         raise ValueError("Deserialization error - invalid version")
     self.nonce = reader.read_uint32()
     self.sender = reader.read_serializable(types.UInt160)
     self.system_fee = reader.read_int64()
     if self.system_fee < 0:
         raise ValueError("Deserialization error - negative system fee")
     self.network_fee = reader.read_int64()
     if self.network_fee < 0:
         raise ValueError("Deserialization error - negative network fee")
     # Impossible overflow, only applicable to the C# implementation where they use longs
     # if (self.system_fee + self.network_fee < self.system_fee):
     #     raise ValueError("Deserialization error - overflow")
     self.valid_until_block = reader.read_uint32()
     self.attributes = reader.read_serializable_list(TransactionAttribute)
     self.cosigners = reader.read_serializable_list(payloads.Cosigner)
     self.script = reader.read_var_bytes(max=65535)
     if len(self.script) == 0:
         raise ValueError("Deserialization error - invalid script length 0")
Ejemplo n.º 12
0
 def deserialize(self, reader: serialization.BinaryReader) -> None:
     self.value = reader.read_var_bytes()
Ejemplo n.º 13
0
 def deserialize(self, reader: BinaryReader) -> None:
     self.script = reader.read_var_bytes()
     self.manifest = reader.read_serializable(manifest.ContractManifest)