Esempio n. 1
0
 def push_bytes(data):
     ms = StreamManager.get_stream()
     writer = BinaryWriter(ms)
     if len(data) == 0:
         raise ValueError("push data error: data is null")
     if len(data) <= int.from_bytes(PUSHBYTES75,
                                    'little') + 1 - int.from_bytes(
                                        PUSHBYTES1, 'little'):
         num = len(data) + int.from_bytes(PUSHBYTES1, 'little') - 1
         writer.write_byte(num)
     elif len(data) < 0x100:
         writer.write_byte(PUSHDATA1)
         writer.write_uint8(len(data))
     elif len(data) < 0x10000:
         writer.write_byte(PUSHDATA2)
         writer.write_uint16(len(data))
     else:
         writer.write_byte(PUSHDATA4)
         writer.write_uint32(len(data))
     writer.write_bytes(data)
     ms.flush()
     res = ms.hexlify()
     StreamManager.release_stream(ms)
     res = bytes_reader(res)
     return res
Esempio n. 2
0
 def parse_attributes(serialized_attributes: str or bytes):
     if len(serialized_attributes) == 0:
         return list()
     if isinstance(serialized_attributes, str):
         stream = StreamManager.get_stream(
             bytearray.fromhex(serialized_attributes))
     elif isinstance(serialized_attributes, bytes):
         stream = StreamManager.get_stream(serialized_attributes)
     else:
         raise SDKException(
             ErrorCode.params_type_error(
                 'bytes or str parameter is required.'))
     reader = BinaryReader(stream)
     attributes_list = []
     while True:
         try:
             attr_key = reader.read_var_bytes().decode('utf-8')
             attr_type = reader.read_var_bytes().decode('utf-8')
             attr_value = reader.read_var_bytes().decode('utf-8')
             attr = dict(Key=attr_key, Type=attr_type, Value=attr_value)
             attributes_list.append(attr)
         except SDKException as e:
             if 10000 < e.args[0] < 20000:
                 break
             else:
                 raise e
     return attributes_list
Esempio n. 3
0
 def parse_pub_keys(did: str, raw_pub_keys: str or bytes) -> list:
     if isinstance(raw_pub_keys, str):
         stream = StreamManager.get_stream(bytearray.fromhex(raw_pub_keys))
     elif isinstance(raw_pub_keys, bytes):
         stream = StreamManager.get_stream(raw_pub_keys)
     else:
         raise SDKException(
             ErrorCode.params_type_error(
                 'bytes or str parameter is required.'))
     reader = BinaryReader(stream)
     pub_keys = list()
     while True:
         try:
             kid = f'{did}#keys-{reader.read_int32()}'
             bytes_key = reader.read_var_bytes()
             hex_pub_key = bytes_key.hex()
             if len(bytes_key) == 33:
                 key_info = dict(PubKeyId=kid,
                                 Type=KeyType.ECDSA.name,
                                 Curve=Curve.P256.name,
                                 Value=hex_pub_key)
             else:
                 key_type = KeyType.from_label(bytes_key[0])
                 curve = Curve.from_label(bytes_key[1])
                 key_info = dict(PubKeyId=kid,
                                 Type=key_type,
                                 Curve=curve,
                                 Value=hex_pub_key)
             pub_keys.append(key_info)
         except SDKException as e:
             if e.args[0] != 10001:
                 raise e
             else:
                 break
     return pub_keys
Esempio n. 4
0
 def test_read_var_int(self):
     value = 123
     writer_stream = StreamManager.get_stream()
     writer = BinaryWriter(writer_stream)
     writer.write_var_int(value)
     reader_stream = StreamManager.get_stream(writer_stream.getbuffer())
     reader = BinaryReader(reader_stream)
     self.assertEqual(reader.read_var_int(), value)
Esempio n. 5
0
 def get_public_key_serialize(self):
     stream = StreamManager.get_stream()
     writer = BinaryWriter(stream)
     if self.__key_type == KeyType.ECDSA:
         writer.write_var_bytes(self.__public_key)
     else:
         raise SDKException(ErrorCode.unknown_asymmetric_key_type)
     stream.flush()
     bytes_stream = stream.hexlify()
     StreamManager.release_stream(stream)
     return bytes_stream
Esempio n. 6
0
 def serialize(self, is_hex: bool = False) -> bytes or str:
     ms = StreamManager.get_stream()
     writer = BinaryWriter(ms)
     writer.write_bytes(self.serialize_unsigned())
     writer.write_var_int(len(self.sig_list))
     for sig in self.sig_list:
         writer.write_bytes(sig.serialize())
     ms.flush()
     bytes_tx = ms.to_bytes()
     StreamManager.release_stream(ms)
     if is_hex:
         return bytes_tx.hex()
     else:
         return bytes_tx
Esempio n. 7
0
 def serialize_unsigned(self) -> bytes or str:
     ms = StreamManager.get_stream()
     writer = BinaryWriter(ms)
     writer.write_uint8(self.version)
     writer.write_uint8(self.tx_type)
     writer.write_uint32(self.nonce)
     writer.write_uint64(self.gas_price)
     writer.write_uint64(self.gas_limit)
     writer.write_bytes(self.payer)
     self.serialize_exclusive_data(writer)
     if self.payload is not None and len(self.payload) != 0:
         writer.write_var_bytes(bytes(self.payload))
     writer.write_var_int(len(self.attributes))
     ms.flush()
     hex_bytes = ms.to_bytes()
     StreamManager.release_stream(ms)
     return hex_bytes
Esempio n. 8
0
    def serialize(self) -> bytes:
        invoke_script = ProgramBuilder.program_from_params(self.__sig_data)
        if len(self.public_keys) == 0:
            raise SDKException(ErrorCode.other_error('Public key in sig is empty.'))

        if len(self.public_keys) == 1:
            verification_script = ProgramBuilder.program_from_pubkey(self.public_keys[0])
        else:
            verification_script = ProgramBuilder.program_from_multi_pubkey(self.m, self.public_keys)
        ms = StreamManager.get_stream()
        writer = BinaryWriter(ms)
        writer.write_var_bytes(invoke_script)
        writer.write_var_bytes(verification_script)
        ms.flush()
        res = ms.hexlify()
        res = bytes_reader(res)
        StreamManager.release_stream(ms)
        return res
Esempio n. 9
0
    def parse_ddo(did: str, serialized_ddo: str or bytes) -> dict:
        """
        This interface is used to deserialize a hexadecimal string into a DDO object in the from of dict.

        :param did: the unique ID for identity.
        :param serialized_ddo: an serialized description object of ONT ID in form of str or bytes.
        :return: a description object of ONT ID in the from of dict.
        """
        if len(serialized_ddo) == 0:
            return dict()
        if isinstance(serialized_ddo, str):
            stream = StreamManager.get_stream(
                bytearray.fromhex(serialized_ddo))
        elif isinstance(serialized_ddo, bytes):
            stream = StreamManager.get_stream(serialized_ddo)
        else:
            raise SDKException(
                ErrorCode.params_type_error(
                    'bytes or str parameter is required.'))
        reader = BinaryReader(stream)
        try:
            public_key_bytes = reader.read_var_bytes()
        except SDKException:
            public_key_bytes = b''
        try:
            attribute_bytes = reader.read_var_bytes()
        except SDKException:
            attribute_bytes = b''
        try:
            recovery_bytes = reader.read_var_bytes()
        except SDKException:
            recovery_bytes = b''
        if len(recovery_bytes) != 0:
            b58_recovery = Address(recovery_bytes).b58encode()
        else:
            b58_recovery = ''
        pub_keys = DID.parse_pub_keys(did, public_key_bytes)
        attribute_list = DID.parse_attributes(attribute_bytes)
        ddo = dict(Owners=pub_keys,
                   Attributes=attribute_list,
                   Recovery=b58_recovery,
                   DID=did)
        return ddo
Esempio n. 10
0
 def get_param_info(program: bytes):
     ms = StreamManager.get_stream(program)
     reader = BinaryReader(ms)
     param_info = []
     while True:
         try:
             res = ProgramBuilder.read_bytes(reader)
         except SDKException:
             break
         param_info.append(res)
     return param_info
Esempio n. 11
0
 def op_code_to_int(op_code: str):
     if op_code.lower() == '4f':
         return -1
     elif op_code == '00':
         return 0
     elif 80 < int(op_code, base=16) < 103:
         return int(op_code, base=16) - 80
     else:
         op_code = bytearray.fromhex(op_code)
         stream = StreamManager.get_stream(op_code)
         reader = BinaryReader(stream)
         op_code = bytearray(reader.read_var_bytes())
         return NeoData.neo_bytearray_to_big_int(op_code)
Esempio n. 12
0
 def deserialize_from(bytes_tx: bytes):
     ms = StreamManager.get_stream(bytes_tx)
     reader = BinaryReader(ms)
     tx = Transaction()
     tx.version = reader.read_uint8()
     tx.tx_type = reader.read_uint8()
     tx.nonce = reader.read_uint32()
     tx.gas_price = reader.read_uint64()
     tx.gas_limit = reader.read_uint64()
     tx.payer = reader.read_bytes(20)
     tx.payload = reader.read_var_bytes()
     attribute_len = reader.read_var_int()
     if attribute_len is 0:
         tx.attributes = bytearray()
     sig_len = reader.read_var_int()
     tx.sig_list = list()
     for _ in range(0, sig_len):
         tx.sig_list.append(Sig.deserialize(reader))
     return tx
Esempio n. 13
0
 def get_program_info(program: bytes) -> ProgramInfo:
     length = len(program)
     end = program[length - 1]
     temp = program[:length - 1]
     ms = StreamManager.get_stream(temp)
     reader = BinaryReader(ms)
     info = ProgramInfo()
     if end == int.from_bytes(CHECKSIG, 'little'):
         pub_keys = ProgramBuilder.read_bytes(reader)
         info.set_pubkey([pub_keys])
         info.set_m(1)
     elif end == int.from_bytes(CHECKMULTISIG, 'little'):
         length = program[len(program) - 2] - int.from_bytes(
             PUSH1, 'little') + 1
         m = reader.read_byte() - int.from_bytes(PUSH1, 'little') + 1
         pub = []
         for _ in range(length):
             pub.append(reader.read_var_bytes())
         info.set_pubkey(pub)
         info.set_m(m)
     return info
Esempio n. 14
0
 def to_dict(item_serialize: str) -> dict:
     stream = StreamManager.get_stream(bytearray.fromhex(item_serialize))
     reader = BinaryReader(stream)
     return NeoData.__deserialize_stack_item(reader)
Esempio n. 15
0
 def deserialize_from(sig_bytes: bytes):
     ms = StreamManager.get_stream(sig_bytes)
     reader = BinaryReader(ms)
     return Sig.deserialize(reader)
Esempio n. 16
0
 def setUp(self):
     self.stream = StreamManager.get_stream()
     self.writer = BinaryWriter(self.stream)