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

chore: Add Keybase HasByNameOrAddress, HasByName and HasByAddress #1313

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions tm2/pkg/crypto/keys/client/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ func execAdd(cfg *addCfg, args []string, io *commands.IO) error {
return err
}

_, err = kb.GetByName(name)
if err == nil {
if has, err := kb.HasByName(name); err == nil && has {
// account exists, ask for user confirmation
response, err2 := io.GetConfirmation(fmt.Sprintf("Override the existing name %s", name))
if err2 != nil {
Expand Down
20 changes: 20 additions & 0 deletions tm2/pkg/crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@
return res, nil
}

// HasByNameOrAddress checks if a key with the name or bech32 string address is in the keybase.
func (kb dbKeybase) HasByNameOrAddress(nameOrBech32 string) (bool, error) {
address, err := crypto.AddressFromBech32(nameOrBech32)
if err != nil {
return kb.HasByName(nameOrBech32)

Check warning on line 175 in tm2/pkg/crypto/keys/keybase.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/keybase.go#L175

Added line #L175 was not covered by tests
} else {
return kb.HasByAddress(address)
}
}

// HasByName checks if a key with the name is in the keybase.
func (kb dbKeybase) HasByName(name string) (bool, error) {
return kb.db.Has(infoKey(name)), nil
}

// HasByAddress checks if a key with the address is in the keybase.
func (kb dbKeybase) HasByAddress(address crypto.Address) (bool, error) {
return kb.db.Has(addrKey(address)), nil
}

// Get returns the public information about one key.
func (kb dbKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
addr, err := crypto.AddressFromBech32(nameOrBech32)
Expand Down
32 changes: 21 additions & 11 deletions tm2/pkg/crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func TestKeyManagement(t *testing.T) {
assert.Empty(t, l)

// create some keys
_, err = cstore.GetByName(n1)
require.Error(t, err)
has, err := cstore.HasByName(n1)
require.NoError(t, err)
require.False(t, has)
i, err := cstore.CreateAccount(n1, mn1, bip39Passphrase, p1, 0, 0)
require.NoError(t, err)
require.Equal(t, n1, i.GetName())
Expand All @@ -91,14 +92,21 @@ func TestKeyManagement(t *testing.T) {
// we can get these keys
i2, err := cstore.GetByName(n2)
require.NoError(t, err)
_, err = cstore.GetByName(n3)
require.NotNil(t, err)
_, err = cstore.GetByAddress(toAddr(i2))
has, err = cstore.HasByName(n3)
require.NoError(t, err)
require.False(t, has)
has, err = cstore.HasByAddress(toAddr(i2))
require.NoError(t, err)
require.True(t, has)
// Also check with HasByNameOrAddress
has, err = cstore.HasByNameOrAddress(crypto.AddressToBech32(toAddr(i2)))
require.NoError(t, err)
require.True(t, has)
addr, err := crypto.AddressFromBech32("g1frtkxv37nq7arvyz5p0mtjqq7hwuvd4dnt892p")
require.NoError(t, err)
_, err = cstore.GetByAddress(addr)
require.NotNil(t, err)
has, err = cstore.HasByAddress(addr)
require.NoError(t, err)
require.False(t, has)

// list shows them in order
keyS, err := cstore.List()
Expand All @@ -117,8 +125,9 @@ func TestKeyManagement(t *testing.T) {
keyS, err = cstore.List()
require.NoError(t, err)
require.Equal(t, 1, len(keyS))
_, err = cstore.GetByName(n1)
require.Error(t, err)
has, err = cstore.HasByName(n1)
require.NoError(t, err)
require.False(t, has)

// create an offline key
o1 := "offline"
Expand Down Expand Up @@ -368,8 +377,9 @@ func TestSeedPhrase(t *testing.T) {
// now, let us delete this key
err = cstore.Delete(n1, p1, false)
require.Nil(t, err, "%+v", err)
_, err = cstore.GetByName(n1)
require.NotNil(t, err)
has, err := cstore.HasByName(n1)
require.NoError(t, err)
require.False(t, has)
}

func ExampleNew() {
Expand Down
30 changes: 30 additions & 0 deletions tm2/pkg/crypto/keys/lazy_keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@
return NewDBKeybase(db).List()
}

func (lkb lazyKeybase) HasByNameOrAddress(nameOrBech32 string) (bool, error) {
db, err := db.NewDB(lkb.name, dbBackend, lkb.dir)
if err != nil {
return false, err
}
defer db.Close()

return NewDBKeybase(db).HasByNameOrAddress(nameOrBech32)

Check warning on line 47 in tm2/pkg/crypto/keys/lazy_keybase.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/lazy_keybase.go#L40-L47

Added lines #L40 - L47 were not covered by tests
}

func (lkb lazyKeybase) HasByName(name string) (bool, error) {
db, err := db.NewDB(lkb.name, dbBackend, lkb.dir)
if err != nil {
return false, err
}
defer db.Close()

return NewDBKeybase(db).HasByName(name)

Check warning on line 57 in tm2/pkg/crypto/keys/lazy_keybase.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/lazy_keybase.go#L50-L57

Added lines #L50 - L57 were not covered by tests
}

func (lkb lazyKeybase) HasByAddress(address crypto.Address) (bool, error) {
db, err := db.NewDB(lkb.name, dbBackend, lkb.dir)
if err != nil {
return false, err
}
defer db.Close()

return NewDBKeybase(db).HasByAddress(address)

Check warning on line 67 in tm2/pkg/crypto/keys/lazy_keybase.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/lazy_keybase.go#L60-L67

Added lines #L60 - L67 were not covered by tests
}

func (lkb lazyKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
db, err := db.NewDB(lkb.name, dbBackend, lkb.dir)
if err != nil {
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 @@ -13,6 +13,9 @@ import (
type Keybase interface {
// CRUD on the keystore
List() ([]Info, error)
HasByNameOrAddress(nameOrBech32 string) (bool, error)
HasByName(name string) (bool, error)
HasByAddress(address crypto.Address) (bool, error)
GetByNameOrAddress(nameOrBech32 string) (Info, error)
GetByName(name string) (Info, error)
GetByAddress(address crypto.Address) (Info, error)
Expand Down
Loading