go-waku/waku/v2/protocol/waku_relay.go

44 lines
1.2 KiB
Go
Raw Normal View History

2021-03-12 15:06:20 -04:00
// Waku Relay module. Thin layer on top of GossipSub.
//
// See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-relay.md
// for spec.
package protocol
import (
"context"
2021-03-22 12:45:13 -04:00
"crypto/sha256"
2021-03-12 15:06:20 -04:00
"github.com/libp2p/go-libp2p-core/host"
libp2pProtocol "github.com/libp2p/go-libp2p-core/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"
2021-03-22 12:45:13 -04:00
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
2021-03-12 15:06:20 -04:00
)
2021-03-18 12:40:47 -04:00
const WakuRelayProtocol = libp2pProtocol.ID("/vac/waku/relay/2.0.0-beta2")
2021-03-12 15:06:20 -04:00
type WakuRelay struct {
2021-03-12 15:06:20 -04:00
p *pubsub.PubSub
}
2021-03-22 12:45:13 -04:00
// Once https://github.com/status-im/nim-waku/issues/420 is fixed, implement a custom messageIdFn
func msgIdFn(pmsg *pubsub_pb.Message) string {
hash := sha256.Sum256(pmsg.Data)
return string(hash[:])
}
2021-03-15 12:07:23 -04:00
func NewWakuRelay(ctx context.Context, h host.Host, opts ...pubsub.Option) (*pubsub.PubSub, error) {
opts = append(opts, pubsub.WithMessageSignaturePolicy(pubsub.StrictNoSign))
2021-03-22 12:45:13 -04:00
opts = append(opts, pubsub.WithNoAuthor())
opts = append(opts, pubsub.WithMessageIdFn(msgIdFn))
2021-03-15 12:07:23 -04:00
2021-03-18 12:40:47 -04:00
gossipSub, err := pubsub.NewGossipSub(ctx, h, []libp2pProtocol.ID{WakuRelayProtocol}, opts...)
2021-03-12 15:06:20 -04:00
if err != nil {
return nil, err
}
w := new(WakuRelay)
2021-03-12 15:06:20 -04:00
w.p = gossipSub
return gossipSub, nil
}