Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whisper: Post returns the hash of sent message #16495

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions whisper/whisperv6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ type newMessageOverride struct {
Padding hexutil.Bytes
}

// Post a message on the Whisper network.
func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, error) {
// Post posts a message on the Whisper network.
// returns the hash of the message in case of success.
func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (hexutil.Bytes, error) {
var (
symKeyGiven = len(req.SymKeyID) > 0
pubKeyGiven = len(req.PublicKey) > 0
Expand All @@ -241,7 +242,7 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, er

// user must specify either a symmetric or an asymmetric key
if (symKeyGiven && pubKeyGiven) || (!symKeyGiven && !pubKeyGiven) {
return false, ErrSymAsym
return nil, ErrSymAsym
}

params := &MessageParams{
Expand All @@ -256,57 +257,68 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, er
// Set key that is used to sign the message
if len(req.Sig) > 0 {
if params.Src, err = api.w.GetPrivateKey(req.Sig); err != nil {
return false, err
return nil, err
}
}

// Set symmetric key that is used to encrypt the message
if symKeyGiven {
if params.Topic == (TopicType{}) { // topics are mandatory with symmetric encryption
return false, ErrNoTopics
return nil, ErrNoTopics
}
if params.KeySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
return false, err
return nil, err
}
if !validateDataIntegrity(params.KeySym, aesKeyLength) {
return false, ErrInvalidSymmetricKey
return nil, ErrInvalidSymmetricKey
}
}

// Set asymmetric key that is used to encrypt the message
if pubKeyGiven {
params.Dst = crypto.ToECDSAPub(req.PublicKey)
if !ValidatePublicKey(params.Dst) {
return false, ErrInvalidPublicKey
return nil, ErrInvalidPublicKey
}
}

// encrypt and sent message
whisperMsg, err := NewSentMessage(params)
if err != nil {
return false, err
return nil, err
}

var result []byte
env, err := whisperMsg.Wrap(params)
if err != nil {
return false, err
return nil, err
}

// send to specific node (skip PoW check)
if len(req.TargetPeer) > 0 {
n, err := discover.ParseNode(req.TargetPeer)
if err != nil {
return false, fmt.Errorf("failed to parse target peer: %s", err)
return nil, fmt.Errorf("failed to parse target peer: %s", err)
}
err = api.w.SendP2PMessage(n.ID[:], env)
if err == nil {
hash := env.Hash()
result = hash[:]
}
return true, api.w.SendP2PMessage(n.ID[:], env)
return result, err
}

// ensure that the message PoW meets the node's minimum accepted PoW
if req.PowTarget < api.w.MinPow() {
return false, ErrTooLowPoW
return nil, ErrTooLowPoW
}

return true, api.w.Send(env)
err = api.w.Send(env)
if err == nil {
hash := env.Hash()
result = hash[:]
}
return result, err
}

//go:generate gencodec -type Criteria -field-override criteriaOverride -out gen_criteria_json.go
Expand Down