2023-02-10 10:43:16 +01:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
std/sequtils,
|
|
|
|
|
chronicles,
|
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
|
eth/keys,
|
|
|
|
|
nimcrypto/sysrand
|
|
|
|
|
import
|
2023-08-09 18:11:50 +01:00
|
|
|
|
../../../common/base64,
|
2023-04-19 13:29:23 +02:00
|
|
|
|
../../../waku_core,
|
2023-04-18 15:22:10 +02:00
|
|
|
|
../../../waku_relay,
|
2023-09-11 12:02:31 +05:30
|
|
|
|
../../../waku_rln_relay,
|
|
|
|
|
../../../waku_rln_relay/rln/wrappers,
|
2023-03-06 17:19:06 +01:00
|
|
|
|
../../waku_node,
|
|
|
|
|
../../message_cache,
|
2023-08-07 15:11:46 +01:00
|
|
|
|
../message
|
2023-02-10 10:43:16 +01:00
|
|
|
|
|
2023-08-16 14:02:22 +05:30
|
|
|
|
from std/times import getTime
|
|
|
|
|
from std/times import toUnix
|
|
|
|
|
|
|
|
|
|
|
2023-02-10 10:43:16 +01:00
|
|
|
|
logScope:
|
|
|
|
|
topics = "waku node jsonrpc relay_api"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const futTimeout* = 5.seconds # Max time to wait for futures
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
MessageCache* = message_cache.MessageCache[PubsubTopic]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Waku Relay JSON-RPC API
|
|
|
|
|
|
|
|
|
|
proc installRelayApiHandlers*(node: WakuNode, server: RpcServer, cache: MessageCache) =
|
|
|
|
|
if node.wakuRelay.isNil():
|
|
|
|
|
debug "waku relay protocol is nil. skipping json rpc api handlers installation"
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let topicHandler = proc(topic: PubsubTopic, message: WakuMessage) {.async.} =
|
|
|
|
|
cache.addMessage(topic, message)
|
|
|
|
|
|
|
|
|
|
# The node may already be subscribed to some topics when Relay API handlers
|
|
|
|
|
# are installed
|
|
|
|
|
for topic in node.wakuRelay.subscribedTopics:
|
|
|
|
|
node.subscribe(topic, topicHandler)
|
|
|
|
|
cache.subscribe(topic)
|
|
|
|
|
|
|
|
|
|
|
2023-02-14 09:19:06 +01:00
|
|
|
|
server.rpc("post_waku_v2_relay_v1_subscriptions") do (topics: seq[PubsubTopic]) -> bool:
|
2023-02-10 10:43:16 +01:00
|
|
|
|
## Subscribes a node to a list of PubSub topics
|
|
|
|
|
debug "post_waku_v2_relay_v1_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Subscribe to all requested topics
|
2023-08-23 09:53:17 -04:00
|
|
|
|
let newTopics = topics.filterIt(not cache.isSubscribed(it))
|
2023-02-10 10:43:16 +01:00
|
|
|
|
|
2023-08-23 09:53:17 -04:00
|
|
|
|
for topic in newTopics:
|
2023-02-10 10:43:16 +01:00
|
|
|
|
cache.subscribe(topic)
|
|
|
|
|
node.subscribe(topic, topicHandler)
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
2023-02-14 09:19:06 +01:00
|
|
|
|
server.rpc("delete_waku_v2_relay_v1_subscriptions") do (topics: seq[PubsubTopic]) -> bool:
|
2023-02-10 10:43:16 +01:00
|
|
|
|
## Unsubscribes a node from a list of PubSub topics
|
|
|
|
|
debug "delete_waku_v2_relay_v1_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Unsubscribe all handlers from requested topics
|
2023-08-23 09:53:17 -04:00
|
|
|
|
let subscribedTopics = topics.filterIt(cache.isSubscribed(it))
|
|
|
|
|
|
|
|
|
|
for topic in subscribedTopics:
|
2023-06-06 19:28:47 +02:00
|
|
|
|
node.unsubscribe(topic)
|
2023-02-10 10:43:16 +01:00
|
|
|
|
cache.unsubscribe(topic)
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
2023-09-01 15:03:59 +02:00
|
|
|
|
server.rpc("post_waku_v2_relay_v1_message") do (pubsubTopic: PubsubTopic, msg: WakuMessageRPC) -> bool:
|
2023-02-10 10:43:16 +01:00
|
|
|
|
## Publishes a WakuMessage to a PubSub topic
|
2023-09-01 15:03:59 +02:00
|
|
|
|
debug "post_waku_v2_relay_v1_message", pubsubTopic=pubsubTopic
|
2023-02-10 10:43:16 +01:00
|
|
|
|
|
2023-02-14 09:19:06 +01:00
|
|
|
|
let payloadRes = base64.decode(msg.payload)
|
|
|
|
|
if payloadRes.isErr():
|
|
|
|
|
raise newException(ValueError, "invalid payload format: " & payloadRes.error)
|
|
|
|
|
|
2023-08-16 14:02:22 +05:30
|
|
|
|
var message = WakuMessage(
|
2023-02-14 09:19:06 +01:00
|
|
|
|
payload: payloadRes.value,
|
|
|
|
|
# TODO: Fail if the message doesn't have a content topic
|
|
|
|
|
contentTopic: msg.contentTopic.get(DefaultContentTopic),
|
|
|
|
|
version: msg.version.get(0'u32),
|
|
|
|
|
timestamp: msg.timestamp.get(Timestamp(0)),
|
|
|
|
|
ephemeral: msg.ephemeral.get(false)
|
|
|
|
|
)
|
2023-09-01 15:03:59 +02:00
|
|
|
|
|
|
|
|
|
# ensure the node is subscribed to the pubsubTopic. otherwise it risks publishing
|
|
|
|
|
# to a topic with no connected peers
|
|
|
|
|
if pubsubTopic notin node.wakuRelay.subscribedTopics():
|
|
|
|
|
raise newException(ValueError, "Failed to publish: Node not subscribed to pubsubTopic: " & pubsubTopic)
|
|
|
|
|
|
|
|
|
|
# if RLN is mounted, append the proof to the message
|
2023-09-11 12:02:31 +05:30
|
|
|
|
if not node.wakuRlnRelay.isNil():
|
|
|
|
|
# append the proof to the message
|
|
|
|
|
let success = node.wakuRlnRelay.appendRLNProof(message,
|
|
|
|
|
float64(getTime().toUnix()))
|
|
|
|
|
if not success:
|
|
|
|
|
raise newException(ValueError, "Failed to publish: error appending RLN proof to message")
|
|
|
|
|
# validate the message before sending it
|
|
|
|
|
let result = node.wakuRlnRelay.validateMessage(message)
|
|
|
|
|
if result == MessageValidationResult.Invalid:
|
|
|
|
|
raise newException(ValueError, "Failed to publish: invalid RLN proof")
|
|
|
|
|
elif result == MessageValidationResult.Spam:
|
|
|
|
|
raise newException(ValueError, "Failed to publish: limit exceeded, try again later")
|
|
|
|
|
elif result == MessageValidationResult.Valid:
|
|
|
|
|
debug "RLN proof validated successfully", pubSubTopic=pubSubTopic
|
2023-09-01 15:03:59 +02:00
|
|
|
|
else:
|
2023-09-11 12:02:31 +05:30
|
|
|
|
raise newException(ValueError, "Failed to publish: unknown RLN proof validation result")
|
2023-09-01 15:03:59 +02:00
|
|
|
|
|
|
|
|
|
# if we reach here its either a non-RLN message or a RLN message with a valid proof
|
2023-09-11 12:02:31 +05:30
|
|
|
|
debug "Publishing message", pubSubTopic=pubSubTopic
|
2023-09-01 15:03:59 +02:00
|
|
|
|
let publishFut = node.publish(pubsubTopic, message)
|
2023-02-10 10:43:16 +01:00
|
|
|
|
if not await publishFut.withTimeout(futTimeout):
|
2023-09-01 15:03:59 +02:00
|
|
|
|
raise newException(ValueError, "Failed to publish: timed out")
|
2023-02-10 10:43:16 +01:00
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
2023-02-14 09:19:06 +01:00
|
|
|
|
server.rpc("get_waku_v2_relay_v1_messages") do (topic: PubsubTopic) -> seq[WakuMessageRPC]:
|
2023-02-10 10:43:16 +01:00
|
|
|
|
## Returns all WakuMessages received on a PubSub topic since the
|
|
|
|
|
## last time this method was called
|
|
|
|
|
debug "get_waku_v2_relay_v1_messages", topic=topic
|
|
|
|
|
|
|
|
|
|
if not cache.isSubscribed(topic):
|
|
|
|
|
raise newException(ValueError, "Not subscribed to topic: " & topic)
|
|
|
|
|
|
|
|
|
|
let msgRes = cache.getMessages(topic, clear=true)
|
|
|
|
|
if msgRes.isErr():
|
|
|
|
|
raise newException(ValueError, "Not subscribed to topic: " & topic)
|
|
|
|
|
|
2023-02-14 09:19:06 +01:00
|
|
|
|
return msgRes.value.map(toWakuMessageRPC)
|
2023-02-10 10:43:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Waku Relay Private JSON-RPC API (Whisper/Waku v1 compatibility)
|
2023-08-07 15:11:46 +01:00
|
|
|
|
## Support for the Relay Private API has been deprecated.
|
|
|
|
|
## This API existed for compatibility with the Waku v1/Whisper spec and encryption schemes.
|
|
|
|
|
## It is recommended to use the Relay API instead.
|