2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-07-16 15:13:36 +00:00
|
|
|
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2020-11-03 20:20:40 +00:00
|
|
|
import
|
|
|
|
std/options,
|
2023-02-07 09:45:25 +00:00
|
|
|
stew/results,
|
|
|
|
eth/keys
|
|
|
|
import
|
2021-07-19 08:14:37 +00:00
|
|
|
../../whisper/whisper_types,
|
2023-02-07 09:45:25 +00:00
|
|
|
../protocol/waku_message
|
2020-11-03 20:20:40 +00:00
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
export whisper_types, keys, options
|
2020-11-03 20:20:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
KeyKind* = enum
|
|
|
|
Symmetric
|
|
|
|
Asymmetric
|
|
|
|
None
|
|
|
|
|
|
|
|
KeyInfo* = object
|
|
|
|
case kind*: KeyKind
|
|
|
|
of Symmetric:
|
|
|
|
symKey*: SymKey
|
|
|
|
of Asymmetric:
|
|
|
|
privKey*: PrivateKey
|
|
|
|
of None:
|
|
|
|
discard
|
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2020-11-03 20:20:40 +00:00
|
|
|
# TODO:
|
|
|
|
# - This is using `DecodedPayload` from Waku v1 / Whisper and could be altered
|
|
|
|
# by making that a case object also, e.g. useful for the version 0, but
|
|
|
|
# especially in the future if there would be yet another version.
|
|
|
|
# - Also reworking that API to use Result instead of Option could make this
|
|
|
|
# cleaner.
|
|
|
|
# - For now this `KeyInfo` is a bit silly also, but perhaps with v2 or
|
|
|
|
# adjustments to Waku v1 encoding, it can be better.
|
|
|
|
proc decodePayload*(message: WakuMessage, keyInfo: KeyInfo):
|
2023-02-07 09:45:25 +00:00
|
|
|
Result[DecodedPayload, cstring] =
|
2020-11-03 20:20:40 +00:00
|
|
|
case message.version
|
|
|
|
of 0:
|
|
|
|
return ok(DecodedPayload(payload:message.payload))
|
|
|
|
of 1:
|
|
|
|
case keyInfo.kind
|
|
|
|
of Symmetric:
|
|
|
|
let decoded = message.payload.decode(none[PrivateKey](),
|
|
|
|
some(keyInfo.symKey))
|
|
|
|
if decoded.isSome():
|
|
|
|
return ok(decoded.get())
|
|
|
|
else:
|
|
|
|
return err("Couldn't decrypt using symmetric key")
|
|
|
|
of Asymmetric:
|
|
|
|
let decoded = message.payload.decode(some(keyInfo.privkey),
|
|
|
|
none[SymKey]())
|
|
|
|
if decoded.isSome():
|
|
|
|
return ok(decoded.get())
|
|
|
|
else:
|
|
|
|
return err("Couldn't decrypt using asymmetric key")
|
2022-04-12 11:50:51 +00:00
|
|
|
else:
|
2020-11-03 20:20:40 +00:00
|
|
|
discard
|
|
|
|
else:
|
|
|
|
return err("Unsupported WakuMessage version")
|
|
|
|
|
|
|
|
# TODO: same story as for `decodedPayload`, but then regarding the `Payload`
|
|
|
|
# object.
|
2022-09-07 15:31:27 +00:00
|
|
|
proc encode*(payload: Payload, version: uint32, rng: var HmacDrbgContext):
|
2023-02-07 09:45:25 +00:00
|
|
|
Result[seq[byte], cstring] =
|
2020-11-03 20:20:40 +00:00
|
|
|
case version
|
|
|
|
of 0:
|
|
|
|
# This is rather silly
|
|
|
|
return ok(payload.payload)
|
|
|
|
of 1:
|
|
|
|
let encoded = encode(rng, payload)
|
|
|
|
if encoded.isSome():
|
|
|
|
return ok(encoded.get())
|
|
|
|
else:
|
|
|
|
return err("Couldn't encode the payload")
|
|
|
|
else:
|
|
|
|
return err("Unsupported WakuMessage version")
|