From eef0c980676b8c5909723698a39f0e3679ecfb40 Mon Sep 17 00:00:00 2001 From: Albert Le Batteux Date: Thu, 14 Dec 2023 16:25:40 +0000 Subject: [PATCH] chore: import secp256k1 for amino parsing (#1178) ## 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 ```
Contributors' checklist... - [ ] 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).
--- tm2/pkg/std/account.go | 5 +++++ tm2/pkg/std/package_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tm2/pkg/std/package_test.go diff --git a/tm2/pkg/std/account.go b/tm2/pkg/std/account.go index 604444b4046..c70f43d22e9 100644 --- a/tm2/pkg/std/account.go +++ b/tm2/pkg/std/account.go @@ -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. diff --git a/tm2/pkg/std/package_test.go b/tm2/pkg/std/package_test.go new file mode 100644 index 00000000000..0a21188737b --- /dev/null +++ b/tm2/pkg/std/package_test.go @@ -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) +}