github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/crypto/ethkeystore/keystore_test.go (about)

     1  package ethkeystore
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
     7  	"github.com/fibonacci-chain/fbc/app/crypto/hd"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/keys"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/tests"
    10  	tmamino "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/encoding/amino"
    11  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/multisig"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestGetEthKey(t *testing.T) { testGetEthKey(t) }
    16  func testGetEthKey(t *testing.T) {
    17  	tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
    18  	tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName)
    19  	multisig.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
    20  
    21  	dir, cleanup := tests.NewTestCaseDir(t)
    22  	defer cleanup()
    23  	kb, err := keys.NewKeyring("keybasename", "test", dir, nil, hd.EthSecp256k1Options()...)
    24  	require.NoError(t, err)
    25  
    26  	tests := []struct {
    27  		name    string
    28  		passwd  string
    29  		keyType keys.SigningAlgo
    30  	}{
    31  		{
    32  			name:    "test-numbers-passwd",
    33  			passwd:  "12345678",
    34  			keyType: hd.EthSecp256k1,
    35  		},
    36  		{
    37  			name:    "test-characters-passwd",
    38  			passwd:  "abcdefgh",
    39  			keyType: hd.EthSecp256k1,
    40  		},
    41  	}
    42  	//generate test key
    43  	for _, tt := range tests {
    44  		_, _, err := kb.CreateMnemonic(tt.name, keys.English, tt.passwd, tt.keyType, "")
    45  		require.NoError(t, err)
    46  
    47  		// Exports private key from keybase using password
    48  		privKey, err := kb.ExportPrivateKeyObject(tt.name, tt.passwd)
    49  		require.NoError(t, err)
    50  
    51  		// Converts tendermint  key to ethereum key
    52  		_, err = EncodeTmKeyToEthKey(privKey)
    53  		require.NoError(t, err)
    54  
    55  	}
    56  }