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

feat: add simple address validity check #1303

Merged
merged 9 commits into from
Oct 28, 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
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.

Loading