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

Conversation

jefft0
Copy link
Contributor

@jefft0 jefft0 commented Oct 27, 2023

The Keybase interface has GetByNameOrAddress, GetByName and GetByAddress. If an application simply wants to check if the key is in the key base, it must use one of these and check for an error, as is done in the gnokey command line.

		_, err = kb.GetByName(name)
		if err == nil {
			// account exists, ask for user confirmation
			...
		}

A GnoMobile app also needs to check for the key. But it's expensive to return the entire key info. The byte buffer must be copies multiple times, in the Keybase and in the gRPC interface, and the key info structure must be reformatted to Protobuf and then to the application's language. And not preferable to generate errors to pass information in normal program flow.

In the underlying DB interface, there is already a Has function. This PR updates the Keybase with the related functions HasByNameOrAddress, HasByName and HasByAddress which use Has to inexpensively return a simple bool. It also updates the gnokey command line to use HasByName to be more clear:

		if has, err := kb.HasByName(name); err == nil && has {
			// account exists, ask for user confirmation
			...
		}

@github-actions github-actions bot added the 📦 🌐 tendermint v2 Issues or PRs tm2 related label Oct 27, 2023
@codecov
Copy link

codecov bot commented Oct 27, 2023

Codecov Report

Attention: 36 lines in your changes are missing coverage. Please review.

Comparison is base (199cd29) 47.87% compared to head (2e1b2e7) 55.11%.
Report is 23 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1313      +/-   ##
==========================================
+ Coverage   47.87%   55.11%   +7.24%     
==========================================
  Files         372      421      +49     
  Lines       63019    67456    +4437     
==========================================
+ Hits        30168    37179    +7011     
+ Misses      30384    27369    -3015     
- Partials     2467     2908     +441     
Files Coverage Δ
tm2/pkg/crypto/keys/types.go 54.43% <ø> (ø)
tm2/pkg/crypto/keys/client/add.go 29.86% <0.00%> (-1.22%) ⬇️
tm2/pkg/crypto/keys/keybase.go 34.79% <0.00%> (-16.10%) ⬇️
tm2/pkg/crypto/keys/lazy_keybase.go 0.00% <0.00%> (ø)

... and 144 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Jeff Thompson <jeff@thefirst.org>
…if possible

Signed-off-by: Jeff Thompson <jeff@thefirst.org>
@jefft0 jefft0 force-pushed the chore/add-Keybase-HasByNameOrAddress branch from c6d0075 to 17e247b Compare October 31, 2023 13:49
Copy link
Member

@moul moul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, makes sense.

@thehowl thehowl merged commit 09d7c52 into gnolang:master Nov 9, 2023
53 checks passed
moul pushed a commit to moul/gno that referenced this pull request Nov 14, 2023
…olang#1313)

The Keybase [interface
has](https://github.com/gnolang/gno/blob/7105d00e10209003ea41fbae7a4d7c463c4167c3/tm2/pkg/crypto/keys/types.go#L16C2-L18)
`GetByNameOrAddress`, `GetByName` and `GetByAddress`. If an application
simply wants to check if the key is in the key base, it must use one of
these and check for an error, as is done [in the gnokey command
line](https://github.com/gnolang/gno/blob/7105d00e10209003ea41fbae7a4d7c463c4167c3/tm2/pkg/crypto/keys/client/add.go#L159-L160).

```
		_, err = kb.GetByName(name)
		if err == nil {
			// account exists, ask for user confirmation
			...
		}
```

A GnoMobile app also needs to check for the key. But it's expensive to
return the entire key info. The byte buffer must be copies multiple
times, in the Keybase and in the gRPC interface, and the key info
structure must be reformatted to Protobuf and then to the application's
language. And not preferable to generate errors to pass information in
normal program flow.

In the underlying `DB` interface, there is already a [Has
function](https://github.com/gnolang/gno/blob/7105d00e10209003ea41fbae7a4d7c463c4167c3/tm2/pkg/db/types.go#L13).
This PR updates the `Keybase` with the related functions
`HasByNameOrAddress`, `HasByName` and `HasByAddress` which use `Has` to
inexpensively return a simple bool. It also updates the gnokey command
line to use `HasByName` to be more clear:

```
		if has, err := kb.HasByName(name); err == nil && has {
			// account exists, ask for user confirmation
			...
		}
```

---------

Signed-off-by: Jeff Thompson <jeff@thefirst.org>
Co-authored-by: Morgan Bazalgette <morgan@morganbaz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related
Projects
Status: Done
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants