github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/keystore/keystore_plain_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2014 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package keystore 26 27 import ( 28 "crypto/rand" 29 "encoding/hex" 30 "fmt" 31 "io/ioutil" 32 "os" 33 "path/filepath" 34 "reflect" 35 "strings" 36 "testing" 37 38 "github.com/ethereum/go-ethereum/common" 39 "github.com/ethereum/go-ethereum/crypto" 40 ) 41 42 func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { 43 d, err := ioutil.TempDir("", "geth-keystore-test") 44 if err != nil { 45 t.Fatal(err) 46 } 47 if encrypted { 48 ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP} 49 } else { 50 ks = &keyStorePlain{d} 51 } 52 return d, ks 53 } 54 55 func TestKeyStorePlain(t *testing.T) { 56 dir, ks := tmpKeyStoreIface(t, false) 57 defer os.RemoveAll(dir) 58 59 pass := "" //未使用,但API要求 60 k1, account, err := storeNewKey(ks, rand.Reader, pass) 61 if err != nil { 62 t.Fatal(err) 63 } 64 k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) 65 if err != nil { 66 t.Fatal(err) 67 } 68 if !reflect.DeepEqual(k1.Address, k2.Address) { 69 t.Fatal(err) 70 } 71 if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) { 72 t.Fatal(err) 73 } 74 } 75 76 func TestKeyStorePassphrase(t *testing.T) { 77 dir, ks := tmpKeyStoreIface(t, true) 78 defer os.RemoveAll(dir) 79 80 pass := "foo" 81 k1, account, err := storeNewKey(ks, rand.Reader, pass) 82 if err != nil { 83 t.Fatal(err) 84 } 85 k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if !reflect.DeepEqual(k1.Address, k2.Address) { 90 t.Fatal(err) 91 } 92 if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) { 93 t.Fatal(err) 94 } 95 } 96 97 func TestKeyStorePassphraseDecryptionFail(t *testing.T) { 98 dir, ks := tmpKeyStoreIface(t, true) 99 defer os.RemoveAll(dir) 100 101 pass := "foo" 102 k1, account, err := storeNewKey(ks, rand.Reader, pass) 103 if err != nil { 104 t.Fatal(err) 105 } 106 if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt { 107 t.Fatalf("wrong error for invalid passphrase\ngot %q\nwant %q", err, ErrDecrypt) 108 } 109 } 110 111 func TestImportPreSaleKey(t *testing.T) { 112 dir, ks := tmpKeyStoreIface(t, true) 113 defer os.RemoveAll(dir) 114 115 //生成的预售密钥文件的文件内容: 116 //python pyethsaletool.py genwallet 117 //密码为“foo” 118 fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}" 119 pass := "foo" 120 account, _, err := importPreSaleKey(ks, []byte(fileContent), pass) 121 if err != nil { 122 t.Fatal(err) 123 } 124 if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") { 125 t.Errorf("imported account has wrong address %x", account.Address) 126 } 127 if !strings.HasPrefix(account.URL.Path, dir) { 128 t.Errorf("imported account file not in keystore directory: %q", account.URL) 129 } 130 } 131 132 //在以太坊JSON测试中测试和使用密钥存储测试; 133 //测试数据密钥存储测试/basic_tests.json 134 type KeyStoreTestV3 struct { 135 Json encryptedKeyJSONV3 136 Password string 137 Priv string 138 } 139 140 type KeyStoreTestV1 struct { 141 Json encryptedKeyJSONV1 142 Password string 143 Priv string 144 } 145 146 func TestV3_PBKDF2_1(t *testing.T) { 147 t.Parallel() 148 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 149 testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t) 150 } 151 152 var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests") 153 154 func skipIfSubmoduleMissing(t *testing.T) { 155 if !common.FileExist(testsSubmodule) { 156 t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule) 157 } 158 } 159 160 func TestV3_PBKDF2_2(t *testing.T) { 161 skipIfSubmoduleMissing(t) 162 t.Parallel() 163 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 164 testDecryptV3(tests["test1"], t) 165 } 166 167 func TestV3_PBKDF2_3(t *testing.T) { 168 skipIfSubmoduleMissing(t) 169 t.Parallel() 170 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 171 testDecryptV3(tests["python_generated_test_with_odd_iv"], t) 172 } 173 174 func TestV3_PBKDF2_4(t *testing.T) { 175 skipIfSubmoduleMissing(t) 176 t.Parallel() 177 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 178 testDecryptV3(tests["evilnonce"], t) 179 } 180 181 func TestV3_Scrypt_1(t *testing.T) { 182 t.Parallel() 183 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 184 testDecryptV3(tests["wikipage_test_vector_scrypt"], t) 185 } 186 187 func TestV3_Scrypt_2(t *testing.T) { 188 skipIfSubmoduleMissing(t) 189 t.Parallel() 190 tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) 191 testDecryptV3(tests["test2"], t) 192 } 193 194 func TestV1_1(t *testing.T) { 195 t.Parallel() 196 tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t) 197 testDecryptV1(tests["test1"], t) 198 } 199 200 func TestV1_2(t *testing.T) { 201 t.Parallel() 202 ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP} 203 addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e") 204 file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e" 205 k, err := ks.GetKey(addr, file, "g") 206 if err != nil { 207 t.Fatal(err) 208 } 209 privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)) 210 expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d" 211 if privHex != expectedHex { 212 t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex)) 213 } 214 } 215 216 func testDecryptV3(test KeyStoreTestV3, t *testing.T) { 217 privBytes, _, err := decryptKeyV3(&test.Json, test.Password) 218 if err != nil { 219 t.Fatal(err) 220 } 221 privHex := hex.EncodeToString(privBytes) 222 if test.Priv != privHex { 223 t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) 224 } 225 } 226 227 func testDecryptV1(test KeyStoreTestV1, t *testing.T) { 228 privBytes, _, err := decryptKeyV1(&test.Json, test.Password) 229 if err != nil { 230 t.Fatal(err) 231 } 232 privHex := hex.EncodeToString(privBytes) 233 if test.Priv != privHex { 234 t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) 235 } 236 } 237 238 func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 { 239 tests := make(map[string]KeyStoreTestV3) 240 err := common.LoadJSON(file, &tests) 241 if err != nil { 242 t.Fatal(err) 243 } 244 return tests 245 } 246 247 func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 { 248 tests := make(map[string]KeyStoreTestV1) 249 err := common.LoadJSON(file, &tests) 250 if err != nil { 251 t.Fatal(err) 252 } 253 return tests 254 } 255 256 func TestKeyForDirectICAP(t *testing.T) { 257 t.Parallel() 258 key := NewKeyForDirectICAP(rand.Reader) 259 if !strings.HasPrefix(key.Address.Hex(), "0x00") { 260 t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex()) 261 } 262 } 263 264 func TestV3_31_Byte_Key(t *testing.T) { 265 t.Parallel() 266 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 267 testDecryptV3(tests["31_byte_key"], t) 268 } 269 270 func TestV3_30_Byte_Key(t *testing.T) { 271 t.Parallel() 272 tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) 273 testDecryptV3(tests["30_byte_key"], t) 274 }