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

// Valid checks if the address is of specific length. Doesn't check prefix or checksum for the address
func (a Address) Valid() bool {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
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.Valid()

if result != tc.expected {
t.Fatalf("Expected: %t, got: %t", tc.expected, result)
}
}
}
Loading