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