github.com/status-im/status-go@v1.1.0/eth-node/bridge/geth/public_waku_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/waku"
    10  	wakucommon "github.com/status-im/status-go/waku/common"
    11  )
    12  
    13  type GethPublicWakuAPIWrapper struct {
    14  	api *waku.PublicWakuAPI
    15  }
    16  
    17  // NewGethPublicWakuAPIWrapper returns an object that wraps Geth's PublicWakuAPI in a types interface
    18  func NewGethPublicWakuAPIWrapper(api *waku.PublicWakuAPI) types.PublicWakuAPI {
    19  	if api == nil {
    20  		panic("PublicWakuAPI cannot be nil")
    21  	}
    22  
    23  	return &GethPublicWakuAPIWrapper{
    24  		api: api,
    25  	}
    26  }
    27  
    28  // AddPrivateKey imports the given private key.
    29  func (w *GethPublicWakuAPIWrapper) 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 *GethPublicWakuAPIWrapper) 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 *GethPublicWakuAPIWrapper) DeleteKeyPair(ctx context.Context, key string) (bool, error) {
    40  	return w.api.DeleteKeyPair(ctx, key)
    41  }
    42  
    43  // NewMessageFilter creates a new filter that can be used to poll for
    44  // (new) messages that satisfy the given criteria.
    45  func (w *GethPublicWakuAPIWrapper) NewMessageFilter(req types.Criteria) (string, error) {
    46  	topics := make([]wakucommon.TopicType, len(req.Topics))
    47  	for index, tt := range req.Topics {
    48  		topics[index] = wakucommon.TopicType(tt)
    49  	}
    50  
    51  	criteria := waku.Criteria{
    52  		SymKeyID:     req.SymKeyID,
    53  		PrivateKeyID: req.PrivateKeyID,
    54  		Sig:          req.Sig,
    55  		MinPow:       req.MinPow,
    56  		Topics:       topics,
    57  		AllowP2P:     req.AllowP2P,
    58  	}
    59  	return w.api.NewMessageFilter(criteria)
    60  }
    61  
    62  func (w *GethPublicWakuAPIWrapper) BloomFilter() []byte {
    63  	return w.api.BloomFilter()
    64  }
    65  
    66  // GetFilterMessages returns the messages that match the filter criteria and
    67  // are received between the last poll and now.
    68  func (w *GethPublicWakuAPIWrapper) GetFilterMessages(id string) ([]*types.Message, error) {
    69  	msgs, err := w.api.GetFilterMessages(id)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	wrappedMsgs := make([]*types.Message, len(msgs))
    75  	for index, msg := range msgs {
    76  		wrappedMsgs[index] = &types.Message{
    77  			Sig:       msg.Sig,
    78  			TTL:       msg.TTL,
    79  			Timestamp: msg.Timestamp,
    80  			Topic:     types.TopicType(msg.Topic),
    81  			Payload:   msg.Payload,
    82  			Padding:   msg.Padding,
    83  			PoW:       msg.PoW,
    84  			Hash:      msg.Hash,
    85  			Dst:       msg.Dst,
    86  			P2P:       msg.P2P,
    87  		}
    88  	}
    89  	return wrappedMsgs, nil
    90  }
    91  
    92  // Post posts a message on the network.
    93  // returns the hash of the message in case of success.
    94  func (w *GethPublicWakuAPIWrapper) Post(ctx context.Context, req types.NewMessage) ([]byte, error) {
    95  	msg := waku.NewMessage{
    96  		SymKeyID:   req.SymKeyID,
    97  		PublicKey:  req.PublicKey,
    98  		Sig:        req.SigID, // Sig is really a SigID
    99  		TTL:        req.TTL,
   100  		Topic:      wakucommon.TopicType(req.Topic),
   101  		Payload:    req.Payload,
   102  		Padding:    req.Padding,
   103  		PowTime:    req.PowTime,
   104  		PowTarget:  req.PowTarget,
   105  		TargetPeer: req.TargetPeer,
   106  		Ephemeral:  req.Ephemeral,
   107  	}
   108  	return w.api.Post(ctx, msg)
   109  }