From be79a8e2797abdba0ae05c4a48273d89a6164819 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Wed, 1 Mar 2023 08:37:32 +0000 Subject: [PATCH] client: Hard-code validate func. The client supported overriding the default server signature validation func. This was originally added to facilitate testing, but at the moment only serves to make using the client more clunky and error prone. --- client/client.go | 25 ++++++++----------------- client/client_test.go | 17 +++-------------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/client/client.go b/client/client.go index 529ce2d..6e19217 100644 --- a/client/client.go +++ b/client/client.go @@ -19,24 +19,15 @@ import ( type Client struct { http.Client - URL string - PubKey []byte - Sign SignFunc - Validate ValidateFunc - Log slog.Logger + URL string + PubKey []byte + // Sign is a function which must be provided to an instance of Client so + // that it can sign request bodies using the PrivKey of the specified + // address. + Sign func(context.Context, string, stdaddr.Address) ([]byte, error) + Log slog.Logger } -// SignFunc is the signature of a function must be provided to an instance of -// Client so that it can sign request bodies using the PrivKey of the specified -// address. -type SignFunc func(context.Context, string, stdaddr.Address) ([]byte, error) - -// ValidateFunc is the signature of a function which must be provided to an -// instance of Client so that it can validate that HTTP responses are signed -// with the provided pubkey. This package provides a default implementation of -// ValidateFunc named ValidateServerResponse. -type ValidateFunc func(resp *http.Response, body []byte, serverPubkey []byte) error - func (c *Client) VspInfo(ctx context.Context) (*types.VspInfoResponse, error) { var resp *types.VspInfoResponse err := c.get(ctx, "/api/v3/vspinfo", &resp) @@ -223,7 +214,7 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre status, http.StatusText(status), respBody) } - err = c.Validate(reply, respBody, c.PubKey) + err = ValidateServerSignature(reply, respBody, c.PubKey) if err != nil { return fmt.Errorf("authenticate server response: %v", err) } diff --git a/client/client_test.go b/client/client_test.go index 8543caa..2f1f809 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -7,19 +7,10 @@ import ( "net/http/httptest" "testing" - "github.com/decred/dcrd/txscript/v4/stdaddr" "github.com/decred/slog" "github.com/decred/vspd/types" ) -func NoopSign(ctx context.Context, message string, address stdaddr.Address) ([]byte, error) { - return nil, nil -} - -func NoopValidate(resp *http.Response, body []byte, serverPubkey []byte) error { - return nil -} - // TestErrorDetails ensures errors returned by client.do contain adequate // information for debugging (HTTP status and response body). func TestErrorDetails(t *testing.T) { @@ -68,11 +59,9 @@ func TestErrorDetails(t *testing.T) { })) client := Client{ - URL: testServer.URL, - PubKey: []byte("fake pubkey"), - Sign: NoopSign, - Validate: NoopValidate, - Log: slog.Disabled, + URL: testServer.URL, + PubKey: []byte("fake pubkey"), + Log: slog.Disabled, } var resp interface{}