Skip to content

Commit

Permalink
implement Decode and encode natively
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Dec 19, 2024
1 parent bd0f553 commit aae5498
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 102 deletions.
4 changes: 2 additions & 2 deletions gnovm/stdlibs/crypto/bech32/bech32.gno
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Decode(bech string) (string, []byte, error) {
for i := 0; i < len(bech); i++ {
if bech[i] < 33 || bech[i] > 126 {
return "", nil, errors.New("invalid character in " +
"string: '" +string(bech[i]) + "'")
"string: '" + string(bech[i]) + "'")
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ func Decode(bech string) (string, []byte, error) {
expected, err := toChars(bech32Checksum(hrp,
decoded[:len(decoded)-6]))
if err == nil {
moreInfo = "Expected "+expected+", got "+checksum+"."
moreInfo = "Expected " + expected + ", got " + checksum + "."
}
return "", nil, errors.New("checksum failed. " + moreInfo)
}
Expand Down
73 changes: 1 addition & 72 deletions gnovm/stdlibs/generated.go

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

28 changes: 27 additions & 1 deletion gnovm/stdlibs/std/crypto.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package std

import "crypto/bech32"

type Address string // NOTE: bech32

func (a Address) String() string {
Expand All @@ -8,10 +10,34 @@ func (a Address) String() string {

// IsValid checks if the address is valid bech32 encoded string
func (a Address) IsValid() bool {
_, _, ok := DecodeBech32(a)
_, _, ok := bech32.DecodeBech32(a)
return ok
}

const RawAddressSize = 20

type RawAddress [RawAddressSize]byte

func EncodeBech32(prefix string, bz [20]byte) Address {
b32, err := bech32.ConvertAndEncode(prefix, bytes[:])
if err != nil {
panic(err) // should not happen
}
return b32
}

func DecodeBech32(addr Address) (prefix string, bz [20]byte, ok bool) {
prefix, bz, err := bech32.Decode(addr)
if err != nil || len(bz) != 20 {
return "", [20]byte{}, false
}
return prefix, [20]byte(bz), true
}

func convertAndEncode(hrp string, data []byte) (string, error) {
converted, err := bech32.ConvertBits(data, 8, 5, true)
if err != nil {
return "", errors.Wrap(err, "encoding bech32 failed")
}
return bech32.Encode(hrp, converted)
}
10 changes: 0 additions & 10 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,11 @@ func DerivePkgAddr(pkgPath string) Address {
return Address(derivePkgAddr(pkgPath))
}

func EncodeBech32(prefix string, bz [20]byte) Address {
return Address(encodeBech32(prefix, bz))
}

func DecodeBech32(addr Address) (prefix string, bz [20]byte, ok bool) {
return decodeBech32(string(addr))
}

// Variations which don't use named types.
func origSend() (denoms []string, amounts []int64)
func origCaller() string
func origPkgAddr() string
func callerAt(n int) string
func getRealm(height int) (address string, pkgPath string)
func derivePkgAddr(pkgPath string) string
func encodeBech32(prefix string, bz [20]byte) string
func decodeBech32(addr string) (prefix string, bz [20]byte, ok bool)
func assertCallerIsRealm()
17 changes: 0 additions & 17 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package std

import (
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/bech32"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
)
Expand Down Expand Up @@ -146,22 +145,6 @@ func X_derivePkgAddr(pkgPath string) string {
return string(gno.DerivePkgAddr(pkgPath).Bech32())
}

func X_encodeBech32(prefix string, bytes [20]byte) string {
b32, err := bech32.ConvertAndEncode(prefix, bytes[:])
if err != nil {
panic(err) // should not happen
}
return b32
}

func X_decodeBech32(addr string) (prefix string, bytes [20]byte, ok bool) {
prefix, bz, err := bech32.Decode(addr)
if err != nil || len(bz) != 20 {
return "", [20]byte{}, false
}
return prefix, [20]byte(bz), true
}

func X_assertCallerIsRealm(m *gno.Machine) {
frame := m.Frames[m.NumFrames()-2]
if path := frame.LastPackage.PkgPath; !gno.IsRealmPath(path) {
Expand Down

0 comments on commit aae5498

Please sign in to comment.