2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2017-01-24 12:49:20 +03:00
|
|
|
package keystore
|
2014-12-31 16:39:33 +02:00
|
|
|
|
|
|
|
import (
|
2016-03-03 02:15:42 +02:00
|
|
|
"crypto/rand"
|
2015-05-24 04:42:10 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-03-03 02:15:42 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-07-11 14:49:14 +03:00
|
|
|
"path/filepath"
|
2014-12-31 16:39:33 +02:00
|
|
|
"reflect"
|
2015-10-06 19:39:42 +03:00
|
|
|
"strings"
|
2014-12-31 16:39:33 +02:00
|
|
|
"testing"
|
2015-05-24 04:42:10 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-03-02 14:57:15 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-12-31 16:39:33 +02:00
|
|
|
)
|
|
|
|
|
2017-01-24 12:49:20 +03:00
|
|
|
func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
|
2016-03-03 02:15:42 +02:00
|
|
|
d, err := ioutil.TempDir("", "geth-keystore-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if encrypted {
|
2018-09-19 19:08:38 +03:00
|
|
|
ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
|
2016-03-03 02:15:42 +02:00
|
|
|
} else {
|
|
|
|
ks = &keyStorePlain{d}
|
|
|
|
}
|
|
|
|
return d, ks
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func TestKeyStorePlain(t *testing.T) {
|
2017-01-24 12:49:20 +03:00
|
|
|
dir, ks := tmpKeyStoreIface(t, false)
|
2016-03-03 02:15:42 +02:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "" // not used but required by API
|
2016-03-03 02:15:42 +02:00
|
|
|
k1, account, err := storeNewKey(ks, rand.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
|
|
|
}
|
2017-02-08 16:53:02 +03:00
|
|
|
k2, err := ks.GetKey(k1.Address, account.URL.Path, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphrase(t *testing.T) {
|
2017-01-24 12:49:20 +03:00
|
|
|
dir, ks := tmpKeyStoreIface(t, true)
|
2016-03-03 02:15:42 +02:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "foo"
|
2016-03-03 02:15:42 +02:00
|
|
|
k1, account, err := storeNewKey(ks, rand.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
|
|
|
}
|
2017-02-08 16:53:02 +03:00
|
|
|
k2, err := ks.GetKey(k1.Address, account.URL.Path, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
|
2017-01-24 12:49:20 +03:00
|
|
|
dir, ks := tmpKeyStoreIface(t, true)
|
2016-03-03 02:15:42 +02:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2014-12-31 16:39:33 +02:00
|
|
|
pass := "foo"
|
2016-03-03 02:15:42 +02:00
|
|
|
k1, account, err := storeNewKey(ks, rand.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
|
|
|
}
|
2017-02-08 16:53:02 +03:00
|
|
|
if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
|
2019-08-12 12:00:38 +03:00
|
|
|
t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 00:55:13 +02:00
|
|
|
|
|
|
|
func TestImportPreSaleKey(t *testing.T) {
|
2017-01-24 12:49:20 +03:00
|
|
|
dir, ks := tmpKeyStoreIface(t, true)
|
2016-03-03 02:15:42 +02:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2015-01-21 00:55:13 +02:00
|
|
|
// 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\"}"
|
|
|
|
pass := "foo"
|
2016-03-03 02:15:42 +02:00
|
|
|
account, _, err := importPreSaleKey(ks, []byte(fileContent), pass)
|
2015-01-21 00:55:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") {
|
|
|
|
t.Errorf("imported account has wrong address %x", account.Address)
|
|
|
|
}
|
2017-02-08 16:53:02 +03:00
|
|
|
if !strings.HasPrefix(account.URL.Path, dir) {
|
2017-01-24 12:49:20 +03:00
|
|
|
t.Errorf("imported account file not in keystore directory: %q", account.URL)
|
2016-03-03 02:15:42 +02:00
|
|
|
}
|
2015-01-21 00:55:13 +02:00
|
|
|
}
|
2015-05-24 04:42:10 +03:00
|
|
|
|
|
|
|
// Test and utils for the key store tests in the Ethereum JSON tests;
|
2016-03-02 14:57:15 +02:00
|
|
|
// testdataKeyStoreTests/basic_tests.json
|
2015-05-24 04:42:10 +03:00
|
|
|
type KeyStoreTestV3 struct {
|
|
|
|
Json encryptedKeyJSONV3
|
|
|
|
Password string
|
|
|
|
Priv string
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyStoreTestV1 struct {
|
|
|
|
Json encryptedKeyJSONV1
|
|
|
|
Password string
|
|
|
|
Priv string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_1(t *testing.T) {
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2016-03-02 14:57:15 +02:00
|
|
|
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:49:14 +03:00
|
|
|
var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
|
|
|
|
|
|
|
|
func skipIfSubmoduleMissing(t *testing.T) {
|
|
|
|
if !common.FileExist(testsSubmodule) {
|
|
|
|
t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 04:42:10 +03:00
|
|
|
func TestV3_PBKDF2_2(t *testing.T) {
|
2017-07-11 14:49:14 +03:00
|
|
|
skipIfSubmoduleMissing(t)
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2017-07-11 14:49:14 +03:00
|
|
|
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["test1"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_3(t *testing.T) {
|
2017-07-11 14:49:14 +03:00
|
|
|
skipIfSubmoduleMissing(t)
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2017-07-11 14:49:14 +03:00
|
|
|
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_PBKDF2_4(t *testing.T) {
|
2017-07-11 14:49:14 +03:00
|
|
|
skipIfSubmoduleMissing(t)
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2017-07-11 14:49:14 +03:00
|
|
|
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["evilnonce"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_Scrypt_1(t *testing.T) {
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2016-03-02 14:57:15 +02:00
|
|
|
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_Scrypt_2(t *testing.T) {
|
2017-07-11 14:49:14 +03:00
|
|
|
skipIfSubmoduleMissing(t)
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2017-07-11 14:49:14 +03:00
|
|
|
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV3(tests["test2"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1_1(t *testing.T) {
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2016-03-02 14:57:15 +02:00
|
|
|
tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t)
|
2015-05-24 04:42:10 +03:00
|
|
|
testDecryptV1(tests["test1"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1_2(t *testing.T) {
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
2018-09-19 19:08:38 +03:00
|
|
|
ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true}
|
2015-05-24 04:42:10 +03:00
|
|
|
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
|
2016-03-03 02:15:42 +02:00
|
|
|
file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
|
|
|
|
k, err := ks.GetKey(addr, file, "g")
|
2015-05-24 04:42:10 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-02 14:57:15 +02:00
|
|
|
privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey))
|
2015-05-24 04:42:10 +03:00
|
|
|
expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
|
|
|
|
if privHex != expectedHex {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
|
|
|
|
privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privHex := hex.EncodeToString(privBytes)
|
|
|
|
if test.Priv != privHex {
|
|
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
|
|
|
|
privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privHex := hex.EncodeToString(privBytes)
|
|
|
|
if test.Priv != privHex {
|
|
|
|
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
|
|
|
|
tests := make(map[string]KeyStoreTestV3)
|
|
|
|
err := common.LoadJSON(file, &tests)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return tests
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
|
|
|
|
tests := make(map[string]KeyStoreTestV1)
|
|
|
|
err := common.LoadJSON(file, &tests)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return tests
|
|
|
|
}
|
2015-10-06 19:39:42 +03:00
|
|
|
|
|
|
|
func TestKeyForDirectICAP(t *testing.T) {
|
2016-03-03 02:15:42 +02:00
|
|
|
t.Parallel()
|
|
|
|
key := NewKeyForDirectICAP(rand.Reader)
|
2015-10-06 19:39:42 +03:00
|
|
|
if !strings.HasPrefix(key.Address.Hex(), "0x00") {
|
|
|
|
t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 22:42:29 +03:00
|
|
|
|
|
|
|
func TestV3_31_Byte_Key(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
|
|
|
|
testDecryptV3(tests["31_byte_key"], t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV3_30_Byte_Key(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
|
|
|
|
testDecryptV3(tests["30_byte_key"], t)
|
|
|
|
}
|