From f4ad03d88857e02106c205495cd29970a3a56ea5 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Apr 2017 13:10:50 +0200 Subject: [PATCH] whisper: minor refactoring --- cmd/wnode/main.go | 6 +++++- whisper/whisperv5/api.go | 12 ++++++++++-- whisper/whisperv5/message.go | 38 ++++++++++++------------------------ whisper/whisperv5/peer.go | 4 +++- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/cmd/wnode/main.go b/cmd/wnode/main.go index 77cd05915e..cb6fec38e7 100644 --- a/cmd/wnode/main.go +++ b/cmd/wnode/main.go @@ -308,7 +308,11 @@ func configureNode() { if *asymmetricMode { if len(*argPub) == 0 { s := scanLine("Please enter the peer's public key: ") - pub = crypto.ToECDSAPub(common.FromHex(s)) + b := common.FromHex(s) + if b == nil { + utils.Fatalf("Error: can not convert hexadecimal string") + } + pub = crypto.ToECDSAPub(b) if !isKeyValid(pub) { utils.Fatalf("Error: invalid public key") } diff --git a/whisper/whisperv5/api.go b/whisper/whisperv5/api.go index bcbcde580b..841bbc2bab 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") } @@ -346,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") } diff --git a/whisper/whisperv5/message.go b/whisper/whisperv5/message.go index b3851da969..4ef469b515 100644 --- a/whisper/whisperv5/message.go +++ b/whisper/whisperv5/message.go @@ -100,35 +100,21 @@ func NewSentMessage(params *MessageParams) (*SentMessage, error) { } // getSizeOfLength returns the number of bytes necessary to encode the entire size padding (including these bytes) -func getSizeOfLength(b []byte) (int, error) { - // prefer clarity over efficiency - var sizeOfLength int - len := len(b) - - // first iteration - if len < 256 { - sizeOfLength = 1 - } else if len < 256*256 { - sizeOfLength = 2 - } else if len < 256*256*256 { - sizeOfLength = 3 - } else { - return 0, errors.New("oversized padding parameter") +func getSizeOfLength(b []byte) (sz int, err error) { + sz = intSize(len(b)) // first iteration + sz = intSize(len(b) + sz) // second iteration + if sz > 3 { + err = errors.New("oversized padding parameter") } + return sz, err +} - // second iteration - total := len + sizeOfLength - if total < 256 { - sizeOfLength = 1 - } else if total < 256*256 { - sizeOfLength = 2 - } else if total < 256*256*256 { - sizeOfLength = 3 - } else { - return 0, errors.New("oversized padding parameter") +// sizeOfIntSize returns minimal number of bytes necessary to encode an integer value +func intSize(i int) (s int) { + for s = 1; i >= 256; s++ { + i /= 256 } - - return sizeOfLength, nil + return s } // appendPadding appends the pseudorandom padding bytes and sets the padding flag. diff --git a/whisper/whisperv5/peer.go b/whisper/whisperv5/peer.go index de367e7a67..179c931795 100644 --- a/whisper/whisperv5/peer.go +++ b/whisper/whisperv5/peer.go @@ -162,7 +162,9 @@ func (p *Peer) broadcast() error { } } } - log.Trace("broadcast", "num. messages", cnt) + if cnt > 0 { + log.Trace("broadcast", "num. messages", cnt) + } return nil }