github.com/verovm/record-replay@v1.9.7/mobile/shhclient.go (about) 1 // Copyright 2018 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 // Contains a wrapper for the Whisper client. 18 19 package geth 20 21 import ( 22 "github.com/ethereum/go-ethereum/whisper/shhclient" 23 whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" 24 ) 25 26 // WhisperClient provides access to the Ethereum APIs. 27 type WhisperClient struct { 28 client *shhclient.Client 29 } 30 31 // NewWhisperClient connects a client to the given URL. 32 func NewWhisperClient(rawurl string) (client *WhisperClient, _ error) { 33 rawClient, err := shhclient.Dial(rawurl) 34 return &WhisperClient{rawClient}, err 35 } 36 37 // GetVersion returns the Whisper sub-protocol version. 38 func (wc *WhisperClient) GetVersion(ctx *Context) (version string, _ error) { 39 return wc.client.Version(ctx.context) 40 } 41 42 // Info returns diagnostic information about the whisper node. 43 func (wc *WhisperClient) GetInfo(ctx *Context) (info *Info, _ error) { 44 rawInfo, err := wc.client.Info(ctx.context) 45 return &Info{&rawInfo}, err 46 } 47 48 // SetMaxMessageSize sets the maximal message size allowed by this node. Incoming 49 // and outgoing messages with a larger size will be rejected. Whisper message size 50 // can never exceed the limit imposed by the underlying P2P protocol (10 Mb). 51 func (wc *WhisperClient) SetMaxMessageSize(ctx *Context, size int32) error { 52 return wc.client.SetMaxMessageSize(ctx.context, uint32(size)) 53 } 54 55 // SetMinimumPoW (experimental) sets the minimal PoW required by this node. 56 // This experimental function was introduced for the future dynamic adjustment of 57 // PoW requirement. If the node is overwhelmed with messages, it should raise the 58 // PoW requirement and notify the peers. The new value should be set relative to 59 // the old value (e.g. double). The old value could be obtained via shh_info call. 60 func (wc *WhisperClient) SetMinimumPoW(ctx *Context, pow float64) error { 61 return wc.client.SetMinimumPoW(ctx.context, pow) 62 } 63 64 // Marks specific peer trusted, which will allow it to send historic (expired) messages. 65 // Note This function is not adding new nodes, the node needs to exists as a peer. 66 func (wc *WhisperClient) MarkTrustedPeer(ctx *Context, enode string) error { 67 return wc.client.MarkTrustedPeer(ctx.context, enode) 68 } 69 70 // NewKeyPair generates a new public and private key pair for message decryption and encryption. 71 // It returns an identifier that can be used to refer to the key. 72 func (wc *WhisperClient) NewKeyPair(ctx *Context) (string, error) { 73 return wc.client.NewKeyPair(ctx.context) 74 } 75 76 // AddPrivateKey stored the key pair, and returns its ID. 77 func (wc *WhisperClient) AddPrivateKey(ctx *Context, key []byte) (string, error) { 78 return wc.client.AddPrivateKey(ctx.context, key) 79 } 80 81 // DeleteKeyPair delete the specifies key. 82 func (wc *WhisperClient) DeleteKeyPair(ctx *Context, id string) (string, error) { 83 return wc.client.DeleteKeyPair(ctx.context, id) 84 } 85 86 // HasKeyPair returns an indication if the node has a private key or 87 // key pair matching the given ID. 88 func (wc *WhisperClient) HasKeyPair(ctx *Context, id string) (bool, error) { 89 return wc.client.HasKeyPair(ctx.context, id) 90 } 91 92 // GetPublicKey return the public key for a key ID. 93 func (wc *WhisperClient) GetPublicKey(ctx *Context, id string) ([]byte, error) { 94 return wc.client.PublicKey(ctx.context, id) 95 } 96 97 // GetPrivateKey return the private key for a key ID. 98 func (wc *WhisperClient) GetPrivateKey(ctx *Context, id string) ([]byte, error) { 99 return wc.client.PrivateKey(ctx.context, id) 100 } 101 102 // NewSymmetricKey generates a random symmetric key and returns its identifier. 103 // Can be used encrypting and decrypting messages where the key is known to both parties. 104 func (wc *WhisperClient) NewSymmetricKey(ctx *Context) (string, error) { 105 return wc.client.NewSymmetricKey(ctx.context) 106 } 107 108 // AddSymmetricKey stores the key, and returns its identifier. 109 func (wc *WhisperClient) AddSymmetricKey(ctx *Context, key []byte) (string, error) { 110 return wc.client.AddSymmetricKey(ctx.context, key) 111 } 112 113 // GenerateSymmetricKeyFromPassword generates the key from password, stores it, and returns its identifier. 114 func (wc *WhisperClient) GenerateSymmetricKeyFromPassword(ctx *Context, passwd string) (string, error) { 115 return wc.client.GenerateSymmetricKeyFromPassword(ctx.context, passwd) 116 } 117 118 // HasSymmetricKey returns an indication if the key associated with the given id is stored in the node. 119 func (wc *WhisperClient) HasSymmetricKey(ctx *Context, id string) (bool, error) { 120 return wc.client.HasSymmetricKey(ctx.context, id) 121 } 122 123 // GetSymmetricKey returns the symmetric key associated with the given identifier. 124 func (wc *WhisperClient) GetSymmetricKey(ctx *Context, id string) ([]byte, error) { 125 return wc.client.GetSymmetricKey(ctx.context, id) 126 } 127 128 // DeleteSymmetricKey deletes the symmetric key associated with the given identifier. 129 func (wc *WhisperClient) DeleteSymmetricKey(ctx *Context, id string) error { 130 return wc.client.DeleteSymmetricKey(ctx.context, id) 131 } 132 133 // Post a message onto the network. 134 func (wc *WhisperClient) Post(ctx *Context, message *NewMessage) (string, error) { 135 return wc.client.Post(ctx.context, *message.newMessage) 136 } 137 138 // NewHeadHandler is a client-side subscription callback to invoke on events and 139 // subscription failure. 140 type NewMessageHandler interface { 141 OnNewMessage(message *Message) 142 OnError(failure string) 143 } 144 145 // SubscribeMessages subscribes to messages that match the given criteria. This method 146 // is only supported on bi-directional connections such as websockets and IPC. 147 // NewMessageFilter uses polling and is supported over HTTP. 148 func (wc *WhisperClient) SubscribeMessages(ctx *Context, criteria *Criteria, handler NewMessageHandler, buffer int) (*Subscription, error) { 149 // Subscribe to the event internally 150 ch := make(chan *whisper.Message, buffer) 151 rawSub, err := wc.client.SubscribeMessages(ctx.context, *criteria.criteria, ch) 152 if err != nil { 153 return nil, err 154 } 155 // Start up a dispatcher to feed into the callback 156 go func() { 157 for { 158 select { 159 case message := <-ch: 160 handler.OnNewMessage(&Message{message}) 161 162 case err := <-rawSub.Err(): 163 if err != nil { 164 handler.OnError(err.Error()) 165 } 166 return 167 } 168 } 169 }() 170 return &Subscription{rawSub}, nil 171 } 172 173 // NewMessageFilter creates a filter within the node. This filter can be used to poll 174 // for new messages (see FilterMessages) that satisfy the given criteria. A filter can 175 // timeout when it was polled for in whisper.filterTimeout. 176 func (wc *WhisperClient) NewMessageFilter(ctx *Context, criteria *Criteria) (string, error) { 177 return wc.client.NewMessageFilter(ctx.context, *criteria.criteria) 178 } 179 180 // DeleteMessageFilter removes the filter associated with the given id. 181 func (wc *WhisperClient) DeleteMessageFilter(ctx *Context, id string) error { 182 return wc.client.DeleteMessageFilter(ctx.context, id) 183 } 184 185 // GetFilterMessages retrieves all messages that are received between the last call to 186 // this function and match the criteria that where given when the filter was created. 187 func (wc *WhisperClient) GetFilterMessages(ctx *Context, id string) (*Messages, error) { 188 rawFilterMessages, err := wc.client.FilterMessages(ctx.context, id) 189 if err != nil { 190 return nil, err 191 } 192 res := make([]*whisper.Message, len(rawFilterMessages)) 193 copy(res, rawFilterMessages) 194 return &Messages{res}, nil 195 }