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