github.com/klaytn/klaytn@v1.10.2/api/api_private_account_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/klaytn/klaytn/accounts"
    26  	"github.com/klaytn/klaytn/accounts/keystore"
    27  	"github.com/klaytn/klaytn/common"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  // TestPrivateAccountAPI_ImportRawKey tests ImportRawKey() and ReplaceRawKey().
    32  func TestPrivateAccountAPI_ImportRawKey(t *testing.T) {
    33  	scryptN := keystore.StandardScryptN
    34  	scryptP := keystore.StandardScryptP
    35  
    36  	// To get JSON files use below.
    37  	// keydir := filepath.Join(".", "keystore")
    38  	keydir, err := ioutil.TempDir("", "klay-test")
    39  	require.NoError(t, err)
    40  	defer os.RemoveAll(keydir)
    41  
    42  	// Assemble the account manager and supported backends
    43  	backends := []accounts.Backend{
    44  		keystore.NewKeyStore(keydir, scryptN, scryptP),
    45  	}
    46  
    47  	api := PrivateAccountAPI{
    48  		am:        accounts.NewManager(backends...),
    49  		nonceLock: new(AddrLocker),
    50  		b:         nil,
    51  	}
    52  
    53  	// 1. Import private key only.
    54  	{
    55  		addr, err := api.ImportRawKey("aebb680a5e596c1d1a01bac78a3985b62c685c5e995d780c176138cb2679ba3e", "1234")
    56  		require.NoError(t, err)
    57  
    58  		require.Equal(t, common.HexToAddress("0x819104a190255e0cedbdd9d5f59a557633d79db1"), addr)
    59  	}
    60  
    61  	// 2. Import Klaytn Wallet Key. Since the same address is already registered, it should fail.
    62  	{
    63  		_, err := api.ImportRawKey("f8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b0x000x819104a190255e0cedbdd9d5f59a557633d79db1", "1234")
    64  		require.Equal(t, fmt.Errorf("account already exists"), err)
    65  	}
    66  
    67  	// 3. Replace Klaytn Wallet key. It should work.
    68  	{
    69  		addr, err := api.ReplaceRawKey("f8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b0x000x819104a190255e0cedbdd9d5f59a557633d79db1", "1234", "1234")
    70  		require.NoError(t, err)
    71  
    72  		require.Equal(t, common.HexToAddress("0x819104a190255e0cedbdd9d5f59a557633d79db1"), addr)
    73  	}
    74  
    75  	// 4. Allowable Wallet key type is 0x00 only.
    76  	{
    77  		_, err := api.ImportRawKey("f8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b0x010x819104a190255e0cedbdd9d5f59a557633d79db1", "1234")
    78  		require.Equal(t, fmt.Errorf("Klaytn wallet key type must be 00."), err)
    79  	}
    80  
    81  	// 5. Should return an error if wrong length.
    82  	{
    83  		_, err := api.ImportRawKey("1ea7b7bc7f525cc936ec65e0e93f146bd6fad4b3158067ad64560defd9bba0b0x010x3b3d49ebac925797b2471c7b01108ba16bb36950", "1234")
    84  		require.Equal(t, fmt.Errorf("invalid hex string"), err)
    85  	}
    86  
    87  	// 6. Should return an error if wrong length.
    88  	{
    89  		_, err := api.ImportRawKey("1ea7b7bc7f525cc936ec65e0e93f146bd6fad4b3158067ad64560defd9bba0b", "1234")
    90  		require.Equal(t, fmt.Errorf("invalid hex string"), err)
    91  	}
    92  
    93  	// 7. Import Klaytn Wallet Key.
    94  	{
    95  		addr, err := api.ImportRawKey("f8cc7c3813ad23817466b1802ee805ee417001fcce9376ab8728c92dd8ea0a6b0x000x819104a190255e0cedbdd9d5f59a557633d79db2", "1234")
    96  		require.NoError(t, err)
    97  
    98  		require.Equal(t, common.HexToAddress("0x819104a190255e0cedbdd9d5f59a557633d79db2"), addr)
    99  	}
   100  }