github.com/cosmos/cosmos-sdk@v0.50.1/crypto/keyring/signing_algorithms_test.go (about)

     1  package keyring
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/cosmos/cosmos-sdk/crypto/hd"
    11  )
    12  
    13  func TestNewSigningAlgoByString(t *testing.T) {
    14  	tests := []struct {
    15  		name         string
    16  		algoStr      string
    17  		isSupported  bool
    18  		expectedAlgo SignatureAlgo
    19  		expectedErr  error
    20  	}{
    21  		{
    22  			"supported algorithm",
    23  			"secp256k1",
    24  			true,
    25  			hd.Secp256k1,
    26  			nil,
    27  		},
    28  		{
    29  			"not supported",
    30  			"notsupportedalgo",
    31  			false,
    32  			nil,
    33  			ErrUnsupportedSigningAlgo,
    34  		},
    35  	}
    36  
    37  	list := SigningAlgoList{hd.Secp256k1}
    38  	for _, tt := range tests {
    39  		tt := tt
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			algorithm, err := NewSigningAlgoFromString(tt.algoStr, list)
    42  			if tt.isSupported {
    43  				require.Equal(t, hd.Secp256k1, algorithm)
    44  			} else {
    45  				require.ErrorIs(t, err, tt.expectedErr)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestDerive(t *testing.T) {
    52  	tests := []struct {
    53  		name            string
    54  		algo            SignatureAlgo
    55  		hdPath          string
    56  		mnemonic        string
    57  		bip39Passphrase string
    58  		derivedPriv     string
    59  	}{
    60  		{
    61  			name:            "secp256k1",
    62  			algo:            hd.Secp256k1,
    63  			hdPath:          "m/44'/118'/0'/0/0",
    64  			mnemonic:        "circle music snake select deal march this romance until often welcome rich staff trigger drip exit there reopen denial insect hockey just wealth process",
    65  			bip39Passphrase: "",
    66  			derivedPriv:     "1b5884fab5c22aeffef369f3454076cec534c75a4ee71add1245e2c8342994a2",
    67  		},
    68  		{
    69  			name:            "secp256k1 with bip39Passphrase",
    70  			algo:            hd.Secp256k1,
    71  			hdPath:          "m/44'/118'/0'/0/0",
    72  			mnemonic:        "circle music snake select deal march this romance until often welcome rich staff trigger drip exit there reopen denial insect hockey just wealth process",
    73  			bip39Passphrase: "test",
    74  			derivedPriv:     "d5f925b9472b3793e839eae7722e5cef6766a91fad55cc42abea5e457ed5f648",
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			derivedPriv, err := tt.algo.Derive()(tt.mnemonic, tt.bip39Passphrase, tt.hdPath)
    80  			require.NoError(t, err)
    81  
    82  			decodedPriv, err := hex.DecodeString(tt.derivedPriv)
    83  			require.NoError(t, err)
    84  			require.Equal(t, derivedPriv, decodedPriv)
    85  		})
    86  	}
    87  }
    88  
    89  func TestAltSigningAlgoList_Contains(t *testing.T) {
    90  	list := SigningAlgoList{hd.Secp256k1}
    91  
    92  	require.True(t, list.Contains(hd.Secp256k1))
    93  	require.False(t, list.Contains(notSupportedAlgo{}))
    94  }
    95  
    96  func TestAltSigningAlgoList_String(t *testing.T) {
    97  	list := SigningAlgoList{hd.Secp256k1, notSupportedAlgo{}}
    98  	require.Equal(t, fmt.Sprintf("%s,notSupported", hd.Secp256k1Type), list.String())
    99  }
   100  
   101  type notSupportedAlgo struct{}
   102  
   103  func (n notSupportedAlgo) Name() hd.PubKeyType {
   104  	return "notSupported"
   105  }
   106  
   107  func (n notSupportedAlgo) Derive() hd.DeriveFn {
   108  	return hd.Secp256k1.Derive()
   109  }
   110  
   111  func (n notSupportedAlgo) Generate() hd.GenerateFn {
   112  	return hd.Secp256k1.Generate()
   113  }