mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-16 18:06:45 +00:00
fd6a71cdd7
* bump_dependencies.md: add nim-results dependency * change imports stew/results to results * switching to Nim 2.0.8 * waku.nimble: reflect the requirement nim 1.6.0 to 2.0.8 Adding --mm:refc as nim 2.0 enables a new garbage collector that we're not yet ready to support * adapt waku code to Nim 2.0 * gcsafe adaptations because Nim 2.0 is more strict
24 lines
1.0 KiB
Nim
24 lines
1.0 KiB
Nim
{.push raises: [].}
|
|
|
|
import json, results, stew/byteutils, ./protocol_types
|
|
|
|
# Encodes a KeystoreMembership credential to a byte sequence
|
|
proc encode*(credential: KeystoreMembership): seq[byte] =
|
|
# 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)
|
|
|
|
# Decodes a byte sequence to a KeystoreMembership credential
|
|
proc decode*(encodedCredential: seq[byte]): KeystoreResult[KeystoreMembership] =
|
|
# TODO: use custom decoding, avoid wordy json
|
|
try:
|
|
# we parse the json decrypted keystoreCredential
|
|
let jsonObject = parseJson(string.fromBytes(encodedCredential))
|
|
return ok(to(jsonObject, KeystoreMembership))
|
|
except JsonParsingError:
|
|
return err(AppKeystoreError(kind: KeystoreJsonError, msg: getCurrentExceptionMsg()))
|
|
except Exception: #parseJson raises Exception
|
|
return err(AppKeystoreError(kind: KeystoreOsError, msg: getCurrentExceptionMsg()))
|