-
Notifications
You must be signed in to change notification settings - Fork 0
/
eip712verify.go
82 lines (68 loc) · 2.01 KB
/
eip712verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package handlers
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/labstack/echo/v4"
)
/**
* @StoredHash will be used to retrive the publicKey of the user
* @Address Fetch the previously stored challenge hash from your database
* @UserSignature address of the OG signer to be verifyed
**/
type VerifyParams struct {
StoredHash string `json:"storedHash"`
Address common.Address `json:"address"`
UserSignature string `json:"userSignature"`
}
type ReturnVerifyData struct {
Verify bool `json:"verify"`
}
func PostEIP712verify(c echo.Context) error {
p := VerifyParams{}
defer c.Request().Body.Close()
err := json.NewDecoder(c.Request().Body).Decode(&p)
if err != nil {
log.Fatalf("Failed reading the request body %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error)
}
log.Printf("all params: %#v\n", p)
storedHash, err := hex.DecodeString(p.StoredHash)
if err != nil {
log.Fatalf("Failed reading the request body %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error)
}
var userAddress = p.Address
signature, _ := hex.DecodeString(p.UserSignature)
if len(signature) != 65 {
return fmt.Errorf("invalid signature length: %d", len(signature))
}
// if signature[64] != 27 && signature[64] != 28 {
// return fmt.Errorf("invalid recovery id: %d", signature[64])
// }
// signature[64] -= 27
// ! prefer this methode to the one above
pubKeyRaw, err := crypto.Ecrecover(storedHash, signature)
if err != nil {
return fmt.Errorf("invalid signature: %s", err.Error())
}
pubKey, err := crypto.UnmarshalPubkey(pubKeyRaw)
if err != nil {
return err
}
recoveredAddr := crypto.PubkeyToAddress(*pubKey)
if !bytes.Equal(userAddress.Bytes(), recoveredAddr.Bytes()) {
return c.JSON(http.StatusOK, ReturnVerifyData{
Verify: false,
})
} else {
return c.JSON(http.StatusOK, ReturnVerifyData{
Verify: true,
})
}
}