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 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")
}

View file

@ -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")
}

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)
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.

View file

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