From 7b68623be289918ab0a16caf425abe8c81568481 Mon Sep 17 00:00:00 2001 From: programmer10110 Date: Thu, 10 Sep 2020 14:51:53 +0300 Subject: [PATCH] getAddress, getPubKey commands --- client/http_server.go | 20 +++++++++++++++++ cmd/dc4bc_cli/commands.go | 46 +++++++++++++++++++++++++++++++++++++++ cmd/dc4bc_cli/main.go | 2 ++ 3 files changed, 68 insertions(+) diff --git a/client/http_server.go b/client/http_server.go index aaf87d8..f03a745 100644 --- a/client/http_server.go +++ b/client/http_server.go @@ -59,6 +59,10 @@ func successResponse(w http.ResponseWriter, response interface{}) { func (c *Client) StartHTTPServer(listenAddr string) error { mux := http.NewServeMux() + + mux.HandleFunc("/getAddress", c.getAddressHandler) + mux.HandleFunc("/getPubKey", c.getPubkeyHandler) + mux.HandleFunc("/sendMessage", c.sendMessageHandler) mux.HandleFunc("/getOperations", c.getOperationsHandler) mux.HandleFunc("/getOperationQRPath", c.getOperationQRPathHandler) @@ -74,6 +78,22 @@ func (c *Client) StartHTTPServer(listenAddr string) error { return http.ListenAndServe(listenAddr, mux) } +func (c *Client) getAddressHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + successResponse(w, c.GetAddr()) +} + +func (c *Client) getPubkeyHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + successResponse(w, c.GetPubKey()) +} + func (c *Client) sendMessageHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") diff --git a/cmd/dc4bc_cli/commands.go b/cmd/dc4bc_cli/commands.go index 66cf104..baaffec 100644 --- a/cmd/dc4bc_cli/commands.go +++ b/cmd/dc4bc_cli/commands.go @@ -140,6 +140,52 @@ func rawGetRequest(url string) (*client.Response, error) { return &response, nil } +func getPubKeyCommand() *cobra.Command { + return &cobra.Command{ + Use: "get_pubkey", + Short: "returns client's pubkey", + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + resp, err := rawGetRequest(fmt.Sprintf("http://%s//getPubKey", listenAddr)) + if err != nil { + return fmt.Errorf("failed to get client's pubkey: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to get client's pubkey: %w", resp.ErrorMessage) + } + fmt.Println(resp.Result.(string)) + return nil + }, + } +} + +func getAddressCommand() *cobra.Command { + return &cobra.Command{ + Use: "get_address", + Short: "returns client's address", + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + resp, err := rawGetRequest(fmt.Sprintf("http://%s//getAddress", listenAddr)) + if err != nil { + return fmt.Errorf("failed to get client's address: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to get client's address: %w", resp.ErrorMessage) + } + fmt.Println(resp.Result.(string)) + return nil + }, + } +} + func rawPostRequest(url string, contentType string, data []byte) (*client.Response, error) { resp, err := http.Post(url, contentType, bytes.NewReader(data)) diff --git a/cmd/dc4bc_cli/main.go b/cmd/dc4bc_cli/main.go index ca74ac6..48b7350 100644 --- a/cmd/dc4bc_cli/main.go +++ b/cmd/dc4bc_cli/main.go @@ -33,6 +33,8 @@ func main() { readOperationFromCameraCommand(), startDKGCommand(), proposeSignMessageCommand(), + getAddressCommand(), + getPubKeyCommand(), ) if err := rootCmd.Execute(); err != nil { log.Fatalf("Failed to execute root command: %v", err)