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

fix: In keybase writeInfo, enforce one name for address #3221

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
9 changes: 8 additions & 1 deletion tm2/pkg/crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,17 @@ func (kb dbKeybase) writeInfo(name string, info Info) error {
kb.db.DeleteSync(addrKey(oldInfo.GetAddress()))
}

addressKey := addrKey(info.GetAddress())
nameKeyForAddress := kb.db.Get(addressKey)
if len(nameKeyForAddress) > 0 {
// Enforce 1-to-1 name to address. Remove the info by the old name with the same address
kb.db.DeleteSync(nameKeyForAddress)
}

serializedInfo := writeInfo(info)
kb.db.SetSync(key, serializedInfo)
// store a pointer to the infokey by address for fast lookup
kb.db.SetSync(addrKey(info.GetAddress()), key)
kb.db.SetSync(addressKey, key)
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions tm2/pkg/crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ func TestSignVerify(t *testing.T) {
i2, err := cstore.CreateAccount(n2, mn2, bip39Passphrase, p2, 0, 0)
require.Nil(t, err)

// Import a public key
// Import a public key into a new store
armor, err := cstore.ExportPubKey(n2)
require.Nil(t, err)
cstore.ImportPubKey(n3, armor)
i3, err := cstore.GetByName(n3)
cstore2 := NewInMemory()
cstore2.ImportPubKey(n3, armor)
i3, err := cstore2.GetByName(n3)
require.NoError(t, err)
require.Equal(t, i3.GetName(), n3)

Expand All @@ -174,6 +175,7 @@ func TestSignVerify(t *testing.T) {
s21, pub2, err := cstore.Sign(n2, p2, d1)
require.Nil(t, err)
require.Equal(t, i2.GetPubKey(), pub2)
require.Equal(t, i3.GetPubKey(), pub2)

s22, pub2, err := cstore.Sign(n2, p2, d2)
require.Nil(t, err)
Expand Down Expand Up @@ -282,11 +284,10 @@ func TestExportImportPubKey(t *testing.T) {
require.NoError(t, err)
// Compare the public keys
require.True(t, john.GetPubKey().Equals(john2.GetPubKey()))
// Ensure the original key hasn't changed
john, err = cstore.GetByName("john")
// Ensure that storing with the address of "john-pubkey-only" removed the entry for "john"
has, err := cstore.HasByName("john")
require.NoError(t, err)
require.Equal(t, john.GetPubKey().Address(), addr)
require.Equal(t, john.GetName(), "john")
require.False(t, has)

// Ensure keys cannot be overwritten
err = cstore.ImportPubKey("john-pubkey-only", armor)
Expand Down
3 changes: 3 additions & 0 deletions tm2/pkg/crypto/keys/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ type Keybase interface {

// CreateAccount creates an account based using the BIP44 path (44'/118'/{account}'/0/{index}
// Encrypt the key to disk using encryptPasswd.
// If an account exists with the same address but a different name, it is replaced by the new name.
// See https://github.com/tendermint/classic/sdk/issues/2095
CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (Info, error)

// Like CreateAccount but from general bip44 params.
// If an account exists with the same address but a different name, it is replaced by the new name.
CreateAccountBip44(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (Info, error)

// CreateLedger creates, stores, and returns a new Ledger key reference
Expand All @@ -43,6 +45,7 @@ type Keybase interface {
CreateMulti(name string, pubkey crypto.PubKey) (info Info, err error)

// The following operations will *only* work on locally-stored keys
// In all import operations, if an account exists with the same address but a different name, it is replaced by the new name.
Rotate(name, oldpass string, getNewpass func() (string, error)) error
Import(name string, armor string) (err error)
ImportPrivKey(name, armor, decryptPassphrase, encryptPassphrase string) error
Expand Down
Loading