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
|
|
|
|
|
|
|
# Instead of using rlpx waku_protocol here, lets do mock waku2_protocol
|
|
|
|
# This should wrap GossipSub, not use EthereumNode here
|
|
|
|
|
2020-04-29 05:19:48 +00: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 09:08:28 +00:00
|
|
|
proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
|
2020-04-29 04:49:27 +00:00
|
|
|
|
|
|
|
# Seems easy enough, lets try to get this first
|
|
|
|
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-09-25 14:02:13 +00:00
|
|
|
rpcsrv.rpc("waku_query") do(topics: seq[string]) -> bool:
|
|
|
|
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-09-25 14:02:13 +00:00
|
|
|
await node.query(HistoryQuery(topics: topics), handler)
|
2020-09-24 02:16:25 +00:00
|
|
|
return true
|