Source code for pypergraph.keystore.kryo

[docs] class Kryo:
[docs] def serialize(self, msg: str, set_references: bool = True) -> str: """ Serialize a message using a custom kryo-like serialization method. Used after encoding the message to sign. :param msg: The string message to serialize. :param set_references: Whether to include references in the prefix. :return: The serialized message as a hexadecimal string. """ prefix = ( "03" + ("01" if set_references else "") + self._utf8_length(len(msg) + 1).hex() ) coded = msg.encode("utf-8").hex() return prefix + coded
@staticmethod def _utf8_length(value: int) -> bytes: """ Encodes the length of a UTF8 string as a variable-length encoded integer. :param value: The value to encode. :return: The encoded length as a bytes object. """ buffer = bytearray() if value >> 6 == 0: # Requires 1 byte buffer.append(value | 0x80) # Set bit 8. elif value >> 13 == 0: # Requires 2 bytes buffer.append(value | 0x40 | 0x80) # Set bits 7 and 8. buffer.append(value >> 6) elif value >> 20 == 0: # Requires 3 bytes buffer.append(value | 0x40 | 0x80) # Set bits 7 and 8. buffer.append((value >> 6) | 0x80) # Set bit 8. buffer.append(value >> 13) elif value >> 27 == 0: # Requires 4 bytes buffer.append(value | 0x40 | 0x80) # Set bits 7 and 8. buffer.append((value >> 6) | 0x80) # Set bit 8. buffer.append((value >> 13) | 0x80) # Set bit 8. buffer.append(value >> 20) else: # Requires 5 bytes buffer.append(value | 0x40 | 0x80) # Set bits 7 and 8. buffer.append((value >> 6) | 0x80) # Set bit 8. buffer.append((value >> 13) | 0x80) # Set bit 8. buffer.append((value >> 20) | 0x80) # Set bit 8. buffer.append(value >> 27) return bytes(buffer)