github.com/luckypickle/go-ethereum-vet@v1.14.2/whisper/shhclient/client.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package shhclient
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/luckypickle/go-ethereum-vet"
    23  	"github.com/luckypickle/go-ethereum-vet/common/hexutil"
    24  	"github.com/luckypickle/go-ethereum-vet/rpc"
    25  	whisper "github.com/luckypickle/go-ethereum-vet/whisper/whisperv6"
    26  )
    27  
    28  // Client defines typed wrappers for the Whisper v6 RPC API.
    29  type Client struct {
    30  	c *rpc.Client
    31  }
    32  
    33  // Dial connects a client to the given URL.
    34  func Dial(rawurl string) (*Client, error) {
    35  	c, err := rpc.Dial(rawurl)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	return NewClient(c), nil
    40  }
    41  
    42  // NewClient creates a client that uses the given RPC client.
    43  func NewClient(c *rpc.Client) *Client {
    44  	return &Client{c}
    45  }
    46  
    47  // Version returns the Whisper sub-protocol version.
    48  func (sc *Client) Version(ctx context.Context) (string, error) {
    49  	var result string
    50  	err := sc.c.CallContext(ctx, &result, "shh_version")
    51  	return result, err
    52  }
    53  
    54  // Info returns diagnostic information about the whisper node.
    55  func (sc *Client) Info(ctx context.Context) (whisper.Info, error) {
    56  	var info whisper.Info
    57  	err := sc.c.CallContext(ctx, &info, "shh_info")
    58  	return info, err
    59  }
    60  
    61  // SetMaxMessageSize sets the maximal message size allowed by this node. Incoming
    62  // and outgoing messages with a larger size will be rejected. Whisper message size
    63  // can never exceed the limit imposed by the underlying P2P protocol (10 Mb).
    64  func (sc *Client) SetMaxMessageSize(ctx context.Context, size uint32) error {
    65  	var ignored bool
    66  	return sc.c.CallContext(ctx, &ignored, "shh_setMaxMessageSize", size)
    67  }
    68  
    69  // SetMinimumPoW (experimental) sets the minimal PoW required by this node.
    70  // This experimental function was introduced for the future dynamic adjustment of
    71  // PoW requirement. If the node is overwhelmed with messages, it should raise the
    72  // PoW requirement and notify the peers. The new value should be set relative to
    73  // the old value (e.g. double). The old value could be obtained via shh_info call.
    74  func (sc *Client) SetMinimumPoW(ctx context.Context, pow float64) error {
    75  	var ignored bool
    76  	return sc.c.CallContext(ctx, &ignored, "shh_setMinPoW", pow)
    77  }
    78  
    79  // MarkTrustedPeer marks specific peer trusted, which will allow it to send historic (expired) messages.
    80  // Note This function is not adding new nodes, the node needs to exists as a peer.
    81  func (sc *Client) MarkTrustedPeer(ctx context.Context, enode string) error {
    82  	var ignored bool
    83  	return sc.c.CallContext(ctx, &ignored, "shh_markTrustedPeer", enode)
    84  }
    85  
    86  // NewKeyPair generates a new public and private key pair for message decryption and encryption.
    87  // It returns an identifier that can be used to refer to the key.
    88  func (sc *Client) NewKeyPair(ctx context.Context) (string, error) {
    89  	var id string
    90  	return id, sc.c.CallContext(ctx, &id, "shh_newKeyPair")
    91  }
    92  
    93  // AddPrivateKey stored the key pair, and returns its ID.
    94  func (sc *Client) AddPrivateKey(ctx context.Context, key []byte) (string, error) {
    95  	var id string
    96  	return id, sc.c.CallContext(ctx, &id, "shh_addPrivateKey", hexutil.Bytes(key))
    97  }
    98  
    99  // DeleteKeyPair delete the specifies key.
   100  func (sc *Client) DeleteKeyPair(ctx context.Context, id string) (string, error) {
   101  	var ignored bool
   102  	return id, sc.c.CallContext(ctx, &ignored, "shh_deleteKeyPair", id)
   103  }
   104  
   105  // HasKeyPair returns an indication if the node has a private key or
   106  // key pair matching the given ID.
   107  func (sc *Client) HasKeyPair(ctx context.Context, id string) (bool, error) {
   108  	var has bool
   109  	return has, sc.c.CallContext(ctx, &has, "shh_hasKeyPair", id)
   110  }
   111  
   112  // PublicKey return the public key for a key ID.
   113  func (sc *Client) PublicKey(ctx context.Context, id string) ([]byte, error) {
   114  	var key hexutil.Bytes
   115  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPublicKey", id)
   116  }
   117  
   118  // PrivateKey return the private key for a key ID.
   119  func (sc *Client) PrivateKey(ctx context.Context, id string) ([]byte, error) {
   120  	var key hexutil.Bytes
   121  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPrivateKey", id)
   122  }
   123  
   124  // NewSymmetricKey generates a random symmetric key and returns its identifier.
   125  // Can be used encrypting and decrypting messages where the key is known to both parties.
   126  func (sc *Client) NewSymmetricKey(ctx context.Context) (string, error) {
   127  	var id string
   128  	return id, sc.c.CallContext(ctx, &id, "shh_newSymKey")
   129  }
   130  
   131  // AddSymmetricKey stores the key, and returns its identifier.
   132  func (sc *Client) AddSymmetricKey(ctx context.Context, key []byte) (string, error) {
   133  	var id string
   134  	return id, sc.c.CallContext(ctx, &id, "shh_addSymKey", hexutil.Bytes(key))
   135  }
   136  
   137  // GenerateSymmetricKeyFromPassword generates the key from password, stores it, and returns its identifier.
   138  func (sc *Client) GenerateSymmetricKeyFromPassword(ctx context.Context, passwd string) (string, error) {
   139  	var id string
   140  	return id, sc.c.CallContext(ctx, &id, "shh_generateSymKeyFromPassword", passwd)
   141  }
   142  
   143  // HasSymmetricKey returns an indication if the key associated with the given id is stored in the node.
   144  func (sc *Client) HasSymmetricKey(ctx context.Context, id string) (bool, error) {
   145  	var found bool
   146  	return found, sc.c.CallContext(ctx, &found, "shh_hasSymKey", id)
   147  }
   148  
   149  // GetSymmetricKey returns the symmetric key associated with the given identifier.
   150  func (sc *Client) GetSymmetricKey(ctx context.Context, id string) ([]byte, error) {
   151  	var key hexutil.Bytes
   152  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getSymKey", id)
   153  }
   154  
   155  // DeleteSymmetricKey deletes the symmetric key associated with the given identifier.
   156  func (sc *Client) DeleteSymmetricKey(ctx context.Context, id string) error {
   157  	var ignored bool
   158  	return sc.c.CallContext(ctx, &ignored, "shh_deleteSymKey", id)
   159  }
   160  
   161  // Post a message onto the network.
   162  func (sc *Client) Post(ctx context.Context, message whisper.NewMessage) (string, error) {
   163  	var hash string
   164  	return hash, sc.c.CallContext(ctx, &hash, "shh_post", message)
   165  }
   166  
   167  // SubscribeMessages subscribes to messages that match the given criteria. This method
   168  // is only supported on bi-directional connections such as websockets and IPC.
   169  // NewMessageFilter uses polling and is supported over HTTP.
   170  func (sc *Client) SubscribeMessages(ctx context.Context, criteria whisper.Criteria, ch chan<- *whisper.Message) (ethereum.Subscription, error) {
   171  	return sc.c.ShhSubscribe(ctx, ch, "messages", criteria)
   172  }
   173  
   174  // NewMessageFilter creates a filter within the node. This filter can be used to poll
   175  // for new messages (see FilterMessages) that satisfy the given criteria. A filter can
   176  // timeout when it was polled for in whisper.filterTimeout.
   177  func (sc *Client) NewMessageFilter(ctx context.Context, criteria whisper.Criteria) (string, error) {
   178  	var id string
   179  	return id, sc.c.CallContext(ctx, &id, "shh_newMessageFilter", criteria)
   180  }
   181  
   182  // DeleteMessageFilter removes the filter associated with the given id.
   183  func (sc *Client) DeleteMessageFilter(ctx context.Context, id string) error {
   184  	var ignored bool
   185  	return sc.c.CallContext(ctx, &ignored, "shh_deleteMessageFilter", id)
   186  }
   187  
   188  // FilterMessages retrieves all messages that are received between the last call to
   189  // this function and match the criteria that where given when the filter was created.
   190  func (sc *Client) FilterMessages(ctx context.Context, id string) ([]*whisper.Message, error) {
   191  	var messages []*whisper.Message
   192  	return messages, sc.c.CallContext(ctx, &messages, "shh_getFilterMessages", id)
   193  }