Skip to content

Commit aae5498

Browse files
committed
implement Decode and encode natively
1 parent bd0f553 commit aae5498

File tree

5 files changed

+30
-102
lines changed

5 files changed

+30
-102
lines changed

gnovm/stdlibs/crypto/bech32/bech32.gno

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Decode(bech string) (string, []byte, error) {
2626
for i := 0; i < len(bech); i++ {
2727
if bech[i] < 33 || bech[i] > 126 {
2828
return "", nil, errors.New("invalid character in " +
29-
"string: '" +string(bech[i]) + "'")
29+
"string: '" + string(bech[i]) + "'")
3030
}
3131
}
3232

@@ -67,7 +67,7 @@ func Decode(bech string) (string, []byte, error) {
6767
expected, err := toChars(bech32Checksum(hrp,
6868
decoded[:len(decoded)-6]))
6969
if err == nil {
70-
moreInfo = "Expected "+expected+", got "+checksum+"."
70+
moreInfo = "Expected " + expected + ", got " + checksum + "."
7171
}
7272
return "", nil, errors.New("checksum failed. " + moreInfo)
7373
}

gnovm/stdlibs/generated.go

+1-72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gnovm/stdlibs/std/crypto.gno

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package std
22

3+
import "crypto/bech32"
4+
35
type Address string // NOTE: bech32
46

57
func (a Address) String() string {
@@ -8,10 +10,34 @@ func (a Address) String() string {
810

911
// IsValid checks if the address is valid bech32 encoded string
1012
func (a Address) IsValid() bool {
11-
_, _, ok := DecodeBech32(a)
13+
_, _, ok := bech32.DecodeBech32(a)
1214
return ok
1315
}
1416

1517
const RawAddressSize = 20
1618

1719
type RawAddress [RawAddressSize]byte
20+
21+
func EncodeBech32(prefix string, bz [20]byte) Address {
22+
b32, err := bech32.ConvertAndEncode(prefix, bytes[:])
23+
if err != nil {
24+
panic(err) // should not happen
25+
}
26+
return b32
27+
}
28+
29+
func DecodeBech32(addr Address) (prefix string, bz [20]byte, ok bool) {
30+
prefix, bz, err := bech32.Decode(addr)
31+
if err != nil || len(bz) != 20 {
32+
return "", [20]byte{}, false
33+
}
34+
return prefix, [20]byte(bz), true
35+
}
36+
37+
func convertAndEncode(hrp string, data []byte) (string, error) {
38+
converted, err := bech32.ConvertBits(data, 8, 5, true)
39+
if err != nil {
40+
return "", errors.Wrap(err, "encoding bech32 failed")
41+
}
42+
return bech32.Encode(hrp, converted)
43+
}

gnovm/stdlibs/std/native.gno

-10
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,11 @@ func DerivePkgAddr(pkgPath string) Address {
4949
return Address(derivePkgAddr(pkgPath))
5050
}
5151

52-
func EncodeBech32(prefix string, bz [20]byte) Address {
53-
return Address(encodeBech32(prefix, bz))
54-
}
55-
56-
func DecodeBech32(addr Address) (prefix string, bz [20]byte, ok bool) {
57-
return decodeBech32(string(addr))
58-
}
59-
6052
// Variations which don't use named types.
6153
func origSend() (denoms []string, amounts []int64)
6254
func origCaller() string
6355
func origPkgAddr() string
6456
func callerAt(n int) string
6557
func getRealm(height int) (address string, pkgPath string)
6658
func derivePkgAddr(pkgPath string) string
67-
func encodeBech32(prefix string, bz [20]byte) string
68-
func decodeBech32(addr string) (prefix string, bz [20]byte, ok bool)
6959
func assertCallerIsRealm()

gnovm/stdlibs/std/native.go

-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package std
22

33
import (
44
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
5-
"github.com/gnolang/gno/tm2/pkg/bech32"
65
"github.com/gnolang/gno/tm2/pkg/crypto"
76
"github.com/gnolang/gno/tm2/pkg/std"
87
)
@@ -146,22 +145,6 @@ func X_derivePkgAddr(pkgPath string) string {
146145
return string(gno.DerivePkgAddr(pkgPath).Bech32())
147146
}
148147

149-
func X_encodeBech32(prefix string, bytes [20]byte) string {
150-
b32, err := bech32.ConvertAndEncode(prefix, bytes[:])
151-
if err != nil {
152-
panic(err) // should not happen
153-
}
154-
return b32
155-
}
156-
157-
func X_decodeBech32(addr string) (prefix string, bytes [20]byte, ok bool) {
158-
prefix, bz, err := bech32.Decode(addr)
159-
if err != nil || len(bz) != 20 {
160-
return "", [20]byte{}, false
161-
}
162-
return prefix, [20]byte(bz), true
163-
}
164-
165148
func X_assertCallerIsRealm(m *gno.Machine) {
166149
frame := m.Frames[m.NumFrames()-2]
167150
if path := frame.LastPackage.PkgPath; !gno.IsRealmPath(path) {

0 commit comments

Comments
 (0)