2020-04-29 12:49:27 +08:00
|
|
|
import
|
2020-08-31 05:32:41 +02:00
|
|
|
std/options,
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
nimcrypto/[sysrand, hmac, sha2],
|
2020-04-29 12:49:27 +08:00
|
|
|
eth/[common, rlp, keys, p2p],
|
2020-08-26 19:28:24 +08:00
|
|
|
../../../protocol/v2/waku_relay,
|
2020-09-11 06:16:45 +02:00
|
|
|
../waku_types, ../wakunode2
|
2020-04-29 12:49:27 +08:00
|
|
|
|
|
|
|
# Instead of using rlpx waku_protocol here, lets do mock waku2_protocol
|
|
|
|
# This should wrap GossipSub, not use EthereumNode here
|
|
|
|
|
2020-04-29 13:19:48 +08:00
|
|
|
# In Waku0/1 we use node.protocolState(Waku) a lot to get information
|
|
|
|
# Also keys to get priate key, etc
|
|
|
|
# Where is the equivalent in Waku/2?
|
|
|
|
# TODO: Extend to get access to protocol state and keys
|
|
|
|
#proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
|
2020-08-28 17:08:28 +08:00
|
|
|
proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
|
2020-04-29 12:49:27 +08:00
|
|
|
|
|
|
|
# Seems easy enough, lets try to get this first
|
|
|
|
rpcsrv.rpc("waku_version") do() -> string:
|
2020-05-19 11:49:25 +08:00
|
|
|
## Returns string of the current Waku protocol version.
|
2020-08-26 19:28:24 +08:00
|
|
|
result = WakuRelayCodec
|
2020-04-29 12:49:27 +08:00
|
|
|
|
2020-05-22 14:18:14 +08:00
|
|
|
# TODO: Implement symkey etc logic
|
2020-09-01 23:20:38 +08:00
|
|
|
rpcsrv.rpc("waku_publish") do(topic: string, payload: seq[byte]) -> bool:
|
2020-09-16 12:23:10 +08:00
|
|
|
let wakuRelay = node.wakuRelay
|
2020-05-22 14:18:14 +08:00
|
|
|
# XXX also future return type
|
2020-09-02 05:15:25 +02:00
|
|
|
# TODO: Shouldn't we really be doing WakuNode publish here?
|
2020-09-10 12:18:35 +08:00
|
|
|
debug "waku_publish", topic=topic, payload=payload
|
2020-09-02 05:15:25 +02:00
|
|
|
discard wakuRelay.publish(topic, payload)
|
2020-05-22 15:28:51 +08:00
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
2020-05-22 15:35:31 +08:00
|
|
|
|
2020-09-11 15:49:45 +08: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 15:35:31 +08:00
|
|
|
# TODO: Handler / Identifier logic
|
|
|
|
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
|
2020-09-10 12:18:35 +08:00
|
|
|
debug "waku_subscribe", topic=topic
|
2020-05-22 15:35:31 +08:00
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
2020-09-11 15:49:45 +08:00
|
|
|
let msg = WakuMessage.init(data)
|
|
|
|
if msg.isOk():
|
|
|
|
debug "waku_subscribe handler", msg=msg
|
2020-09-22 10:14:39 +08:00
|
|
|
var readable_str = cast[string](msg[].payload)
|
|
|
|
info "Hit subscribe handler", topic=topic, msg=msg[], payload=readable_str
|
2020-09-11 15:49:45 +08:00
|
|
|
else:
|
|
|
|
warn "waku_subscribe decode error", msg=msg
|
2020-09-22 10:14:39 +08:00
|
|
|
info "waku_subscribe raw data string", str=cast[string](data)
|
2020-05-22 15:35:31 +08:00
|
|
|
|
2020-09-16 12:23:10 +08:00
|
|
|
# XXX: Can we make this context async to use await?
|
|
|
|
discard node.subscribe(topic, handler)
|
2020-05-22 14:18:14 +08:00
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
2020-09-24 04:16:25 +02:00
|
|
|
|
2020-09-25 16:02:13 +02:00
|
|
|
rpcsrv.rpc("waku_query") do(topics: seq[string]) -> bool:
|
|
|
|
debug "waku_query"
|
2020-09-24 04:16:25 +02:00
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(response: HistoryResponse) {.gcsafe.} =
|
2020-09-25 16:02:13 +02:00
|
|
|
info "Hit response handler", messages=response.messages
|
2020-09-24 04:16:25 +02:00
|
|
|
|
2020-09-25 16:02:13 +02:00
|
|
|
await node.query(HistoryQuery(topics: topics), handler)
|
2020-09-24 04:16:25 +02:00
|
|
|
return true
|
2020-10-02 14:48:56 +02:00
|
|
|
|
|
|
|
rpcsrv.rpc("waku_subscribe_filter") do(topic: string, contentFilters: seq[seq[string]]) -> bool:
|
|
|
|
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:
|
|
|
|
filters.add(ContentFilter(topics: topics))
|
|
|
|
|
|
|
|
await node.subscribe(FilterRequest(topic: topic, contentFilters: filters), handler)
|
|
|
|
return true
|