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