github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/whisper/shhclient/client.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:51</date>
    10  //</624342688132501504>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package shhclient
    29  
    30  import (
    31  	"context"
    32  
    33  	"github.com/ethereum/go-ethereum"
    34  	"github.com/ethereum/go-ethereum/common/hexutil"
    35  	"github.com/ethereum/go-ethereum/rpc"
    36  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
    37  )
    38  
    39  //
    40  type Client struct {
    41  	c *rpc.Client
    42  }
    43  
    44  //
    45  func Dial(rawurl string) (*Client, error) {
    46  	c, err := rpc.Dial(rawurl)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return NewClient(c), nil
    51  }
    52  
    53  //
    54  func NewClient(c *rpc.Client) *Client {
    55  	return &Client{c}
    56  }
    57  
    58  //
    59  func (sc *Client) Version(ctx context.Context) (string, error) {
    60  	var result string
    61  	err := sc.c.CallContext(ctx, &result, "shh_version")
    62  	return result, err
    63  }
    64  
    65  //
    66  func (sc *Client) Info(ctx context.Context) (whisper.Info, error) {
    67  	var info whisper.Info
    68  	err := sc.c.CallContext(ctx, &info, "shh_info")
    69  	return info, err
    70  }
    71  
    72  //
    73  //
    74  //
    75  func (sc *Client) SetMaxMessageSize(ctx context.Context, size uint32) error {
    76  	var ignored bool
    77  	return sc.c.CallContext(ctx, &ignored, "shh_setMaxMessageSize", size)
    78  }
    79  
    80  //
    81  //
    82  //
    83  //
    84  //
    85  func (sc *Client) SetMinimumPoW(ctx context.Context, pow float64) error {
    86  	var ignored bool
    87  	return sc.c.CallContext(ctx, &ignored, "shh_setMinPoW", pow)
    88  }
    89  
    90  //
    91  //
    92  func (sc *Client) MarkTrustedPeer(ctx context.Context, enode string) error {
    93  	var ignored bool
    94  	return sc.c.CallContext(ctx, &ignored, "shh_markTrustedPeer", enode)
    95  }
    96  
    97  //
    98  //
    99  func (sc *Client) NewKeyPair(ctx context.Context) (string, error) {
   100  	var id string
   101  	return id, sc.c.CallContext(ctx, &id, "shh_newKeyPair")
   102  }
   103  
   104  //
   105  func (sc *Client) AddPrivateKey(ctx context.Context, key []byte) (string, error) {
   106  	var id string
   107  	return id, sc.c.CallContext(ctx, &id, "shh_addPrivateKey", hexutil.Bytes(key))
   108  }
   109  
   110  //
   111  func (sc *Client) DeleteKeyPair(ctx context.Context, id string) (string, error) {
   112  	var ignored bool
   113  	return id, sc.c.CallContext(ctx, &ignored, "shh_deleteKeyPair", id)
   114  }
   115  
   116  //
   117  //
   118  func (sc *Client) HasKeyPair(ctx context.Context, id string) (bool, error) {
   119  	var has bool
   120  	return has, sc.c.CallContext(ctx, &has, "shh_hasKeyPair", id)
   121  }
   122  
   123  //
   124  func (sc *Client) PublicKey(ctx context.Context, id string) ([]byte, error) {
   125  	var key hexutil.Bytes
   126  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPublicKey", id)
   127  }
   128  
   129  //
   130  func (sc *Client) PrivateKey(ctx context.Context, id string) ([]byte, error) {
   131  	var key hexutil.Bytes
   132  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPrivateKey", id)
   133  }
   134  
   135  //
   136  //
   137  func (sc *Client) NewSymmetricKey(ctx context.Context) (string, error) {
   138  	var id string
   139  	return id, sc.c.CallContext(ctx, &id, "shh_newSymKey")
   140  }
   141  
   142  //
   143  func (sc *Client) AddSymmetricKey(ctx context.Context, key []byte) (string, error) {
   144  	var id string
   145  	return id, sc.c.CallContext(ctx, &id, "shh_addSymKey", hexutil.Bytes(key))
   146  }
   147  
   148  //
   149  func (sc *Client) GenerateSymmetricKeyFromPassword(ctx context.Context, passwd string) (string, error) {
   150  	var id string
   151  	return id, sc.c.CallContext(ctx, &id, "shh_generateSymKeyFromPassword", passwd)
   152  }
   153  
   154  //
   155  func (sc *Client) HasSymmetricKey(ctx context.Context, id string) (bool, error) {
   156  	var found bool
   157  	return found, sc.c.CallContext(ctx, &found, "shh_hasSymKey", id)
   158  }
   159  
   160  //
   161  func (sc *Client) GetSymmetricKey(ctx context.Context, id string) ([]byte, error) {
   162  	var key hexutil.Bytes
   163  	return []byte(key), sc.c.CallContext(ctx, &key, "shh_getSymKey", id)
   164  }
   165  
   166  //
   167  func (sc *Client) DeleteSymmetricKey(ctx context.Context, id string) error {
   168  	var ignored bool
   169  	return sc.c.CallContext(ctx, &ignored, "shh_deleteSymKey", id)
   170  }
   171  
   172  //
   173  func (sc *Client) Post(ctx context.Context, message whisper.NewMessage) (string, error) {
   174  	var hash string
   175  	return hash, sc.c.CallContext(ctx, &hash, "shh_post", message)
   176  }
   177  
   178  //
   179  //
   180  //
   181  func (sc *Client) SubscribeMessages(ctx context.Context, criteria whisper.Criteria, ch chan<- *whisper.Message) (ethereum.Subscription, error) {
   182  	return sc.c.ShhSubscribe(ctx, ch, "messages", criteria)
   183  }
   184  
   185  //
   186  //
   187  //
   188  func (sc *Client) NewMessageFilter(ctx context.Context, criteria whisper.Criteria) (string, error) {
   189  	var id string
   190  	return id, sc.c.CallContext(ctx, &id, "shh_newMessageFilter", criteria)
   191  }
   192  
   193  //
   194  func (sc *Client) DeleteMessageFilter(ctx context.Context, id string) error {
   195  	var ignored bool
   196  	return sc.c.CallContext(ctx, &ignored, "shh_deleteMessageFilter", id)
   197  }
   198  
   199  //
   200  //
   201  func (sc *Client) FilterMessages(ctx context.Context, id string) ([]*whisper.Message, error) {
   202  	var messages []*whisper.Message
   203  	return messages, sc.c.CallContext(ctx, &messages, "shh_getFilterMessages", id)
   204  }
   205