From 4bafb4bb6b0d308a6334d0d5ed1f479e830546b2 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Wed, 6 Oct 2021 09:56:17 +0200 Subject: [PATCH] add keycard pair and generateKey methods --- backends/backend.nim | 6 ++++-- backends/base/keycard.nim | 7 +++++++ backends/mock/keycard.nim | 5 +++++ backends/statusgo/keycard.nim | 16 ++++++++++++++++ status/keycard.nim | 12 ++++++++++++ types/keycard.nim | 4 ++++ 6 files changed, 48 insertions(+), 2 deletions(-) diff --git a/backends/backend.nim b/backends/backend.nim index c15c68c..fe2aeb2 100644 --- a/backends/backend.nim +++ b/backends/backend.nim @@ -5,9 +5,11 @@ from base/bookmarks as bookmarks_methods import storeBookmark, updateBookmark, g export storeBookmark, updateBookmark, getBookmarks, deleteBookmark from base/keycard as keycard_methods import keycardStart, keycardStop, keycardSelect, keycardPair, - keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication + keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication, + keycardUnpair, keycardGenerateKey export keycardStart, keycardStop, keycardSelect, keycardPair, - keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication + keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication, + keycardUnpair, keycardGenerateKey import statusgo/bookmarks as statusgo_bookmarks import mock/bookmarks as mock_bookmarks diff --git a/backends/base/keycard.nim b/backends/base/keycard.nim index 742007b..3f6bfbf 100644 --- a/backends/base/keycard.nim +++ b/backends/base/keycard.nim @@ -24,3 +24,10 @@ method keycardExportKey*(self: Backend, derive: bool, makeCurrent: bool, onlyPub method keycardGetStatusApplication*(self: Backend): KeycardStatus = raise newException(ValueError, "No implementation available") + +method keycardUnpair*(self: Backend, index: int) = + raise newException(ValueError, "No implementation available") + +method keycardGenerateKey*(self: Backend): string = + raise newException(ValueError, "No implementation available") + diff --git a/backends/mock/keycard.nim b/backends/mock/keycard.nim index 39484e1..3b1cb2b 100644 --- a/backends/mock/keycard.nim +++ b/backends/mock/keycard.nim @@ -20,3 +20,8 @@ method keycardExportKey*(self: MockBackend, derive: bool, makeCurrent: bool, onl method keycardGetStatusApplication*(self: MockBackend): KeycardStatus = result = KeycardStatus() + +method keycardUnpair*(self: MockBackend, index: int) = discard + +method keycardGenerateKey*(self: MockBackend): string = + result = "0x00" diff --git a/backends/statusgo/keycard.nim b/backends/statusgo/keycard.nim index 77e2155..bb9e880 100644 --- a/backends/statusgo/keycard.nim +++ b/backends/statusgo/keycard.nim @@ -91,3 +91,19 @@ method keycardGetStatusApplication*(self: StatusGoBackend): KeycardStatus = pukRetryCount: parsedResponse["status"]["pukRetryCount"].getInt(), keyInitialized: parsedResponse["status"]["keyInitialized"].getBool() ) + +method keycardUnpair*(self: StatusGoBackend, index: int) = + let inputJSON = %* { + "index": index, + } + let response = keycard_go.unpair($inputJSON) + let parsedResponse = parseJson(response) + if not parsedResponse{"ok"}.getBool(): + raise KeycardUnpairException(error: parsedResponse{"error"}.getStr()) + +method keycardGenerateKey*(self: StatusGoBackend): string = + let response = keycard_go.generateKey() + let parsedResponse = parseJson(response) + if not parsedResponse{"ok"}.getBool(): + raise KeycardGenerateKeyException(error: parsedResponse{"error"}.getStr()) + result = parsedResponse["keyUID"].getStr() diff --git a/status/keycard.nim b/status/keycard.nim index 1f26e3d..b2a8214 100644 --- a/status/keycard.nim +++ b/status/keycard.nim @@ -58,3 +58,15 @@ proc getStatusApplication*(self: KeycardModel): KeycardStatus = result = self.backend.keycardGetStatusApplication() except: raise + +proc unpair*(self: KeycardModel, index: int) = + try: + self.backend.keycardUnpair(index) + except: + raise + +proc generateKey*(self: KeycardModel): string = + try: + result = self.backend.keycardGenerateKey() + except: + raise diff --git a/types/keycard.nim b/types/keycard.nim index 7396b80..a5295ae 100644 --- a/types/keycard.nim +++ b/types/keycard.nim @@ -13,6 +13,10 @@ type KeycardOpenSecureChannelException* = ref object of KeycardException type KeycardGetStatusException* = ref object of KeycardException +type KeycardUnpairException* = ref object of KeycardException + +type KeycardGenerateKeyException* = ref object of KeycardException + type KeycardVerifyPINException* = ref object of KeycardException pinRetry*: int64