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: full std.Address validation #1799

Merged
merged 6 commits into from
Mar 28, 2024
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
20 changes: 9 additions & 11 deletions examples/gno.land/r/demo/foo20/foo20_test.gno
thehowl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"std"
"testing"

"gno.land/p/demo/users"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
)

func TestReadOnlyPublicMethods(t *testing.T) {
admin := users.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
manfred := users.AddressOrName("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")
unknown := std.Address("g1u0000000000000000000000000000000000000")
admin := pusers.AddressOrName("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
manfred := pusers.AddressOrName("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")

type test struct {
name string
Expand All @@ -25,7 +25,6 @@ func TestReadOnlyPublicMethods(t *testing.T) {
{"BalanceOf(admin)", 10000000000, func() uint64 { return BalanceOf(admin) }},
{"BalanceOf(manfred)", 100000000, func() uint64 { return BalanceOf(manfred) }},
{"Allowance(admin, manfred)", 0, func() uint64 { return Allowance(admin, manfred) }},
{"BalanceOf(unknown)", 0, func() uint64 { return BalanceOf(users.AddressOrName(unknown)) }},
}
for _, tc := range tests {
if tc.fn() != tc.balance {
Expand All @@ -34,18 +33,17 @@ func TestReadOnlyPublicMethods(t *testing.T) {
}
}

// unknown uses the faucet.
std.TestSetOrigCaller(unknown)
// admin uses the faucet.
std.TestSetOrigCaller(users.Resolve(admin))
Faucet()

// check balances #2.
{
tests := []test{
{"TotalSupply", 10110000000, func() uint64 { return TotalSupply() }},
{"BalanceOf(admin)", 10000000000, func() uint64 { return BalanceOf(admin) }},
{"BalanceOf(admin)", 10010000000, func() uint64 { return BalanceOf(admin) }},
{"BalanceOf(manfred)", 100000000, func() uint64 { return BalanceOf(manfred) }},
{"Allowance(admin, manfred)", 0, func() uint64 { return Allowance(admin, manfred) }},
{"BalanceOf(unknown)", 10000000, func() uint64 { return BalanceOf(users.AddressOrName(unknown)) }},
}
for _, tc := range tests {
if tc.fn() != tc.balance {
Expand All @@ -68,8 +66,8 @@ func TestErrConditions(t *testing.T) {
std.TestSetOrigCaller(admin)
{
tests := []test{
{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(users.AddressOrName(admin), 1) }},
{"Approve(empty, 1))", "invalid address", func() { Approve(users.AddressOrName(empty), 1) }},
{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(pusers.AddressOrName(admin), 1) }},
{"Approve(empty, 1))", "invalid address", func() { Approve(pusers.AddressOrName(empty), 1) }},
}
for _, tc := range tests {
shouldPanicWithMsg(t, tc.fn, tc.msg)
Expand Down
12 changes: 12 additions & 0 deletions gno.land/cmd/gnoland/testdata/pr-1799.txtar
thehowl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test for https://github.com/gnolang/gno/pull/xxxx
loadpkg gno.land/r/demo/foo20

gnoland start

# execute Faucet
gnokey maketx call -pkgpath gno.land/r/demo/foo20 -func Faucet -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout 'OK!'

# execute Transfer for invalid address
! gnokey maketx call -pkgpath gno.land/r/demo/foo20 -func Transfer -args g1ubwj0apf60hd90txhnh855fkac34rxlsvua0aa -args 1 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stderr '"gnokey" error: --= Error =--\nData: invalid address'
5 changes: 3 additions & 2 deletions gnovm/stdlibs/std/crypto.gno
thehowl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ 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
// IsValid checks if the address is valid bech32 encoded string
func (a Address) IsValid() bool {
return len(a) == RawAddressSize*2 // hex length
_, _, ok := DecodeBech32(a)
return ok
}

const RawAddressSize = 20
Expand Down
5 changes: 3 additions & 2 deletions gnovm/stdlibs/stdshim/crypto.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ 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
// IsValid checks if the address is valid bech32 encoded string
func (a Address) IsValid() bool {
return len(a) == RawAddressSize*2 // hex length
_, _, ok := DecodeBech32(a)
return ok
}

const RawAddressSize = 20
Expand Down
Loading