diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2017-04-27 03:05:48 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-04-27 03:05:48 +0800 |
commit | 95f0bd0acf301bf8415747c4ff050e8a4dfdc864 (patch) | |
tree | 13edad970fb30a8e12151b2ec1a443b0ad970d2b /whisper/whisperv5/api.go | |
parent | 8dce4c283dda3a8e10aa30dadab05a8c0dd9e19d (diff) | |
download | dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.gz dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.bz2 dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.lz dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.xz dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.zst dexon-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.zip |
whisper: message format refactoring (#14335)
* whisper: salt removed from AES encryption
* whisper: padding format updated
* whisper: padding test added
* whisper: padding refactored, tests fixed
* whisper: padding test updated
* whisper: wnode bugfix
* whisper: send/receive protocol updated
* whisper: minor update
* whisper: bugfix in test
* whisper: updated parameter names and comments
* whisper: functions renamed
* whisper: minor refactoring
Diffstat (limited to 'whisper/whisperv5/api.go')
-rw-r--r-- | whisper/whisperv5/api.go | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/whisper/whisperv5/api.go b/whisper/whisperv5/api.go index 579efba9e..841bbc2ba 100644 --- a/whisper/whisperv5/api.go +++ b/whisper/whisperv5/api.go @@ -214,7 +214,6 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) { } filter := Filter{ - Src: crypto.ToECDSAPub(common.FromHex(args.SignedWith)), PoW: args.MinPoW, Messages: make(map[common.Hash]*ReceivedMessage), AllowP2P: args.AllowP2P, @@ -233,6 +232,11 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) { } if len(args.SignedWith) > 0 { + sb := common.FromHex(args.SignedWith) + if sb == nil { + return "", errors.New("subscribe: SignedWith parameter is invalid") + } + filter.Src = crypto.ToECDSAPub(sb) if !ValidatePublicKey(filter.Src) { return "", errors.New("subscribe: invalid 'SignedWith' field") } @@ -269,9 +273,10 @@ func (api *PublicWhisperAPI) Unsubscribe(id string) { api.whisper.Unsubscribe(id) } -// GetSubscriptionMessages retrieves all the new messages matched by a filter since the last retrieval. -func (api *PublicWhisperAPI) GetSubscriptionMessages(filterId string) []*WhisperMessage { - f := api.whisper.GetFilter(filterId) +// GetSubscriptionMessages retrieves all the new messages matched by the corresponding +// subscription filter since the last retrieval. +func (api *PublicWhisperAPI) GetNewSubscriptionMessages(id string) []*WhisperMessage { + f := api.whisper.GetFilter(id) if f != nil { newMail := f.Retrieve() return toWhisperMessages(newMail) @@ -279,10 +284,10 @@ func (api *PublicWhisperAPI) GetSubscriptionMessages(filterId string) []*Whisper return toWhisperMessages(nil) } -// GetMessages retrieves all the floating messages that match a specific filter. +// GetMessages retrieves all the floating messages that match a specific subscription filter. // It is likely to be called once per session, right after Subscribe call. -func (api *PublicWhisperAPI) GetMessages(filterId string) []*WhisperMessage { - all := api.whisper.Messages(filterId) +func (api *PublicWhisperAPI) GetFloatingMessages(id string) []*WhisperMessage { + all := api.whisper.Messages(id) return toWhisperMessages(all) } @@ -345,7 +350,11 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error { return errors.New("post: topic is missing for symmetric encryption") } } else if args.Type == "asym" { - params.Dst = crypto.ToECDSAPub(common.FromHex(args.Key)) + kb := common.FromHex(args.Key) + if kb == nil { + return errors.New("post: public key for asymmetric encryption is invalid") + } + params.Dst = crypto.ToECDSAPub(kb) if !ValidatePublicKey(params.Dst) { return errors.New("post: public key for asymmetric encryption is invalid") } @@ -354,9 +363,9 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error { } // encrypt and send - message := NewSentMessage(¶ms) - if message == nil { - return errors.New("post: failed create new message, probably due to failed rand function (OS level)") + message, err := NewSentMessage(¶ms) + if err != nil { + return err } envelope, err := message.Wrap(¶ms) if err != nil { @@ -383,7 +392,7 @@ type PostArgs struct { Type string `json:"type"` // "sym"/"asym" (symmetric or asymmetric) TTL uint32 `json:"ttl"` // time-to-live in seconds SignWith string `json:"signWith"` // id of the signing key - Key string `json:"key"` // id of encryption key + Key string `json:"key"` // key id (in case of sym) or public key (in case of asym) Topic hexutil.Bytes `json:"topic"` // topic (4 bytes) Padding hexutil.Bytes `json:"padding"` // optional padding bytes Payload hexutil.Bytes `json:"payload"` // payload to be encrypted @@ -474,7 +483,6 @@ type WhisperMessage struct { // NewWhisperMessage converts an internal message into an API version. func NewWhisperMessage(message *ReceivedMessage) *WhisperMessage { msg := WhisperMessage{ - Topic: common.ToHex(message.Topic[:]), Payload: common.ToHex(message.Payload), Padding: common.ToHex(message.Padding), Timestamp: message.Sent, @@ -483,11 +491,20 @@ func NewWhisperMessage(message *ReceivedMessage) *WhisperMessage { Hash: common.ToHex(message.EnvelopeHash.Bytes()), } + if len(message.Topic) == TopicLength { + msg.Topic = common.ToHex(message.Topic[:]) + } if message.Dst != nil { - msg.Dst = common.ToHex(crypto.FromECDSAPub(message.Dst)) + b := crypto.FromECDSAPub(message.Dst) + if b != nil { + msg.Dst = common.ToHex(b) + } } if isMessageSigned(message.Raw[0]) { - msg.Src = common.ToHex(crypto.FromECDSAPub(message.SigToPubKey())) + b := crypto.FromECDSAPub(message.SigToPubKey()) + if b != nil { + msg.Src = common.ToHex(b) + } } return &msg } |