go-waku/library/lightpush.go

57 lines
1.4 KiB
Go
Raw Normal View History

2023-08-10 13:30:38 +00:00
package library
import (
"context"
"time"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/ethereum/go-ethereum/common/hexutil"
2022-10-19 19:39:32 +00:00
"github.com/libp2p/go-libp2p/core/peer"
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
)
func lightpushPublish(msg *pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
if wakuState.node == nil {
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()
}
var lpOptions []lightpush.Option
if peerID != "" {
p, err := peer.Decode(peerID)
if err != nil {
return "", err
}
lpOptions = append(lpOptions, lightpush.WithPeer(p))
} else {
lpOptions = append(lpOptions, lightpush.WithAutomaticPeerSelection())
}
if pubsubTopic != "" {
lpOptions = append(lpOptions, lightpush.WithPubSubTopic(pubsubTopic))
}
hash, err := wakuState.node.Lightpush().Publish(ctx, msg, lpOptions...)
return hexutil.Encode(hash), err
}
2023-08-10 13:30:38 +00:00
// LightpushPublish is used to publish a WakuMessage in a pubsub topic using Lightpush protocol
func LightpushPublish(messageJSON string, pubsubTopic string, peerID string, ms int) (string, error) {
msg, err := wakuMessage(messageJSON)
if err != nil {
2023-08-10 13:30:38 +00:00
return "", err
}
2023-10-28 23:37:53 +00:00
return lightpushPublish(msg, pubsubTopic, peerID, ms)
}