From 5cf58a78e54ce4e32a2c972a0394973fa34e565f Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Thu, 30 Sep 2021 16:36:28 +0300 Subject: [PATCH] implement pairing storage --- src/app/keycard/core.nim | 8 +++++++- src/app/keycard/pairing.nim | 30 ++++++++++++++++++++++++++++++ src/app/keycard/view.nim | 3 +++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/app/keycard/pairing.nim diff --git a/src/app/keycard/core.nim b/src/app/keycard/core.nim index 7d24aecf9e..af02b1c4ab 100644 --- a/src/app/keycard/core.nim +++ b/src/app/keycard/core.nim @@ -1,6 +1,6 @@ import NimQml, chronicles, std/wrapnils import status/[signals, status, keycard] -import view +import view, pairing logScope: topics = "keycard-model" @@ -24,6 +24,12 @@ proc reset*(self: KeycardController) = discard proc attemptOpenSecureChannel(self: KeycardController) : bool = + let pairing = self.view.pairings.getPairing(self.view.appInfo.instanceUID) + + if pairing == "": + return false + + # actually open secure channel return false proc getCardState(self: KeycardController) = diff --git a/src/app/keycard/pairing.nim b/src/app/keycard/pairing.nim new file mode 100644 index 0000000000..d94c98f483 --- /dev/null +++ b/src/app/keycard/pairing.nim @@ -0,0 +1,30 @@ +import json, os, std/wrapnils + +import ../../constants + +let PAIRINGSTORE = joinPath(DATADIR, "keycard-pairings.json") + +type KeycardPairingController* = ref object + store: JsonNode + +proc newPairingController*(): KeycardPairingController = + result = KeycardPairingController() + if fileExists(PAIRINGSTORE): + result.store = parseJSON(readFile(PAIRINGSTORE)) + else: + result.store = %*{} + +proc save(self: KeycardPairingController) = + writeFile(PAIRINGSTORE, $self.store) + +proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: string) = + self.store[instanceUID] = %* pairing + self.save() + +proc removePairing*(self: KeycardPairingController, instanceUID: string) = + self.store.delete(instanceUID) + self.save() + +proc getPairing*(self: KeycardPairingController, instanceUID: string): string = + let node = self.store{instanceUID} + return ?.node.getStr() diff --git a/src/app/keycard/view.nim b/src/app/keycard/view.nim index 4a1c24793e..38df5aee56 100644 --- a/src/app/keycard/view.nim +++ b/src/app/keycard/view.nim @@ -1,6 +1,7 @@ import NimQml, chronicles import status/[status, keycard] import status/types/keycard as keycardtypes +import pairing logScope: topics = "keycard-model" @@ -20,6 +21,7 @@ type QtObject: type KeycardView* = ref object of QObject status*: Status + pairings*: KeycardPairingController cardState*: CardState appInfo*: KeycardApplicationInfo @@ -32,6 +34,7 @@ QtObject: proc newKeycardView*(status: Status): KeycardView = new(result, delete) result.status = status + result.pairings = newPairingController() result.setup proc cardConnected*(self: KeycardView) {.signal.}