From e55f01bba3c01e50439b48fd03243ee5a9357b65 Mon Sep 17 00:00:00 2001 From: Marcin Mielniczuk Date: Sun, 28 Apr 2019 21:54:28 +0200 Subject: [PATCH 1/2] Add .mypy_cache to .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9cf97601..acc2ff71 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ syntax: glob *.pyc *.pyo __pycache__/ -*.*~ \ No newline at end of file +*.*~ +.mypy_cache + -- GitLab From 8ed38b2a0f095bee5503ffea4bb5c83ce8a0db72 Mon Sep 17 00:00:00 2001 From: Marcin Mielniczuk Date: Sun, 28 Apr 2019 21:55:17 +0200 Subject: [PATCH 2/2] Add API to query the fingerprints from the app --- omemo/backend/liteaxolotlstore.py | 7 ++++--- omemo/backend/util.py | 4 +++- omemo/plugin.py | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/omemo/backend/liteaxolotlstore.py b/omemo/backend/liteaxolotlstore.py index 231969e7..31f6dfb1 100644 --- a/omemo/backend/liteaxolotlstore.py +++ b/omemo/backend/liteaxolotlstore.py @@ -18,6 +18,7 @@ import time import sqlite3 from collections import namedtuple +from typing import List from axolotl.state.axolotlstore import AxolotlStore from axolotl.state.signedprekeyrecord import SignedPreKeyRecord @@ -229,7 +230,7 @@ class LiteAxolotlStore(AxolotlStore): CREATE TABLE IF NOT EXISTS secret ( device_id INTEGER, public_key BLOB, private_key BLOB); INSERT INTO secret (device_id, public_key, private_key) - SELECT registration_id + 1, public_key, private_key + SELECT registration_id + 1, public_key, private_key FROM identities WHERE recipient_id = -1; """ @@ -540,7 +541,7 @@ class LiteAxolotlStore(AxolotlStore): result = self._con.execute(query, (recipientId, public_key)).fetchone() return result.trust if result is not None else None - def getFingerprints(self, jid): + def getFingerprints(self, jid: str) -> list: query = '''SELECT recipient_id as "recipient_id [jid]", public_key as "public_key [pk]", trust, @@ -549,7 +550,7 @@ class LiteAxolotlStore(AxolotlStore): WHERE recipient_id = ? ORDER BY trust ASC''' return self._con.execute(query, (jid,)).fetchall() - def getMucFingerprints(self, jids): + def getMucFingerprints(self, jids: List[str]) -> list: query = ''' SELECT recipient_id as "recipient_id [jid]", public_key as "public_key [pk]", diff --git a/omemo/backend/util.py b/omemo/backend/util.py index f63801c3..edc31482 100644 --- a/omemo/backend/util.py +++ b/omemo/backend/util.py @@ -34,6 +34,8 @@ class Trust(IntEnum): UNDECIDED = 2 +# This is not a method of IdentityKeyExtended, because we also want to use it +# for axolotl.identitykey.IdentityKey def get_fingerprint(identity_key, formatted=False): public_key = identity_key.getPublicKey().serialize() fingerprint = binascii.hexlify(public_key).decode()[2:] @@ -52,5 +54,5 @@ class IdentityKeyExtended(IdentityKey): def __hash__(self): return hash(self.publicKey.serialize()) - def get_fingerprint(self, formatted=False): + def get_fingerprint(self, formatted: bool = False) -> str: return get_fingerprint(self, formatted=formatted) diff --git a/omemo/plugin.py b/omemo/plugin.py index 40dc694a..e37982a4 100644 --- a/omemo/plugin.py +++ b/omemo/plugin.py @@ -21,6 +21,7 @@ import binascii import threading from enum import IntEnum, unique from pathlib import Path +from typing import Dict from gi.repository import GLib from gi.repository import Gtk @@ -28,6 +29,7 @@ from gi.repository import Gdk from gajim import dialogs from gajim.common import app, ged +from gajim.common.const import Trust as GajimTrust from gajim.plugins import GajimPlugin from gajim.plugins.plugins_i18n import _ from gajim.groupchat_control import GroupchatControl @@ -36,6 +38,7 @@ from omemo import file_crypto from omemo.gtk.key import KeyDialog from omemo.gtk.config import OMEMOConfigDialog from omemo.backend.aes import aes_encrypt_file +from omemo.backend.util import IdentityKeyExtended, Trust AXOLOTL_MISSING = 'You are missing Python3-Axolotl or use an outdated version' @@ -329,3 +332,21 @@ class OmemoPlugin(GajimPlugin): if msg is None: return chat_control.print_conversation_line(msg, 'status', '', None) + + def get_fingerprints(self, account: str, jid: str, + groupchat: bool = False) -> Dict[str, GajimTrust]: + omemo = self.get_omemo(account) + + if groupchat: + members = list(omemo.backend.get_muc_members(jid)) + fpr_list = omemo.backend.storage.getMucFingerprints(members) + else: + fpr_list = omemo.backend.storage.getFingerprints(jid) + + # every row is like: + # recipient_id: str, public_key: omemo.backend.util.IdentityKeyExtended, trust: int, timestamp: Optional[int] + # FIXME, that code is awful + return { + row.public_key.get_fingerprint(): GajimTrust[Trust(row.trust).name] + for row in fpr_list + } -- GitLab