From cc4ddd4b51317ff7a4faa251b97e72855456c8e6 Mon Sep 17 00:00:00 2001 From: Calico Nino <54007257+CalicoNino@users.noreply.github.com> Date: Wed, 19 Jun 2024 20:38:42 -0400 Subject: [PATCH] fix(gosdk): fix keyring import functions (#1932) * fix: go-sdk add signer fix for kring * refactor: removing unused go-sdk keyring funcs * chore: update changelog --- CHANGELOG.md | 1 + app/evmante_sigverify_test.go | 6 ++-- app/evmante_validate_basic_test.go | 1 - gosdk/keys.go | 45 ++++++------------------------ gosdk/keys_test.go | 11 +++++--- 5 files changed, 21 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8406c721c..6a9986ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1906](https://github.com/NibiruChain/nibiru/pull/1906) - feat(wasm): increase contract size limit to 3MB - [#1908](https://github.com/NibiruChain/nibiru/pull/1908) - chore: make pebbledb the default db backend - [#1931](https://github.com/NibiruChain/nibiru/pull/1931) - feat(ibc): add `wasm` route to IBC router +- [#1932](https://github.com/NibiruChain/nibiru/pull/1932) - fix(gosdk): fix keyring import functions #### Nibiru EVM diff --git a/app/evmante_sigverify_test.go b/app/evmante_sigverify_test.go index 2b70e5eac..be48262a9 100644 --- a/app/evmante_sigverify_test.go +++ b/app/evmante_sigverify_test.go @@ -11,8 +11,10 @@ import ( tf "github.com/NibiruChain/nibiru/x/tokenfactory/types" ) -var InvalidChainID = big.NewInt(987654321) -var RandomAddress = evmtest.NewEthAccInfo().EthAddr.Hex() +var ( + InvalidChainID = big.NewInt(987654321) + RandomAddress = evmtest.NewEthAccInfo().EthAddr.Hex() +) func (s *TestSuite) TestEthSigVerificationDecorator() { testCases := []struct { diff --git a/app/evmante_validate_basic_test.go b/app/evmante_validate_basic_test.go index 934a69580..f3b74d4fc 100644 --- a/app/evmante_validate_basic_test.go +++ b/app/evmante_validate_basic_test.go @@ -248,7 +248,6 @@ func buildEthMsg( gasLimit uint64, from string, to *common.Address, - ) *evm.MsgEthereumTx { ethContractCreationTxParams := &evm.EvmTxArgs{ ChainID: chainID, diff --git a/gosdk/keys.go b/gosdk/keys.go index 7d5daa167..b61c2f6a4 100644 --- a/gosdk/keys.go +++ b/gosdk/keys.go @@ -1,10 +1,9 @@ package gosdk import ( - "github.com/cosmos/cosmos-sdk/crypto" + "fmt" + "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,7 +20,8 @@ func NewKeyring() keyring.Keyring { return keyring.NewInMemory(EncodingConfig().Codec) } -// TODO: Is this needed? +// TODO: Is it necessary to add support for interacting with local file system +// keyring? // import ( // "bufio" // "os" @@ -40,44 +40,17 @@ func NewKeyring() keyring.Keyring { // ) // } -func PrivKeyFromMnemonic( +func AddSignerToKeyringSecp256k1( kring keyring.Keyring, mnemonic string, keyName string, -) (cryptotypes.PrivKey, sdk.AccAddress, error) { +) (sdk.AccAddress, error) { algo := hd.Secp256k1 overwrite := true - addr, secret, err := sdktestutil.GenerateSaveCoinKey( + addr, secretMnem, err := sdktestutil.GenerateSaveCoinKey( kring, keyName, mnemonic, overwrite, algo, ) if err != nil { - return &secp256k1.PrivKey{}, sdk.AccAddress{}, err - } - privKey := secp256k1.GenPrivKeyFromSecret([]byte(secret)) - return privKey, addr, err -} - -func CreateSigner( - mnemonic string, - kring keyring.Keyring, - keyName string, -) (kringRecord *keyring.Record, privKey cryptotypes.PrivKey, err error) { - privKey, _, err = PrivKeyFromMnemonic(kring, mnemonic, keyName) - if err != nil { - return kringRecord, privKey, err + return nil, fmt.Errorf("%w : Failed Key Generation with mnemonic %s", err, secretMnem) } - kringRecord, err = CreateSignerFromPrivKey(privKey, keyName) - return kringRecord, privKey, err -} - -func CreateSignerFromPrivKey( - privKey cryptotypes.PrivKey, keyName string, -) (*keyring.Record, error) { - return keyring.NewLocalRecord(keyName, privKey, privKey.PubKey()) -} -func AddSignerToKeyring( - kring keyring.Keyring, privKey cryptotypes.PrivKey, keyName string, -) error { - passphrase := "password" - armor := crypto.EncryptArmorPrivKey(privKey, passphrase, privKey.Type()) - return kring.ImportPrivKey(keyName, armor, passphrase) + return addr, err } diff --git a/gosdk/keys_test.go b/gosdk/keys_test.go index b790cfbb8..e45b27d58 100644 --- a/gosdk/keys_test.go +++ b/gosdk/keys_test.go @@ -3,6 +3,7 @@ package gosdk_test import ( "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/NibiruChain/nibiru/gosdk" @@ -14,6 +15,7 @@ func TestCreateSigner(t *testing.T) { testCases := []struct { testName string mnemonic string + wantAddr string expectErr bool }{ { @@ -24,6 +26,7 @@ func TestCreateSigner(t *testing.T) { { testName: "good input (localnet genesis)", mnemonic: LOCALNET_VALIDATOR_MNEMONIC, + wantAddr: "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl", expectErr: false, }, } @@ -32,15 +35,15 @@ func TestCreateSigner(t *testing.T) { t.Run(tc.testName, func(t *testing.T) { kring := gosdk.NewKeyring() keyName := "" - signer, privKey, err := gosdk.CreateSigner(tc.mnemonic, kring, keyName) + + gotAddr, err := gosdk.AddSignerToKeyringSecp256k1(kring, tc.mnemonic, keyName) + if tc.expectErr { require.Error(t, err) return } - require.NoError(t, err) - require.NotNil(t, signer.PubKey) - err = gosdk.AddSignerToKeyring(kring, privKey, privKey.PubKey().String()) + assert.EqualValues(t, gotAddr.String(), tc.wantAddr) require.NoError(t, err) }) }