@@ -46,18 +46,18 @@ private let uriPattern = "(([^:?#\\s]+):)?(([^?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*
4646public final class EIP4361 {
4747
4848 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
6161 }
6262
6363 private static let domain = " (?< \( EIP4361Field . domain. rawValue) >([^?#]*)) wants you to sign in with your Ethereum account: "
@@ -77,9 +77,9 @@ public final class EIP4361 {
7777 " ^ \( domain) \( address) \( statementParagraph) \( uri) \( version) \( chainId) \( nonce) \( issuedAt) \( expirationTime) \( notBefore) \( requestId) \( resourcesParagraph) $ "
7878 }
7979
80- private static var _eip4361OptionalPattern : String !
80+ private static var _eip4361OptionalPattern : String ?
8181 private static var eip4361OptionalPattern : String {
82- guard _eip4361OptionalPattern == nil else { return _eip4361OptionalPattern! }
82+ if let _eip4361OptionalPattern = _eip4361OptionalPattern { return _eip4361OptionalPattern }
8383
8484 let domain = " (?< \( EIP4361Field . domain. rawValue) >(.*)) wants you to sign in with your Ethereum account: "
8585 let address = " \\ n(?< \( EIP4361Field . address. rawValue) >.*) \\ n \\ n "
@@ -106,20 +106,27 @@ public final class EIP4361 {
106106 " \( requestId) " ,
107107 " \( resourcesParagraph) $ " ]
108108
109- _eip4361OptionalPattern = patternParts. joined ( )
110- return _eip4361OptionalPattern!
109+ let eip4361OptionalPattern = patternParts. joined ( )
110+ _eip4361OptionalPattern = eip4361OptionalPattern
111+ return eip4361OptionalPattern
111112 }
112113
113114 public static func validate( _ message: String ) -> EIP4361ValidationResponse {
115+ // swiftlint:disable force_try
114116 let siweConstantMessageRegex = try ! NSRegularExpression ( pattern: " ^ \( domain) \\ n " )
115117 guard siweConstantMessageRegex. firstMatch ( in: message, range: message. fullNSRange) != nil else {
116118 return EIP4361ValidationResponse ( isEIP4361: false , eip4361: nil , capturedFields: [ : ] )
117119 }
118120
119121 let eip4361Regex = try ! NSRegularExpression ( pattern: eip4361OptionalPattern)
122+ // swiftlint:enable force_try
120123 var capturedFields : [ EIP4361Field : String ] = [ : ]
121124 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
122128 capturedFields [ . init( rawValue: key) !] = value
129+ // swiftlint:enable force_unwrapping
123130 }
124131 return EIP4361ValidationResponse ( isEIP4361: true ,
125132 eip4361: EIP4361 ( message) ,
@@ -153,7 +160,9 @@ public final class EIP4361 {
153160 public let resources : [ URL ] ?
154161
155162 public init ? ( _ message: String ) {
163+ // swiftlint:disable force_try
156164 let eip4361Regex = try ! NSRegularExpression ( pattern: EIP4361 . eip4361Pattern)
165+ // swiftlint:enable force_try
157166 let groups = eip4361Regex. captureGroups ( string: message)
158167 let dateFormatter = ISO8601DateFormatter ( )
159168 dateFormatter. formatOptions = [ . withInternetDateTime, . withFractionalSeconds]
0 commit comments