2023-02-08 15:26:23 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import json, stew/[results, byteutils], ./protocol_types
|
2023-02-08 15:26:23 +00:00
|
|
|
|
2023-08-29 12:16:21 +00:00
|
|
|
# Encodes a KeystoreMembership credential to a byte sequence
|
|
|
|
proc encode*(credential: KeystoreMembership): seq[byte] =
|
2023-02-08 15:26:23 +00:00
|
|
|
# TODO: use custom encoding, avoid wordy json
|
|
|
|
var stringCredential: string
|
|
|
|
# NOTE: toUgly appends to the string, doesn't replace its contents
|
|
|
|
stringCredential.toUgly(%credential)
|
|
|
|
return toBytes(stringCredential)
|
|
|
|
|
2023-08-29 12:16:21 +00:00
|
|
|
# Decodes a byte sequence to a KeystoreMembership credential
|
|
|
|
proc decode*(encodedCredential: seq[byte]): KeystoreResult[KeystoreMembership] =
|
2023-02-08 15:26:23 +00:00
|
|
|
# TODO: use custom decoding, avoid wordy json
|
|
|
|
try:
|
|
|
|
# we parse the json decrypted keystoreCredential
|
|
|
|
let jsonObject = parseJson(string.fromBytes(encodedCredential))
|
2023-08-29 12:16:21 +00:00
|
|
|
return ok(to(jsonObject, KeystoreMembership))
|
2023-02-08 15:26:23 +00:00
|
|
|
except JsonParsingError:
|
2024-03-15 23:08:47 +00:00
|
|
|
return err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg()))
|
2023-02-08 15:26:23 +00:00
|
|
|
except Exception: #parseJson raises Exception
|
2024-03-15 23:08:47 +00:00
|
|
|
return err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg()))
|