Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the EIP712 getmessage response #1872

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,25 +387,67 @@ 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 {
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
}

responseBytes, err := json.Marshal(response)
if err != nil {
walletExt.Logger().Error("error marshaling JSON response", log.ErrKey, 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
Loading