2020-04-29 04:49:27 +00:00
|
|
|
import
|
2020-08-31 03:32:41 +00:00
|
|
|
std/options,
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
nimcrypto/[sysrand, hmac, sha2],
|
2020-04-29 04:49:27 +00:00
|
|
|
eth/[common, rlp, keys, p2p],
|
2020-08-26 11:28:24 +00:00
|
|
|
../../../protocol/v2/waku_relay,
|
2020-09-11 04:16:45 +00:00
|
|
|
../waku_types, ../wakunode2
|
2020-04-29 04:49:27 +00:00
|
|
|
|
2020-08-28 09:08:28 +00:00
|
|
|
proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
|
2020-04-29 04:49:27 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("waku_version") do() -> string:
|
2020-05-19 03:49:25 +00:00
|
|
|
## Returns string of the current Waku protocol version.
|
2020-08-26 11:28:24 +00:00
|
|
|
result = WakuRelayCodec
|
2020-04-29 04:49:27 +00:00
|
|
|
|
2020-05-22 06:18:14 +00:00
|
|
|
# TODO: Implement symkey etc logic
|
2020-09-01 15:20:38 +00:00
|
|
|
rpcsrv.rpc("waku_publish") do(topic: string, payload: seq[byte]) -> bool:
|
2020-09-16 04:23:10 +00:00
|
|
|
let wakuRelay = node.wakuRelay
|
2020-05-22 06:18:14 +00:00
|
|
|
# XXX also future return type
|
2020-09-02 03:15:25 +00:00
|
|
|
# TODO: Shouldn't we really be doing WakuNode publish here?
|
2020-09-10 04:18:35 +00:00
|
|
|
debug "waku_publish", topic=topic, payload=payload
|
2020-09-02 03:15:25 +00:00
|
|
|
discard wakuRelay.publish(topic, payload)
|
2020-05-22 07:28:51 +00:00
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2020-09-11 07:49:45 +00:00
|
|
|
rpcsrv.rpc("waku_publish2") do(topic: string, payload: seq[byte]) -> bool:
|
|
|
|
let msg = WakuMessage.init(payload)
|
|
|
|
if msg.isOk():
|
|
|
|
debug "waku_publish", msg=msg
|
|
|
|
else:
|
|
|
|
warn "waku_publish decode error", msg=msg
|
|
|
|
|
|
|
|
debug "waku_publish", topic=topic, payload=payload, msg=msg[]
|
|
|
|
node.publish(topic, msg[])
|
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
|
|
|
|
2020-05-22 07:35:31 +00:00
|
|
|
# TODO: Handler / Identifier logic
|
|
|
|
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
|
2020-09-10 04:18:35 +00:00
|
|
|
debug "waku_subscribe", topic=topic
|
2020-05-22 07:35:31 +00:00
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-09-11 07:49:45 +00:00
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
debug "waku_subscribe handler", msg=msg
|
2020-09-22 02:14:39 +00:00
|
|
|
var readable_str = cast[string](msg[].payload)
|
|
|
|
info "Hit subscribe handler", topic=topic, msg=msg[], payload=readable_str
|
2020-09-11 07:49:45 +00:00
|
|
|
else:
|
|
|
|
warn "waku_subscribe decode error", msg=msg
|
2020-09-22 02:14:39 +00:00
|
|
|
info "waku_subscribe raw data string", str=cast[string](data)
|
2020-05-22 07:35:31 +00:00
|
|
|
|
2020-09-16 04:23:10 +00:00
|
|
|
# XXX: Can we make this context async to use await?
|
|
|
|
discard node.subscribe(topic, handler)
|
2020-05-22 06:18:14 +00:00
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
2020-09-24 02:16:25 +00:00
|
|
|
|
2020-10-21 08:55:06 +00:00
|
|
|
rpcsrv.rpc("waku_query") do(topics: seq[int]) -> bool:
|
2020-09-25 14:02:13 +00:00
|
|
|
debug "waku_query"
|
2020-09-24 02:16:25 +00:00
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(response: HistoryResponse) {.gcsafe.} =
|
2020-09-25 14:02:13 +00:00
|
|
|
info "Hit response handler", messages=response.messages
|
2020-09-24 02:16:25 +00:00
|
|
|
|
2020-10-21 08:55:06 +00:00
|
|
|
var contentTopics = newSeq[ContentTopic]()
|
|
|
|
for topic in topics:
|
|
|
|
contentTopics.add(ContentTopic(topic))
|
|
|
|
|
|
|
|
await node.query(HistoryQuery(topics: contentTopics), handler)
|
2020-09-24 02:16:25 +00:00
|
|
|
return true
|
2020-10-02 12:48:56 +00:00
|
|
|
|
2020-10-21 08:55:06 +00:00
|
|
|
rpcsrv.rpc("waku_subscribe_filter") do(topic: string, contentFilters: seq[seq[int]]) -> bool:
|
2020-10-02 12:48:56 +00:00
|
|
|
debug "waku_subscribe_filter"
|
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(msg: WakuMessage) {.gcsafe, closure.} =
|
|
|
|
info "Hit subscribe response", message=msg
|
|
|
|
|
|
|
|
var filters = newSeq[ContentFilter]()
|
|
|
|
for topics in contentFilters:
|
2020-10-21 08:55:06 +00:00
|
|
|
var contentTopics = newSeq[ContentTopic]()
|
|
|
|
for topic in topics:
|
|
|
|
contentTopics.add(ContentTopic(topic))
|
|
|
|
filters.add(ContentFilter(topics: contentTopics))
|
2020-10-02 12:48:56 +00:00
|
|
|
|
|
|
|
await node.subscribe(FilterRequest(topic: topic, contentFilters: filters), handler)
|
|
|
|
return true
|
2020-10-06 03:33:28 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("waku_info") do() -> string:
|
|
|
|
debug "waku_node_info"
|
|
|
|
|
|
|
|
let wakuInfo = node.info()
|
|
|
|
let listenStr = wakuInfo.listenStr
|
|
|
|
info "Listening on", full = listenStr
|
|
|
|
|
|
|
|
return listenStr
|