Skip to content

Commit

Permalink
feat: add simple address validity check (gnolang#1303)
Browse files Browse the repository at this point in the history
## Description

This PR adds a simple `Valid()` check to the Address type. It is there
to prevent most human errors, and doesn't actually validate the math
behind bech32 (for now) - it relies only on the length of the address.

Running `gno test .` will run the unit test for the `Valid()` function.
Closes: gnolang#1298 

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
Co-authored-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
2 people authored and gfanton committed Nov 9, 2023
1 parent e7b87e2 commit 0afdba3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/grc/grc1155/util.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
const zeroAddress std.Address = ""

func isValidAddress(addr std.Address) bool {
if addr.String() == "" {
if !addr.IsValid() {
return false
}
return true
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/grc/grc20/util.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "std"
const zeroAddress = std.Address("")

func checkIsValidAddress(addr std.Address) error {
if addr.String() == "" {
if !addr.IsValid() {
return ErrInvalidAddress
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/grc/grc721/util.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
var zeroAddress = std.Address("")

func isValidAddress(addr std.Address) error {
if addr.String() == "" {
if !addr.IsValid() {
return ErrInvalidAddress
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions gnovm/stdlibs/std/crypto.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ func (a Address) String() string {
return string(a)
}

// IsValid checks if the address is of specific length. Doesn't check prefix or checksum for the address
func (a Address) IsValid() bool {
return len(a) == RawAddressSize*2 // hex length
}

const RawAddressSize = 20

type RawAddress [RawAddressSize]byte
30 changes: 30 additions & 0 deletions gnovm/stdlibs/std/crypto_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package std

import (
"testing"
)

func TestValid(t *testing.T) {
type test struct {
inputAddress Address
expected bool
}

testCases := []test{
{inputAddress: "g1f4v282mwyhu29afke4vq5r2xzcm6z3ftnugcnv", expected: true},
{inputAddress: "g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa", expected: true},
{inputAddress: "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", expected: true},
{inputAddress: "g14da4n9hcynyzz83q607uu8keuh9hwlv42ra6fa", expected: true},
{inputAddress: "", expected: false},
{inputAddress: "000000000000", expected: false},
{inputAddress: "0000000000000000000000000000000000000000000000000000000000000000000000", expected: false},
}

for _, tc := range testCases {
result := tc.inputAddress.IsValid()

if result != tc.expected {
t.Fatalf("Expected: %t, got: %t", tc.expected, result)
}
}
}
5 changes: 5 additions & 0 deletions gnovm/stdlibs/stdshim/crypto.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ func (a Address) String() string {
return string(a)
}

// IsValid checks if the address is of specific length. Doesn't check prefix or checksum for the address
func (a Address) IsValid() bool {
return len(a) == RawAddressSize*2 // hex length
}

const RawAddressSize = 20

type RawAddress [RawAddressSize]byte
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
go.opencensus.io v0.22.5 // indirect
Expand Down
10 changes: 5 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0afdba3

Please sign in to comment.