2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-07-22 08:43:41 +00:00
|
|
|
|
2020-11-24 03:44:37 +00:00
|
|
|
import
|
2021-03-23 08:04:51 +00:00
|
|
|
std/[options, json],
|
2020-12-23 09:33:28 +00:00
|
|
|
eth/keys,
|
|
|
|
../../../v1/node/rpc/hexstrings,
|
2021-04-08 09:55:19 +00:00
|
|
|
../../protocol/waku_message,
|
2022-09-28 11:36:05 +00:00
|
|
|
../../protocol/waku_store,
|
2022-11-09 17:50:18 +00:00
|
|
|
../../protocol/waku_store/rpc,
|
2022-02-17 15:00:15 +00:00
|
|
|
../../utils/time,
|
2021-01-22 09:39:16 +00:00
|
|
|
../waku_payload,
|
2020-11-24 03:44:37 +00:00
|
|
|
./jsonrpc_types
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
export hexstrings
|
|
|
|
|
2021-03-22 15:13:56 +00:00
|
|
|
## Json marshalling
|
|
|
|
|
|
|
|
proc `%`*(value: WakuMessage): JsonNode =
|
|
|
|
## This ensures that seq[byte] fields are marshalled to hex-format JStrings
|
|
|
|
## (as defined in `hexstrings.nim`) rather than the default JArray[JInt]
|
|
|
|
let jObj = newJObject()
|
|
|
|
for k, v in value.fieldPairs:
|
|
|
|
jObj[k] = %v
|
|
|
|
return jObj
|
|
|
|
|
2020-11-24 03:44:37 +00:00
|
|
|
## Conversion tools
|
|
|
|
## Since the Waku v2 JSON-RPC API has its own defined types,
|
|
|
|
## we need to convert between these and the types for the Nim API
|
|
|
|
|
2022-11-09 17:50:18 +00:00
|
|
|
proc toPagingInfo*(pagingOptions: StorePagingOptions): PagingInfoRPC =
|
2022-11-17 19:40:08 +00:00
|
|
|
PagingInfoRPC(
|
|
|
|
pageSize: some(pagingOptions.pageSize),
|
|
|
|
cursor: pagingOptions.cursor,
|
|
|
|
direction: if pagingOptions.forward: some(PagingDirectionRPC.FORWARD)
|
|
|
|
else: some(PagingDirectionRPC.BACKWARD)
|
|
|
|
)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
2022-11-09 17:50:18 +00:00
|
|
|
proc toPagingOptions*(pagingInfo: PagingInfoRPC): StorePagingOptions =
|
2022-11-17 19:40:08 +00:00
|
|
|
StorePagingOptions(
|
|
|
|
pageSize: pagingInfo.pageSize.get(0'u64),
|
|
|
|
cursor: pagingInfo.cursor,
|
|
|
|
forward: if pagingInfo.direction.isNone(): true
|
|
|
|
else: pagingInfo.direction.get() == PagingDirectionRPC.FORWARD
|
|
|
|
)
|
2020-11-24 03:44:37 +00:00
|
|
|
|
2022-11-09 17:50:18 +00:00
|
|
|
proc toJsonRPCStoreResponse*(response: HistoryResponse): StoreResponse =
|
|
|
|
StoreResponse(
|
|
|
|
messages: response.messages,
|
|
|
|
pagingOptions: if response.cursor.isNone(): none(StorePagingOptions)
|
|
|
|
else: some(StorePagingOptions(
|
2022-11-23 09:08:00 +00:00
|
|
|
pageSize: uint64(response.messages.len), # This field will be deprecated soon
|
|
|
|
forward: true, # Hardcoded. This field will be deprecated soon
|
2022-11-09 17:50:18 +00:00
|
|
|
cursor: response.cursor.map(toRPC)
|
|
|
|
))
|
|
|
|
)
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
|
|
proc toWakuMessage*(relayMessage: WakuRelayMessage, version: uint32): WakuMessage =
|
2022-02-17 15:00:15 +00:00
|
|
|
var t: Timestamp
|
2022-11-23 09:08:00 +00:00
|
|
|
if relayMessage.timestamp.isSome:
|
|
|
|
t = relayMessage.timestamp.get
|
|
|
|
else:
|
2021-07-21 18:05:10 +00:00
|
|
|
# incoming WakuRelayMessages with no timestamp will get 0 timestamp
|
2022-02-17 15:00:15 +00:00
|
|
|
t = Timestamp(0)
|
2020-11-30 09:44:57 +00:00
|
|
|
WakuMessage(payload: relayMessage.payload,
|
2022-11-09 14:00:11 +00:00
|
|
|
contentTopic: relayMessage.contentTopic.get(DefaultContentTopic),
|
2021-07-21 18:05:10 +00:00
|
|
|
version: version,
|
2022-11-23 09:08:00 +00:00
|
|
|
timestamp: t)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
proc toWakuMessage*(relayMessage: WakuRelayMessage, version: uint32, rng: ref HmacDrbgContext, symkey: Option[SymKey], pubKey: Option[keys.PublicKey]): WakuMessage =
|
2020-12-23 09:33:28 +00:00
|
|
|
let payload = Payload(payload: relayMessage.payload,
|
|
|
|
dst: pubKey,
|
|
|
|
symkey: symkey)
|
|
|
|
|
2022-02-17 15:00:15 +00:00
|
|
|
var t: Timestamp
|
2022-11-23 09:08:00 +00:00
|
|
|
if relayMessage.timestamp.isSome:
|
|
|
|
t = relayMessage.timestamp.get
|
|
|
|
else:
|
2021-07-21 18:05:10 +00:00
|
|
|
# incoming WakuRelayMessages with no timestamp will get 0 timestamp
|
2022-02-17 15:00:15 +00:00
|
|
|
t = Timestamp(0)
|
2021-07-21 18:05:10 +00:00
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
WakuMessage(payload: payload.encode(version, rng[]).get(),
|
2022-11-09 14:00:11 +00:00
|
|
|
contentTopic: relayMessage.contentTopic.get(DefaultContentTopic),
|
2021-07-21 18:05:10 +00:00
|
|
|
version: version,
|
2022-11-23 09:08:00 +00:00
|
|
|
timestamp: t)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
proc toWakuRelayMessage*(message: WakuMessage, symkey: Option[SymKey], privateKey: Option[keys.PrivateKey]): WakuRelayMessage =
|
|
|
|
let
|
2022-11-23 09:08:00 +00:00
|
|
|
keyInfo = if symkey.isSome(): KeyInfo(kind: Symmetric, symKey: symkey.get())
|
2020-12-23 09:33:28 +00:00
|
|
|
elif privateKey.isSome(): KeyInfo(kind: Asymmetric, privKey: privateKey.get())
|
2021-07-16 22:28:35 +00:00
|
|
|
else: KeyInfo(kind: KeyKind.None)
|
2020-12-23 09:33:28 +00:00
|
|
|
decoded = decodePayload(message, keyInfo)
|
|
|
|
|
|
|
|
WakuRelayMessage(payload: decoded.get().payload,
|
2021-07-21 18:05:10 +00:00
|
|
|
contentTopic: some(message.contentTopic),
|
|
|
|
timestamp: some(message.timestamp))
|
2020-12-23 09:33:28 +00:00
|
|
|
|