github.com/status-im/status-go@v1.1.0/eth-node/bridge/geth/public_wakuv2_api.go (about) 1 package gethbridge 2 3 import ( 4 "context" 5 6 "github.com/ethereum/go-ethereum/common/hexutil" 7 8 "github.com/status-im/status-go/eth-node/types" 9 "github.com/status-im/status-go/wakuv2" 10 wakucommon "github.com/status-im/status-go/wakuv2/common" 11 ) 12 13 type gethPublicWakuV2APIWrapper struct { 14 api *wakuv2.PublicWakuAPI 15 } 16 17 // NewGethPublicWakuAPIWrapper returns an object that wraps Geth's PublicWakuAPI in a types interface 18 func NewGethPublicWakuV2APIWrapper(api *wakuv2.PublicWakuAPI) types.PublicWakuAPI { 19 if api == nil { 20 panic("PublicWakuV2API cannot be nil") 21 } 22 23 return &gethPublicWakuV2APIWrapper{ 24 api: api, 25 } 26 } 27 28 // AddPrivateKey imports the given private key. 29 func (w *gethPublicWakuV2APIWrapper) AddPrivateKey(ctx context.Context, privateKey types.HexBytes) (string, error) { 30 return w.api.AddPrivateKey(ctx, hexutil.Bytes(privateKey)) 31 } 32 33 // GenerateSymKeyFromPassword derives a key from the given password, stores it, and returns its ID. 34 func (w *gethPublicWakuV2APIWrapper) GenerateSymKeyFromPassword(ctx context.Context, passwd string) (string, error) { 35 return w.api.GenerateSymKeyFromPassword(ctx, passwd) 36 } 37 38 // DeleteKeyPair removes the key with the given key if it exists. 39 func (w *gethPublicWakuV2APIWrapper) DeleteKeyPair(ctx context.Context, key string) (bool, error) { 40 return w.api.DeleteKeyPair(ctx, key) 41 } 42 43 func (w *gethPublicWakuV2APIWrapper) BloomFilter() []byte { 44 return w.api.BloomFilter() 45 } 46 47 // NewMessageFilter creates a new filter that can be used to poll for 48 // (new) messages that satisfy the given criteria. 49 func (w *gethPublicWakuV2APIWrapper) NewMessageFilter(req types.Criteria) (string, error) { 50 topics := make([]wakucommon.TopicType, len(req.Topics)) 51 for index, tt := range req.Topics { 52 topics[index] = wakucommon.TopicType(tt) 53 } 54 55 criteria := wakuv2.Criteria{ 56 SymKeyID: req.SymKeyID, 57 PrivateKeyID: req.PrivateKeyID, 58 Sig: req.Sig, 59 PubsubTopic: req.PubsubTopic, 60 ContentTopics: topics, 61 } 62 return w.api.NewMessageFilter(criteria) 63 } 64 65 // GetFilterMessages returns the messages that match the filter criteria and 66 // are received between the last poll and now. 67 func (w *gethPublicWakuV2APIWrapper) GetFilterMessages(id string) ([]*types.Message, error) { 68 msgs, err := w.api.GetFilterMessages(id) 69 if err != nil { 70 return nil, err 71 } 72 73 wrappedMsgs := make([]*types.Message, len(msgs)) 74 for index, msg := range msgs { 75 wrappedMsgs[index] = &types.Message{ 76 Sig: msg.Sig, 77 Timestamp: msg.Timestamp, 78 PubsubTopic: msg.PubsubTopic, 79 Topic: types.TopicType(msg.ContentTopic), 80 Payload: msg.Payload, 81 Padding: msg.Padding, 82 Hash: msg.Hash, 83 Dst: msg.Dst, 84 } 85 } 86 return wrappedMsgs, nil 87 } 88 89 // Post posts a message on the network. 90 // returns the hash of the message in case of success. 91 func (w *gethPublicWakuV2APIWrapper) Post(ctx context.Context, req types.NewMessage) ([]byte, error) { 92 msg := wakuv2.NewMessage{ 93 SymKeyID: req.SymKeyID, 94 PublicKey: req.PublicKey, 95 Sig: req.SigID, // Sig is really a SigID 96 PubsubTopic: req.PubsubTopic, 97 ContentTopic: wakucommon.TopicType(req.Topic), 98 Payload: req.Payload, 99 Padding: req.Padding, 100 TargetPeer: req.TargetPeer, 101 Ephemeral: req.Ephemeral, 102 Priority: req.Priority, 103 } 104 return w.api.Post(ctx, msg) 105 }