Oskar Thorén e5f0f36f01
Add basic rpc scripts to publish and subscribe (#140)
* Add basic rpc scripts to publish and subscribe

* Fix publish topic and payload script

Also change parameter name in waku relay due to weird shadowing of log
topic:

DBG 2020-09-09 12:07:54+08:00 waku_publish                               tid=8795 file=wakurpc.nim:30 topic=waku payload=@[]
DBG 2020-09-09 12:07:54+08:00 publish                                    tid=8795 file=waku_relay.nim:65 topic=WakuRelay

Above should show topic=waku but it gets topic=WakuRelay from log scope
for some reason.

* Scripts take arguments

* Add basic nangang tutorial

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* meh

* ENsure subscribe call succeeds

Co-authored-by: Kim De Mey <kim.demey@gmail.com>
2020-09-10 12:18:35 +08:00

51 lines
1.9 KiB
Nim

import
std/options,
json_rpc/rpcserver,
nimcrypto/[sysrand, hmac, sha2],
eth/[common, rlp, keys, p2p],
../../../protocol/v2/waku_relay,
../waku_types
# Instead of using rlpx waku_protocol here, lets do mock waku2_protocol
# This should wrap GossipSub, not use EthereumNode here
# 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) =
proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) =
# Seems easy enough, lets try to get this first
rpcsrv.rpc("waku_version") do() -> string:
## Returns string of the current Waku protocol version.
result = WakuRelayCodec
# TODO: Implement symkey etc logic
rpcsrv.rpc("waku_publish") do(topic: string, payload: seq[byte]) -> bool:
# XXX Why is casting necessary here but not in Nim node API?
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
# XXX also future return type
# TODO: Shouldn't we really be doing WakuNode publish here?
debug "waku_publish", topic=topic, payload=payload
discard wakuRelay.publish(topic, payload)
return true
#if not result:
# raise newException(ValueError, "Message could not be posted")
# TODO: Handler / Identifier logic
rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool:
debug "waku_subscribe", topic=topic
let wakuRelay = cast[WakuRelay](node.switch.pubSub.get())
# XXX: Hacky in-line handler
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
info "Hit subscribe handler", topic=topic, data=data
# TODO: Shouldn't we really be doing WakuNode subscribe here?
discard wakuRelay.subscribe(topic, handler)
return true
#if not result:
# raise newException(ValueError, "Message could not be posted")