2020-04-29 04:49:27 +00:00
|
|
|
import
|
|
|
|
json_rpc/rpcserver, tables, options,
|
|
|
|
eth/[common, rlp, keys, p2p],
|
2020-04-29 05:02:42 +00:00
|
|
|
../../../protocol/v2/waku_protocol,
|
2020-04-29 04:49:27 +00:00
|
|
|
nimcrypto/[sysrand, hmac, sha2, pbkdf2],
|
2020-05-22 06:12:05 +00:00
|
|
|
../../v1/rpc/[rpc_types, hexstrings, key_storage],
|
2020-05-22 07:35:31 +00:00
|
|
|
../waku_types,
|
|
|
|
libp2p/protocols/pubsub/pubsub
|
2020-04-29 04:49:27 +00:00
|
|
|
|
|
|
|
from stew/byteutils import hexToSeqByte, hexToByteArray
|
|
|
|
|
|
|
|
# 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-05-22 06:18:14 +00:00
|
|
|
proc setupWakuRPC*(wakuProto: WakuProto, 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.
|
|
|
|
result = WakuSubCodec
|
2020-04-29 04:49:27 +00:00
|
|
|
|
2020-05-22 06:18:14 +00:00
|
|
|
# TODO: Implement symkey etc logic
|
2020-05-22 07:28:51 +00:00
|
|
|
rpcsrv.rpc("waku_publish") do(topic: string, message: string) -> bool:
|
2020-05-22 06:18:14 +00:00
|
|
|
let data = cast[seq[byte]](message)
|
|
|
|
# Assumes someone subscribing on this topic
|
|
|
|
#let wakuSub = wakuProto.switch.pubsub
|
|
|
|
let wakuSub = cast[WakuSub](wakuProto.switch.pubSub.get())
|
|
|
|
# XXX also future return type
|
2020-05-22 07:28:51 +00:00
|
|
|
discard wakuSub.publish(topic, data)
|
|
|
|
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:
|
|
|
|
let wakuSub = cast[WakuSub](wakuProto.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
|
|
|
|
|
|
|
|
discard wakuSub.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-04-29 04:49:27 +00:00
|
|
|
|