aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/messages_test.go
blob: cba103011d6e69e84e99655bcab5d54c0b49b71b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package whisper

import (
    "bytes"
    "crypto/elliptic"
    "fmt"
    "testing"

    "github.com/ethereum/go-ethereum/crypto"
)

func TestSign(t *testing.T) {
    prv, _ := crypto.GenerateKey()
    msg := NewMessage([]byte("hello world"))
    msg.sign(prv)

    pubKey := msg.Recover()
    p1 := elliptic.Marshal(crypto.S256(), prv.PublicKey.X, prv.PublicKey.Y)
    p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y)

    if !bytes.Equal(p1, p2) {
        t.Error("recovered pub key did not match")
    }
}

func TestMessageEncryptDecrypt(t *testing.T) {
    prv1, _ := crypto.GenerateKey()
    prv2, _ := crypto.GenerateKey()

    data := []byte("hello world")
    msg := NewMessage(data)
    envelope, err := msg.Seal(DefaultPow, Opts{
        From: prv1,
        To:   &prv2.PublicKey,
    })
    if err != nil {
        fmt.Println(err)
        t.FailNow()
    }

    msg1, err := envelope.Open(prv2)
    if err != nil {
        fmt.Println(err)
        t.FailNow()
    }

    if !bytes.Equal(msg1.Payload, data) {
        fmt.Println("encryption error. data did not match")
        t.FailNow()
    }
}