getAddress, getPubKey commands

This commit is contained in:
programmer10110 2020-09-10 14:51:53 +03:00
parent 3d6e6556f6
commit 7b68623be2
3 changed files with 68 additions and 0 deletions

View File

@ -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")

View File

@ -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))

View File

@ -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)