From db58dad79f937547d7eb508e148e7745b72ce2a4 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 20 Mar 2024 11:55:01 +0900 Subject: [PATCH] fix: std.Address validation --- gno.land/cmd/gnoland/testdata/pr-1799.txtar | 12 ++++++++++++ gnovm/stdlibs/std/crypto.gno | 5 +++-- gnovm/stdlibs/stdshim/crypto.gno | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 gno.land/cmd/gnoland/testdata/pr-1799.txtar diff --git a/gno.land/cmd/gnoland/testdata/pr-1799.txtar b/gno.land/cmd/gnoland/testdata/pr-1799.txtar new file mode 100644 index 00000000000..4a648adba0e --- /dev/null +++ b/gno.land/cmd/gnoland/testdata/pr-1799.txtar @@ -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' \ No newline at end of file diff --git a/gnovm/stdlibs/std/crypto.gno b/gnovm/stdlibs/std/crypto.gno index 8d005dccf5c..b6240a49bc8 100644 --- a/gnovm/stdlibs/std/crypto.gno +++ b/gnovm/stdlibs/std/crypto.gno @@ -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.String()) + return ok } const RawAddressSize = 20 diff --git a/gnovm/stdlibs/stdshim/crypto.gno b/gnovm/stdlibs/stdshim/crypto.gno index 8d005dccf5c..b6240a49bc8 100644 --- a/gnovm/stdlibs/stdshim/crypto.gno +++ b/gnovm/stdlibs/stdshim/crypto.gno @@ -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.String()) + return ok } const RawAddressSize = 20