2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2024-07-09 11:14:28 +00:00
|
|
|
import results
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../waku_core, ../waku_noise/noise_types, ../waku_noise/noise_utils
|
2023-02-07 09:45:25 +00:00
|
|
|
|
|
|
|
# Decodes a WakuMessage to a PayloadV2
|
|
|
|
# Currently, this is just a wrapper over deserializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
2024-03-15 23:08:47 +00:00
|
|
|
proc decodePayloadV2*(
|
|
|
|
message: WakuMessage
|
|
|
|
): Result[PayloadV2, cstring] {.raises: [NoiseMalformedHandshake, NoisePublicKeyError].} =
|
2023-02-07 09:45:25 +00:00
|
|
|
# We check message version (only 2 is supported in this proc)
|
|
|
|
case message.version
|
|
|
|
of 2:
|
|
|
|
# We attempt to decode the WakuMessage payload
|
|
|
|
let deserializedPayload2 = deserializePayloadV2(message.payload)
|
|
|
|
if deserializedPayload2.isOk():
|
|
|
|
return ok(deserializedPayload2.get())
|
|
|
|
else:
|
|
|
|
return err("Failed to decode WakuMessage")
|
|
|
|
else:
|
|
|
|
return err("Wrong message version while decoding payload")
|
|
|
|
|
|
|
|
# Encodes a PayloadV2 to a WakuMessage
|
|
|
|
# Currently, this is just a wrapper over serializePayloadV2 and encryption/decryption is done on top (no KeyInfo)
|
2024-03-15 23:08:47 +00:00
|
|
|
proc encodePayloadV2*(
|
|
|
|
payload2: PayloadV2, contentTopic: ContentTopic = default(ContentTopic)
|
|
|
|
): Result[WakuMessage, cstring] {.
|
|
|
|
raises: [NoiseMalformedHandshake, NoisePublicKeyError]
|
|
|
|
.} =
|
2023-02-07 09:45:25 +00:00
|
|
|
# We attempt to encode the PayloadV2
|
|
|
|
let serializedPayload2 = serializePayloadV2(payload2)
|
|
|
|
if not serializedPayload2.isOk():
|
|
|
|
return err("Failed to encode PayloadV2")
|
|
|
|
|
|
|
|
# If successful, we create and return a WakuMessage
|
2024-03-15 23:08:47 +00:00
|
|
|
let msg = WakuMessage(
|
|
|
|
payload: serializedPayload2.get(), version: 2, contentTopic: contentTopic
|
|
|
|
)
|
2023-02-07 09:45:25 +00:00
|
|
|
|
|
|
|
return ok(msg)
|