swarm/pss: Make validate msg addr funcs return error directly

This commit is contained in:
lash 2018-12-17 12:09:50 +01:00
parent 3959cebad0
commit 65c5e18500
2 changed files with 22 additions and 16 deletions

View file

@ -157,22 +157,22 @@ func (pssapi *API) StringToTopic(topicstring string) (Topic, error) {
}
func (pssapi *API) SendAsym(pubkeyhex string, topic Topic, msg hexutil.Bytes) error {
if !checkMsg(msg) {
return errors.New("invalid message")
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendAsym(pubkeyhex, topic, msg[:])
}
func (pssapi *API) SendSym(symkeyhex string, topic Topic, msg hexutil.Bytes) error {
if !checkMsg(msg) {
return errors.New("invalid message")
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendSym(symkeyhex, topic, msg[:])
}
func (pssapi *API) SendRaw(addr hexutil.Bytes, topic Topic, msg hexutil.Bytes) error {
if !checkMsg(msg) {
return errors.New("invalid message")
if err := validateMsg(msg); err != nil {
return err
}
return pssapi.Pss.SendRaw(PssAddress(addr), topic, msg[:])
}
@ -187,6 +187,9 @@ func (pssapi *API) GetPeerAddress(pubkeyhex string, topic Topic) (PssAddress, er
return pssapi.Pss.getPeerAddress(pubkeyhex, topic)
}
func checkMsg(msg []byte) bool {
return len(msg) > 0
func validateMsg(msg []byte) error {
if len(msg) == 0 {
return errors.New("invalid message length")
}
return nil
}

View file

@ -531,8 +531,8 @@ func (p *Pss) isSelfPossibleRecipient(msg *PssMsg, prox bool) bool {
// The value in `address` will be used as a routing hint for the
// public key / topic association
func (p *Pss) SetPeerPublicKey(pubkey *ecdsa.PublicKey, topic Topic, address PssAddress) error {
if !checkAddress(address) {
return errors.New("invalid address")
if err := validateAddress(address); err != nil {
return err
}
pubkeybytes := crypto.FromECDSAPub(pubkey)
if len(pubkeybytes) == 0 {
@ -575,8 +575,8 @@ func (p *Pss) GenerateSymmetricKey(topic Topic, address PssAddress, addToCache b
// Returns a string id that can be used to retrieve the key bytes
// from the whisper backend (see pss.GetSymmetricKey())
func (p *Pss) SetSymmetricKey(key []byte, topic Topic, address PssAddress, addtocache bool) (string, error) {
if !checkAddress(address) {
return "", errors.New("invalid address")
if err := validateAddress(address); err != nil {
return "", err
}
return p.setSymmetricKey(key, topic, address, addtocache, true)
}
@ -759,8 +759,8 @@ func (p *Pss) enqueue(msg *PssMsg) error {
//
// Will fail if raw messages are disallowed
func (p *Pss) SendRaw(address PssAddress, topic Topic, msg []byte) error {
if !checkAddress(address) {
return errors.New("invalid address")
if err := validateAddress(address); err != nil {
return err
}
pssMsgParams := &msgParams{
raw: true,
@ -1040,6 +1040,9 @@ func (p *Pss) digestBytes(msg []byte) pssDigest {
return digest
}
func checkAddress(addr PssAddress) bool {
return len(addr) <= addressLength
func validateAddress(addr PssAddress) error {
if len(addr) > addressLength {
return errors.New("address too long")
}
return nil
}