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-05-27 04:07:11 +00:00
|
|
|
../waku_types
|
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-07-23 02:53:29 +00:00
|
|
|
rpcsrv.rpc("waku_publish") do(topic: string, message: seq[byte]) -> bool:
|
2020-08-28 09:08:28 +00:00
|
|
|
# XXX Why is casting necessary here but not in Nim node API?
|
|
|
|
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
|
2020-05-22 06:18:14 +00:00
|
|
|
# XXX also future return type
|
2020-08-28 09:08:28 +00:00
|
|
|
discard wakuRelay.publish(topic, message)
|
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
|
|
|
|
|
|
|
# TODO: Handler / Identifier logic
|
|
|
|
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
|
2020-08-28 09:08:28 +00:00
|
|
|
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
|
2020-05-22 07:35:31 +00:00
|
|
|
|
|
|
|
# XXX: Hacky in-line handler
|
|
|
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
|
|
|
info "Hit subscribe handler", topic=topic, data=data
|
|
|
|
|
2020-08-28 09:08:28 +00:00
|
|
|
discard wakuRelay.subscribe(topic, handler)
|
2020-05-22 06:18:14 +00:00
|
|
|
return true
|
|
|
|
#if not result:
|
|
|
|
# raise newException(ValueError, "Message could not be posted")
|
|
|
|
|