Skip to content

Commit 55c43af

Browse files
fix: ISO8601 didn't support fractional seconds by default
1 parent b78fc3d commit 55c43af

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Sources/web3swift/Utils/EIP/EIP4361.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class EIP4361 {
6868
let eip4361Regex = try! NSRegularExpression(pattern: EIP4361.eip4361Pattern)
6969
let groups = eip4361Regex.captureGroups(string: message)
7070
let dateFormatter = ISO8601DateFormatter()
71-
71+
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
7272
guard let domain = groups["domain"],
7373
let rawAddress = groups["address"],
7474
let address = EthereumAddress(rawAddress),
@@ -115,6 +115,7 @@ public final class EIP4361 {
115115
descriptionParts.append("\nChain ID: \(chainId.description)")
116116
descriptionParts.append("\nNonce: \(nonce)")
117117
let dateFormatter = ISO8601DateFormatter()
118+
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
118119
descriptionParts.append("\nIssued At: \(dateFormatter.string(from: issuedAt))")
119120
if let expirationTime = expirationTime {
120121
descriptionParts.append("\nExpiration Time: \(dateFormatter.string(from: expirationTime))")

Tests/web3swiftTests/localTests/EIP4361Test.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ class EIP4361Test: XCTestCase {
1414

1515
/// Parsing Sign in with Ethereum message
1616
func test_EIP4361Parsing() {
17-
let rawSiweMessage = "service.invalid wants you to sign in with your Ethereum account:\n0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\n\nI accept the ServiceOrg Terms of Service: https://service.invalid/tos\n\nURI: https://service.invalid/login\nVersion: 1\nChain ID: 1\nNonce: 32891756\nIssued At: 2021-09-30T16:25:24Z\nExpiration Time: 2021-09-29T15:25:24Z\nNot Before: 2021-10-28T14:25:24Z\nRequest ID: random-request-id_STRING!@$%%&\nResources:\n- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/\n- https://example.com/my-web2-claim.json"
17+
let rawSiweMessage = "service.invalid wants you to sign in with your Ethereum account:\n0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\n\nI accept the ServiceOrg Terms of Service: https://service.invalid/tos\n\nURI: https://service.invalid/login\nVersion: 1\nChain ID: 1\nNonce: 32891756\nIssued At: 2021-09-30T16:25:24.345Z\nExpiration Time: 2021-09-29T15:25:24.234Z\nNot Before: 2021-10-28T14:25:24.123Z\nRequest ID: random-request-id_STRING!@$%%&\nResources:\n- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/\n- https://example.com/my-web2-claim.json"
1818
guard let siweMessage = EIP4361(rawSiweMessage) else {
1919
XCTFail("Failed to parse SIWE message.")
2020
return
2121
}
2222

2323
let dateFormatter = ISO8601DateFormatter()
24+
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
2425
XCTAssertEqual(siweMessage.domain, "service.invalid")
2526
XCTAssertEqual(siweMessage.address, EthereumAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")!)
2627
XCTAssertEqual(siweMessage.statement, "I accept the ServiceOrg Terms of Service: https://service.invalid/tos")
2728
XCTAssertEqual(siweMessage.uri, URL(string: "https://service.invalid/login")!)
2829
XCTAssertEqual(siweMessage.version, 1)
2930
XCTAssertEqual(siweMessage.chainId, 1)
3031
XCTAssertEqual(siweMessage.nonce, "32891756")
31-
XCTAssertEqual(siweMessage.issuedAt, dateFormatter.date(from: "2021-09-30T16:25:24Z")!)
32-
XCTAssertEqual(siweMessage.expirationTime, dateFormatter.date(from: "2021-09-29T15:25:24Z")!)
33-
XCTAssertEqual(siweMessage.notBefore, dateFormatter.date(from: "2021-10-28T14:25:24Z")!)
32+
XCTAssertEqual(siweMessage.issuedAt, dateFormatter.date(from: "2021-09-30T16:25:24.345Z")!)
33+
XCTAssertEqual(siweMessage.expirationTime, dateFormatter.date(from: "2021-09-29T15:25:24.234Z")!)
34+
XCTAssertEqual(siweMessage.notBefore, dateFormatter.date(from: "2021-10-28T14:25:24.123Z")!)
3435
XCTAssertEqual(siweMessage.requestId, "random-request-id_STRING!@$%%&")
3536
XCTAssertEqual(siweMessage.resources, [URL(string: "ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/")!,
3637
URL(string: "https://example.com/my-web2-claim.json")!])

0 commit comments

Comments
 (0)