Skip to content

Commit

Permalink
some construction endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Jan 20, 2025
1 parent 7541f9e commit 3d0bd6e
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func MakeCodec() (*codec.ProtoCodec, codectypes.InterfaceRegistry) {
})
cdc := codec.NewProtoCodec(ir)

sdk.RegisterInterfaces(ir)
authcodec.RegisterInterfaces(ir)
bankcodec.RegisterInterfaces(ir)
cryptocodec.RegisterInterfaces(ir)
Expand Down
84 changes: 84 additions & 0 deletions tests/systemtests/construction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//go:build system_test

package systemtests

import (
"encoding/base64"
"encoding/hex"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"

"cosmossdk.io/systemtests"

"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
)

func TestDerive(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

rosetta.restart(t)
rosettaRest := newRestClient(rosetta)

pubKey := secp256k1.GenPrivKey().PubKey()
addr, err := address.NewBech32Codec("cosmos").BytesToString(pubKey.Address().Bytes())
assert.NoError(t, err)

hexPk := strings.Split(pubKey.String(), "{")[1]
res, err := rosettaRest.constructionDerive(hexPk[:len(hexPk)-1])
assert.NoError(t, err)
assert.Equal(t, addr, gjson.GetBytes(res, "address").String())
}

func TestHash(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

cli := systemtests.NewCLIWrapper(t, sut, verbose)
fromAddr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", fromAddr, "10000000stake"},
)
toAddr := cli.AddKey("account2")

rosetta.restart(t)
rosettaRest := newRestClient(rosetta)

rsp := cli.RunCommandWithArgs("tx", "bank", "send", fromAddr, toAddr, "10stake", "--generate-only")
tempFile := systemtests.StoreTempFile(t, []byte(rsp))
rsp = cli.RunCommandWithArgs(cli.WithTXFlags("tx", "sign", tempFile.Name(), "--from", fromAddr)...)
tempFile = systemtests.StoreTempFile(t, []byte(rsp))
rsp = cli.RunCommandWithArgs("tx", "encode", tempFile.Name())

txBytes, err := base64.StdEncoding.DecodeString(rsp)
assert.NoError(t, err)
hexTx := hex.EncodeToString(txBytes)

res, err := rosettaRest.constructionHash(hexTx)
assert.NoError(t, err)
assert.NotEmpty(t, gjson.GetBytes(res, "transaction_identifier.hash"))
}

func TestMetadata(t *testing.T) {
sut.ResetChain(t)
sut.StartChain(t)

rosetta.restart(t)
rosettaRest := newRestClient(rosetta)

pubKey := secp256k1.GenPrivKey().PubKey()
hexPk := strings.Split(pubKey.String(), "{")[1]

metadata := make(map[string]interface{})
metadata["gas_price"] = `"123uatom"`
metadata["gas_limit"] = 423

res, err := rosettaRest.constructionMetadata(hexPk, metadata)
assert.NoError(t, err)
assert.Equal(t, gjson.GetBytes(res, "metadata.gas_price").String(), "123uatom")
assert.Greater(t, gjson.GetBytes(res, "suggested_fee.0.value").Int(), int64(0))
}
47 changes: 44 additions & 3 deletions tests/systemtests/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ func (c *restClient) mempool() ([]byte, error) {
}

func (c *restClient) accountBalance(address string, opts ...func() string) ([]byte, error) {
accountIdentifier := fmt.Sprintf(`"account_identifier":{"address": "%s"}`, address)

bodyOpts := make([]string, len(opts))
for i, opt := range opts {
bodyOpts[i] = opt()
}

args := []string{c.networkIdentifier, accountIdentifier}
args := []string{c.networkIdentifier, fmt.Sprintf(`"account_identifier":{"address": "%s"}`, address)}
args = append(args, bodyOpts...)
jsonBody := buildBody(args...)

Expand All @@ -115,3 +113,46 @@ func withBlockIdentifier(height string) func() string {
func buildBody(args ...string) string {
return fmt.Sprintf(`{%s}`, strings.Join(args, ","))
}

func (c *restClient) constructionDerive(hexPk string) ([]byte, error) {
res, err := c.client.R().SetBody(
fmt.Sprintf(`{%s, %s}`, c.networkIdentifier, publicKeyBody(hexPk, "secp256k1")),
).Post("/construction/derive")
if err != nil {
return nil, err
}

return res.Body(), nil
}

func publicKeyBody(hexPk, keyType string) string {
return fmt.Sprintf(`"public_key":{"hex_bytes":"%s", "curve_type":"%s"}`, hexPk, keyType)
}

func (c *restClient) constructionHash(hexTx string) ([]byte, error) {
res, err := c.client.R().SetBody(
fmt.Sprintf(`{%s, "signed_transaction": "%s"}`, c.networkIdentifier, hexTx),
).Post("/construction/hash")
if err != nil {
return nil, err
}

return res.Body(), nil
}

func (c *restClient) constructionMetadata(hexPk string, options map[string]interface{}) ([]byte, error) {
optParts := make([]string, 0, len(options))
for k, v := range options {
optParts = append(optParts, fmt.Sprintf(`"%s":%v`, k, v))
}
optionsStr := strings.Join(optParts, ",")
body := fmt.Sprintf(`{%s, "options":{%s}, %s}`, c.networkIdentifier, optionsStr, publicKeyBody(hexPk, "secp256k1"))
res, err := c.client.R().SetBody(
body,
).Post("/construction/metadata")
if err != nil {
return nil, err
}

return res.Body(), nil
}

0 comments on commit 3d0bd6e

Please sign in to comment.