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: Cherry pick PRs for HasByName, etc. and fix GeyByAddress error type #5

Merged
merged 3 commits into from
Oct 30, 2023
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
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
26 changes: 25 additions & 1 deletion 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
}
}

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

// Get returns the public information about one key.
func (kb dbKeybase) GetByNameOrAddress(nameOrBech32 string) (Info, error) {
addr, err := crypto.AddressFromBech32(nameOrBech32)
Expand All @@ -189,7 +213,7 @@ func (kb dbKeybase) GetByName(name string) (Info, error) {
func (kb dbKeybase) GetByAddress(address crypto.Address) (Info, error) {
ik := kb.db.Get(addrKey(address))
if len(ik) == 0 {
return nil, fmt.Errorf("key with address %s not found", address)
return nil, keyerror.NewErrKeyNotFound(fmt.Sprintf("key with address %s not found", address))
}
bs := kb.db.Get(ik)
return readInfo(bs)
Expand Down
2 changes: 2 additions & 0 deletions tm2/pkg/crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
"github.com/gnolang/gno/tm2/pkg/crypto/keys/keyerror"
)

func TestCreateAccountInvalidMnemonic(t *testing.T) {
Expand Down Expand Up @@ -99,6 +100,7 @@ func TestKeyManagement(t *testing.T) {
require.NoError(t, err)
_, err = cstore.GetByAddress(addr)
require.NotNil(t, err)
require.True(t, keyerror.IsErrKeyNotFound(err))

// list shows them in order
keyS, err := cstore.List()
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)
}

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)
}

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)
}

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
Loading