Skip to content

Commit 2dcf75a

Browse files
gluk256fjl
authored andcommitted
whisper/shhapi, whisper/whisperv5: refactoring (bnb-chain#3364)
* Filter refactoring * API tests added + bugfix * fixed the error logs * FilterID fixed * test cases fixed * key generation updated * POW updated * got rid of redundant stuff
1 parent 671fd94 commit 2dcf75a

14 files changed

Lines changed: 828 additions & 650 deletions

‎whisper/shhapi/api.go‎

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ func (api *PublicWhisperAPI) DeleteSymKey(name string) error {
149149

150150
// NewWhisperFilter creates and registers a new message filter to watch for inbound whisper messages.
151151
// Returns the ID of the newly created Filter.
152-
func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber, error) {
152+
func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
153153
if api.whisper == nil {
154-
return nil, whisperOffLineErr
154+
return 0, whisperOffLineErr
155155
}
156156

157157
filter := whisperv5.Filter{
158-
Src: crypto.ToECDSAPub(args.From),
158+
Src: crypto.ToECDSAPub(common.FromHex(args.From)),
159159
KeySym: api.whisper.GetSymKey(args.KeyName),
160160
PoW: args.PoW,
161161
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
@@ -173,62 +173,62 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber,
173173
if len(args.Topics) == 0 {
174174
info := "NewFilter: at least one topic must be specified"
175175
glog.V(logger.Error).Infof(info)
176-
return nil, errors.New(info)
176+
return 0, errors.New(info)
177177
}
178178

179179
if len(args.KeyName) != 0 && len(filter.KeySym) == 0 {
180180
info := "NewFilter: key was not found by name: " + args.KeyName
181181
glog.V(logger.Error).Infof(info)
182-
return nil, errors.New(info)
182+
return 0, errors.New(info)
183183
}
184184

185185
if len(args.To) == 0 && len(filter.KeySym) == 0 {
186186
info := "NewFilter: filter must contain either symmetric or asymmetric key"
187187
glog.V(logger.Error).Infof(info)
188-
return nil, errors.New(info)
188+
return 0, errors.New(info)
189189
}
190190

191191
if len(args.To) != 0 && len(filter.KeySym) != 0 {
192192
info := "NewFilter: filter must not contain both symmetric and asymmetric key"
193193
glog.V(logger.Error).Infof(info)
194-
return nil, errors.New(info)
194+
return 0, errors.New(info)
195195
}
196196

197197
if len(args.To) > 0 {
198-
dst := crypto.ToECDSAPub(args.To)
198+
dst := crypto.ToECDSAPub(common.FromHex(args.To))
199199
if !whisperv5.ValidatePublicKey(dst) {
200200
info := "NewFilter: Invalid 'To' address"
201201
glog.V(logger.Error).Infof(info)
202-
return nil, errors.New(info)
202+
return 0, errors.New(info)
203203
}
204204
filter.KeyAsym = api.whisper.GetIdentity(string(args.To))
205205
if filter.KeyAsym == nil {
206206
info := "NewFilter: non-existent identity provided"
207207
glog.V(logger.Error).Infof(info)
208-
return nil, errors.New(info)
208+
return 0, errors.New(info)
209209
}
210210
}
211211

212212
if len(args.From) > 0 {
213213
if !whisperv5.ValidatePublicKey(filter.Src) {
214214
info := "NewFilter: Invalid 'From' address"
215215
glog.V(logger.Error).Infof(info)
216-
return nil, errors.New(info)
216+
return 0, errors.New(info)
217217
}
218218
}
219219

220220
id := api.whisper.Watch(&filter)
221-
return rpc.NewHexNumber(id), nil
221+
return id, nil
222222
}
223223

224224
// UninstallFilter disables and removes an existing filter.
225-
func (api *PublicWhisperAPI) UninstallFilter(filterId rpc.HexNumber) {
226-
api.whisper.Unwatch(filterId.Int())
225+
func (api *PublicWhisperAPI) UninstallFilter(filterId uint32) {
226+
api.whisper.Unwatch(filterId)
227227
}
228228

229229
// GetFilterChanges retrieves all the new messages matched by a filter since the last retrieval.
230-
func (api *PublicWhisperAPI) GetFilterChanges(filterId rpc.HexNumber) []WhisperMessage {
231-
f := api.whisper.GetFilter(filterId.Int())
230+
func (api *PublicWhisperAPI) GetFilterChanges(filterId uint32) []WhisperMessage {
231+
f := api.whisper.GetFilter(filterId)
232232
if f != nil {
233233
newMail := f.Retrieve()
234234
return toWhisperMessages(newMail)
@@ -237,8 +237,8 @@ func (api *PublicWhisperAPI) GetFilterChanges(filterId rpc.HexNumber) []WhisperM
237237
}
238238

239239
// GetMessages retrieves all the known messages that match a specific filter.
240-
func (api *PublicWhisperAPI) GetMessages(filterId rpc.HexNumber) []WhisperMessage {
241-
all := api.whisper.Messages(filterId.Int())
240+
func (api *PublicWhisperAPI) GetMessages(filterId uint32) []WhisperMessage {
241+
all := api.whisper.Messages(filterId)
242242
return toWhisperMessages(all)
243243
}
244244

@@ -259,7 +259,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
259259

260260
params := whisperv5.MessageParams{
261261
TTL: args.TTL,
262-
Dst: crypto.ToECDSAPub(args.To),
262+
Dst: crypto.ToECDSAPub(common.FromHex(args.To)),
263263
KeySym: api.whisper.GetSymKey(args.KeyName),
264264
Topic: args.Topic,
265265
Payload: args.Payload,
@@ -269,7 +269,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
269269
}
270270

271271
if len(args.From) > 0 {
272-
pub := crypto.ToECDSAPub(args.From)
272+
pub := crypto.ToECDSAPub(common.FromHex(args.From))
273273
if !whisperv5.ValidatePublicKey(pub) {
274274
info := "Post: Invalid 'From' address"
275275
glog.V(logger.Error).Infof(info)
@@ -284,7 +284,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
284284
}
285285

286286
filter := api.whisper.GetFilter(args.FilterID)
287-
if filter == nil && args.FilterID > -1 {
287+
if filter == nil && args.FilterID > 0 {
288288
info := fmt.Sprintf("Post: wrong filter id %d", args.FilterID)
289289
glog.V(logger.Error).Infof(info)
290290
return errors.New(info)
@@ -321,13 +321,13 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
321321
return errors.New(info)
322322
}
323323

324-
if len(args.To) == 0 && len(args.KeyName) == 0 {
324+
if len(args.To) == 0 && len(params.KeySym) == 0 {
325325
info := "Post: message must be encrypted either symmetrically or asymmetrically"
326326
glog.V(logger.Error).Infof(info)
327327
return errors.New(info)
328328
}
329329

330-
if len(args.To) != 0 && len(args.KeyName) != 0 {
330+
if len(args.To) != 0 && len(params.KeySym) != 0 {
331331
info := "Post: ambigous encryption method requested"
332332
glog.V(logger.Error).Infof(info)
333333
return errors.New(info)
@@ -368,60 +368,21 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
368368

369369
type PostArgs struct {
370370
TTL uint32 `json:"ttl"`
371-
From rpc.HexBytes `json:"from"`
372-
To rpc.HexBytes `json:"to"`
371+
From string `json:"from"`
372+
To string `json:"to"`
373373
KeyName string `json:"keyname"`
374374
Topic whisperv5.TopicType `json:"topic"`
375375
Padding rpc.HexBytes `json:"padding"`
376376
Payload rpc.HexBytes `json:"payload"`
377377
WorkTime uint32 `json:"worktime"`
378378
PoW float64 `json:"pow"`
379-
FilterID int `json:"filter"`
380-
PeerID rpc.HexBytes `json:"directP2P"`
381-
}
382-
383-
func (args *PostArgs) UnmarshalJSON(data []byte) (err error) {
384-
var obj struct {
385-
TTL uint32 `json:"ttl"`
386-
From rpc.HexBytes `json:"from"`
387-
To rpc.HexBytes `json:"to"`
388-
KeyName string `json:"keyname"`
389-
Topic whisperv5.TopicType `json:"topic"`
390-
Payload rpc.HexBytes `json:"payload"`
391-
Padding rpc.HexBytes `json:"padding"`
392-
WorkTime uint32 `json:"worktime"`
393-
PoW float64 `json:"pow"`
394-
FilterID rpc.HexBytes `json:"filter"`
395-
PeerID rpc.HexBytes `json:"directP2P"`
396-
}
397-
398-
if err := json.Unmarshal(data, &obj); err != nil {
399-
return err
400-
}
401-
402-
args.TTL = obj.TTL
403-
args.From = obj.From
404-
args.To = obj.To
405-
args.KeyName = obj.KeyName
406-
args.Topic = obj.Topic
407-
args.Payload = obj.Payload
408-
args.Padding = obj.Padding
409-
args.WorkTime = obj.WorkTime
410-
args.PoW = obj.PoW
411-
args.FilterID = -1
412-
args.PeerID = obj.PeerID
413-
414-
if obj.FilterID != nil {
415-
x := whisperv5.BytesToIntBigEndian(obj.FilterID)
416-
args.FilterID = int(x)
417-
}
418-
419-
return nil
379+
FilterID uint32 `json:"filterID"`
380+
PeerID rpc.HexBytes `json:"peerID"`
420381
}
421382

422383
type WhisperFilterArgs struct {
423-
To []byte
424-
From []byte
384+
To string
385+
From string
425386
KeyName string
426387
PoW float64
427388
Topics []whisperv5.TopicType
@@ -433,8 +394,8 @@ type WhisperFilterArgs struct {
433394
func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
434395
// Unmarshal the JSON message and sanity check
435396
var obj struct {
436-
To rpc.HexBytes `json:"to"`
437-
From rpc.HexBytes `json:"from"`
397+
To string `json:"to"`
398+
From string `json:"from"`
438399
KeyName string `json:"keyname"`
439400
PoW float64 `json:"pow"`
440401
Topics []interface{} `json:"topics"`

0 commit comments

Comments
 (0)