2023-08-10 09:30:38 -04:00
|
|
|
package library
|
2022-04-12 08:12:14 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2022-04-12 08:12:14 -04:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-10-19 15:39:32 -04:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-11-09 15:53:01 -04:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
2022-04-12 08:12:14 -04:00
|
|
|
)
|
|
|
|
|
2023-12-15 10:46:21 -04:00
|
|
|
func lightpushPublish(instance *WakuInstance, msg *pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return "", err
|
2022-04-12 08:12:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
2023-12-15 10:46:21 -04:00
|
|
|
ctx, cancel = context.WithTimeout(instance.ctx, time.Duration(int(ms))*time.Millisecond)
|
2022-04-12 08:12:14 -04:00
|
|
|
defer cancel()
|
|
|
|
} else {
|
2023-12-15 10:46:21 -04:00
|
|
|
ctx = instance.ctx
|
2022-04-12 08:12:14 -04:00
|
|
|
}
|
|
|
|
|
2024-02-05 08:53:15 -04:00
|
|
|
var lpOptions []lightpush.RequestOption
|
2022-04-12 08:12:14 -04:00
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
lpOptions = append(lpOptions, lightpush.WithPeer(p))
|
|
|
|
} else {
|
2022-08-15 13:13:45 -04:00
|
|
|
lpOptions = append(lpOptions, lightpush.WithAutomaticPeerSelection())
|
2022-04-12 08:12:14 -04:00
|
|
|
}
|
|
|
|
|
2023-09-29 10:43:25 +05:30
|
|
|
if pubsubTopic != "" {
|
|
|
|
lpOptions = append(lpOptions, lightpush.WithPubSubTopic(pubsubTopic))
|
|
|
|
}
|
|
|
|
|
2023-12-15 10:46:21 -04:00
|
|
|
hash, err := instance.node.Lightpush().Publish(ctx, msg, lpOptions...)
|
2022-04-12 08:12:14 -04:00
|
|
|
return hexutil.Encode(hash), err
|
|
|
|
}
|
|
|
|
|
2023-08-10 09:30:38 -04:00
|
|
|
// LightpushPublish is used to publish a WakuMessage in a pubsub topic using Lightpush protocol
|
2023-12-15 10:46:21 -04:00
|
|
|
func LightpushPublish(instance *WakuInstance, messageJSON string, pubsubTopic string, peerID string, ms int) (string, error) {
|
2022-04-12 08:12:14 -04:00
|
|
|
msg, err := wakuMessage(messageJSON)
|
|
|
|
if err != nil {
|
2023-08-10 09:30:38 -04:00
|
|
|
return "", err
|
2022-04-12 08:12:14 -04:00
|
|
|
}
|
|
|
|
|
2023-12-15 10:46:21 -04:00
|
|
|
return lightpushPublish(instance, msg, pubsubTopic, peerID, ms)
|
2022-04-12 08:12:14 -04:00
|
|
|
}
|