github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/keystore/keystore_passphrase_test.go (about) 1 package keystore 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 8 "github.com/neatlab/neatio/utilities/crypto" 9 10 "github.com/neatlab/neatio/utilities/common" 11 ) 12 13 const ( 14 veryLightScryptN = 2 15 veryLightScryptP = 1 16 ) 17 18 func TestKeyEncryptDecrypt(t *testing.T) { 19 keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json") 20 if err != nil { 21 t.Fatal(err) 22 } 23 password := "" 24 address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8") 25 26 for i := 0; i < 3; i++ { 27 28 if _, err := DecryptKey(keyjson, password+"bad"); err == nil { 29 t.Errorf("test %d: json key decrypted with bad password", i) 30 } 31 32 key, err := DecryptKey(keyjson, password) 33 if err != nil { 34 t.Fatalf("test %d: json key failed to decrypt: %v", i, err) 35 } 36 if key.Address != address { 37 t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address) 38 } 39 40 password += "new data appended" 41 if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil { 42 t.Errorf("test %d: failed to recrypt key %v", i, err) 43 } 44 } 45 } 46 47 func TestEncryptKey(t *testing.T) { 48 49 } 50 51 func TestDecryptKey(t *testing.T) { 52 keyjson, err := ioutil.ReadFile("testdata/keystore/UTC--2019-10-17T06-41-37.816846000Z--3K7YBykphE6N8jFGVbNAWfvor94i9nigU8") 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 password := "neatio" 58 address := common.HexToAddress("0e0ffd4c684b325be82f120a7938c7d938ff3dca") 59 key, err := DecryptKey(keyjson, password) 60 fmt.Printf("private key %x\n", crypto.FromECDSA(key.PrivateKey)) 61 if err != nil { 62 t.Fatalf("json key failed to decrypt err=%v\n", err) 63 } 64 65 fmt.Printf("address are key.Address %x, address %x\n", key.Address, address) 66 if key.Address != address { 67 t.Errorf("address mismatch: have %v, want %v\n", key.Address.String(), address.String()) 68 } 69 }