Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support providing custom keyring for keystore. (backport #12453) #12489

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (telemetry) [#12405](https://github.com/cosmos/cosmos-sdk/pull/12405) Add _query_ calls metric to telemetry.

### Improvements

* [#12453](https://github.com/cosmos/cosmos-sdk/pull/12453) Add `NewInMemoryWithKeyring` function which allows the creation of in memory `keystore` instances with a specified set of existing items.

## [v0.46.0-rc2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc2) - 2022-07-05

### Features
Expand Down
8 changes: 7 additions & 1 deletion crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ type Options struct {
// purposes and on-the-fly key generation.
// Keybase options can be applied when generating this new Keybase.
func NewInMemory(cdc codec.Codec, opts ...Option) Keyring {
return newKeystore(keyring.NewArrayKeyring(nil), cdc, BackendMemory, opts...)
return NewInMemoryWithKeyring(keyring.NewArrayKeyring(nil), cdc, opts...)
}

// NewInMemoryWithKeyring returns an in memory keyring using the specified keyring.Keyring
// as the backing keyring.
func NewInMemoryWithKeyring(kr keyring.Keyring, cdc codec.Codec, opts ...Option) Keyring {
return newKeystore(kr, cdc, BackendMemory, opts...)
}

// New creates a new instance of a keyring.
Expand Down
44 changes: 44 additions & 0 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -455,6 +456,49 @@ func TestInMemoryLanguage(t *testing.T) {
require.Equal(t, "unsupported language: only english is supported", err.Error())
}

func TestInMemoryWithKeyring(t *testing.T) {
priv := cryptotypes.PrivKey(secp256k1.GenPrivKey())
pub := priv.PubKey()

cdc := getCodec()
_, err := NewLocalRecord("test record", priv, pub)

multi := multisig.NewLegacyAminoPubKey(
1, []cryptotypes.PubKey{
pub,
},
)

appName := "test-app"

legacyMultiInfo, err := NewLegacyMultiInfo(appName, multi)
require.NoError(t, err)
serializedLegacyMultiInfo := MarshalInfo(legacyMultiInfo)

kb := NewInMemoryWithKeyring(keyring.NewArrayKeyring([]keyring.Item{
{
Key: appName + ".info",
Data: serializedLegacyMultiInfo,
Description: "test description",
},
}), cdc)

t.Run("key exists", func(t *testing.T) {
_, err := kb.Key(appName)
require.NoError(t, err)
})

t.Run("key deleted", func(t *testing.T) {
err := kb.Delete(appName)
require.NoError(t, err)

t.Run("key is gone", func(t *testing.T) {
_, err := kb.Key(appName)
require.Error(t, err)
})
})
}

func TestInMemoryCreateMultisig(t *testing.T) {
cdc := getCodec()
kb, err := New("keybasename", "memory", "", nil, cdc)
Expand Down