whisper: API updated + new tests

This commit is contained in:
Vlad 2017-03-16 18:24:49 +01:00
parent 6eb6853d9e
commit 74678aacc9
5 changed files with 68 additions and 24 deletions

View file

@ -198,11 +198,16 @@ func (api *PublicWhisperAPI) HasSymmetricKey(id string) (bool, error) {
return res, nil return res, nil
} }
func (api *PublicWhisperAPI) GetSymmetricKey(name string) ([]byte, error) { func (api *PublicWhisperAPI) GetSymmetricKey(name string) (string, error) {
if api.whisper == nil { if api.whisper == nil {
return nil, whisperOffLineErr return "", whisperOffLineErr
} }
return api.whisper.GetSymKey(name)
b, err := api.whisper.GetSymKey(name)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", b), nil
} }
// DeleteSymKey deletes the key associated with the name string if it exists. // DeleteSymKey deletes the key associated with the name string if it exists.
@ -232,14 +237,14 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
err := ValidateKeyID(args.Key) err := ValidateKeyID(args.Key)
if err != nil { if err != nil {
info := "NewFilter: " + err.Error() info := "Subscribe: " + err.Error()
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
if len(args.SignedWith) > 0 { if len(args.SignedWith) > 0 {
if !ValidatePublicKey(filter.Src) { if !ValidatePublicKey(filter.Src) {
info := "NewFilter: Invalid 'SignedWith' field" info := "Subscribe: Invalid 'SignedWith' field"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
@ -247,18 +252,18 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
if args.Symmetric { if args.Symmetric {
if len(args.Topics) == 0 { if len(args.Topics) == 0 {
info := "NewFilter: at least one topic must be specified with symmetric encryption" info := "Subscribe: at least one topic must be specified with symmetric encryption"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
symKey, err := api.whisper.GetSymKey(args.Key) symKey, err := api.whisper.GetSymKey(args.Key)
if err != nil { if err != nil {
info := "NewFilter: invalid key ID" info := "Subscribe: invalid key ID"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
if !validateSymmetricKey(symKey) { if !validateSymmetricKey(symKey) {
info := "NewFilter: retrieved key is invalid" info := "Subscribe: retrieved key is invalid"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
@ -268,12 +273,12 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
} else { } else {
filter.KeyAsym, err = api.whisper.GetPrivateKey(args.Key) filter.KeyAsym, err = api.whisper.GetPrivateKey(args.Key)
if err != nil { if err != nil {
info := "NewFilter: invalid key ID" info := "Subscribe: invalid key ID"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
if filter.KeyAsym == nil { if filter.KeyAsym == nil {
info := "NewFilter: non-existent identity provided" info := "Subscribe: non-existent identity provided"
log.Error(info) log.Error(info)
return "", errors.New(info) return "", errors.New(info)
} }
@ -288,7 +293,7 @@ func (api *PublicWhisperAPI) Unsubscribe(id string) {
} }
// GetFilterChanges retrieves all the new messages matched by a filter since the last retrieval. // GetFilterChanges retrieves all the new messages matched by a filter since the last retrieval.
func (api *PublicWhisperAPI) GetFilterChanges(filterId string) []*WhisperMessage { func (api *PublicWhisperAPI) GetSubscriptionMessages(filterId string) []*WhisperMessage {
f := api.whisper.GetFilter(filterId) f := api.whisper.GetFilter(filterId)
if f != nil { if f != nil {
newMail := f.Retrieve() newMail := f.Retrieve()
@ -409,6 +414,10 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
return errors.New(info) return errors.New(info)
} }
return api.whisper.SendP2PMessage(n.ID[:], envelope) return api.whisper.SendP2PMessage(n.ID[:], envelope)
} else if args.PowTarget < api.whisper.minPoW {
info := "Post: target PoW is less than minimum PoW, the message can not be sent"
log.Error(info)
return errors.New(info)
} }
return api.whisper.Send(envelope) return api.whisper.Send(envelope)

View file

@ -17,7 +17,6 @@
package whisperv5 package whisperv5
import ( import (
"bytes"
"encoding/json" "encoding/json"
"testing" "testing"
"time" "time"
@ -43,7 +42,7 @@ func TestBasic(t *testing.T) {
t.Fatalf("wrong version: %d.", ver) t.Fatalf("wrong version: %d.", ver)
} }
mail := api.GetFilterChanges("non-existent-id") mail := api.GetSubscriptionMessages("non-existent-id")
if len(mail) != 0 { if len(mail) != 0 {
t.Fatalf("failed GetFilterChanges: premature result") t.Fatalf("failed GetFilterChanges: premature result")
} }
@ -148,7 +147,7 @@ func TestBasic(t *testing.T) {
t.Fatalf("failed GetSymKey(id2): %s.", err) t.Fatalf("failed GetSymKey(id2): %s.", err)
} }
if !bytes.Equal(k1, k2) { if k1 != k2 {
t.Fatalf("installed keys are not equal") t.Fatalf("installed keys are not equal")
} }
@ -171,7 +170,7 @@ func TestBasic(t *testing.T) {
func TestUnmarshalFilterArgs(t *testing.T) { func TestUnmarshalFilterArgs(t *testing.T) {
s := []byte(`{ s := []byte(`{
"type":"asym", "type":"sym",
"key":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d", "key":"0x70c87d191324e6712a591f304b4eedef6ad9bb9d",
"signedWith":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83", "signedWith":"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83",
"minPoW":2.34, "minPoW":2.34,
@ -185,7 +184,7 @@ func TestUnmarshalFilterArgs(t *testing.T) {
t.Fatalf("failed UnmarshalJSON: %s.", err) t.Fatalf("failed UnmarshalJSON: %s.", err)
} }
if f.Symmetric { if !f.Symmetric {
t.Fatalf("wrong type.") t.Fatalf("wrong type.")
} }
if f.Key != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" { if f.Key != "0x70c87d191324e6712a591f304b4eedef6ad9bb9d" {
@ -282,7 +281,7 @@ func waitForMessages(api *PublicWhisperAPI, id string, target int) []*WhisperMes
// timeout: 2 seconds // timeout: 2 seconds
result := make([]*WhisperMessage, 0, target) result := make([]*WhisperMessage, 0, target)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
mail := api.GetFilterChanges(id) mail := api.GetSubscriptionMessages(id)
if len(mail) > 0 { if len(mail) > 0 {
for _, m := range mail { for _, m := range mail {
result = append(result, m) result = append(result, m)
@ -592,3 +591,40 @@ func TestIntegrationSymWithFilter(t *testing.T) {
t.Fatalf("failed to decrypt second message: %s.", text) t.Fatalf("failed to decrypt second message: %s.", text)
} }
} }
func TestKey(t *testing.T) {
w := New()
api := NewPublicWhisperAPI(w)
if api == nil {
t.Fatalf("failed to create API.")
}
k, err := api.AddSymmetricKeyFromPassword("wwww")
if err != nil {
t.Fatalf("failed to create key: %s.", err)
}
s, err := api.GetSymmetricKey(k)
if err != nil {
t.Fatalf("failed to get sym key: %s.", err)
}
b := common.FromHex(s)
k2, err := api.AddSymmetricKeyDirect(b)
if err != nil {
t.Fatalf("failed to add sym key: %s.", err)
}
s2, err := api.GetSymmetricKey(k2)
if err != nil {
t.Fatalf("failed to get sym key: %s.", err)
}
if s != "448652d595bd6ec00b2a9ea220ad6c26592d9bf4cf79023d3c1b30cb681e6e07" {
t.Fatalf("wrong key from password")
}
if s != s2 {
t.Fatalf("wrong key")
}
}

View file

@ -21,7 +21,6 @@ package whisperv5
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
gmath "math" gmath "math"
"math/big" "math/big"
@ -121,7 +120,7 @@ func (e *Envelope) Seal(options *MessageParams) error {
} }
if target > 0 && bestBit < target { if target > 0 && bestBit < target {
return errors.New("Failed to reach the PoW target, insufficient work time") return fmt.Errorf("Failed to reach the PoW target, specified pow time (%d seconds) was insufficient", options.WorkTime)
} }
return nil return nil

View file

@ -257,7 +257,7 @@ func sendMsg(t *testing.T, expected bool, id int) {
return return
} }
opt := MessageParams{KeySym: sharedKey, Topic: sharedTopic, Payload: expectedMessage, PoW: 0.00000001} opt := MessageParams{KeySym: sharedKey, Topic: sharedTopic, Payload: expectedMessage, PoW: 0.00000001, WorkTime: 1}
if !expected { if !expected {
opt.KeySym[0]++ opt.KeySym[0]++
opt.Topic[0]++ opt.Topic[0]++
@ -267,12 +267,12 @@ func sendMsg(t *testing.T, expected bool, id int) {
msg := NewSentMessage(&opt) msg := NewSentMessage(&opt)
envelope, err := msg.Wrap(&opt) envelope, err := msg.Wrap(&opt)
if err != nil { if err != nil {
t.Fatalf("failed to seal message.") t.Fatalf("failed to seal message: %s", err)
} }
err = nodes[id].shh.Send(envelope) err = nodes[id].shh.Send(envelope)
if err != nil { if err != nil {
t.Fatalf("failed to send message.") t.Fatalf("failed to send message: %s", err)
} }
} }

View file

@ -348,7 +348,7 @@ func (w *Whisper) GetSymKey(id string) ([]byte, error) {
if w.symKeys[id] != nil { if w.symKeys[id] != nil {
return w.symKeys[id], nil return w.symKeys[id], nil
} }
return nil, fmt.Errorf("non-existent ID") return nil, fmt.Errorf("non-existent key ID")
} }
// Watch installs a new message handler to run in case a matching packet arrives // Watch installs a new message handler to run in case a matching packet arrives
@ -383,7 +383,7 @@ func (w *Whisper) Send(envelope *Envelope) error {
// Start implements node.Service, starting the background data propagation thread // Start implements node.Service, starting the background data propagation thread
// of the Whisper protocol. // of the Whisper protocol.
func (w *Whisper) Start(*p2p.Server) error { func (w *Whisper) Start(*p2p.Server) error {
log.Info(fmt.Sprint("Whisper started")) log.Info(fmt.Sprintf("Whisper v%d started", ProtocolVersion))
go w.update() go w.update()
numCPU := runtime.NumCPU() numCPU := runtime.NumCPU()