Skip to content

Commit

Permalink
fix the EIP712 getmessage response
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Apr 10, 2024
1 parent f7fef30 commit ef4d220
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,25 +387,68 @@ func getMessageRequestHandler(walletExt *rpcapi.Services, conn UserConn) {
return
}

// create the response structure
type JSONResponse struct {
// create the response structure for EIP712 message where the message is a JSON object
type JSONResponseEIP712 struct {
Message json.RawMessage `json:"message"`
Type string `json:"type"`
}

// create the response structure for personal sign message where the message is a string
type JSONResponsePersonal struct {
Message string `json:"message"`
Type string `json:"type"`
}

// get string representation of the message format
messageFormat := viewingkey.GetBestFormat(formatsSlice)
messageFormatString := viewingkey.GetSignatureTypeString(messageFormat)
responseBytes := []byte{}
if messageFormat == viewingkey.PersonalSign {
response := JSONResponsePersonal{
Message: message,
Type: messageFormatString,
}

response := JSONResponse{
Message: message,
Type: messageFormatString,
}
responseBytes, err = json.Marshal(response)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("error marshaling JSON response: %w", err))
return
}
} else if messageFormat == viewingkey.EIP712Signature {

responseBytes, err := json.Marshal(response)
if err != nil {
walletExt.Logger().Error("error marshaling JSON response", log.ErrKey, err)
return
var messageMap map[string]interface{}
err = json.Unmarshal([]byte(message), &messageMap)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("error unmarshaling JSON: %w", err))
return
}

if domainMap, ok := messageMap["domain"].(map[string]interface{}); ok {
delete(domainMap, "salt")
delete(domainMap, "verifyingContract")
}

if typesMap, ok := messageMap["types"].(map[string]interface{}); ok {
delete(typesMap, "EIP712Domain")
}

// Marshal the modified map back to JSON
modifiedMessage, err := json.Marshal(messageMap)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("error marshaling modified JSON: %w", err))
return
}

response := JSONResponseEIP712{
Message: modifiedMessage,
Type: messageFormatString,
}

responseBytes, err = json.Marshal(response)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("error marshaling JSON response: %w", err))
return
}
}

err = conn.WriteResponse(responseBytes)
Expand Down

0 comments on commit ef4d220

Please sign in to comment.