示例#1
0
 def parse_pub_keys(ont_id: 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'{ont_id}#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_ddo(ont_id: str, ddo: str) -> dict:
        if ddo == "":
            return dict()
        ms = StreamManager.GetStream(a2b_hex(ddo))
        reader = BinaryReader(ms)
        try:
            publickey_bytes = reader.read_var_bytes()
        except Exception as e:
            raise e
        try:
            attribute_bytes = reader.read_var_bytes()
        except Exception as e:
            attribute_bytes = bytearray()
        try:
            recovery_bytes = reader.read_var_bytes()
        except Exception as e:
            recovery_bytes = bytearray()
        pubKey_list = []
        if len(publickey_bytes) != 0:
            ms = StreamManager.GetStream(publickey_bytes)
            reader2 = BinaryReader(ms)
            while True:
                try:
                    index = reader2.read_int32()
                    d = {}
                    d['PubKeyId'] = ont_id + "#keys-" + str(index)
                    pubkey = reader2.read_var_bytes()
                    if len(pubkey) == 33:
                        d["Type"] = KeyType.ECDSA.name
                        d["Curve"] = Curve.P256.name
                        d["Value"] = pubkey.hex()
                    else:
                        d["Type"] = KeyType.from_label(pubkey[0])
                        d["Curve"] = Curve.from_label(pubkey[1])
                        d["Value"] = pubkey.hex()
                    pubKey_list.append(d)
                except Exception as e:
                    break
        attribute_list = []
        if len(attribute_bytes) != 0:
            ms = StreamManager.GetStream(attribute_bytes)
            reader2 = BinaryReader(ms)

            while True:
                try:
                    d = {}
                    key = reader2.read_var_bytes()
                    if len(key) == 0:
                        break
                    d["Key"] = str(key, 'utf-8')
                    d["Type"] = str(reader2.read_var_bytes(), 'utf-8')
                    d["Value"] = str(reader2.read_var_bytes(), 'utf-8')
                    attribute_list.append(d)
                except Exception as e:
                    break
        d2 = {}
        d2["Owners"] = pubKey_list
        d2["Attributes"] = attribute_list
        if len(recovery_bytes) != 0:
            addr = Address(recovery_bytes)
            d2["Recovery"] = addr.b58encode()
        d2["OntId"] = ont_id
        return d2
示例#3
0
 def test_from_label(self):
     self.assertRaises(SDKException, Curve.from_label, 0)
     curve_lst = ['P224', 'P256', 'P384', 'P521']
     label_lst = [1, 2, 3, 4]
     for index, label in enumerate(label_lst):
         self.assertEqual(curve_lst[index], Curve.from_label(label))