go-nwaku/nwaku/jsonrpc.go

110 lines
2.9 KiB
Go
Raw Normal View History

2021-06-09 12:29:17 +00:00
package main
import (
"context"
"fmt"
"time"
"github.com/ethereum/go-ethereum/rpc"
)
// NOTE Can be generalized with different transports (HTTP, IPC, WS etc)
// https://github.com/ethereum/go-ethereum/blob/master/rpc/client.go#L169
2021-06-10 08:11:19 +00:00
// Later: Generalize with CallContext (not prio)
2021-06-10 07:28:49 +00:00
// TODO Move to types
2021-06-09 12:29:17 +00:00
type WakuInfo struct {
ListenStr string `json:"listenStr"`
}
2021-06-10 07:43:36 +00:00
// XXX in this case Payload isn't string but something else
// panic: json: cannot unmarshal array into Go struct field WakuMessage.messages.payload of type string
// TODO This should be toy-chat protobuf probably
type WakuMessage struct {
2021-06-10 08:35:40 +00:00
// TODO Should be hex encoded string here
2021-06-10 07:43:36 +00:00
Payload []byte `json:"payload"`
ContentTopic string `json:"contentTopic"`
Version int `json:"version"`
Timestamp float64 `json:"timestamp"`
}
2021-06-10 08:35:40 +00:00
type WakuRelayMessage struct {
Payload string `json:"payload"`
ContentTopic string `json:"contentTopic"`
// Version int `json:"version"`
// Timestamp float64 `json:"timestamp"`
}
2021-06-10 07:43:36 +00:00
type StoreResponse struct {
Messages []WakuMessage `json:"messages"`
2021-06-10 07:28:49 +00:00
}
type ContentFilter struct {
ContentTopic string `json:"contentTopic"`
}
2021-06-10 08:11:19 +00:00
func getWakuDebugInfo(client *rpc.Client) WakuInfo {
2021-06-09 12:29:17 +00:00
var wakuInfo WakuInfo
2021-06-10 08:11:19 +00:00
2021-06-09 12:29:17 +00:00
if err := client.Call(&wakuInfo, "get_waku_v2_debug_v1_info"); err != nil {
panic(err)
}
2021-06-10 07:28:49 +00:00
2021-06-10 08:11:19 +00:00
return wakuInfo
}
2021-06-10 08:35:40 +00:00
// TODO Support more args
2021-06-10 08:11:19 +00:00
func getWakuStoreMessages(client *rpc.Client, contentTopic string) StoreResponse {
var storeResponse StoreResponse
var contentFilter = ContentFilter{contentTopic}
2021-06-10 07:28:49 +00:00
var contentFilters []ContentFilter
2021-06-10 08:11:19 +00:00
2021-06-10 07:28:49 +00:00
contentFilters = append(contentFilters, contentFilter)
2021-06-10 08:11:19 +00:00
if err := client.Call(&storeResponse, "get_waku_v2_store_v1_messages", "", contentFilters); err != nil {
2021-06-10 07:28:49 +00:00
panic(err)
}
2021-06-10 08:11:19 +00:00
return storeResponse
2021-06-09 12:29:17 +00:00
}
2021-06-10 07:28:49 +00:00
2021-06-10 08:35:40 +00:00
func postWakuRelayMessage(client *rpc.Client, message WakuRelayMessage) bool {
var topic = "/waku/2/default-waku/proto"
var res bool
if err := client.Call(&res, "post_waku_v2_relay_v1_message", topic, message); err != nil {
panic(err)
}
return res
}
2021-06-10 08:43:13 +00:00
// TODO Subscribe, then poll for getting messages
// https://rfc.vac.dev/spec/16/#post_waku_v2_relay_v1_subscriptions
// https://rfc.vac.dev/spec/16/#get_waku_v2_relay_v1_messages
// For now, just do query and publish
2021-06-10 08:11:19 +00:00
func main() {
fmt.Println("JSON RPC request...")
_, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Assumes node started
client, _ := rpc.Dial("http://127.0.0.1:8545")
2021-06-10 08:35:40 +00:00
// Get node info
2021-06-10 08:11:19 +00:00
var wakuInfo = getWakuDebugInfo(client)
fmt.Println("WakuInfo ListenStr", wakuInfo.ListenStr)
2021-06-10 08:35:40 +00:00
// Query messages
2021-06-10 08:11:19 +00:00
var contentTopic = "/toy-chat/2/huilong/proto"
var storeResponse = getWakuStoreMessages(client, contentTopic)
fmt.Println("Fetched", len(storeResponse.Messages), "messages")
2021-06-10 08:35:40 +00:00
// Publish
var message = WakuRelayMessage{Payload: "0x1a2b3c4d5e6f", ContentTopic: contentTopic}
var res = postWakuRelayMessage(client, message)
fmt.Println("Publish", res)
2021-06-10 08:11:19 +00:00
}