diff --git a/sessions/proto/sessions.gen.go b/sessions/proto/sessions.gen.go index ed71936..3b39ab2 100644 --- a/sessions/proto/sessions.gen.go +++ b/sessions/proto/sessions.gen.go @@ -1,6 +1,6 @@ // sessions v0.0.1 67f782e8acfe452f905078a7423ed5d27c6639a8 // -- -// Code generated by webrpc-gen@v0.20.3 with golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.21.3 with golang generator. DO NOT EDIT. // // webrpc-gen -schema=sessions.ridl -target=golang -pkg=proto -client -out=./clients/sessions.gen.go package proto @@ -14,11 +14,16 @@ import ( "io" "net/http" "net/url" + "strings" "time" "github.com/0xsequence/go-sequence/lib/prototyp" ) +const WebrpcHeader = "Webrpc" + +const WebrpcHeaderValue = "webrpc@v0.21.3;gen-golang@v0.16.2;sessions@v0.0.1" + // WebRPC description and code-gen version func WebRPCVersion() string { return "v1" @@ -34,6 +39,57 @@ func WebRPCSchemaHash() string { return "67f782e8acfe452f905078a7423ed5d27c6639a8" } +type WebrpcGenVersions struct { + WebrpcGenVersion string + CodeGenName string + CodeGenVersion string + SchemaName string + SchemaVersion string +} + +func VersionFromHeader(h http.Header) (*WebrpcGenVersions, error) { + if h.Get(WebrpcHeader) == "" { + return nil, fmt.Errorf("header is empty or missing") + } + + versions, err := parseWebrpcGenVersions(h.Get(WebrpcHeader)) + if err != nil { + return nil, fmt.Errorf("webrpc header is invalid: %w", err) + } + + return versions, nil +} + +func parseWebrpcGenVersions(header string) (*WebrpcGenVersions, error) { + versions := strings.Split(header, ";") + if len(versions) < 3 { + return nil, fmt.Errorf("expected at least 3 parts while parsing webrpc header: %v", header) + } + + _, webrpcGenVersion, ok := strings.Cut(versions[0], "@") + if !ok { + return nil, fmt.Errorf("webrpc gen version could not be parsed from: %s", versions[0]) + } + + tmplTarget, tmplVersion, ok := strings.Cut(versions[1], "@") + if !ok { + return nil, fmt.Errorf("tmplTarget and tmplVersion could not be parsed from: %s", versions[1]) + } + + schemaName, schemaVersion, ok := strings.Cut(versions[2], "@") + if !ok { + return nil, fmt.Errorf("schema name and schema version could not be parsed from: %s", versions[2]) + } + + return &WebrpcGenVersions{ + WebrpcGenVersion: webrpcGenVersion, + CodeGenName: tmplTarget, + CodeGenVersion: tmplVersion, + SchemaName: schemaName, + SchemaVersion: schemaVersion, + }, nil +} + // // Common types // @@ -212,70 +268,77 @@ type TransactionBundle struct { Signature prototyp.Hash `json:"signature"` } -var ( - methods = map[string]method{ - "/rpc/Sessions/Ping": { - Name: "Ping", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/Config": { - Name: "Config", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/Wallets": { - Name: "Wallets", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/DeployHash": { - Name: "DeployHash", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/ConfigUpdates": { - Name: "ConfigUpdates", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/Migrations": { - Name: "Migrations", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveConfig": { - Name: "SaveConfig", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveWallet": { - Name: "SaveWallet", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveSignature": { - Name: "SaveSignature", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveSignerSignatures": { - Name: "SaveSignerSignatures", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveSignerSignatures2": { - Name: "SaveSignerSignatures2", - Service: "Sessions", - Annotations: map[string]string{}, - }, - "/rpc/Sessions/SaveMigration": { - Name: "SaveMigration", - Service: "Sessions", - Annotations: map[string]string{}, - }, +var methods = map[string]method{ + "/rpc/Sessions/Ping": { + Name: "Ping", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/Config": { + Name: "Config", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/Wallets": { + Name: "Wallets", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/DeployHash": { + Name: "DeployHash", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/ConfigUpdates": { + Name: "ConfigUpdates", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/Migrations": { + Name: "Migrations", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveConfig": { + Name: "SaveConfig", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveWallet": { + Name: "SaveWallet", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveSignature": { + Name: "SaveSignature", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveSignerSignatures": { + Name: "SaveSignerSignatures", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveSignerSignatures2": { + Name: "SaveSignerSignatures2", + Service: "Sessions", + Annotations: map[string]string{}, + }, + "/rpc/Sessions/SaveMigration": { + Name: "SaveMigration", + Service: "Sessions", + Annotations: map[string]string{}, + }, +} + +func WebrpcMethods() map[string]method { + res := make(map[string]method, len(methods)) + for k, v := range methods { + res[k] = v } -) + + return res +} var WebRPCServices = map[string][]string{ "Sessions": { @@ -371,7 +434,7 @@ func (c *sessionsClient) Ping(ctx context.Context) error { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -391,7 +454,7 @@ func (c *sessionsClient) Config(ctx context.Context, imageHash string) (int, int if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -413,7 +476,7 @@ func (c *sessionsClient) Wallets(ctx context.Context, signer string, cursor *uin if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -433,7 +496,7 @@ func (c *sessionsClient) DeployHash(ctx context.Context, wallet string) (string, if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -454,7 +517,7 @@ func (c *sessionsClient) ConfigUpdates(ctx context.Context, wallet string, fromI if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -476,7 +539,7 @@ func (c *sessionsClient) Migrations(ctx context.Context, wallet string, fromVers if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -493,7 +556,7 @@ func (c *sessionsClient) SaveConfig(ctx context.Context, version int, config int if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -510,7 +573,7 @@ func (c *sessionsClient) SaveWallet(ctx context.Context, version int, deployConf if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -531,7 +594,7 @@ func (c *sessionsClient) SaveSignature(ctx context.Context, wallet string, diges if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -551,7 +614,7 @@ func (c *sessionsClient) SaveSignerSignatures(ctx context.Context, wallet string if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -571,7 +634,7 @@ func (c *sessionsClient) SaveSignerSignatures2(ctx context.Context, wallet strin if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -595,7 +658,7 @@ func (c *sessionsClient) SaveMigration(ctx context.Context, wallet string, fromV if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -632,6 +695,7 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType } req.Header.Set("Accept", contentType) req.Header.Set("Content-Type", contentType) + req.Header.Set(WebrpcHeader, WebrpcHeaderValue) if headers, ok := HTTPRequestHeaders(ctx); ok { for k := range headers { for _, v := range headers[k] { @@ -646,15 +710,15 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) (*http.Response, error) { reqBody, err := json.Marshal(in) if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("failed to marshal JSON body: %w", err) } if err = ctx.Err(); err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("aborted because context was done: %w", err) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("could not build request: %w", err) } resp, err := client.Do(req) @@ -665,12 +729,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read server error response body: %w", err) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal server error: %w", err) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -681,12 +745,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read response body: %w", err) } err = json.Unmarshal(respBody, &out) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal JSON response body: %w", err) } }