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