message_test.go 964 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package whisper
  2. import (
  3. "bytes"
  4. "crypto/elliptic"
  5. "fmt"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/crypto"
  8. )
  9. func TestSign(t *testing.T) {
  10. prv, _ := crypto.GenerateKey()
  11. msg := NewMessage([]byte("hello world"))
  12. msg.sign(prv)
  13. pubKey := msg.Recover()
  14. p1 := elliptic.Marshal(crypto.S256(), prv.PublicKey.X, prv.PublicKey.Y)
  15. p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y)
  16. if !bytes.Equal(p1, p2) {
  17. t.Error("recovered pub key did not match")
  18. }
  19. }
  20. func TestMessageEncryptDecrypt(t *testing.T) {
  21. prv1, _ := crypto.GenerateKey()
  22. prv2, _ := crypto.GenerateKey()
  23. data := []byte("hello world")
  24. msg := NewMessage(data)
  25. envelope, err := msg.Seal(DefaultPow, Opts{
  26. From: prv1,
  27. To: &prv2.PublicKey,
  28. })
  29. if err != nil {
  30. fmt.Println(err)
  31. t.FailNow()
  32. }
  33. msg1, err := envelope.Open(prv2)
  34. if err != nil {
  35. t.Error(err)
  36. t.FailNow()
  37. }
  38. if !bytes.Equal(msg1.Payload, data) {
  39. t.Error("encryption error. data did not match")
  40. }
  41. }