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