Skip to content

Commit

Permalink
implement DerivePkgAddr on gno
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Jan 8, 2025
1 parent d03b132 commit bf2167b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 47 deletions.
5 changes: 3 additions & 2 deletions examples/gno.land/r/demo/keystore/keystore_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

func TestRender(t *testing.T) {
// For some reason non native function fails with
// gno.land/r/demo/keystore/keystore_test.gno:15:3: constant overflows (code=2)
// https://github.com/gnolang/gno/pull/3375 changed const -> var :
// For some reason non native functions fails on constants with
// constant overflows (code=2), this does not happens when using a variable
// TODO: check this issue after and if this PR is merged
var (
author1 std.Address = testutils.TestAddress("author1")
Expand Down
28 changes: 0 additions & 28 deletions gnovm/stdlibs/generated.go

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

35 changes: 27 additions & 8 deletions gnovm/stdlibs/std/crypto.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package std

import (
"crypto/bech32"
"crypto/sha256"
"errors"
)

type Address string // NOTE: bech32

// bech32AddrPrefix defines the Bech32 prefix of an address
const bech32AddrPrefix = "g"

func (a Address) String() string {
return string(a)
}
Expand Down Expand Up @@ -34,14 +38,7 @@ func DecodeBech32(addr Address) (string, [20]byte, bool) {
if err != nil || len(bz) != 20 {
return "", [20]byte{}, false
}
/* For some reason [20]byte(bz) fails with: 'cannot convert []uint8 to ArrayKind'
Maybe there is an issue to create
*/
result := [20]byte{}
for index, b := range bz {
result[index] = b
}
return prefix, result, true
return prefix, convertTo20Byte(bz), true
}

func convertAndEncode(hrp string, data []byte) (string, error) {
Expand All @@ -63,3 +60,25 @@ func decodeAndConvert(bech string) (string, []byte, error) {
}
return hrp, converted, nil
}

func DerivePkgAddr(pkgPath string) Address {
data := sumTruncated([]byte("pkgPath:" + pkgPath))
return EncodeBech32(bech32AddrPrefix, data)
}

func sumTruncated(bz []byte) [20]byte {
hash := sha256.Sum256(bz)
return convertTo20Byte(hash[:RawAddressSize])
}

func convertTo20Byte(in []byte) [20]byte {
/* For some reason [20]byte(bz) fails with: 'cannot convert []uint8 to ArrayKind'
Maybe there is an issue to create
*/
result := [20]byte{}
for index, b := range in {
result[index] = b
}

return result
}
19 changes: 19 additions & 0 deletions gnovm/stdlibs/std/crypto_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ func TestValid(t *testing.T) {
}
}
}

func TestDerivePkgAddr(t *testing.T) {
type test struct {
inputPath string
expected string
}

testCases := []test{
{inputPath: "gno.land/r/gnoland/faucet", expected: "g1ttrq7mp4zy6dssnmgyyktnn4hcj3ys8xhju0n7"},
{inputPath: "gno.land/r/demo/tamagotchi", expected: "g1a3tu874agjlkrpzt9x90xv3uzncapcn959yte4"},
}

for _, tc := range testCases {
result := DerivePkgAddr(tc.inputPath)
if result.String() != tc.expected {
t.Fatalf("Expected: %t, got: %t", tc.expected, result)
}
}
}
5 changes: 0 additions & 5 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ func GetCallerAt(n int) Address {
return Address(callerAt(n))
}

func DerivePkgAddr(pkgPath string) Address {
return Address(derivePkgAddr(pkgPath))
}

// 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 assertCallerIsRealm()
4 changes: 0 additions & 4 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ func currentRealm(m *gno.Machine) (address, pkgPath string) {
return X_getRealm(m, 0)
}

func X_derivePkgAddr(pkgPath string) string {
return string(gno.DerivePkgAddr(pkgPath).Bech32())
}

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 bf2167b

Please sign in to comment.