2026-07-16 14:02:17 -03:00
|
|
|
import std/times
|
2024-07-09 13:14:28 +02:00
|
|
|
import confutils, chronicles, chronos, results
|
2026-07-16 12:35:00 -03:00
|
|
|
import protobuf_serialization, protobuf_serialization/pkg/results
|
2024-03-06 18:44:33 +05:30
|
|
|
|
2026-06-08 13:37:53 +02:00
|
|
|
import logos_delivery/waku/[waku_core, common/protobuf]
|
2024-03-06 18:44:33 +05:30
|
|
|
|
2026-07-16 12:35:00 -03:00
|
|
|
export times, confutils, chronicles, chronos, results, waku_core, protobuf
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
type SerializedKey* = seq[byte]
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
type WakuStealthCommitmentMsg* = object
|
|
|
|
|
request*: bool
|
2026-07-16 14:02:17 -03:00
|
|
|
spendingPubKey*: Opt[SerializedKey]
|
|
|
|
|
viewingPubKey*: Opt[SerializedKey]
|
|
|
|
|
ephemeralPubKey*: Opt[SerializedKey]
|
|
|
|
|
stealthCommitment*: Opt[SerializedKey]
|
|
|
|
|
viewTag*: Opt[uint64]
|
2024-03-06 18:44:33 +05:30
|
|
|
|
2026-07-16 12:35:00 -03:00
|
|
|
type WakuStealthCommitmentMsgPB {.proto2.} = object
|
|
|
|
|
request {.fieldNumber: 1, pint.}: Opt[uint64]
|
|
|
|
|
spendingPubKey {.fieldNumber: 2.}: Opt[seq[byte]]
|
|
|
|
|
viewingPubKey {.fieldNumber: 3.}: Opt[seq[byte]]
|
|
|
|
|
stealthCommitment {.fieldNumber: 4.}: Opt[seq[byte]]
|
|
|
|
|
ephemeralPubKey {.fieldNumber: 5.}: Opt[seq[byte]]
|
|
|
|
|
viewTag {.fieldNumber: 6, pint.}: Opt[uint64]
|
|
|
|
|
|
|
|
|
|
proc nonEmptyKey(o: Opt[seq[byte]]): Opt[SerializedKey] =
|
|
|
|
|
if o.isSome() and o.get().len > 0:
|
|
|
|
|
o
|
|
|
|
|
else:
|
|
|
|
|
Opt.none(SerializedKey)
|
|
|
|
|
|
|
|
|
|
proc decode*(T: type WakuStealthCommitmentMsg, buffer: seq[byte]): ProtobufResult[T] =
|
|
|
|
|
var pb: WakuStealthCommitmentMsgPB
|
|
|
|
|
try:
|
|
|
|
|
pb = Protobuf.decode(buffer, WakuStealthCommitmentMsgPB)
|
|
|
|
|
except SerializationError:
|
|
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
|
|
|
|
|
2024-03-06 18:44:33 +05:30
|
|
|
var msg = WakuStealthCommitmentMsg()
|
2026-07-16 12:35:00 -03:00
|
|
|
msg.request = pb.request.get(0'u64) == 1
|
|
|
|
|
msg.spendingPubKey = nonEmptyKey(pb.spendingPubKey)
|
|
|
|
|
msg.viewingPubKey = nonEmptyKey(pb.viewingPubKey)
|
2024-03-16 00:08:47 +01:00
|
|
|
|
2024-03-06 18:44:33 +05:30
|
|
|
if msg.spendingPubKey.isSome() and msg.viewingPubKey.isSome():
|
2026-07-16 14:02:17 -03:00
|
|
|
msg.stealthCommitment = Opt.none(SerializedKey)
|
|
|
|
|
msg.viewTag = Opt.none(uint64)
|
2024-03-06 18:44:33 +05:30
|
|
|
return ok(msg)
|
|
|
|
|
if msg.spendingPubKey.isSome() and msg.viewingPubKey.isNone():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
if msg.spendingPubKey.isNone() and msg.viewingPubKey.isSome():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
if msg.request == true and msg.spendingPubKey.isNone() and msg.viewingPubKey.isNone():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
|
2026-07-16 12:35:00 -03:00
|
|
|
msg.stealthCommitment = nonEmptyKey(pb.stealthCommitment)
|
|
|
|
|
msg.ephemeralPubKey = nonEmptyKey(pb.ephemeralPubKey)
|
2024-03-06 18:44:33 +05:30
|
|
|
|
2026-07-16 12:35:00 -03:00
|
|
|
let viewTag = pb.viewTag.get(0'u64)
|
2024-03-16 00:08:47 +01:00
|
|
|
msg.viewTag =
|
|
|
|
|
if viewTag != 0:
|
2026-07-16 14:02:17 -03:00
|
|
|
Opt.some(viewTag)
|
2024-03-16 00:08:47 +01:00
|
|
|
else:
|
2026-07-16 14:02:17 -03:00
|
|
|
Opt.none(uint64)
|
2024-03-16 00:08:47 +01:00
|
|
|
|
|
|
|
|
if msg.stealthCommitment.isNone() and msg.viewTag.isNone() and
|
|
|
|
|
msg.ephemeralPubKey.isNone():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
if msg.stealthCommitment.isSome() and msg.viewTag.isNone():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
if msg.stealthCommitment.isNone() and msg.viewTag.isSome():
|
2026-07-16 12:35:00 -03:00
|
|
|
return err(protobuf.ProtobufError(kind: ProtobufErrorKind.DecodeFailure))
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
if msg.stealthCommitment.isSome() and msg.viewTag.isSome():
|
2026-07-16 14:02:17 -03:00
|
|
|
msg.spendingPubKey = Opt.none(SerializedKey)
|
|
|
|
|
msg.viewingPubKey = Opt.none(SerializedKey)
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
ok(msg)
|
|
|
|
|
|
2026-07-16 12:35:00 -03:00
|
|
|
proc encode*(msg: WakuStealthCommitmentMsg): seq[byte] =
|
|
|
|
|
Protobuf.encode(
|
|
|
|
|
WakuStealthCommitmentMsgPB(
|
|
|
|
|
request: Opt.some(uint64(msg.request)),
|
|
|
|
|
spendingPubKey: msg.spendingPubKey,
|
|
|
|
|
viewingPubKey: msg.viewingPubKey,
|
|
|
|
|
stealthCommitment: msg.stealthCommitment,
|
|
|
|
|
ephemeralPubKey: msg.ephemeralPubKey,
|
|
|
|
|
viewTag: msg.viewTag,
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-03-06 18:44:33 +05:30
|
|
|
|
|
|
|
|
func toByteSeq*(str: string): seq[byte] {.inline.} =
|
|
|
|
|
## Converts a string to the corresponding byte sequence.
|
|
|
|
|
@(str.toOpenArrayByte(0, str.high))
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc constructRequest*(
|
|
|
|
|
spendingPubKey: SerializedKey, viewingPubKey: SerializedKey
|
|
|
|
|
): WakuStealthCommitmentMsg =
|
|
|
|
|
WakuStealthCommitmentMsg(
|
|
|
|
|
request: true,
|
2026-07-16 14:02:17 -03:00
|
|
|
spendingPubKey: Opt.some(spendingPubKey),
|
|
|
|
|
viewingPubKey: Opt.some(viewingPubKey),
|
2024-03-16 00:08:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
proc constructResponse*(
|
|
|
|
|
stealthCommitment: SerializedKey, ephemeralPubKey: SerializedKey, viewTag: uint64
|
|
|
|
|
): WakuStealthCommitmentMsg =
|
|
|
|
|
WakuStealthCommitmentMsg(
|
|
|
|
|
request: false,
|
2026-07-16 14:02:17 -03:00
|
|
|
stealthCommitment: Opt.some(stealthCommitment),
|
|
|
|
|
ephemeralPubKey: Opt.some(ephemeralPubKey),
|
|
|
|
|
viewTag: Opt.some(viewTag),
|
2024-03-16 00:08:47 +01:00
|
|
|
)
|