@@ -46,18 +46,18 @@ private let uriPattern = "(([^:?#\\s]+):)?(([^?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*
46
46
public final class EIP4361 {
47
47
48
48
public enum EIP4361Field : String {
49
- case domain = " domain "
50
- case address = " address "
51
- case statement = " statement "
52
- case uri = " uri "
53
- case version = " version "
54
- case chainId = " chainId "
55
- case nonce = " nonce "
56
- case issuedAt = " issuedAt "
57
- case expirationTime = " expirationTime "
58
- case notBefore = " notBefore "
59
- case requestId = " requestId "
60
- case resources = " resources "
49
+ case domain
50
+ case address
51
+ case statement
52
+ case uri
53
+ case version
54
+ case chainId
55
+ case nonce
56
+ case issuedAt
57
+ case expirationTime
58
+ case notBefore
59
+ case requestId
60
+ case resources
61
61
}
62
62
63
63
private static let domain = " (?< \( EIP4361Field . domain. rawValue) >([^?#]*)) wants you to sign in with your Ethereum account: "
@@ -77,9 +77,9 @@ public final class EIP4361 {
77
77
" ^ \( domain) \( address) \( statementParagraph) \( uri) \( version) \( chainId) \( nonce) \( issuedAt) \( expirationTime) \( notBefore) \( requestId) \( resourcesParagraph) $ "
78
78
}
79
79
80
- private static var _eip4361OptionalPattern : String !
80
+ private static var _eip4361OptionalPattern : String ?
81
81
private static var eip4361OptionalPattern : String {
82
- guard _eip4361OptionalPattern == nil else { return _eip4361OptionalPattern! }
82
+ if let _eip4361OptionalPattern = _eip4361OptionalPattern { return _eip4361OptionalPattern }
83
83
84
84
let domain = " (?< \( EIP4361Field . domain. rawValue) >(.*)) wants you to sign in with your Ethereum account: "
85
85
let address = " \\ n(?< \( EIP4361Field . address. rawValue) >.*) \\ n \\ n "
@@ -106,20 +106,27 @@ public final class EIP4361 {
106
106
" \( requestId) " ,
107
107
" \( resourcesParagraph) $ " ]
108
108
109
- _eip4361OptionalPattern = patternParts. joined ( )
110
- return _eip4361OptionalPattern!
109
+ let eip4361OptionalPattern = patternParts. joined ( )
110
+ _eip4361OptionalPattern = eip4361OptionalPattern
111
+ return eip4361OptionalPattern
111
112
}
112
113
113
114
public static func validate( _ message: String ) -> EIP4361ValidationResponse {
115
+ // swiftlint:disable force_try
114
116
let siweConstantMessageRegex = try ! NSRegularExpression ( pattern: " ^ \( domain) \\ n " )
115
117
guard siweConstantMessageRegex. firstMatch ( in: message, range: message. fullNSRange) != nil else {
116
118
return EIP4361ValidationResponse ( isEIP4361: false , eip4361: nil , capturedFields: [ : ] )
117
119
}
118
120
119
121
let eip4361Regex = try ! NSRegularExpression ( pattern: eip4361OptionalPattern)
122
+ // swiftlint:enable force_try
120
123
var capturedFields : [ EIP4361Field : String ] = [ : ]
121
124
for (key, value) in eip4361Regex. captureGroups ( string: message) {
125
+ /// We are using EIP4361Field.rawValue to create regular expression.
126
+ /// These values must decode back from raw representation always.
127
+ // swiftlint:disable force_unwrapping
122
128
capturedFields [ . init( rawValue: key) !] = value
129
+ // swiftlint:enable force_unwrapping
123
130
}
124
131
return EIP4361ValidationResponse ( isEIP4361: true ,
125
132
eip4361: EIP4361 ( message) ,
@@ -153,7 +160,9 @@ public final class EIP4361 {
153
160
public let resources : [ URL ] ?
154
161
155
162
public init ? ( _ message: String ) {
163
+ // swiftlint:disable force_try
156
164
let eip4361Regex = try ! NSRegularExpression ( pattern: EIP4361 . eip4361Pattern)
165
+ // swiftlint:enable force_try
157
166
let groups = eip4361Regex. captureGroups ( string: message)
158
167
let dateFormatter = ISO8601DateFormatter ( )
159
168
dateFormatter. formatOptions = [ . withInternetDateTime, . withFractionalSeconds]
0 commit comments