Пример #1
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
Пример #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
Пример #3
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)
Пример #4
0
    def deserialize(reader: BinaryReader):
        invocation_script = reader.read_var_bytes()
        verification_script = reader.read_var_bytes()
        sig = Sig()
        sig.__sig_data = ProgramBuilder.get_param_info(invocation_script)
        info = ProgramBuilder.get_program_info(verification_script)

        sig.public_keys = info.pubkeys
        sig.m = info.m
        return sig
Пример #5
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)
Пример #6
0
 def read_bytes(reader: BinaryReader):
     code = reader.read_byte()
     if code == int.from_bytes(PUSHDATA4, 'little'):
         temp = reader.read_uint32()
         key_len = temp
     elif code == int.from_bytes(PUSHDATA2, 'little'):
         temp = reader.read_uint16()
         key_len = int(temp)
     elif code == int.from_bytes(PUSHDATA1, 'little'):
         temp = reader.read_uint8()
         key_len = int(temp)
     elif int.from_bytes(PUSHBYTES75, 'little') >= code >= int.from_bytes(
             PUSHBYTES1, 'little'):
         key_len = code - int.from_bytes(PUSHBYTES1, 'little') + 1
     else:
         key_len = 0
     res = reader.read_bytes(key_len)
     return res
Пример #7
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
Пример #8
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
Пример #9
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
Пример #10
0
 def __deserialize_stack_item(reader: BinaryReader) -> dict or bytearray:
     param_type = reader.read_byte()
     if param_type == BuildParams.Type.bytearray_type.value:
         b = reader.read_var_bytes()
         return b
     elif param_type == BuildParams.Type.bool_type.value:
         return reader.read_bool()
     elif param_type == BuildParams.Type.int_type.value:
         b = reader.read_var_bytes()
         return NeoData.__big_int_from_bytes(bytearray(b))
     elif param_type == BuildParams.Type.struct_type.value or param_type == BuildParams.Type.array_type.value:
         count = reader.read_var_int()
         item_list = list()
         for _ in range(count):
             item = NeoData.__deserialize_stack_item(reader)
             item_list.append(item)
         if param_type == BuildParams.Type.struct_type.value:
             return Struct(item_list)
         return item_list
     elif param_type == BuildParams.Type.dict_type.value:
         count = reader.read_var_int()
         item_dict = dict()
         for _ in range(count):
             key = NeoData.__deserialize_stack_item(reader)
             value = NeoData.__deserialize_stack_item(reader)
             item_dict[key] = value
         return item_dict
     else:
         raise SDKException(ErrorCode.other_error('type error'))
Пример #11
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
Пример #12
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)
Пример #13
0
 def deserialize_from(sig_bytes: bytes):
     ms = StreamManager.get_stream(sig_bytes)
     reader = BinaryReader(ms)
     return Sig.deserialize(reader)