github.com/Gessiux/neatchain@v1.3.1/chain/accounts/keystore/keystore_passphrase_test.go (about)

     1  // Copyright 2016 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  	"fmt"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	"github.com/Gessiux/neatchain/utilities/crypto"
    25  
    26  	"github.com/Gessiux/neatchain/utilities/common"
    27  )
    28  
    29  const (
    30  	veryLightScryptN = 2
    31  	veryLightScryptP = 1
    32  )
    33  
    34  // Tests that a json key file can be decrypted and encrypted in multiple rounds.
    35  func TestKeyEncryptDecrypt(t *testing.T) {
    36  	keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	password := ""
    41  	address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8")
    42  
    43  	// Do a few rounds of decryption and encryption
    44  	for i := 0; i < 3; i++ {
    45  		// Try a bad password first
    46  		if _, err := DecryptKey(keyjson, password+"bad"); err == nil {
    47  			t.Errorf("test %d: json key decrypted with bad password", i)
    48  		}
    49  		// Decrypt with the correct password
    50  		key, err := DecryptKey(keyjson, password)
    51  		if err != nil {
    52  			t.Fatalf("test %d: json key failed to decrypt: %v", i, err)
    53  		}
    54  		if key.Address != address {
    55  			t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address)
    56  		}
    57  		// Recrypt with a new password and start over
    58  		password += "new data appended"
    59  		if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil {
    60  			t.Errorf("test %d: failed to recrypt key %v", i, err)
    61  		}
    62  	}
    63  }
    64  
    65  func TestEncryptKey(t *testing.T) {
    66  	//privateKeyHex := "182e4cc598610e2e8fe3c23a9b3145c8500cb8bc1ec44d0faf4e7b3452ac5ca0"
    67  	//key, err := crypto.HexToECDSA(privateKeyHex)
    68  	//if err != nil {
    69  	//	t.Fatalf("failed to decode privatekey")
    70  	//}
    71  	//password := "neatchain"
    72  	//keyjson, err := EncryptKey(key, password, veryLightScryptN, veryLightScryptP)
    73  	//if err != nil {
    74  	//	t.Errorf("failed to encrypt key %v", err)
    75  	//}
    76  
    77  }
    78  
    79  func TestDecryptKey(t *testing.T) {
    80  	keyjson, err := ioutil.ReadFile("testdata/keystore/UTC--2019-10-17T06-41-37.816846000Z--3K7YBykphE6N8jFGVbNAWfvor94i9nigU8")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	password := "neatchain"
    86  	address := common.StringToAddress("3K7YBykphE6N8jFGVbNAWfvor94i9nigU8")
    87  	key, err := DecryptKey(keyjson, password)
    88  	fmt.Printf("private key %x\n", crypto.FromECDSA(key.PrivateKey))
    89  	if err != nil {
    90  		t.Fatalf("json key failed to decrypt err=%v\n", err)
    91  	}
    92  
    93  	fmt.Printf("address are key.Address %x, address %x\n", key.Address, address)
    94  	if key.Address != address {
    95  		t.Errorf("address mismatch: have %v, want %v\n", key.Address.String(), address.String())
    96  	}
    97  }