2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
var relaySubscriptions map[string]*relay.Subscription = make(map[string]*relay.Subscription)
|
|
|
|
var relaySubsMutex sync.Mutex
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
func RelayEnoughPeers(topic string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
topicToCheck := protocol.DefaultPubsubTopic().String()
|
|
|
|
if topic != "" {
|
|
|
|
topicToCheck = topic
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
return PrepareJSONResponse(wakuState.node.Relay().EnoughPeersToPublishToTopic(topicToCheck), nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 22:16:20 +00:00
|
|
|
func relayPublish(msg *pb.WakuMessage, pubsubTopic string, ms int) (string, error) {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-04-12 12:12:14 +00:00
|
|
|
return "", errWakuNodeNotReady
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
hash, err := wakuState.node.Relay().PublishToTopic(ctx, msg, pubsubTopic)
|
2022-04-12 12:12:14 +00:00
|
|
|
return hexutil.Encode(hash), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RelayPublish(messageJSON string, topic string, ms int) string {
|
|
|
|
msg, err := wakuMessage(messageJSON)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := relayPublish(msg, getTopic(topic), int(ms))
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RelayPublishEncodeAsymmetric(messageJSON string, topic string, publicKey string, optionalSigningKey string, ms int) string {
|
|
|
|
msg, err := wakuMessageAsymmetricEncoding(messageJSON, publicKey, optionalSigningKey)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := relayPublish(msg, getTopic(topic), int(ms))
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RelayPublishEncodeSymmetric(messageJSON string, topic string, symmetricKey string, optionalSigningKey string, ms int) string {
|
|
|
|
msg, err := wakuMessageSymmetricEncoding(messageJSON, symmetricKey, optionalSigningKey)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := relayPublish(msg, getTopic(topic), int(ms))
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-17 17:30:59 +00:00
|
|
|
func relaySubscribe(topic string) error {
|
2022-04-12 12:12:14 +00:00
|
|
|
topicToSubscribe := getTopic(topic)
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
relaySubsMutex.Lock()
|
|
|
|
defer relaySubsMutex.Unlock()
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
_, ok := relaySubscriptions[topicToSubscribe]
|
2022-04-12 12:12:14 +00:00
|
|
|
if ok {
|
2023-02-17 17:30:59 +00:00
|
|
|
return nil
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
subscription, err := wakuState.node.Relay().SubscribeToTopic(context.Background(), topicToSubscribe)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2023-02-17 17:30:59 +00:00
|
|
|
return err
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
relaySubscriptions[topicToSubscribe] = subscription
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
go func(subscription *relay.Subscription) {
|
2023-05-05 09:49:15 +00:00
|
|
|
for envelope := range subscription.Ch {
|
2022-04-12 12:12:14 +00:00
|
|
|
send("message", toSubscriptionMessage(envelope))
|
|
|
|
}
|
2022-04-25 19:31:26 +00:00
|
|
|
}(subscription)
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2023-02-17 17:30:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RelaySubscribe(topic string) string {
|
|
|
|
if wakuState.node == nil {
|
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeJSONResponse(relaySubscribe(topic))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 19:20:31 +00:00
|
|
|
func RelayTopics() string {
|
|
|
|
if wakuState.node == nil {
|
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrepareJSONResponse(wakuState.node.Relay().Topics(), nil)
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
func RelayUnsubscribe(topic string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
topicToUnsubscribe := getTopic(topic)
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
relaySubsMutex.Lock()
|
|
|
|
defer relaySubsMutex.Unlock()
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
subscription, ok := relaySubscriptions[topicToUnsubscribe]
|
2022-04-12 12:12:14 +00:00
|
|
|
if ok {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subscription.Unsubscribe()
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
delete(relaySubscriptions, topicToUnsubscribe)
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
err := wakuState.node.Relay().Unsubscribe(context.Background(), topicToUnsubscribe)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|