github.com/ava-labs/subnet-evm@v0.6.4/accounts/keystore/plain_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2014 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package keystore
    28  
    29  import (
    30  	"crypto/rand"
    31  	"encoding/hex"
    32  	"fmt"
    33  	"path/filepath"
    34  	"reflect"
    35  	"strings"
    36  	"testing"
    37  
    38  	"github.com/ethereum/go-ethereum/common"
    39  )
    40  
    41  func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
    42  	d := t.TempDir()
    43  	if encrypted {
    44  		ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true}
    45  	} else {
    46  		ks = &keyStorePlain{d}
    47  	}
    48  	return d, ks
    49  }
    50  
    51  func TestKeyStorePlain(t *testing.T) {
    52  	_, ks := tmpKeyStoreIface(t, false)
    53  
    54  	pass := "" // not used but required by API
    55  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    64  		t.Fatal(err)
    65  	}
    66  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    67  		t.Fatal(err)
    68  	}
    69  }
    70  
    71  func TestKeyStorePassphrase(t *testing.T) {
    72  	_, ks := tmpKeyStoreIface(t, true)
    73  
    74  	pass := "foo"
    75  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	k2, err := ks.GetKey(k1.Address, account.URL.Path, pass)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	if !reflect.DeepEqual(k1.Address, k2.Address) {
    84  		t.Fatal(err)
    85  	}
    86  	if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) {
    87  		t.Fatal(err)
    88  	}
    89  }
    90  
    91  func TestKeyStorePassphraseDecryptionFail(t *testing.T) {
    92  	_, ks := tmpKeyStoreIface(t, true)
    93  
    94  	pass := "foo"
    95  	k1, account, err := storeNewKey(ks, rand.Reader, pass)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt {
   100  		t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt)
   101  	}
   102  }
   103  
   104  func TestImportPreSaleKey(t *testing.T) {
   105  	dir, ks := tmpKeyStoreIface(t, true)
   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 testDecryptV3(test KeyStoreTestV3, t *testing.T) {
   187  	privBytes, _, err := decryptKeyV3(&test.Json, test.Password)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	privHex := hex.EncodeToString(privBytes)
   192  	if test.Priv != privHex {
   193  		t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex))
   194  	}
   195  }
   196  
   197  func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 {
   198  	tests := make(map[string]KeyStoreTestV3)
   199  	err := common.LoadJSON(file, &tests)
   200  	if err != nil {
   201  		t.Fatal(err)
   202  	}
   203  	return tests
   204  }
   205  
   206  func TestKeyForDirectICAP(t *testing.T) {
   207  	t.Parallel()
   208  	key := NewKeyForDirectICAP(rand.Reader)
   209  	if !strings.HasPrefix(key.Address.Hex(), "0x00") {
   210  		t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex())
   211  	}
   212  }
   213  
   214  func TestV3_31_Byte_Key(t *testing.T) {
   215  	t.Parallel()
   216  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   217  	testDecryptV3(tests["31_byte_key"], t)
   218  }
   219  
   220  func TestV3_30_Byte_Key(t *testing.T) {
   221  	t.Parallel()
   222  	tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t)
   223  	testDecryptV3(tests["30_byte_key"], t)
   224  }