Skip to content

Commit

Permalink
chore: Add Keybase HasByNameOrAddress
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Oct 30, 2023
1 parent 199cd29 commit e8c2917
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
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
24 changes: 24 additions & 0 deletions tm2/pkg/crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,30 @@ func (kb dbKeybase) List() ([]Info, error) {
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) {
has, err := kb.HasByAddress(nameOrBech32)
if err != nil {
return kb.HasByName(nameOrBech32)
} else {
return has, nil
}

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

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/keybase.go#L172-L178

Added lines #L172 - L178 were not covered by tests
}

// 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

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

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/keybase.go#L182-L183

Added lines #L182 - L183 were not covered by tests
}

// HasByAddress checks if a key with the bech32 string address is in the keybase.
func (kb dbKeybase) HasByAddress(bech32Address string) (bool, error) {
addr, err := crypto.AddressFromBech32(bech32Address)
if err != nil {
return false, err
}
return kb.db.Has(addrKey(addr)), nil

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

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/crypto/keys/keybase.go#L187-L192

Added lines #L187 - L192 were not covered by tests
}

// Get returns the public information about one key.
func (kb dbKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
addr, err := crypto.AddressFromBech32(nameOrBech32)
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 @@ func (lkb lazyKeybase) List() ([]Info, error) {
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(bech32Address string) (bool, error) {
db, err := db.NewDB(lkb.name, dbBackend, lkb.dir)
if err != nil {
return false, err
}
defer db.Close()

return NewDBKeybase(db).HasByAddress(bech32Address)

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(bech32Address string) (bool, error)
GetByNameOrAddress(nameOrBech32 string) (Info, error)
GetByName(name string) (Info, error)
GetByAddress(address crypto.Address) (Info, error)
Expand Down

0 comments on commit e8c2917

Please sign in to comment.