diff --git a/CHANGELOG.md b/CHANGELOG.md index b0f29ae9ef19..d7f7c5e1e483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default. * [#12153](https://github.com/cosmos/cosmos-sdk/pull/12153) Add a new `NewSimulationManagerFromAppModules` constructor, to simplify simulation wiring. * [#12187](https://github.com/cosmos/cosmos-sdk/pull/12187) Add batch operation for x/nft module. +* [#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. ### State Machine Breaking diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 5434a27eed62..370e0111cec4 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -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. diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index 66c326598b71..e2d994187752 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -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" ) @@ -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)