From 5fd312a041849fcc6fea611ded66875e7cdf8a10 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Tue, 5 Oct 2021 11:35:07 +0200 Subject: [PATCH] add keycardGetStatusApplication --- backends/backend.nim | 4 ++-- backends/base/keycard.nim | 3 +++ backends/mock/keycard.nim | 3 +++ backends/statusgo/keycard.nim | 11 +++++++++++ status/keycard.nim | 6 ++++++ test_nim/keycard.nim | 15 +++++++++++++++ types/keycard.nim | 8 ++++++++ 7 files changed, 48 insertions(+), 2 deletions(-) diff --git a/backends/backend.nim b/backends/backend.nim index ba3214d..c15c68c 100644 --- a/backends/backend.nim +++ b/backends/backend.nim @@ -5,9 +5,9 @@ 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 + keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication export keycardStart, keycardStop, keycardSelect, keycardPair, - keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey + keycardOpenSecureChannel, keycardVerifyPin, keycardExportKey, keycardGetStatusApplication 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 52b6a0d..742007b 100644 --- a/backends/base/keycard.nim +++ b/backends/base/keycard.nim @@ -21,3 +21,6 @@ method keycardVerifyPin*(self: Backend, pin: string) = method keycardExportKey*(self: Backend, derive: bool, makeCurrent: bool, onlyPublic: bool, path: string): string = raise newException(ValueError, "No implementation available") + +method keycardGetStatusApplication*(self: Backend): KeycardStatus = + raise newException(ValueError, "No implementation available") diff --git a/backends/mock/keycard.nim b/backends/mock/keycard.nim index 6a7cc71..39484e1 100644 --- a/backends/mock/keycard.nim +++ b/backends/mock/keycard.nim @@ -17,3 +17,6 @@ method keycardVerifyPin*(self: MockBackend, pin: string) = discard method keycardExportKey*(self: MockBackend, derive: bool, makeCurrent: bool, onlyPublic: bool, path: string): string = result = "0x00" + +method keycardGetStatusApplication*(self: MockBackend): KeycardStatus = + result = KeycardStatus() diff --git a/backends/statusgo/keycard.nim b/backends/statusgo/keycard.nim index 0048cc9..77e2155 100644 --- a/backends/statusgo/keycard.nim +++ b/backends/statusgo/keycard.nim @@ -80,3 +80,14 @@ method keycardExportKey*(self: StatusGoBackend, derive: bool, makeCurrent: bool, raise KeycardSelectException(error: parsedResponse{"error"}.getStr()) result = parsedResponse["key"].getStr() + +method keycardGetStatusApplication*(self: StatusGoBackend): KeycardStatus = + let response = keycard_go.getStatusApplication() + let parsedResponse = parseJson(response) + if not parsedResponse{"ok"}.getBool(): + raise KeycardGetStatusException(error: parsedResponse{"error"}.getStr()) + result = KeycardStatus( + pinRetryCount: parsedResponse["status"]["pinRetryCount"].getInt(), + pukRetryCount: parsedResponse["status"]["pukRetryCount"].getInt(), + keyInitialized: parsedResponse["status"]["keyInitialized"].getBool() + ) diff --git a/status/keycard.nim b/status/keycard.nim index 0bc5c2a..1f26e3d 100644 --- a/status/keycard.nim +++ b/status/keycard.nim @@ -52,3 +52,9 @@ proc exportKey*(self: KeycardModel, derive: bool, makeCurrent: bool, onlyPublic: result = self.backend.keycardExportKey(derive, makeCurrent, onlyPublic, path) except: raise + +proc getStatusApplication*(self: KeycardModel): KeycardStatus = + try: + result = self.backend.keycardGetStatusApplication() + except: + raise diff --git a/test_nim/keycard.nim b/test_nim/keycard.nim index 7454c78..4076302 100644 --- a/test_nim/keycard.nim +++ b/test_nim/keycard.nim @@ -49,3 +49,18 @@ suite "#Keycard": except KeycardException as ex: echo "keycard exception" echo repr(ex) + + test "getStatusApplication": + try: + statuslib_instance.keycard.start() + discard statuslib_instance.keycard.select() + statuslib_instance.keycard.openSecureChannel(0, "000000") + let info = statuslib_instance.keycard.getStatusApplication() + # echo repr(info) + statuslib_instance.keycard.stop() + except KeycardGetStatusException as ex: + echo "keycard get status exception" + echo repr(ex) + except KeycardException as ex: + echo "keycard exception" + echo repr(ex) diff --git a/types/keycard.nim b/types/keycard.nim index 042711f..7396b80 100644 --- a/types/keycard.nim +++ b/types/keycard.nim @@ -11,6 +11,8 @@ type KeycardPairException* = ref object of KeycardException type KeycardOpenSecureChannelException* = ref object of KeycardException +type KeycardGetStatusException* = ref object of KeycardException + type KeycardVerifyPINException* = ref object of KeycardException pinRetry*: int64 @@ -29,3 +31,9 @@ type KeycardApplicationInfo* = ref object type KeycardPairingInfo* = ref object key*: string index*: int64 + +type KeycardStatus* = ref object + pinRetryCount*: int64 + pukRetryCount *: int64 + keyInitialized*: bool + path*: string