Esempio n. 1
0
    def getKey(self, keyId):
        """Retrieves keys from dht.

        Args:
            keyId: String or spki.Hash

        Returns:
            List or None: List of parsed spki.PublicKeys or None if 
            they didn't parse or none were found.
        """

        if isinstance(keyId, spki.Hash):
            key = hashToB64(keyId) + '-key'
        else:
            key = keyId + '-key'
        keys = yield self.dht.get(key)

        if keys is None:
            returnValue(None)

        parsedKeys = []
        for key in keys:
            try:
                k = spki.parse(key)
                if isinstance(keyId, spki.Hash):
                    if k.getPrincipal() == keyId:
                        returnValue(k)
                        break
                else:
                    parsedKeys.append(k)
            except sexp.ParseError:
                # ignore invalid data from dht
                continue
        if parsedKeys != []:
            returnValue(parsedKeys)
        else:
            returnValue(None)