Skip to content

Commit

Permalink
fix(gosdk): fix keyring import functions (#1932)
Browse files Browse the repository at this point in the history
* fix: go-sdk add signer fix for kring

* refactor: removing unused go-sdk keyring funcs

* chore: update changelog
  • Loading branch information
CalicoNino authored Jun 20, 2024
1 parent d3cdb15 commit cc4ddd4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions app/evmante_sigverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion app/evmante_validate_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ func buildEthMsg(
gasLimit uint64,
from string,
to *common.Address,

) *evm.MsgEthereumTx {
ethContractCreationTxParams := &evm.EvmTxArgs{
ChainID: chainID,
Expand Down
45 changes: 9 additions & 36 deletions gosdk/keys.go
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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"
Expand All @@ -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
}
11 changes: 7 additions & 4 deletions gosdk/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gosdk_test
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/gosdk"
Expand All @@ -14,6 +15,7 @@ func TestCreateSigner(t *testing.T) {
testCases := []struct {
testName string
mnemonic string
wantAddr string
expectErr bool
}{
{
Expand All @@ -24,6 +26,7 @@ func TestCreateSigner(t *testing.T) {
{
testName: "good input (localnet genesis)",
mnemonic: LOCALNET_VALIDATOR_MNEMONIC,
wantAddr: "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl",
expectErr: false,
},
}
Expand All @@ -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)
})
}
Expand Down

0 comments on commit cc4ddd4

Please sign in to comment.