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