Skip to content

Commit 865d58b

Browse files
authored
feat: Support providing custom keyring for keystore. (backport #12453) (#12489)
1 parent 846b32b commit 865d58b

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
4141

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

44+
### Improvements
45+
46+
* [#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.
47+
4448
## [v0.46.0-rc2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc2) - 2022-07-05
4549

4650
### Features

crypto/keyring/keyring.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ type Options struct {
150150
// purposes and on-the-fly key generation.
151151
// Keybase options can be applied when generating this new Keybase.
152152
func NewInMemory(cdc codec.Codec, opts ...Option) Keyring {
153-
return newKeystore(keyring.NewArrayKeyring(nil), cdc, BackendMemory, opts...)
153+
return NewInMemoryWithKeyring(keyring.NewArrayKeyring(nil), cdc, opts...)
154+
}
155+
156+
// NewInMemoryWithKeyring returns an in memory keyring using the specified keyring.Keyring
157+
// as the backing keyring.
158+
func NewInMemoryWithKeyring(kr keyring.Keyring, cdc codec.Codec, opts ...Option) Keyring {
159+
return newKeystore(kr, cdc, BackendMemory, opts...)
154160
}
155161

156162
// New creates a new instance of a keyring.

crypto/keyring/keyring_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
2222
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
2323
"github.com/cosmos/cosmos-sdk/crypto/types"
24+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
2425
sdk "github.com/cosmos/cosmos-sdk/types"
2526
)
2627

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

459+
func TestInMemoryWithKeyring(t *testing.T) {
460+
priv := cryptotypes.PrivKey(secp256k1.GenPrivKey())
461+
pub := priv.PubKey()
462+
463+
cdc := getCodec()
464+
_, err := NewLocalRecord("test record", priv, pub)
465+
466+
multi := multisig.NewLegacyAminoPubKey(
467+
1, []cryptotypes.PubKey{
468+
pub,
469+
},
470+
)
471+
472+
appName := "test-app"
473+
474+
legacyMultiInfo, err := NewLegacyMultiInfo(appName, multi)
475+
require.NoError(t, err)
476+
serializedLegacyMultiInfo := MarshalInfo(legacyMultiInfo)
477+
478+
kb := NewInMemoryWithKeyring(keyring.NewArrayKeyring([]keyring.Item{
479+
{
480+
Key: appName + ".info",
481+
Data: serializedLegacyMultiInfo,
482+
Description: "test description",
483+
},
484+
}), cdc)
485+
486+
t.Run("key exists", func(t *testing.T) {
487+
_, err := kb.Key(appName)
488+
require.NoError(t, err)
489+
})
490+
491+
t.Run("key deleted", func(t *testing.T) {
492+
err := kb.Delete(appName)
493+
require.NoError(t, err)
494+
495+
t.Run("key is gone", func(t *testing.T) {
496+
_, err := kb.Key(appName)
497+
require.Error(t, err)
498+
})
499+
})
500+
}
501+
458502
func TestInMemoryCreateMultisig(t *testing.T) {
459503
cdc := getCodec()
460504
kb, err := New("keybasename", "memory", "", nil, cdc)

0 commit comments

Comments
 (0)