Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

converter.gno

// Package ctg is a simple utility package with helpers
// for bech32 address conversions.
package ctg

import (
	"crypto/bech32"

	"gno.land/p/nt/ufmt/v0"
)

// ConvertCosmosToGno takes a Bech32 Cosmos address (prefix "cosmos")
// and returns the same address re-encoded with the gno.land prefix "g".
func ConvertCosmosToGno(addr string) (address, error) {
	prefix, decoded, err := bech32.Decode(addr)
	if err != nil {
		return "", ufmt.Errorf("bech32 decode failed: %v", err)
	}

	if prefix != "cosmos" {
		return "", ufmt.Errorf("expected a cosmos address, got prefix %q", prefix)
	}

	return address(mustEncode("g", decoded)), nil
}

func mustEncode(hrp string, data []byte) string {
	enc, err := bech32.Encode(hrp, data)
	if err != nil {
		panic(err)
	}
	return enc
}

// ConvertAnyToGno converts *any* valid Bech32 address to its gno.land form
// by preserving the underlying payload but replacing the prefix with "g".
// No prefix check is performed; invalid Bech32 input still returns an error.
func ConvertAnyToGno(addr string) (address, error) {
	_, decoded, err := bech32.Decode(addr)
	if err != nil {
		return "", ufmt.Errorf("bech32 decode failed: %v", err)
	}
	return address(mustEncode("g", decoded)), nil
}

// ConvertGnoToAny converts a gno.land address (prefixed with "g") to another Bech32
// prefix given by prefix. The function ensures the source address really
// is a gno.land address before proceeding.
//
// Example:
//
//	cosmosAddr, _ := ConvertGnoToAny("cosmos", "g1k98jx9...")
//	fmt.Println(cosmosAddr) // → cosmos1....
func ConvertGnoToAny(prefix string, addr address) (string, error) {
	origPrefix, decoded, err := bech32.Decode(string(addr))
	if err != nil {
		return "", ufmt.Errorf("bech32 decode failed: %v", err)
	}
	if origPrefix != "g" {
		return "", ufmt.Errorf("expected a gno address but got prefix %q", origPrefix)
	}
	return mustEncode(prefix, decoded), nil
}

converter_test.gno

package ctg

import (
	"testing"
)

func TestConvertKnownAddress(t *testing.T) {
	const (
		cosmosAddr = "cosmos1jg8mtutu9khhfwc4nxmuhcpftf0pajdh6svrgs"
		gnoAddr    = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
	)
	got, err := ConvertCosmosToGno(cosmosAddr)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != gnoAddr {
		t.Fatalf("got %s, want %s", got, gnoAddr)
	}
}

func TestConvertCosmosToGno(t *testing.T) {
	decoded := []byte{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
	}

	cosmosAddr := mustEncode("cosmos", decoded)
	wantGno := mustEncode("g", decoded)

	got, err := ConvertCosmosToGno(cosmosAddr)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if string(got) != wantGno {
		t.Fatalf("got %s, want %s", got, wantGno)
	}

	// invalid bech32
	if _, err := ConvertCosmosToGno("not-bech32"); err == nil {
		t.Fatalf("expected error for invalid bech32")
	}

	// wrong prefix
	gAddr := mustEncode("g", decoded)
	if _, err := ConvertCosmosToGno(gAddr); err == nil {
		t.Fatalf("expected error for non-cosmos prefix")
	}
}

func TestConvertAnyToGno(t *testing.T) {
	payload := []byte{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
	}

	tests := []struct {
		name    string
		input   string
		want    string
		wantErr bool
	}{
		{
			name:  "cosmos→g",
			input: mustEncode("cosmos", payload),
			want:  mustEncode("g", payload),
		},
		{
			name:  "osmo→g",
			input: mustEncode("osmo", payload),
			want:  mustEncode("g", payload),
		},
		{
			name:    "invalid bech32",
			input:   "xyz123",
			wantErr: true,
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got, err := ConvertAnyToGno(tc.input)
			if tc.wantErr {
				if err == nil {
					t.Fatalf("expected error, got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if string(got) != tc.want {
				t.Fatalf("got %s, want %s", got, tc.want)
			}
		})
	}
}

func TestConvertGnoToAny(t *testing.T) {
	payload := []byte{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
		0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00,
	}

	gno := address(mustEncode("g", payload))

	t.Run("g→cosmos", func(t *testing.T) {
		got, err := ConvertGnoToAny("cosmos", gno)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if got != mustEncode("cosmos", payload) {
			t.Fatalf("conversion incorrect: %s", got)
		}
	})

	t.Run("g→foobar", func(t *testing.T) {
		got, err := ConvertGnoToAny("foobar", gno)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if got != mustEncode("foobar", payload) {
			t.Fatalf("conversion incorrect: %s", got)
		}
	})

	t.Run("g→osmo", func(t *testing.T) {
		got, err := ConvertGnoToAny("osmo", gno)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if got != mustEncode("osmo", payload) {
			t.Fatalf("conversion incorrect: %s", got)
		}
	})

	t.Run("wrong source prefix", func(t *testing.T) {
		cosmos := mustEncode("cosmos", payload)
		if _, err := ConvertGnoToAny("g", address(cosmos)); err == nil {
			t.Fatalf("expected error for non-g source prefix")
		}
	})

	t.Run("invalid bech32", func(t *testing.T) {
		if _, err := ConvertGnoToAny("cosmos", address("nope")); err == nil {
			t.Fatalf("expected error for invalid bech32")
		}
	})
}

gnomod.toml

module = "gno.land/p/leon/ctg/v0"
gno = "0.9"

[addpkg]
  creator = "g125em6arxsnj49vx35f0n0z34putv5ty3376fg5"