2014-12-31 16:39:33 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2015-02-04 18:06:06 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/randentropy"
|
2015-02-23 12:28:20 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-12-31 16:39:33 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
2015-02-23 12:28:20 +02:00
|
|
|
ks := NewKeyStorePlain(ethutil.DefaultDataDir())
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "" // not used but required by API
|
2015-02-13 16:38:18 +02:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
k2 := new(Key)
|
2015-01-25 03:07:20 +02:00
|
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
err = ks.DeleteKey(k2.Address, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
2015-02-23 12:28:20 +02:00
|
|
|
ks := NewKeyStorePassphrase(ethutil.DefaultDataDir())
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "foo"
|
2015-02-13 16:38:18 +02:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
k2 := new(Key)
|
2015-01-25 03:07:20 +02:00
|
|
|
k2, err = ks.GetKey(k1.Address, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-01-25 03:07:20 +02:00
|
|
|
if !reflect.DeepEqual(k1.Address, k2.Address) {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
err = ks.DeleteKey(k2.Address, pass) // also to clean up created files
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
2015-02-23 12:28:20 +02:00
|
|
|
ks := NewKeyStorePassphrase(ethutil.DefaultDataDir())
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "foo"
|
2015-02-13 16:38:18 +02:00
|
|
|
k1, err := ks.GenerateNewKey(randentropy.Reader, pass)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
_, err = ks.GetKey(k1.Address, "bar") // wrong passphrase
|
2014-12-31 16:39:33 +02:00
|
|
|
if err == nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
err = ks.DeleteKey(k1.Address, "bar") // wrong passphrase
|
2014-12-31 16:39:33 +02:00
|
|
|
if err == nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 03:07:20 +02:00
|
|
|
err = ks.DeleteKey(k1.Address, pass) // to clean up
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-15 18:45:45 +02:00
|
|
|
t.Fatal(err)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 00:55:13 +02:00
|
|
|
|
|
|
|
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\"}"
|
2015-02-23 12:28:20 +02:00
|
|
|
ks := NewKeyStorePassphrase(ethutil.DefaultDataDir())
|
2015-01-21 00:55:13 +02:00
|
|
|
pass := "foo"
|
|
|
|
_, err := ImportPreSaleKey(ks, []byte(fileContent), pass)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|