From 8cf85630452acf7deb9820e872e86ae6f094985a Mon Sep 17 00:00:00 2001 From: Fabio Oshiro Date: Thu, 20 Mar 2025 17:54:46 -0300 Subject: [PATCH] refactor: add /trasaction/nonce and /transaction/submit endpoints --- internal/paio/generated.go | 343 ++++++++++++++--------------------- internal/paio/oapi-paio.yaml | 71 ++------ internal/paio/paio.go | 199 +------------------- 3 files changed, 169 insertions(+), 444 deletions(-) diff --git a/internal/paio/generated.go b/internal/paio/generated.go index 821d2ac1..859851b6 100644 --- a/internal/paio/generated.go +++ b/internal/paio/generated.go @@ -71,9 +71,6 @@ type Cartesi712_TypedData_Message_Nonce struct { union json.RawMessage } -// Error Detailed error message. -type Error = string - // GetNonce defines model for GetNonce. type GetNonce struct { // AppContract App contract address @@ -89,24 +86,6 @@ type NonceResponse struct { Nonce *int `json:"nonce,omitempty"` } -// PaioTransaction defines model for PaioTransaction. -type PaioTransaction struct { - Signature string `json:"signature"` - - // TypedData Base 64 - TypedData string `json:"typedData"` -} - -// SaveTransactionRequest defines model for SaveTransactionRequest. -type SaveTransactionRequest struct { - // Message ABI encoded - Message string `json:"message"` - - // MsgSender (optional) sender address to check the signature - MsgSender *string `json:"msg_sender,omitempty"` - Signature string `json:"signature"` -} - // TransactionError defines model for TransactionError. type TransactionError struct { // Message Detailed error message @@ -119,18 +98,15 @@ type TransactionResponse struct { Id *string `json:"id,omitempty"` } +// SendCartesiTransactionDeprecatedJSONRequestBody defines body for SendCartesiTransactionDeprecated for application/json ContentType. +type SendCartesiTransactionDeprecatedJSONRequestBody = Cartesi712 + // GetNonceJSONRequestBody defines body for GetNonce for application/json ContentType. type GetNonceJSONRequestBody = GetNonce // SendCartesiTransactionJSONRequestBody defines body for SendCartesiTransaction for application/json ContentType. type SendCartesiTransactionJSONRequestBody = Cartesi712 -// SaveTransactionJSONRequestBody defines body for SaveTransaction for application/json ContentType. -type SaveTransactionJSONRequestBody = SaveTransactionRequest - -// SendTransactionJSONRequestBody defines body for SendTransaction for application/json ContentType. -type SendTransactionJSONRequestBody = PaioTransaction - // AsCartesi712TypedDataMessageMaxGasPrice0 returns the union data inside the Cartesi712_TypedData_Message_MaxGasPrice as a Cartesi712TypedDataMessageMaxGasPrice0 func (t Cartesi712_TypedData_Message_MaxGasPrice) AsCartesi712TypedDataMessageMaxGasPrice0() (Cartesi712TypedDataMessageMaxGasPrice0, error) { var body Cartesi712TypedDataMessageMaxGasPrice0 @@ -328,6 +304,14 @@ func WithRequestEditorFn(fn RequestEditorFn) ClientOption { // The interface specification for the client above. type ClientInterface interface { + // GetNonceDeprecated request + GetNonceDeprecated(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) + + // SendCartesiTransactionDeprecatedWithBody request with any body + SendCartesiTransactionDeprecatedWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + + SendCartesiTransactionDeprecated(ctx context.Context, body SendCartesiTransactionDeprecatedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) + // GetNonceWithBody request with any body GetNonceWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -337,32 +321,10 @@ type ClientInterface interface { SendCartesiTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) SendCartesiTransaction(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) - - // SaveTransactionWithBody request with any body - SaveTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) - - SaveTransaction(ctx context.Context, body SaveTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) - - // SendTransactionWithBody request with any body - SendTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) - - SendTransaction(ctx context.Context, body SendTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) -} - -func (c *Client) GetNonceWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewGetNonceRequestWithBody(c.Server, contentType, body) - if err != nil { - return nil, err - } - req = req.WithContext(ctx) - if err := c.applyEditors(ctx, req, reqEditors); err != nil { - return nil, err - } - return c.Client.Do(req) } -func (c *Client) GetNonce(ctx context.Context, body GetNonceJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewGetNonceRequest(c.Server, body) +func (c *Client) GetNonceDeprecated(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetNonceDeprecatedRequest(c.Server) if err != nil { return nil, err } @@ -373,8 +335,8 @@ func (c *Client) GetNonce(ctx context.Context, body GetNonceJSONRequestBody, req return c.Client.Do(req) } -func (c *Client) SendCartesiTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSendCartesiTransactionRequestWithBody(c.Server, contentType, body) +func (c *Client) SendCartesiTransactionDeprecatedWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewSendCartesiTransactionDeprecatedRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } @@ -385,8 +347,8 @@ func (c *Client) SendCartesiTransactionWithBody(ctx context.Context, contentType return c.Client.Do(req) } -func (c *Client) SendCartesiTransaction(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSendCartesiTransactionRequest(c.Server, body) +func (c *Client) SendCartesiTransactionDeprecated(ctx context.Context, body SendCartesiTransactionDeprecatedJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewSendCartesiTransactionDeprecatedRequest(c.Server, body) if err != nil { return nil, err } @@ -397,8 +359,8 @@ func (c *Client) SendCartesiTransaction(ctx context.Context, body SendCartesiTra return c.Client.Do(req) } -func (c *Client) SaveTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSaveTransactionRequestWithBody(c.Server, contentType, body) +func (c *Client) GetNonceWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetNonceRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } @@ -409,8 +371,8 @@ func (c *Client) SaveTransactionWithBody(ctx context.Context, contentType string return c.Client.Do(req) } -func (c *Client) SaveTransaction(ctx context.Context, body SaveTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSaveTransactionRequest(c.Server, body) +func (c *Client) GetNonce(ctx context.Context, body GetNonceJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetNonceRequest(c.Server, body) if err != nil { return nil, err } @@ -421,8 +383,8 @@ func (c *Client) SaveTransaction(ctx context.Context, body SaveTransactionJSONRe return c.Client.Do(req) } -func (c *Client) SendTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSendTransactionRequestWithBody(c.Server, contentType, body) +func (c *Client) SendCartesiTransactionWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewSendCartesiTransactionRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } @@ -433,8 +395,8 @@ func (c *Client) SendTransactionWithBody(ctx context.Context, contentType string return c.Client.Do(req) } -func (c *Client) SendTransaction(ctx context.Context, body SendTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewSendTransactionRequest(c.Server, body) +func (c *Client) SendCartesiTransaction(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewSendCartesiTransactionRequest(c.Server, body) if err != nil { return nil, err } @@ -445,19 +407,8 @@ func (c *Client) SendTransaction(ctx context.Context, body SendTransactionJSONRe return c.Client.Do(req) } -// NewGetNonceRequest calls the generic GetNonce builder with application/json body -func NewGetNonceRequest(server string, body GetNonceJSONRequestBody) (*http.Request, error) { - var bodyReader io.Reader - buf, err := json.Marshal(body) - if err != nil { - return nil, err - } - bodyReader = bytes.NewReader(buf) - return NewGetNonceRequestWithBody(server, "application/json", bodyReader) -} - -// NewGetNonceRequestWithBody generates requests for GetNonce with any type of body -func NewGetNonceRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewGetNonceDeprecatedRequest generates requests for GetNonceDeprecated +func NewGetNonceDeprecatedRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -475,29 +426,27 @@ func NewGetNonceRequestWithBody(server string, contentType string, body io.Reade return nil, err } - req, err := http.NewRequest("POST", queryURL.String(), body) + req, err := http.NewRequest("POST", queryURL.String(), nil) if err != nil { return nil, err } - req.Header.Add("Content-Type", contentType) - return req, nil } -// NewSendCartesiTransactionRequest calls the generic SendCartesiTransaction builder with application/json body -func NewSendCartesiTransactionRequest(server string, body SendCartesiTransactionJSONRequestBody) (*http.Request, error) { +// NewSendCartesiTransactionDeprecatedRequest calls the generic SendCartesiTransactionDeprecated builder with application/json body +func NewSendCartesiTransactionDeprecatedRequest(server string, body SendCartesiTransactionDeprecatedJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) - return NewSendCartesiTransactionRequestWithBody(server, "application/json", bodyReader) + return NewSendCartesiTransactionDeprecatedRequestWithBody(server, "application/json", bodyReader) } -// NewSendCartesiTransactionRequestWithBody generates requests for SendCartesiTransaction with any type of body -func NewSendCartesiTransactionRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewSendCartesiTransactionDeprecatedRequestWithBody generates requests for SendCartesiTransactionDeprecated with any type of body +func NewSendCartesiTransactionDeprecatedRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -525,19 +474,19 @@ func NewSendCartesiTransactionRequestWithBody(server string, contentType string, return req, nil } -// NewSaveTransactionRequest calls the generic SaveTransaction builder with application/json body -func NewSaveTransactionRequest(server string, body SaveTransactionJSONRequestBody) (*http.Request, error) { +// NewGetNonceRequest calls the generic GetNonce builder with application/json body +func NewGetNonceRequest(server string, body GetNonceJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) - return NewSaveTransactionRequestWithBody(server, "application/json", bodyReader) + return NewGetNonceRequestWithBody(server, "application/json", bodyReader) } -// NewSaveTransactionRequestWithBody generates requests for SaveTransaction with any type of body -func NewSaveTransactionRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewGetNonceRequestWithBody generates requests for GetNonce with any type of body +func NewGetNonceRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -545,7 +494,7 @@ func NewSaveTransactionRequestWithBody(server string, contentType string, body i return nil, err } - operationPath := fmt.Sprintf("/transaction") + operationPath := fmt.Sprintf("/transaction/nonce") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -565,19 +514,19 @@ func NewSaveTransactionRequestWithBody(server string, contentType string, body i return req, nil } -// NewSendTransactionRequest calls the generic SendTransaction builder with application/json body -func NewSendTransactionRequest(server string, body SendTransactionJSONRequestBody) (*http.Request, error) { +// NewSendCartesiTransactionRequest calls the generic SendCartesiTransaction builder with application/json body +func NewSendCartesiTransactionRequest(server string, body SendCartesiTransactionJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) - return NewSendTransactionRequestWithBody(server, "application/json", bodyReader) + return NewSendCartesiTransactionRequestWithBody(server, "application/json", bodyReader) } -// NewSendTransactionRequestWithBody generates requests for SendTransaction with any type of body -func NewSendTransactionRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewSendCartesiTransactionRequestWithBody generates requests for SendCartesiTransaction with any type of body +func NewSendCartesiTransactionRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -585,7 +534,7 @@ func NewSendTransactionRequestWithBody(server string, contentType string, body i return nil, err } - operationPath := fmt.Sprintf("/transactions") + operationPath := fmt.Sprintf("/transaction/submit") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -648,6 +597,14 @@ func WithBaseURL(baseURL string) ClientOption { // ClientWithResponsesInterface is the interface specification for the client with responses above. type ClientWithResponsesInterface interface { + // GetNonceDeprecatedWithResponse request + GetNonceDeprecatedWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetNonceDeprecatedResponse, error) + + // SendCartesiTransactionDeprecatedWithBodyWithResponse request with any body + SendCartesiTransactionDeprecatedWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendCartesiTransactionDeprecatedResponse, error) + + SendCartesiTransactionDeprecatedWithResponse(ctx context.Context, body SendCartesiTransactionDeprecatedJSONRequestBody, reqEditors ...RequestEditorFn) (*SendCartesiTransactionDeprecatedResponse, error) + // GetNonceWithBodyWithResponse request with any body GetNonceWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*GetNonceResponse, error) @@ -657,26 +614,16 @@ type ClientWithResponsesInterface interface { SendCartesiTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) SendCartesiTransactionWithResponse(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) - - // SaveTransactionWithBodyWithResponse request with any body - SaveTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SaveTransactionResponse, error) - - SaveTransactionWithResponse(ctx context.Context, body SaveTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SaveTransactionResponse, error) - - // SendTransactionWithBodyWithResponse request with any body - SendTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendTransactionResponse, error) - - SendTransactionWithResponse(ctx context.Context, body SendTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SendTransactionResponse, error) } -type GetNonceResponse struct { +type GetNonceDeprecatedResponse struct { Body []byte HTTPResponse *http.Response JSON200 *NonceResponse } // Status returns HTTPResponse.Status -func (r GetNonceResponse) Status() string { +func (r GetNonceDeprecatedResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -684,14 +631,14 @@ func (r GetNonceResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r GetNonceResponse) StatusCode() int { +func (r GetNonceDeprecatedResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -type SendCartesiTransactionResponse struct { +type SendCartesiTransactionDeprecatedResponse struct { Body []byte HTTPResponse *http.Response JSON201 *TransactionResponse @@ -699,7 +646,7 @@ type SendCartesiTransactionResponse struct { } // Status returns HTTPResponse.Status -func (r SendCartesiTransactionResponse) Status() string { +func (r SendCartesiTransactionDeprecatedResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -707,22 +654,21 @@ func (r SendCartesiTransactionResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r SendCartesiTransactionResponse) StatusCode() int { +func (r SendCartesiTransactionDeprecatedResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -type SaveTransactionResponse struct { +type GetNonceResponse struct { Body []byte HTTPResponse *http.Response - JSON201 *TransactionResponse - JSON400 *TransactionError + JSON200 *NonceResponse } // Status returns HTTPResponse.Status -func (r SaveTransactionResponse) Status() string { +func (r GetNonceResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -730,21 +676,22 @@ func (r SaveTransactionResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r SaveTransactionResponse) StatusCode() int { +func (r GetNonceResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -type SendTransactionResponse struct { +type SendCartesiTransactionResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *string + JSON201 *TransactionResponse + JSON400 *TransactionError } // Status returns HTTPResponse.Status -func (r SendTransactionResponse) Status() string { +func (r SendCartesiTransactionResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -752,90 +699,82 @@ func (r SendTransactionResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r SendTransactionResponse) StatusCode() int { +func (r SendCartesiTransactionResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } -// GetNonceWithBodyWithResponse request with arbitrary body returning *GetNonceResponse -func (c *ClientWithResponses) GetNonceWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*GetNonceResponse, error) { - rsp, err := c.GetNonceWithBody(ctx, contentType, body, reqEditors...) +// GetNonceDeprecatedWithResponse request returning *GetNonceDeprecatedResponse +func (c *ClientWithResponses) GetNonceDeprecatedWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetNonceDeprecatedResponse, error) { + rsp, err := c.GetNonceDeprecated(ctx, reqEditors...) if err != nil { return nil, err } - return ParseGetNonceResponse(rsp) + return ParseGetNonceDeprecatedResponse(rsp) } -func (c *ClientWithResponses) GetNonceWithResponse(ctx context.Context, body GetNonceJSONRequestBody, reqEditors ...RequestEditorFn) (*GetNonceResponse, error) { - rsp, err := c.GetNonce(ctx, body, reqEditors...) +// SendCartesiTransactionDeprecatedWithBodyWithResponse request with arbitrary body returning *SendCartesiTransactionDeprecatedResponse +func (c *ClientWithResponses) SendCartesiTransactionDeprecatedWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendCartesiTransactionDeprecatedResponse, error) { + rsp, err := c.SendCartesiTransactionDeprecatedWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } - return ParseGetNonceResponse(rsp) + return ParseSendCartesiTransactionDeprecatedResponse(rsp) } -// SendCartesiTransactionWithBodyWithResponse request with arbitrary body returning *SendCartesiTransactionResponse -func (c *ClientWithResponses) SendCartesiTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) { - rsp, err := c.SendCartesiTransactionWithBody(ctx, contentType, body, reqEditors...) - if err != nil { - return nil, err - } - return ParseSendCartesiTransactionResponse(rsp) -} - -func (c *ClientWithResponses) SendCartesiTransactionWithResponse(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) { - rsp, err := c.SendCartesiTransaction(ctx, body, reqEditors...) +func (c *ClientWithResponses) SendCartesiTransactionDeprecatedWithResponse(ctx context.Context, body SendCartesiTransactionDeprecatedJSONRequestBody, reqEditors ...RequestEditorFn) (*SendCartesiTransactionDeprecatedResponse, error) { + rsp, err := c.SendCartesiTransactionDeprecated(ctx, body, reqEditors...) if err != nil { return nil, err } - return ParseSendCartesiTransactionResponse(rsp) + return ParseSendCartesiTransactionDeprecatedResponse(rsp) } -// SaveTransactionWithBodyWithResponse request with arbitrary body returning *SaveTransactionResponse -func (c *ClientWithResponses) SaveTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SaveTransactionResponse, error) { - rsp, err := c.SaveTransactionWithBody(ctx, contentType, body, reqEditors...) +// GetNonceWithBodyWithResponse request with arbitrary body returning *GetNonceResponse +func (c *ClientWithResponses) GetNonceWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*GetNonceResponse, error) { + rsp, err := c.GetNonceWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } - return ParseSaveTransactionResponse(rsp) + return ParseGetNonceResponse(rsp) } -func (c *ClientWithResponses) SaveTransactionWithResponse(ctx context.Context, body SaveTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SaveTransactionResponse, error) { - rsp, err := c.SaveTransaction(ctx, body, reqEditors...) +func (c *ClientWithResponses) GetNonceWithResponse(ctx context.Context, body GetNonceJSONRequestBody, reqEditors ...RequestEditorFn) (*GetNonceResponse, error) { + rsp, err := c.GetNonce(ctx, body, reqEditors...) if err != nil { return nil, err } - return ParseSaveTransactionResponse(rsp) + return ParseGetNonceResponse(rsp) } -// SendTransactionWithBodyWithResponse request with arbitrary body returning *SendTransactionResponse -func (c *ClientWithResponses) SendTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendTransactionResponse, error) { - rsp, err := c.SendTransactionWithBody(ctx, contentType, body, reqEditors...) +// SendCartesiTransactionWithBodyWithResponse request with arbitrary body returning *SendCartesiTransactionResponse +func (c *ClientWithResponses) SendCartesiTransactionWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) { + rsp, err := c.SendCartesiTransactionWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } - return ParseSendTransactionResponse(rsp) + return ParseSendCartesiTransactionResponse(rsp) } -func (c *ClientWithResponses) SendTransactionWithResponse(ctx context.Context, body SendTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SendTransactionResponse, error) { - rsp, err := c.SendTransaction(ctx, body, reqEditors...) +func (c *ClientWithResponses) SendCartesiTransactionWithResponse(ctx context.Context, body SendCartesiTransactionJSONRequestBody, reqEditors ...RequestEditorFn) (*SendCartesiTransactionResponse, error) { + rsp, err := c.SendCartesiTransaction(ctx, body, reqEditors...) if err != nil { return nil, err } - return ParseSendTransactionResponse(rsp) + return ParseSendCartesiTransactionResponse(rsp) } -// ParseGetNonceResponse parses an HTTP response from a GetNonceWithResponse call -func ParseGetNonceResponse(rsp *http.Response) (*GetNonceResponse, error) { +// ParseGetNonceDeprecatedResponse parses an HTTP response from a GetNonceDeprecatedWithResponse call +func ParseGetNonceDeprecatedResponse(rsp *http.Response) (*GetNonceDeprecatedResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &GetNonceResponse{ + response := &GetNonceDeprecatedResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -853,15 +792,15 @@ func ParseGetNonceResponse(rsp *http.Response) (*GetNonceResponse, error) { return response, nil } -// ParseSendCartesiTransactionResponse parses an HTTP response from a SendCartesiTransactionWithResponse call -func ParseSendCartesiTransactionResponse(rsp *http.Response) (*SendCartesiTransactionResponse, error) { +// ParseSendCartesiTransactionDeprecatedResponse parses an HTTP response from a SendCartesiTransactionDeprecatedWithResponse call +func ParseSendCartesiTransactionDeprecatedResponse(rsp *http.Response) (*SendCartesiTransactionDeprecatedResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &SendCartesiTransactionResponse{ + response := &SendCartesiTransactionDeprecatedResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -886,59 +825,59 @@ func ParseSendCartesiTransactionResponse(rsp *http.Response) (*SendCartesiTransa return response, nil } -// ParseSaveTransactionResponse parses an HTTP response from a SaveTransactionWithResponse call -func ParseSaveTransactionResponse(rsp *http.Response) (*SaveTransactionResponse, error) { +// ParseGetNonceResponse parses an HTTP response from a GetNonceWithResponse call +func ParseGetNonceResponse(rsp *http.Response) (*GetNonceResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &SaveTransactionResponse{ + response := &GetNonceResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { - case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 201: - var dest TransactionResponse - if err := json.Unmarshal(bodyBytes, &dest); err != nil { - return nil, err - } - response.JSON201 = &dest - - case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 400: - var dest TransactionError + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest NonceResponse if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } - response.JSON400 = &dest + response.JSON200 = &dest } return response, nil } -// ParseSendTransactionResponse parses an HTTP response from a SendTransactionWithResponse call -func ParseSendTransactionResponse(rsp *http.Response) (*SendTransactionResponse, error) { +// ParseSendCartesiTransactionResponse parses an HTTP response from a SendCartesiTransactionWithResponse call +func ParseSendCartesiTransactionResponse(rsp *http.Response) (*SendCartesiTransactionResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &SendTransactionResponse{ + response := &SendCartesiTransactionResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { - case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest string + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 201: + var dest TransactionResponse if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } - response.JSON200 = &dest + response.JSON201 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 400: + var dest TransactionError + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON400 = &dest } @@ -949,16 +888,16 @@ func ParseSendTransactionResponse(rsp *http.Response) (*SendTransactionResponse, type ServerInterface interface { // Get Nonce // (POST /nonce) - GetNonce(ctx echo.Context) error + GetNonceDeprecated(ctx echo.Context) error // (POST /submit) + SendCartesiTransactionDeprecated(ctx echo.Context) error + // Get Nonce + // (POST /transaction/nonce) + GetNonce(ctx echo.Context) error + + // (POST /transaction/submit) SendCartesiTransaction(ctx echo.Context) error - // Save transaction - // (POST /transaction) - SaveTransaction(ctx echo.Context) error - // Send Paio's transaction - // (POST /transactions) - SendTransaction(ctx echo.Context) error } // ServerInterfaceWrapper converts echo contexts to parameters. @@ -966,39 +905,39 @@ type ServerInterfaceWrapper struct { Handler ServerInterface } -// GetNonce converts echo context to params. -func (w *ServerInterfaceWrapper) GetNonce(ctx echo.Context) error { +// GetNonceDeprecated converts echo context to params. +func (w *ServerInterfaceWrapper) GetNonceDeprecated(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments - err = w.Handler.GetNonce(ctx) + err = w.Handler.GetNonceDeprecated(ctx) return err } -// SendCartesiTransaction converts echo context to params. -func (w *ServerInterfaceWrapper) SendCartesiTransaction(ctx echo.Context) error { +// SendCartesiTransactionDeprecated converts echo context to params. +func (w *ServerInterfaceWrapper) SendCartesiTransactionDeprecated(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments - err = w.Handler.SendCartesiTransaction(ctx) + err = w.Handler.SendCartesiTransactionDeprecated(ctx) return err } -// SaveTransaction converts echo context to params. -func (w *ServerInterfaceWrapper) SaveTransaction(ctx echo.Context) error { +// GetNonce converts echo context to params. +func (w *ServerInterfaceWrapper) GetNonce(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments - err = w.Handler.SaveTransaction(ctx) + err = w.Handler.GetNonce(ctx) return err } -// SendTransaction converts echo context to params. -func (w *ServerInterfaceWrapper) SendTransaction(ctx echo.Context) error { +// SendCartesiTransaction converts echo context to params. +func (w *ServerInterfaceWrapper) SendCartesiTransaction(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments - err = w.Handler.SendTransaction(ctx) + err = w.Handler.SendCartesiTransaction(ctx) return err } @@ -1030,9 +969,9 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL Handler: si, } - router.POST(baseURL+"/nonce", wrapper.GetNonce) - router.POST(baseURL+"/submit", wrapper.SendCartesiTransaction) - router.POST(baseURL+"/transaction", wrapper.SaveTransaction) - router.POST(baseURL+"/transactions", wrapper.SendTransaction) + router.POST(baseURL+"/nonce", wrapper.GetNonceDeprecated) + router.POST(baseURL+"/submit", wrapper.SendCartesiTransactionDeprecated) + router.POST(baseURL+"/transaction/nonce", wrapper.GetNonce) + router.POST(baseURL+"/transaction/submit", wrapper.SendCartesiTransaction) } diff --git a/internal/paio/oapi-paio.yaml b/internal/paio/oapi-paio.yaml index b6cc92ca..fa658030 100644 --- a/internal/paio/oapi-paio.yaml +++ b/internal/paio/oapi-paio.yaml @@ -13,7 +13,7 @@ info: paths: /submit: post: - operationId: sendCartesiTransaction + operationId: sendCartesiTransactionDeprecated requestBody: content: application/json: @@ -33,20 +33,15 @@ paths: application/json: schema: $ref: "#/components/schemas/TransactionError" - /transaction: + /transaction/submit: post: - operationId: saveTransaction - summary: Save transaction - description: | - A good description - + operationId: sendCartesiTransaction requestBody: content: application/json: schema: - $ref: "#/components/schemas/SaveTransactionRequest" + $ref: "#/components/schemas/Cartesi712" required: true - responses: "201": description: Transaction created @@ -60,16 +55,9 @@ paths: application/json: schema: $ref: "#/components/schemas/TransactionError" - - default: - description: Error response. - content: - text/plain: - schema: - $ref: "#/components/schemas/Error" /nonce: post: - operationId: getNonce + operationId: getNonceDeprecated summary: Get Nonce desctiption: A good description @@ -82,50 +70,27 @@ paths: schema: $ref: "#/components/schemas/NonceResponse" - requestBody: - content: - application/json: - schema: - $ref: "#/components/schemas/GetNonce" - required: true - - /transactions: + /transaction/nonce: post: - operationId: sendTransaction - summary: Send Paio's transaction - description: | + operationId: getNonce + summary: Get Nonce + desctiption: A good description - - requestBody: - content: - application/json: - schema: - $ref: "#/components/schemas/PaioTransaction" - required: true - + responses: "200": - description: PaioTransaction accepted + description: Nonce content: application/json: schema: - type: string - example: "no rollup request available" - - "202": - description: Finish accepted but try again to obtain the next request. - content: - text/plain: - schema: - type: string - example: "no rollup request available" + $ref: "#/components/schemas/NonceResponse" - default: - description: Error response. - content: - text/plain: - schema: - $ref: "#/components/schemas/Error" + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/GetNonce" + required: true components: schemas: diff --git a/internal/paio/paio.go b/internal/paio/paio.go index 2a1b0e2f..64bf296f 100644 --- a/internal/paio/paio.go +++ b/internal/paio/paio.go @@ -10,15 +10,12 @@ import ( "log/slog" "math/big" "net/http" - "strings" "time" "github.com/calindra/nonodo/internal/sequencers/avail" - "github.com/calindra/nonodo/internal/sequencers/paiodecoder" "github.com/cartesi/rollups-graphql/pkg/commons" "github.com/cartesi/rollups-graphql/pkg/convenience/model" "github.com/cartesi/rollups-graphql/pkg/convenience/repository" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" @@ -45,21 +42,16 @@ type PaioAPI struct { paioNonceUrl string } -func (p *PaioAPI) getChainID(ctx context.Context) (*big.Int, error) { - if p.chainID != nil { - return p.chainID, nil - } - client, err := ethclient.DialContext(ctx, p.EvmRpcUrl) - if err != nil { - return nil, fmt.Errorf("ethclient dial error: %w", err) - } - defer client.Close() - chainId, err := client.ChainID(ctx) - if err != nil { - return nil, fmt.Errorf("ethclient chainId error: %w", err) - } - slog.Info("Using", "chainId", chainId.Uint64()) - return chainId, nil +// GetNonceDeprecated implements ServerInterface. +func (p *PaioAPI) GetNonceDeprecated(ctx echo.Context) error { + slog.Warn("Deprecated endpoint, please use /transaction/nonce instead") + return p.GetNonce(ctx) +} + +// SendCartesiTransactionDeprecated implements ServerInterface. +func (p *PaioAPI) SendCartesiTransactionDeprecated(ctx echo.Context) error { + slog.Warn("Deprecated endpoint, please use /transaction/submit instead") + return p.SendCartesiTransaction(ctx) } func (p *PaioAPI) getBlockNumber(ctx context.Context) (uint64, error) { @@ -75,33 +67,6 @@ func (p *PaioAPI) getBlockNumber(ctx context.Context) (uint64, error) { return blockNumber, nil } -// SendTransaction implements ServerInterface. -func (p *PaioAPI) SendTransaction(ctx echo.Context) error { - var request SendTransactionJSONRequestBody - stdCtx, cancel := context.WithCancel(ctx.Request().Context()) - defer cancel() - if err := ctx.Bind(&request); err != nil { - return err - } - slog.Debug("Sending Avail transaction", "request", request) - sigAndData := commons.SigAndData{ - Signature: request.Signature, - TypedData: request.TypedData, - } - jsonPayload, err := json.Marshal(sigAndData) - if err != nil { - slog.Error("Error json.Marshal message:", "err", err) - return err - } - hash, err := p.availClient.DefaultSubmit(stdCtx, string(jsonPayload)) - if err != nil { - slog.Error("Error DefaultSubmit message:", "err", err) - return err - } - _ = ctx.String(http.StatusOK, hash.Hex()) - return nil -} - func (p *PaioAPI) getNonceFromPaio(user common.Address, app common.Address) (*NonceResponse, error) { url := p.paioNonceUrl payload := map[string]string{ @@ -173,150 +138,6 @@ func (p *PaioAPI) GetNonce(ctx echo.Context) error { return ctx.JSON(http.StatusOK, response) } -func (p *PaioAPI) SaveTransaction(ctx echo.Context) error { - var request SaveTransactionJSONRequestBody - stdCtx, cancel := context.WithCancel(ctx.Request().Context()) - defer cancel() - if err := ctx.Bind(&request); err != nil { - return err - } - - if request.Signature == "" { - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "signature is required"}) - } - - if request.Message == "" { - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "message is required"}) - } - - // decode the ABI from message - // https://github.com/fabiooshiro/frontend-web-cartesi/blob/16913e945ef687bd07b6c3900d63cb23d69390b1/src/Input.tsx#L195C13-L212C15 - decoder, err := abi.JSON(strings.NewReader(DEFINITION)) - if err != nil { - slog.Error("error decoding ABI:", "err", err) - return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": "avail: error decoding ABI"}) - } - method, ok := decoder.Methods["signingMessage"] - if !ok { - slog.Error("error getting method signingMessage", "err", err) - return ctx.JSON(http.StatusInternalServerError, echo.Map{"error": "avail: error getting method signingMessage"}) - } - - // decode the message, message don't have 4 bytes of method id - message := common.Hex2Bytes(strings.TrimPrefix(request.Message, "0x")) - data := make(map[string]any) - err = method.Inputs.UnpackIntoMap(data, message) - if err != nil { - slog.Error("error unpacking message", "err", err) - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "avail: error unpacking message"}) - } - - // Validate the data from the message - app, ok := data["app"].(common.Address) - if !ok { - slog.Error("error extracting app from message", "err", err) - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "avail: error extracting app from message"}) - } - nonce, ok := data["nonce"].(uint64) - if !ok { - slog.Error("error extracting nonce from message", "err", err) - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "avail: error extracting nonce from message"}) - } - maxGasPrice, ok := data["max_gas_price"].(*big.Int) - if !ok { - slog.Error("error extracting max_gas_price from message", "err", err) - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "avail: error extracting max_gas_price from message"}) - } - dataBytes, ok := data["data"].([]byte) - if !ok { - slog.Error("error extracting data from message", "err", err) - return ctx.JSON(http.StatusBadRequest, echo.Map{"error": "avail: error extracting data from message"}) - } - - chainId, err := p.getChainID(stdCtx) - if err != nil { - return fmt.Errorf("ethclient dial error: %w", err) - } - - // fill the typedData - // https://github.com/fabiooshiro/frontend-web-cartesi/blob/16913e945ef687bd07b6c3900d63cb23d69390b1/src/Input.tsx#L65 - typedData := paiodecoder.CreateTypedData( - app, nonce, maxGasPrice, dataBytes, chainId, - ) - - typeJSON, err := json.Marshal(typedData) - if err != nil { - return fmt.Errorf("error marshalling typed data: %w", err) - } - - // set the typedData as string json below - sigAndData := commons.SigAndData{ - Signature: request.Signature, - TypedData: base64.StdEncoding.EncodeToString(typeJSON), - } - jsonPayload, err := json.Marshal(sigAndData) - if err != nil { - slog.Error("Error json.Marshal message:", "err", err) - return err - } - slog.Debug("SaveTransaction", "jsonPayload", string(jsonPayload)) - msgSender, _, signature, err := commons.ExtractSigAndData(string(jsonPayload)) - - if err != nil { - slog.Error("Error:", "err", err) - return err - } - - if request.MsgSender != nil && common.HexToAddress(*request.MsgSender) != msgSender { - msg := "wrong signature" - return ctx.JSON(http.StatusBadRequest, TransactionError{Message: &msg}) - } - - dappAddress := app.String() - payload := string(dataBytes) - - slog.Info("Input saved", - "dappAddress", dappAddress, - "msgSender", msgSender, - "nonce", nonce, - "maxGasPrice", maxGasPrice, - "payload", payload, - ) - - payload = strings.TrimPrefix(payload, "0x") - - inputCount, err := p.inputRepository.Count(stdCtx, nil) - - if err != nil { - slog.Error("Error counting inputs:", "err", err) - return err - } - - txId := fmt.Sprintf("0x%s", common.Bytes2Hex(crypto.Keccak256(signature))) - createdInput, err := p.inputRepository.Create(stdCtx, model.AdvanceInput{ - ID: txId, - Index: int(inputCount), - MsgSender: msgSender, - Payload: payload, - AppContract: common.HexToAddress(dappAddress), - InputBoxIndex: -2, - Type: "Avail", - }) - - if err != nil { - slog.Error("Error creating inputs:", "err", err) - return err - } - - slog.Info("Input created", "id", createdInput.ID) - - response := TransactionResponse{ - Id: &txId, - } - - return ctx.JSON(http.StatusOK, response) -} - // SendCartesiTransaction implements ServerInterface. func (p *PaioAPI) SendCartesiTransaction(ctx echo.Context) error { var request SendCartesiTransactionJSONRequestBody