github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/account_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package account
     7  
     8  import (
     9  	"crypto/ecdsa"
    10  	"math/rand"
    11  	"strconv"
    12  	"testing"
    13  
    14  	"github.com/ethereum/go-ethereum/accounts/keystore"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/iotexproject/go-pkgs/crypto"
    18  	"github.com/iotexproject/go-pkgs/hash"
    19  	"github.com/iotexproject/iotex-address/address"
    20  
    21  	"github.com/iotexproject/iotex-core/ioctl/config"
    22  )
    23  
    24  func TestAccount(t *testing.T) {
    25  	r := require.New(t)
    26  
    27  	testWallet := t.TempDir()
    28  	config.ReadConfig.Wallet = testWallet
    29  
    30  	ks := keystore.NewKeyStore(config.ReadConfig.Wallet, keystore.StandardScryptN, keystore.StandardScryptP)
    31  	r.NotNil(ks)
    32  
    33  	// create accounts
    34  	nonce := strconv.FormatInt(rand.Int63(), 10)
    35  	passwd := "3dj,<>@@SF{}rj0ZF#" + nonce
    36  
    37  	account, err := ks.NewAccount(passwd)
    38  	r.NoError(err)
    39  	addr, err := address.FromBytes(account.Address.Bytes())
    40  	r.NoError(err)
    41  	r.True(IsSignerExist(addr.String()))
    42  
    43  	CryptoSm2 = true
    44  	account2, err := crypto.GenerateKeySm2()
    45  	r.NoError(err)
    46  	r.NotNil(account2)
    47  	addr2 := account2.PublicKey().Address()
    48  	r.NotNil(addr2)
    49  	r.False(IsSignerExist(addr2.String()))
    50  	_, err = keyStoreAccountToPrivateKey(addr2.String(), passwd)
    51  	r.Contains(err.Error(), "does not match all local keys")
    52  	filePath := sm2KeyPath(addr2)
    53  	addrString, err := storeKey(account2.HexString(), config.ReadConfig.Wallet, passwd)
    54  	r.NoError(err)
    55  	r.Equal(addr2.String(), addrString)
    56  	r.True(IsSignerExist(addr2.String()))
    57  	path, err := findSm2PemFile(addr2)
    58  	r.NoError(err)
    59  	r.Equal(filePath, path)
    60  
    61  	accounts, err := listSm2Account()
    62  	r.NoError(err)
    63  	r.Equal(1, len(accounts))
    64  	r.Equal(addr2.String(), accounts[0])
    65  
    66  	// test keystore conversion and signing
    67  	CryptoSm2 = false
    68  	prvKey, err := keyStoreAccountToPrivateKey(addr.String(), passwd)
    69  	r.NoError(err)
    70  	msg := hash.Hash256b([]byte(nonce))
    71  	sig, err := prvKey.Sign(msg[:])
    72  	r.NoError(err)
    73  	r.True(prvKey.PublicKey().Verify(msg[:], sig))
    74  
    75  	CryptoSm2 = true
    76  	prvKey2, err := keyStoreAccountToPrivateKey(addr2.String(), passwd)
    77  	r.NoError(err)
    78  	msg2 := hash.Hash256b([]byte(nonce))
    79  	sig2, err := prvKey2.Sign(msg2[:])
    80  	r.NoError(err)
    81  	r.True(prvKey2.PublicKey().Verify(msg2[:], sig2))
    82  
    83  	// test import existing key
    84  	sk, err := crypto.GenerateKey()
    85  	r.NoError(err)
    86  	p256k1, ok := sk.EcdsaPrivateKey().(*ecdsa.PrivateKey)
    87  	r.Equal(true, ok)
    88  	account, err = ks.ImportECDSA(p256k1, passwd)
    89  	r.NoError(err)
    90  	r.Equal(sk.PublicKey().Hash(), account.Address.Bytes())
    91  }