2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func lightpushPublish(msg pb.WakuMessage, pubsubTopic string, peerID string, ms int) (string, error) {
|
|
|
|
if wakuNode == 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.LightPushOption
|
|
|
|
if peerID != "" {
|
|
|
|
p, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
lpOptions = append(lpOptions, lightpush.WithPeer(p))
|
|
|
|
} else {
|
2022-08-15 17:13:45 +00:00
|
|
|
lpOptions = append(lpOptions, lightpush.WithAutomaticPeerSelection())
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := wakuNode.Lightpush().PublishToTopic(ctx, &msg, pubsubTopic, lpOptions...)
|
|
|
|
return hexutil.Encode(hash), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func LightpushPublish(messageJSON string, topic string, peerID 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 := lightpushPublish(msg, getTopic(topic), peerID, ms)
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LightpushPublishEncodeAsymmetric(messageJSON string, topic string, peerID 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 := lightpushPublish(msg, getTopic(topic), peerID, ms)
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LightpushPublishEncodeSymmetric(messageJSON string, topic string, peerID 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 := lightpushPublish(msg, getTopic(topic), peerID, ms)
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(hash, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|