github.com/ethereum/go-ethereum@v1.14.3/accounts/keystore/plain_test.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package keystore
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"path/filepath"
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/crypto"
    30  )
    31  
    32  func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
    33  	d := t.TempDir()
    34  	if encrypted {
    35  		ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
    36  	} else {
    37  		ks = &keyStorePlain{d}
    38  	}
    39  	return d, ks
    40  }
    41  
    42  func TestKeyStorePlain(t *testing.T) {
    43  	t.Parallel()
    44  	_, ks := tmpKeyStoreIface(t, false)
    45  
    46  	pass := "" // not used but required by API
    47  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    56  		t.Fatal(err)
    57  	}
    58  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    59  		t.Fatal(err)
    60  	}
    61  }
    62  
    63  func TestKeyStorePassphrase(t *testing.T) {
    64  	t.Parallel()
    65  	_, ks := tmpKeyStoreIface(t, true)
    66  
    67  	pass := "foo"
    68  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    77  		t.Fatal(err)
    78  	}
    79  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    80  		t.Fatal(err)
    81  	}
    82  }
    83  
    84  func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    85  	t.Parallel()
    86  	_, ks := tmpKeyStoreIface(t, true)
    87  
    88  	pass := "foo"
    89  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
    94  		t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
    95  	}
    96  }
    97  
    98  func TestImportPreSaleKey(t *testing.T) {
    99  	t.Parallel()
   100  	dir, ks := tmpKeyStoreIface(t, true)
   101  
   102  	// file content of a presale key file generated with:
   103  	// python pyethsaletool.py genwallet
   104  	// with password "foo"
   105  	fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}"
   106  	pass := "foo"
   107  	account, _, err := importPreSaleKey(ks, []byte(fileContent), pass)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") {
   112  		t.Errorf("imported account has wrong address %x", account.Address)
   113  	}
   114  	if !strings.HasPrefix(account.URL.Path, dir) {
   115  		t.Errorf("imported account file not in keystore directory: %q", account.URL)
   116  	}
   117  }
   118  
   119  // Test and utils for the key store tests in the Ethereum JSON tests;
   120  // testdataKeyStoreTests/basic_tests.json
   121  type KeyStoreTestV3 struct {
   122  	Json     encryptedKeyJSONV3
   123  	Password string
   124  	Priv     string
   125  }
   126  
   127  type KeyStoreTestV1 struct {
   128  	Json     encryptedKeyJSONV1
   129  	Password string
   130  	Priv     string
   131  }
   132  
   133  func TestV3_PBKDF2_1(t *testing.T) {
   134  	t.Parallel()
   135  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   136  	testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t)
   137  }
   138  
   139  var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests")
   140  
   141  func skipIfSubmoduleMissing(t *testing.T) {
   142  	if !common.FileExist(testsSubmodule) {
   143  		t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule)
   144  	}
   145  }
   146  
   147  func TestV3_PBKDF2_2(t *testing.T) {
   148  	skipIfSubmoduleMissing(t)
   149  	t.Parallel()
   150  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   151  	testDecryptV3(tests["test1"], t)
   152  }
   153  
   154  func TestV3_PBKDF2_3(t *testing.T) {
   155  	skipIfSubmoduleMissing(t)
   156  	t.Parallel()
   157  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   158  	testDecryptV3(tests["python_generated_test_with_odd_iv"], t)
   159  }
   160  
   161  func TestV3_PBKDF2_4(t *testing.T) {
   162  	skipIfSubmoduleMissing(t)
   163  	t.Parallel()
   164  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   165  	testDecryptV3(tests["evilnonce"], t)
   166  }
   167  
   168  func TestV3_Scrypt_1(t *testing.T) {
   169  	t.Parallel()
   170  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   171  	testDecryptV3(tests["wikipage_test_vector_scrypt"], t)
   172  }
   173  
   174  func TestV3_Scrypt_2(t *testing.T) {
   175  	skipIfSubmoduleMissing(t)
   176  	t.Parallel()
   177  	tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t)
   178  	testDecryptV3(tests["test2"], t)
   179  }
   180  
   181  func TestV1_1(t *testing.T) {
   182  	t.Parallel()
   183  	tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t)
   184  	testDecryptV1(tests["test1"], t)
   185  }
   186  
   187  func TestV1_2(t *testing.T) {
   188  	t.Parallel()
   189  	ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true}
   190  	addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
   191  	file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
   192  	k, err := ks.GetKey(addr, file, "g")
   193  	if err != nil {
   194  		t.Fatal(err)
   195  	}
   196  	privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey))
   197  	expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
   198  	if privHex != expectedHex {
   199  		t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
   200  	}
   201  }
   202  
   203  func testDecryptV3(test KeyStoreTestV3, t *testing.T) {
   204  	privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
   205  	if err != nil {
   206  		t.Fatal(err)
   207  	}
   208  	privHex := hex.EncodeToString(privBytes)
   209  	if test.Priv != privHex {
   210  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   211  	}
   212  }
   213  
   214  func testDecryptV1(test KeyStoreTestV1, t *testing.T) {
   215  	privBytes, _, err := decryptKeyV1(&test.Json, test.Password)
   216  	if err != nil {
   217  		t.Fatal(err)
   218  	}
   219  	privHex := hex.EncodeToString(privBytes)
   220  	if test.Priv != privHex {
   221  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   222  	}
   223  }
   224  
   225  func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
   226  	tests := make(map[string]KeyStoreTestV3)
   227  	err := common.LoadJSON(file, &tests)
   228  	if err != nil {
   229  		t.Fatal(err)
   230  	}
   231  	return tests
   232  }
   233  
   234  func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 {
   235  	tests := make(map[string]KeyStoreTestV1)
   236  	err := common.LoadJSON(file, &tests)
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  	return tests
   241  }
   242  
   243  func TestKeyForDirectICAP(t *testing.T) {
   244  	t.Parallel()
   245  	key := NewKeyForDirectICAP(rand.Reader)
   246  	if !strings.HasPrefix(key.Address.Hex(), "0x00") {
   247  		t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
   248  	}
   249  }
   250  
   251  func TestV3_31_Byte_Key(t *testing.T) {
   252  	t.Parallel()
   253  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   254  	testDecryptV3(tests["31_byte_key"], t)
   255  }
   256  
   257  func TestV3_30_Byte_Key(t *testing.T) {
   258  	t.Parallel()
   259  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   260  	testDecryptV3(tests["30_byte_key"], t)
   261  }