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 8 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/gnolang/gno
go 1.20

require (
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c
github.com/btcsuite/btcd v0.22.3
github.com/btcsuite/btcd/btcutil v1.0.0
github.com/btcsuite/btcutil v1.0.2
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
Copy link
Member

Choose a reason for hiding this comment

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

Ehh, I'd say upgrading btcd/btcutil is something that should be done in another PR, especially considering #1064

I'd suggest reverting changes here + go.sum

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was accidental, sorry. Will revert them :)

Copy link
Member

@moul moul Oct 27, 2023

Choose a reason for hiding this comment

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

Oops, my mistake. I attempted to fix the previous commit but didn't thoroughly check it.

Edit: nope, was introduced before my commit; I'll fix now.

github.com/cockroachdb/apd v1.1.0
github.com/davecgh/go-spew v1.1.1
github.com/dgraph-io/badger/v3 v3.2103.4
Expand Down
19 changes: 9 additions & 10 deletions go.sum

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

Loading