2020-07-30 08:09:13 -07:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-09-04 08:35:22 -07:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2020-07-30 08:09:13 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-08-20 08:08:11 -07:00
|
|
|
"github.com/depools/dc4bc/client/types"
|
2020-09-04 08:35:22 -07:00
|
|
|
"github.com/depools/dc4bc/fsm/fsm"
|
|
|
|
spf "github.com/depools/dc4bc/fsm/state_machines/signature_proposal_fsm"
|
|
|
|
sif "github.com/depools/dc4bc/fsm/state_machines/signing_proposal_fsm"
|
|
|
|
"github.com/google/uuid"
|
2020-07-31 07:55:47 -07:00
|
|
|
"image"
|
2020-07-30 08:09:13 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-08-03 23:34:10 -07:00
|
|
|
|
|
|
|
"github.com/depools/dc4bc/qr"
|
|
|
|
"github.com/depools/dc4bc/storage"
|
2020-07-30 08:09:13 -07:00
|
|
|
)
|
|
|
|
|
2020-09-03 10:09:25 -07:00
|
|
|
type Response struct {
|
2020-09-04 08:35:22 -07:00
|
|
|
ErrorMessage string `json:"error_message,omitempty"`
|
|
|
|
Result interface{} `json:"result"`
|
2020-09-03 10:09:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func rawResponse(w http.ResponseWriter, response []byte) {
|
|
|
|
if _, err := w.Write(response); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to write response: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorResponse(w http.ResponseWriter, statusCode int, error string) {
|
2020-07-30 08:09:13 -07:00
|
|
|
w.WriteHeader(statusCode)
|
2020-09-03 10:09:25 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
resp := Response{ErrorMessage: error}
|
|
|
|
respBz, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to marshal response: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := w.Write(respBz); err != nil {
|
2020-07-30 08:09:13 -07:00
|
|
|
panic(fmt.Sprintf("failed to write response: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:09:25 -07:00
|
|
|
func successResponse(w http.ResponseWriter, response interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
resp := Response{Result: response}
|
|
|
|
respBz, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to marshal response: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := w.Write(respBz); err != nil {
|
2020-07-30 08:09:13 -07:00
|
|
|
panic(fmt.Sprintf("failed to write response: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) StartHTTPServer(listenAddr string) error {
|
2020-08-09 14:37:53 -07:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/sendMessage", c.sendMessageHandler)
|
|
|
|
mux.HandleFunc("/getOperations", c.getOperationsHandler)
|
|
|
|
mux.HandleFunc("/getOperationQRPath", c.getOperationQRPathHandler)
|
|
|
|
|
|
|
|
mux.HandleFunc("/readProcessedOperation", c.readProcessedOperationFromBodyHandler)
|
|
|
|
mux.HandleFunc("/getOperationQR", c.getOperationQRToBodyHandler)
|
2020-09-04 09:40:15 -07:00
|
|
|
mux.HandleFunc("/handleProcessedOperationJSON", c.handleJSONOperationHandler)
|
2020-08-09 14:37:53 -07:00
|
|
|
|
2020-09-04 08:35:22 -07:00
|
|
|
mux.HandleFunc("/startDKG", c.startDKGHandler)
|
|
|
|
mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler)
|
2020-08-09 14:37:53 -07:00
|
|
|
|
2020-09-04 08:35:22 -07:00
|
|
|
c.Logger.Log("Starting HTTP server on address: %s", listenAddr)
|
2020-08-09 14:37:53 -07:00
|
|
|
return http.ListenAndServe(listenAddr, mux)
|
2020-07-30 08:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) sendMessageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqBytes, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to read request body: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2020-09-04 08:35:22 -07:00
|
|
|
defer r.Body.Close()
|
2020-07-30 08:09:13 -07:00
|
|
|
|
|
|
|
var msg storage.Message
|
|
|
|
if err = json.Unmarshal(reqBytes, &msg); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to unmarshal message: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.SendMessage(msg); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to send message to the storage: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:09:25 -07:00
|
|
|
successResponse(w, "ok")
|
2020-07-30 08:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getOperationsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
operations, err := c.GetOperations()
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get operations: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:09:25 -07:00
|
|
|
successResponse(w, operations)
|
2020-07-30 08:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getOperationQRPathHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
operationID := r.URL.Query().Get("operationID")
|
|
|
|
|
2020-09-01 08:06:37 -07:00
|
|
|
qrPaths, err := c.GetOperationQRPath(operationID)
|
2020-07-30 08:09:13 -07:00
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get operation QR path: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:27:39 -07:00
|
|
|
successResponse(w, qrPaths)
|
2020-07-30 08:09:13 -07:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:55:47 -07:00
|
|
|
func (c *Client) getOperationQRToBodyHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
operationID := r.URL.Query().Get("operationID")
|
|
|
|
|
|
|
|
operationJSON, err := c.getOperationJSON(operationID)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get operation in JSON: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedData, err := qr.EncodeQR(operationJSON)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to encode operation: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/jpeg")
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(encodedData)))
|
2020-09-03 10:09:25 -07:00
|
|
|
rawResponse(w, encodedData)
|
2020-07-31 07:55:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) readProcessedOperationFromBodyHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to parse multipat form: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _, err := r.FormFile("qr")
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to retrieve a file: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to decode an image: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qrData, err := qr.ReadDataFromQR(img)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:08:11 -07:00
|
|
|
var operation types.Operation
|
2020-07-31 07:55:47 -07:00
|
|
|
if err = json.Unmarshal(qrData, &operation); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("failed to unmarshal processed operation: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := c.handleProcessedOperation(operation); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to handle an operation: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2020-09-03 10:09:25 -07:00
|
|
|
|
|
|
|
successResponse(w, "ok")
|
2020-09-04 08:35:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) startDKGHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to read body: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
dkgRoundID := md5.Sum(reqBody)
|
|
|
|
message, err := c.buildMessage(hex.EncodeToString(dkgRoundID[:]), spf.EventInitProposal, reqBody)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to build message: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = c.SendMessage(*message); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to send message: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
successResponse(w, "ok")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) proposeSignDataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to read body: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
var req map[string][]byte
|
|
|
|
if err = json.Unmarshal(reqBody, &req); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to umarshal request: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := c.buildMessage(hex.EncodeToString(req["dkgID"]), sif.EventSigningStart, req["data"])
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to build message: %v", err))
|
|
|
|
}
|
|
|
|
if err = c.SendMessage(*message); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to send message: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
successResponse(w, "ok")
|
|
|
|
}
|
|
|
|
|
2020-09-04 09:40:15 -07:00
|
|
|
func (c *Client) handleJSONOperationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to read body: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
var req types.Operation
|
|
|
|
if err = json.Unmarshal(reqBody, &req); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to umarshal request: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.handleProcessedOperation(req); err != nil {
|
|
|
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to handle processed operation: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
successResponse(w, "ok")
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:35:22 -07:00
|
|
|
func (c *Client) buildMessage(dkgRoundID string, event fsm.Event, data []byte) (*storage.Message, error) {
|
|
|
|
message := storage.Message{
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
DkgRoundID: dkgRoundID,
|
|
|
|
Event: string(event),
|
|
|
|
Data: data,
|
|
|
|
SenderAddr: c.GetAddr(),
|
|
|
|
}
|
|
|
|
signature, err := c.signMessage(message.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to sign message: %w", err)
|
|
|
|
}
|
|
|
|
message.Signature = signature
|
|
|
|
return &message, nil
|
2020-07-31 07:55:47 -07:00
|
|
|
}
|