2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-02-08 15:26:23 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import json, results, stew/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-10-02 22:05:49 +00:00
|
|
|
return err(
|
|
|
|
AppKeystoreError(
|
|
|
|
kind: KeystoreJsonError,
|
|
|
|
msg: "error during decoding credentials: " & getCurrentExceptionMsg(),
|
|
|
|
)
|
|
|
|
)
|
2023-02-08 15:26:23 +00:00
|
|
|
except Exception: #parseJson raises Exception
|
2024-10-02 22:05:49 +00:00
|
|
|
return err(
|
|
|
|
AppKeystoreError(
|
|
|
|
kind: KeystoreOsError,
|
|
|
|
msg: "error in conversion_utils decode: " & getCurrentExceptionMsg(),
|
|
|
|
)
|
|
|
|
)
|