-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproof.go
185 lines (158 loc) · 4.46 KB
/
proof.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package ethauth
import (
"fmt"
"time"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/go-ethereum/crypto"
)
type Proof struct {
// "eth" prefix
Prefix string
// Account addres (in hex)
Address string
// Claims object, aka, the message key of an EIP712 signature
Claims Claims
// Signature of the message by the account address above (in hex)
Signature string
// Extra bytes in hex format used for signature validation
// ie. useful for counterfactual smart wallets
Extra string
}
func NewProof() *Proof {
return &Proof{
Prefix: ETHAuthPrefix,
Claims: Claims{
ETHAuthVersion: ETHAuthVersion,
},
}
}
func (t *Proof) Message() ([]byte, error) {
return t.Claims.Message()
}
func (t *Proof) MessageDigest() ([]byte, error) {
return t.Claims.MessageDigest()
}
func (t *Proof) MessageTypedData() (*ethcoder.TypedData, error) {
return t.Claims.TypedData()
}
type Claims struct {
App string `json:"app,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
Nonce uint64 `json:"n,omitempty"`
Type string `json:"typ,omitempty"`
Origin string `json:"ogn,omitempty"`
ETHAuthVersion string `json:"v,omitempty"`
}
func (c *Claims) SetIssuedAtNow() {
c.IssuedAt = time.Now().UTC().Unix()
}
func (c *Claims) SetExpiryIn(tm time.Duration) {
c.ExpiresAt = time.Now().UTC().Unix() + int64(tm.Seconds())
}
func (c Claims) Valid() error {
now := time.Now().Unix()
drift := int64(5 * 60) // 5 minutes
max := int64(time.Duration(time.Hour*24*365).Seconds()) + drift // 1 year
if c.ETHAuthVersion == "" {
return fmt.Errorf("claims: ethauth version is empty")
}
if c.App == "" {
return fmt.Errorf("claims: app is empty")
}
if c.IssuedAt > now+drift {
return fmt.Errorf("claims: proof is issued from the future - check if device clock is synced.")
}
if c.ExpiresAt < now-drift || c.ExpiresAt > now+max || (c.IssuedAt != 0 && c.IssuedAt < now-max) {
return fmt.Errorf("claims: proof has expired")
}
return nil
}
func (c Claims) Map() map[string]interface{} {
m := map[string]interface{}{}
if c.App != "" {
m["app"] = c.App
}
if c.IssuedAt != 0 {
m["iat"] = c.IssuedAt
}
if c.ExpiresAt != 0 {
m["exp"] = c.ExpiresAt
}
if c.Nonce != 0 {
m["n"] = c.Nonce
}
if c.Type != "" {
m["typ"] = c.Type
}
if c.Origin != "" {
m["ogn"] = c.Origin
}
if c.ETHAuthVersion != "" {
m["v"] = c.ETHAuthVersion
}
return m
}
func (c Claims) TypedData() (*ethcoder.TypedData, error) {
td := ðcoder.TypedData{
Types: ethcoder.TypedDataTypes{
"EIP712Domain": {
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
},
"Claims": {},
},
PrimaryType: "Claims",
Domain: eip712Domain,
Message: c.Map(),
}
if len(td.Message) == 0 {
return nil, fmt.Errorf("ethauth: claims is empty")
}
claimsType := []ethcoder.TypedDataArgument{}
if c.App != "" {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "app", Type: "string"})
}
if c.IssuedAt != 0 {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "iat", Type: "int64"})
}
if c.ExpiresAt != 0 {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "exp", Type: "int64"})
}
if c.Nonce != 0 {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "n", Type: "uint64"})
}
if c.Type != "" {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "typ", Type: "string"})
}
if c.Origin != "" {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "ogn", Type: "string"})
}
if c.ETHAuthVersion != "" {
claimsType = append(claimsType, ethcoder.TypedDataArgument{Name: "v", Type: "string"})
}
td.Types["Claims"] = claimsType
return td, nil
}
func (c Claims) Message() ([]byte, error) {
if err := c.Valid(); err != nil {
return nil, fmt.Errorf("claims are invalid - %w", err)
}
typedData, err := c.TypedData()
if err != nil {
return nil, fmt.Errorf("ethauth: failed to compute claims typed data - %w", err)
}
_, encodedTypedData, err := typedData.Encode()
if err != nil {
return nil, fmt.Errorf("ethauth: failed to encode claims typed data - %w", err)
}
return encodedTypedData, nil
}
func (c Claims) MessageDigest() ([]byte, error) {
encodedTypedData, err := c.Message()
if err != nil {
return nil, fmt.Errorf("ethauth: failed to compute claims message digest - %w", err)
}
digest := crypto.Keccak256(encodedTypedData)
return digest, nil
}