whisper: minor refactoring

This commit is contained in:
Vlad 2017-04-25 13:10:50 +02:00
parent dd25b630a8
commit f4ad03d888
4 changed files with 30 additions and 30 deletions

View file

@ -308,7 +308,11 @@ func configureNode() {
if *asymmetricMode { if *asymmetricMode {
if len(*argPub) == 0 { if len(*argPub) == 0 {
s := scanLine("Please enter the peer's public key: ") 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) { if !isKeyValid(pub) {
utils.Fatalf("Error: invalid public key") utils.Fatalf("Error: invalid public key")
} }

View file

@ -214,7 +214,6 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
} }
filter := Filter{ filter := Filter{
Src: crypto.ToECDSAPub(common.FromHex(args.SignedWith)),
PoW: args.MinPoW, PoW: args.MinPoW,
Messages: make(map[common.Hash]*ReceivedMessage), Messages: make(map[common.Hash]*ReceivedMessage),
AllowP2P: args.AllowP2P, AllowP2P: args.AllowP2P,
@ -233,6 +232,11 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
} }
if len(args.SignedWith) > 0 { 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) { if !ValidatePublicKey(filter.Src) {
return "", errors.New("subscribe: invalid 'SignedWith' field") 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") return errors.New("post: topic is missing for symmetric encryption")
} }
} else if args.Type == "asym" { } 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) { if !ValidatePublicKey(params.Dst) {
return errors.New("post: public key for asymmetric encryption is invalid") return errors.New("post: public key for asymmetric encryption is invalid")
} }

View file

@ -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) // getSizeOfLength returns the number of bytes necessary to encode the entire size padding (including these bytes)
func getSizeOfLength(b []byte) (int, error) { func getSizeOfLength(b []byte) (sz int, err error) {
// prefer clarity over efficiency sz = intSize(len(b)) // first iteration
var sizeOfLength int sz = intSize(len(b) + sz) // second iteration
len := len(b) if sz > 3 {
err = errors.New("oversized padding parameter")
// 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")
} }
return sz, err
}
// second iteration // sizeOfIntSize returns minimal number of bytes necessary to encode an integer value
total := len + sizeOfLength func intSize(i int) (s int) {
if total < 256 { for s = 1; i >= 256; s++ {
sizeOfLength = 1 i /= 256
} else if total < 256*256 {
sizeOfLength = 2
} else if total < 256*256*256 {
sizeOfLength = 3
} else {
return 0, errors.New("oversized padding parameter")
} }
return s
return sizeOfLength, nil
} }
// appendPadding appends the pseudorandom padding bytes and sets the padding flag. // appendPadding appends the pseudorandom padding bytes and sets the padding flag.

View file

@ -162,7 +162,9 @@ func (p *Peer) broadcast() error {
} }
} }
} }
if cnt > 0 {
log.Trace("broadcast", "num. messages", cnt) log.Trace("broadcast", "num. messages", cnt)
}
return nil return nil
} }