diff --git a/tests/e2e/machine/rest.go b/tests/e2e/machine/rest.go new file mode 100644 index 0000000..27215ec --- /dev/null +++ b/tests/e2e/machine/rest.go @@ -0,0 +1,99 @@ +package machine + +import ( + "fmt" + "planetmint-go/testutil" + machinetypes "planetmint-go/x/machine/types" + + "github.com/cosmos/cosmos-sdk/codec/types" + txtypes "github.com/cosmos/cosmos-sdk/types/tx" + "github.com/cosmos/cosmos-sdk/types/tx/signing" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +func (s *E2ETestSuite) TestAttestMachineREST() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + // Query Sequence Number + k, err := val.ClientCtx.Keyring.Key("machine") + s.Require().NoError(err) + + addr, err := k.GetAddress() + s.Require().NoError(err) + + reqAccountInfo := fmt.Sprintf("%s/cosmos/auth/v1beta1/account_info/%s", baseURL, addr.String()) + respAccountInfo, err := testutil.GetRequest(reqAccountInfo) + s.Require().NoError(err) + + s.T().Log(string(respAccountInfo)) + + var resAccountInfo authtypes.QueryAccountInfoResponse + err = val.ClientCtx.Codec.UnmarshalJSON(respAccountInfo, &resAccountInfo) + s.Require().NoError(err) + + s.T().Log(resAccountInfo.Info.AccountNumber) + // Create Attest Machine TX + machine := machinetypes.Machine{ + Name: "machine", + Ticker: "machine_ticker", + Issued: 1, + Amount: 1000, + Precision: 8, + IssuerPlanetmint: pubKey, + IssuerLiquid: pubKey, + MachineId: pubKey, + Metadata: &machinetypes.Metadata{ + AdditionalDataCID: "CID", + Gps: "{\"Latitude\":\"-48.876667\",\"Longitude\":\"-123.393333\"}", + }, + } + // machineJSON, err := json.Marshal(&machine) + // s.Require().NoError(err) + + msg := machinetypes.MsgAttestMachine{ + Creator: string(addr), + Machine: &machine, + } + + msg.Type() + + // Encode TX + reqEncodeTx := fmt.Sprintf("%s/cosmos/tx/encode", baseURL) + + signerInfos := make([]txtypes.SignerInfo, 1) + signerInfos[1].PublicKey = &types.Any{ + TypeUrl: "/cosmos.crypto.secp256k1.PubKey", + Value: k.PubKey.Value, + } + signerInfos[1].ModeInfo = &txtypes.ModeInfo{ + Sum: &txtypes.ModeInfo_Single_{ + Single: &txtypes.ModeInfo_Single{ + Mode: signing.SignMode_SIGN_MODE_DIRECT, + }, + }, + } + signerInfos[1].Sequence = resAccountInfo.Info.Sequence + + reqEncodeTxBody := txtypes.TxEncodeRequest{ + Tx: &txtypes.Tx{ + Body: &txtypes.TxBody{ + // Messages: make([]*types.Any, 0), + }, + AuthInfo: &txtypes.AuthInfo{ + SignerInfos: signerInfos, + Tip: nil, + }, + Signatures: [][]byte{}, + }, + } + s.Require().NoError(err) + // var resEncodeTX TXEncodeRequest + respEncodeTx, err := testutil.PostRequest(reqEncodeTx, "application/json", []byte(reqEncodeTxBody)) + s.Require().NoError(err) + + s.T().Log(respEncodeTx) + // Send encoded TX to /cosmos/tx/v1beta1/txs + + // Query Machine by Pubkey +} diff --git a/testutil/rest.go b/testutil/rest.go new file mode 100644 index 0000000..180a7be --- /dev/null +++ b/testutil/rest.go @@ -0,0 +1,46 @@ +package testutil + +import ( + "bytes" + "fmt" + "io" + "net/http" +) + +// GetRequest defines a wrapper around an HTTP GET request with a provided URL. +// An error is returned if the request or reading the body fails. +func GetRequest(url string) ([]byte, error) { + res, err := http.Get(url) //nolint:gosec // only used for testing + if err != nil { + return nil, err + } + defer func() { + _ = res.Body.Close() + }() + + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, err + } + + return body, nil +} + +// PostRequest defines a wrapper around an HTTP POST request with a provided URL and data. +// An error is returned if the request or reading the body fails. +func PostRequest(url, contentType string, data []byte) ([]byte, error) { + res, err := http.Post(url, contentType, bytes.NewBuffer(data)) //nolint:gosec // only used for testing + if err != nil { + return nil, fmt.Errorf("error while sending post request: %w", err) + } + defer func() { + _ = res.Body.Close() + }() + + bz, err := io.ReadAll(res.Body) + if err != nil { + return nil, fmt.Errorf("error reading response body: %w", err) + } + + return bz, nil +}