validation=kdict[KEY_VALIDATION_KEY], # TODO: verify for validation.
)
-
- def keymanager_doc_id(ktype, address, private=False):
- """
- Return the document id for the document containing a key for
- C{address}.
-
- :param address: The type of the key.
- :type address: KeyType
- :param address: The address bound to the key.
- :type address: str
- :param private: Whether the key is private or not.
- :type private: bool
- :return: The document id for the document that stores a key bound to
- C{address}.
- :rtype: str
- """
- leap_assert(is_address(address), "Wrong address format: %s" % address)
- ktype = str(ktype)
- visibility = KEY_PRIVATE_KEY if private else 'public'
- return sha256('keymanager-'+address+'-'+ktype+'-'+visibility).hexdigest()
-
--
#
# Abstraction for encryption keys
#
"""
Initialize this Encryption Scheme.
- @param soledad: A Soledad instance for local storage of keys.
- @type soledad: leap.soledad.Soledad
+ :param soledad: A Soledad instance for local storage of keys.
+ :type soledad: leap.soledad.Soledad
"""
self._soledad = soledad
+ self._init_indexes()
+
+ def _init_indexes(self):
+ """
+ Initialize the database indexes.
+ """
+ # Ask the database for currently existing indexes.
+ db_indexes = dict(self._soledad.list_indexes())
+ # Loop through the indexes we expect to find.
+ for name, expression in INDEXES.items():
+ if name not in db_indexes:
+ # The index does not yet exist.
+ self._soledad.create_index(name, *expression)
+ continue
+ if expression == db_indexes[name]:
+ # The index exists and is up to date.
+ continue
+ # The index exists but the definition is not what expected, so we
+ # delete it and add the proper index expression.
+ self._soledad.delete_index(name)
+ self._soledad.create_index(name, *expression)
@abstractmethod
def get_key(self, address, private=False):
If C{private} is True, looks for a private key instead of a public.
- @param address: The address bound to the key.
- @type address: str
- @param private: Whether to look for a private key.
- @type private: bool
- @return: The document with the key or None if it does not exist.
- @rtype: leap.soledad.backends.leap_backend.LeapDocument
+ :param address: The address bound to the key.
+ :type address: str
+ :param private: Whether to look for a private key.
+ :type private: bool
+ :return: The document with the key or None if it does not exist.
+ :rtype: leap.soledad.backends.leap_backend.LeapDocument
"""
- return self._soledad.get_doc(
- keymanager_doc_id(OpenPGPKey, address, private))
+ doclist = self._soledad.get_from_index(
+ TAGS_ADDRESS_PRIVATE_INDEX,
+ KEYMANAGER_KEY_TAG,
+ address,
+ '1' if private else '0')
+ if len(doclist) is 0:
+ return None
+ leap_assert(
+ len(doclist) is 1,
+ 'Found more than one %s key for address!' %
+ 'private' if private else 'public')
+ return doclist.pop()
def delete_key(self, key):
"""