2021-07-22 08:43:41 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
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,
|
2020-11-24 04:34:32 +00:00
|
|
|
../../protocol/waku_store/waku_store_types,
|
2021-04-08 09:55:19 +00:00
|
|
|
../../protocol/waku_message,
|
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
|
|
|
|
|
|
|
|
proc toPagingInfo*(pagingOptions: StorePagingOptions): PagingInfo =
|
|
|
|
PagingInfo(pageSize: pagingOptions.pageSize,
|
|
|
|
cursor: if pagingOptions.cursor.isSome: pagingOptions.cursor.get else: Index(),
|
|
|
|
direction: if pagingOptions.forward: PagingDirection.FORWARD else: PagingDirection.BACKWARD)
|
|
|
|
|
|
|
|
proc toPagingOptions*(pagingInfo: PagingInfo): StorePagingOptions =
|
|
|
|
StorePagingOptions(pageSize: pagingInfo.pageSize,
|
|
|
|
cursor: some(pagingInfo.cursor),
|
|
|
|
forward: if pagingInfo.direction == PagingDirection.FORWARD: true else: false)
|
|
|
|
|
|
|
|
proc toStoreResponse*(historyResponse: HistoryResponse): StoreResponse =
|
|
|
|
StoreResponse(messages: historyResponse.messages,
|
|
|
|
pagingOptions: if historyResponse.pagingInfo != PagingInfo(): some(historyResponse.pagingInfo.toPagingOptions()) else: none(StorePagingOptions))
|
2020-11-30 09:44:57 +00:00
|
|
|
|
|
|
|
proc toWakuMessage*(relayMessage: WakuRelayMessage, version: uint32): WakuMessage =
|
2021-04-08 09:55:19 +00:00
|
|
|
const defaultCT = ContentTopic("/waku/2/default-content/proto")
|
2021-07-21 18:05:10 +00:00
|
|
|
var t: float64
|
|
|
|
if relayMessage.timestamp.isSome:
|
|
|
|
t = relayMessage.timestamp.get
|
|
|
|
else:
|
|
|
|
# incoming WakuRelayMessages with no timestamp will get 0 timestamp
|
|
|
|
t = float64(0)
|
2020-11-30 09:44:57 +00:00
|
|
|
WakuMessage(payload: relayMessage.payload,
|
|
|
|
contentTopic: if relayMessage.contentTopic.isSome: relayMessage.contentTopic.get else: defaultCT,
|
2021-07-21 18:05:10 +00:00
|
|
|
version: version,
|
|
|
|
timestamp: t)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
proc toWakuMessage*(relayMessage: WakuRelayMessage, version: uint32, rng: ref BrHmacDrbgContext, symkey: Option[SymKey], pubKey: Option[keys.PublicKey]): WakuMessage =
|
|
|
|
# @TODO global definition for default content topic
|
2021-04-08 09:55:19 +00:00
|
|
|
const defaultCT = ContentTopic("/waku/2/default-content/proto")
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
let payload = Payload(payload: relayMessage.payload,
|
|
|
|
dst: pubKey,
|
|
|
|
symkey: symkey)
|
|
|
|
|
2021-07-21 18:05:10 +00:00
|
|
|
var t: float64
|
|
|
|
if relayMessage.timestamp.isSome:
|
|
|
|
t = relayMessage.timestamp.get
|
|
|
|
else:
|
|
|
|
# incoming WakuRelayMessages with no timestamp will get 0 timestamp
|
|
|
|
t = float64(0)
|
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
WakuMessage(payload: payload.encode(version, rng[]).get(),
|
|
|
|
contentTopic: if relayMessage.contentTopic.isSome: relayMessage.contentTopic.get else: defaultCT,
|
2021-07-21 18:05:10 +00:00
|
|
|
version: version,
|
|
|
|
timestamp: t)
|
2020-12-23 09:33:28 +00:00
|
|
|
|
|
|
|
proc toWakuRelayMessage*(message: WakuMessage, symkey: Option[SymKey], privateKey: Option[keys.PrivateKey]): WakuRelayMessage =
|
|
|
|
# @TODO global definition for default content topic
|
|
|
|
|
|
|
|
let
|
|
|
|
keyInfo = if symkey.isSome(): KeyInfo(kind: Symmetric, symKey: symkey.get())
|
|
|
|
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
|
|
|
|