aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/key_store_test.go
blob: 6e50afe34acbdba32ca9f6d72513b957c0b291de (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package crypto

import (
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto/randentropy"
    "reflect"
    "testing"
)

func TestKeyStorePlain(t *testing.T) {
    ks := NewKeyStorePlain(common.DefaultDataDir())
    pass := "" // not used but required by API
    k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
    if err != nil {
        t.Fatal(err)
    }

    k2 := new(Key)
    k2, err = ks.GetKey(k1.Address, pass)
    if err != nil {
        t.Fatal(err)
    }

    if !reflect.DeepEqual(k1.Address, k2.Address) {
        t.Fatal(err)
    }

    if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
        t.Fatal(err)
    }

    err = ks.DeleteKey(k2.Address, pass)
    if err != nil {
        t.Fatal(err)
    }
}

func TestKeyStorePassphrase(t *testing.T) {
    ks := NewKeyStorePassphrase(common.DefaultDataDir())
    pass := "foo"
    k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
    if err != nil {
        t.Fatal(err)
    }
    k2 := new(Key)
    k2, err = ks.GetKey(k1.Address, pass)
    if err != nil {
        t.Fatal(err)
    }
    if !reflect.DeepEqual(k1.Address, k2.Address) {
        t.Fatal(err)
    }

    if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
        t.Fatal(err)
    }

    err = ks.DeleteKey(k2.Address, pass) // also to clean up created files
    if err != nil {
        t.Fatal(err)
    }
}

func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    ks := NewKeyStorePassphrase(common.DefaultDataDir())
    pass := "foo"
    k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
    if err != nil {
        t.Fatal(err)
    }

    _, err = ks.GetKey(k1.Address, "bar") // wrong passphrase
    if err == nil {
        t.Fatal(err)
    }

    err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase
    if err == nil {
        t.Fatal(err)
    }

    err = ks.DeleteKey(k1.Address, pass) // to clean up
    if err != nil {
        t.Fatal(err)
    }
}

func TestImportPreSaleKey(t *testing.T) {
    // file content of a presale key file generated with:
    // python pyethsaletool.py genwallet
    // with password "foo"
    fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
    ks := NewKeyStorePassphrase(common.DefaultDataDir())
    pass := "foo"
    _, err := ImportPreSaleKey(ks, []byte(fileContent), pass)
    if err != nil {
        t.Fatal(err)
    }
}