Skip to content

Commit

Permalink
chore: import secp256k1 for amino parsing (#1178)
Browse files Browse the repository at this point in the history
## Amino failed to parse crypto.PubKey

### Description

This simple client isn't working to get account because amino don't know
the struct `BaseAccount.Pubkey crypto.PubKey`

This simple main can be fixed by importing, i believe it will make dev
experience easier by putting it in `std` rather than clients

```
_ "github.com/gnolang/gno/tm2/pkg/crypto/secp256k1"
```

```go
package main

import (
	"fmt"

	"github.com/gnolang/gno/tm2/pkg/amino"
	rpcClient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
	"github.com/gnolang/gno/tm2/pkg/std"
)


// Client is the TM2 HTTP client
type Client struct {
	client rpcClient.Client
}

// NewClient creates a new TM2 HTTP client
func NewClient(remote string) *Client {
	return &Client{
		client: rpcClient.NewHTTP(remote, ""),
	}
}

func (c *Client) GetAccount(address string) (std.Account, error) {
	path := fmt.Sprintf("auth/accounts/%s", address)

	queryResponse, err := c.client.ABCIQuery(path, []byte{})
	if err != nil {
		return nil, fmt.Errorf("unable to execute ABCI query, %w", err)
	}

	var queryData struct{ BaseAccount std.BaseAccount }

	if err := amino.UnmarshalJSON(queryResponse.Response.Data, &queryData); err != nil {
		return nil, err
	}

	return &queryData.BaseAccount, nil
}

func main() {
	c := NewClient("http://rpc.gnochess.com:80")
	acc, err := c.GetAccount("g1x90eh5ejc22548hjqznm2egyvn8ny36lqu460f")
	fmt.Println(acc, err)
}
```

```
runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0xc020780390 stack=[0xc020780000, 0xc040780000]
fatal error: stack overflow

runtime stack:
runtime.throw({0x9842fa?, 0xd4dea0?})
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/runtime/panic.go:1047 +0x5d fp=0x7f681368cc18 sp=0x7f681368cbe8 pc=0x43907d
runtime.newstack()
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/runtime/stack.go:1103 +0x5cc fp=0x7f681368cdd0 sp=0x7f681368cc18 pc=0x452d0c
runtime.morestack()
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/runtime/asm_amd64.s:570 +0x8b fp=0x7f681368cdd8 sp=0x7f681368cdd0 pc=0x46a32b

goroutine 1 [running]:
fmt.(*fmt).padString(0xc00de1ff20?, {0x8961be, 0x7})
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/fmt/format.go:108 +0x299 fp=0xc0207803a0 sp=0xc020780398 pc=0x4dac39
fmt.(*fmt).fmtS(0xc0207803f8?, {0x8961be?, 0x8961bd?})
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/fmt/format.go:359 +0x3f fp=0xc0207803d8 sp=0xc0207803a0 pc=0x4db75f
fmt.(*pp).fmtString(0x8ca000?, {0x8961be?, 0x8ca000?}, 0x0?)
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/fmt/print.go:474 +0x86 fp=0xc020780428 sp=0xc0207803d8 pc=0x4de566
fmt.(*pp).handleMethods(0xc00de1fee0, 0x100800?)
        /nix/store/akhjsmrrsakcnj8x3xgygvizhccbyn0v-go-1.19.3/share/go/src/fmt/print.go:65

[....] It's a stack overflow due to recursion
```

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
albttx committed Dec 14, 2023
1 parent a338929 commit eef0c98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tm2/pkg/std/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/errors"

_ "github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
_ "github.com/gnolang/gno/tm2/pkg/crypto/mock"
_ "github.com/gnolang/gno/tm2/pkg/crypto/multisig"
_ "github.com/gnolang/gno/tm2/pkg/crypto/secp256k1"
)

// Account is an interface used to store coins at a given address within state.
Expand Down
26 changes: 26 additions & 0 deletions tm2/pkg/std/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package std_test

import (
"testing"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/require"
)

func TestAminoBaseAccount(t *testing.T) {
b := []byte(`{
"address": "g1x90eh5ejc22548hjqznm2egyvn8ny36lqu460f",
"coins": "4200000ugnot",
"public_key": {
"@type": "/tm.PubKeySecp256k1",
"value": "AwMzujfppqEi8lozMVD8ORENUR8SIE06VLNP8FGL0aQ2"
},
"account_number": "159",
"sequence": "33"
}`)
acc := std.BaseAccount{}

err := amino.UnmarshalJSON(b, &acc)
require.NoError(t, err)
}

0 comments on commit eef0c98

Please sign in to comment.