status-lib/backends/statusgo/keycard.nim

83 lines
3.1 KiB
Nim
Raw Normal View History

Keycard initial integration (#63) * add nim-keycard-go to makefile * fix keycard-go build * add keycard.nim * remove test make task * remove debug line from makefile * fix import keycard_go * add keycard-go to .PHONY * add keycard start/stop/select methods * use https url for submodule * add KeycardApplication info and return it from select * update nim-keycard-go version * fix select return type * actually return the result * update nim-keycard-go * add keycard methods to backend * add base/mock keycard backends * imports * export keycard methods in backend * update start/stop keycard implementation * add KeycardStarted signal * fix keycard started signal * rename to KeycardConnected signal * fix keycard signal renamed * add keycardgo in makefile tasks * add back build/.gitignore * fix .gitignore * fix .gitignore * Makefile: export keycard vars * add keycard lib to nimble file * use spaces instead of tabs in non-recipe sections of Makefile * use install_name_tool on libkeycard on macOS * in GHA ubuntu environment install libpcsclite-dev with apt at start of workflow * add Keycard exceptions * remove useless test * remove useless return * move keycard types to /types * reraise exception from status/keycard.nim * remove unused import * add keycard commands: opensecure channel, pair, verify pin, export key * fix last keycard commands * add exportKey params * update nim-keycard-go Co-authored-by: Michele Balistreri <michele@bitgamma.com> Co-authored-by: Michael Bradley, Jr <michaelsbradleyjr@gmail.com>
2021-10-04 21:21:07 +00:00
import json
import keycard_go
import ../../types/[keycard]
import ../types
import ./core
method keycardStart*(self: StatusGoBackend) =
let response = keycard_go.start()
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardStartException(error: parsedResponse{"error"}.getStr())
method keycardStop*(self: StatusGoBackend) =
let response = keycard_go.stop()
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardStopException(error: parsedResponse{"error"}.getStr())
method keycardSelect*(self: StatusGoBackend): KeycardApplicationInfo =
let response = keycard_go.select()
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardSelectException(error: parsedResponse{"error"}.getStr())
return KeycardApplicationInfo(
installed: parsedResponse["applicationInfo"]["installed"].getBool(),
initialized: parsedResponse["applicationInfo"]["initialized"].getBool(),
instanceUID: parsedResponse["applicationInfo"]["instanceUID"].getStr(),
secureChannelPublicKey: parsedResponse["applicationInfo"]["secureChannelPublicKey"].getStr(),
version: parsedResponse["applicationInfo"]["version"].getInt(),
availableSlots: parsedResponse["applicationInfo"]["availableSlots"].getInt(),
keyUID: parsedResponse["applicationInfo"]["keyUID"].getStr(),
capabilities: parsedResponse["applicationInfo"]["capabilities"].getInt()
)
method keycardPair*(self: StatusGoBackend, pairingPassword: string): KeycardPairingInfo =
let inputJSON = %* {
"pairingPassword": pairingPassword
}
let response = keycard_go.pair($inputJSON)
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardPairException(error: parsedResponse{"error"}.getStr())
result = KeycardPairingInfo(
key: parsedResponse["pairingInfo"]["key"].getStr(),
index: parsedResponse["pairingInfo"]["index"].getInt(),
)
method keycardOpenSecureChannel*(self: StatusGoBackend, index: int, key: string) =
let inputJSON = %* {
"key": key,
"index": index
}
let response = keycard_go.openSecureChannel($inputJSON)
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardOpenSecureChannelException(error: parsedResponse{"error"}.getStr())
method keycardVerifyPin*(self: StatusGoBackend, pin: string) =
let inputJSON = %* {
"pin": pin
}
let response = keycard_go.verifyPin($inputJSON)
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardVerifyPINException(error: parsedResponse{"error"}.getStr())
method keycardExportKey*(self: StatusGoBackend, derive: bool, makeCurrent: bool, onlyPublic: bool, path: string): string =
let inputJSON = %* {
"derive": derive,
"makeCurrent": makeCurrent,
"onlyPublic": onlyPublic,
"path": path
}
let response = keycard_go.exportKey($inputJSON)
let parsedResponse = parseJson(response)
if not parsedResponse{"ok"}.getBool():
raise KeycardSelectException(error: parsedResponse{"error"}.getStr())
result = parsedResponse["key"].getStr()