mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
whisper: minor refactoring
This commit is contained in:
parent
dd25b630a8
commit
f4ad03d888
4 changed files with 30 additions and 30 deletions
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
|
|
|
||||||
|
|
@ -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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue