-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
74 lines (65 loc) · 3.25 KB
/
Copy patherrors.go
File metadata and controls
74 lines (65 loc) · 3.25 KB
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
package siwe
import "fmt"
// ErrorType is a structured error type for SIWE validation and verification.
type ErrorType string
// Error type codes. These match the identifiers used by the canonical
// TypeScript implementation (@signinwithethereum/siwe).
const (
ErrExpiredMessage ErrorType = "Expired message."
ErrInvalidDomain ErrorType = "Invalid domain."
ErrSchemeMismatch ErrorType = "Scheme does not match provided scheme for verification."
ErrDomainMismatch ErrorType = "Domain does not match provided domain for verification."
ErrNonceMismatch ErrorType = "Nonce does not match provided nonce for verification."
ErrURIMismatch ErrorType = "URI does not match provided URI for verification."
ErrChainIDMismatch ErrorType = "Chain ID does not match provided chain ID for verification."
ErrRequestIDMismatch ErrorType = "Request ID does not match provided request ID for verification."
ErrInvalidAddress ErrorType = "Invalid address."
ErrInvalidURI ErrorType = "URI does not conform to RFC 3986."
ErrInvalidNonce ErrorType = "Nonce size smaller then 8 characters or is not alphanumeric."
ErrNotYetValidMessage ErrorType = "Message is not valid yet."
ErrInvalidSignature ErrorType = "Signature does not match address of the message."
ErrInvalidSignatureChain ErrorType = "Contract wallet verification provider chain does not match message chain ID."
ErrInvalidTimeFormat ErrorType = "Invalid time format."
ErrInvalidMessageVersion ErrorType = "Invalid message version."
ErrUnableToParseMessage ErrorType = "Unable to parse the message."
ErrMissingDomain ErrorType = "Domain is required for verification."
ErrMissingNonce ErrorType = "Nonce is required for verification."
ErrMissingURI ErrorType = "URI is required for verification."
ErrMissingChainID ErrorType = "Chain ID is required for verification."
ErrInvalidParams ErrorType = "Invalid parameters passed to verify."
ErrMalformedMessage ErrorType = "Message could not be prepared for signing."
ErrInvalidStatement ErrorType = "Statement contains invalid characters."
ErrInvalidRequestID ErrorType = "Request ID contains invalid characters."
)
// Error is a structured SIWE error. It carries a type code plus optional
// expected/received context so callers can match against specific failures
// without parsing free-form messages.
type Error struct {
Type ErrorType
Expected string
Received string
}
func (e *Error) Error() string {
switch {
case e.Expected != "" && e.Received != "":
return fmt.Sprintf("%s Expected: %s. Received: %s.", e.Type, e.Expected, e.Received)
case e.Expected != "":
return fmt.Sprintf("%s Expected: %s.", e.Type, e.Expected)
case e.Received != "":
return fmt.Sprintf("%s Received: %s.", e.Type, e.Received)
default:
return string(e.Type)
}
}
// Is supports errors.Is matching by type code.
func (e *Error) Is(target error) bool {
t, ok := target.(*Error)
if !ok {
return false
}
return e.Type == t.Type
}
func newError(t ErrorType) error { return &Error{Type: t} }
func newErrorWith(t ErrorType, expected, received string) error {
return &Error{Type: t, Expected: expected, Received: received}
}