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