2023-08-10 13:30:38 +00:00
package library
2022-04-12 12:12:14 +00:00
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
)
2023-02-06 22:16:20 +00:00
func lightpushPublish ( msg * pb . WakuMessage , pubsubTopic string , peerID 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-07-19 16:25:35 +00:00
var lpOptions [ ] lightpush . Option
2022-04-12 12:12:14 +00:00
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
}
2023-02-16 22:09:21 +00:00
hash , err := wakuState . node . Lightpush ( ) . PublishToTopic ( ctx , msg , pubsubTopic , lpOptions ... )
2022-04-12 12:12:14 +00:00
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 , topic string , peerID string , ms int ) ( string , error ) {
2022-04-12 12:12:14 +00:00
msg , err := wakuMessage ( messageJSON )
if err != nil {
2023-08-10 13:30:38 +00:00
return "" , err
2022-04-12 12:12:14 +00:00
}
2023-08-10 13:30:38 +00:00
return lightpushPublish ( msg , getTopic ( topic ) , peerID , ms )
2022-04-12 12:12:14 +00:00
}
2023-08-10 13:30:38 +00:00
// LightpushPublishEncodeAsymmetric is used to publish a WakuMessage in a pubsub topic using Lightpush protocol, and encrypting the message with some public key
func LightpushPublishEncodeAsymmetric ( messageJSON string , topic string , peerID string , publicKey string , optionalSigningKey string , ms int ) ( string , error ) {
2022-04-12 12:12:14 +00:00
msg , err := wakuMessageAsymmetricEncoding ( messageJSON , publicKey , optionalSigningKey )
if err != nil {
2023-08-10 13:30:38 +00:00
return "" , err
2022-04-12 12:12:14 +00:00
}
2023-08-10 13:30:38 +00:00
return lightpushPublish ( msg , getTopic ( topic ) , peerID , ms )
2022-04-12 12:12:14 +00:00
}
2023-08-10 13:30:38 +00:00
// LightpushPublishEncodeSymmetric is used to publish a WakuMessage in a pubsub topic using Lightpush protocol, and encrypting the message with a symmetric key
func LightpushPublishEncodeSymmetric ( messageJSON string , topic string , peerID string , symmetricKey string , optionalSigningKey string , ms int ) ( string , error ) {
2022-04-12 12:12:14 +00:00
msg , err := wakuMessageSymmetricEncoding ( messageJSON , symmetricKey , optionalSigningKey )
if err != nil {
2023-08-10 13:30:38 +00:00
return "" , err
2022-04-12 12:12:14 +00:00
}
2023-08-10 13:30:38 +00:00
return lightpushPublish ( msg , getTopic ( topic ) , peerID , ms )
2022-04-12 12:12:14 +00:00
}