-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathSIWEFromCacaoPayloadFormatterTests.swift
More file actions
345 lines (293 loc) · 14 KB
/
Copy pathSIWEFromCacaoPayloadFormatterTests.swift
File metadata and controls
345 lines (293 loc) · 14 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import Foundation
@testable import WalletConnectUtils
@testable import WalletConnectSign
import XCTest
class SIWEFromCacaoPayloadFormatterTests: XCTestCase {
var sut: SIWEFromCacaoPayloadFormatter!
let address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
override func setUp() {
sut = SIWEFromCacaoPayloadFormatter()
}
func testFormatMessage() throws {
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: AuthPayload.stub(), account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
// MARK: - CAIP-122 Tests
func testFormatMessageBitcoin() throws {
let bitcoinAccount = Account("bip122:000000000019d6689c085ae165831e93:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")!
let expectedMessage =
"""
service.invalid wants you to sign in with your Bitcoin account:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: 000000000019d6689c085ae165831e93
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: AuthPayload.stub(), account: bitcoinAccount)
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testFormatMessageSolana() throws {
let solanaAccount = Account("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv")!
let expectedMessage =
"""
service.invalid wants you to sign in with your Solana account:
7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: 5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: AuthPayload.stub(), account: solanaAccount)
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testFormatMessageWithAnyNamespace() throws {
let cosmosAccount = Account("cosmos:cosmoshub-4:cosmos1abcdefghijklmnopqrstuvwxyz0123456789")!
let expectedMessage =
"""
service.invalid wants you to sign in with your Cosmos account:
cosmos1abcdefghijklmnopqrstuvwxyz0123456789
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: cosmoshub-4
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(authPayload: AuthPayload.stub(), account: cosmosAccount)
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
// MARK: - Existing Tests (Updated for Ethereum)
func testNilStatement() throws {
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/
- https://example.com/my-web2-claim.json
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(statement: nil)
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testNilResources() throws {
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(resources: nil)
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testResourcesEmptyArray() throws {
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(resources: [])
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testNilAllOptionalParams() throws {
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(statement: nil, resources: nil)
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testWithValidRecapAndStatement() throws {
let validRecapUrn = "urn:recap:eyJhdHQiOiB7ImVpcDE1NSI6IHsicmVxdWVzdC9ldGhfc2VuZFRyYW5zYWN0aW9uIjogW10sICJyZXF1ZXN0L3BlcnNvbmFsX3NpZ24iOiBbXX19fQ=="
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I accept the ServiceOrg Terms of Service: https://service.invalid/tos I further authorize the stated URI to perform the following actions on my behalf: (1) 'request': 'eth_sendTransaction', 'personal_sign' for 'eip155'.
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- urn:recap:eyJhdHQiOiB7ImVpcDE1NSI6IHsicmVxdWVzdC9ldGhfc2VuZFRyYW5zYWN0aW9uIjogW10sICJyZXF1ZXN0L3BlcnNvbmFsX3NpZ24iOiBbXX19fQ==
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(resources: [validRecapUrn])
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testWithValidRecapAndNoStatement() throws {
let validRecapUrn = "urn:recap:eyJhdHQiOiB7ImVpcDE1NSI6IHsicmVxdWVzdC9ldGhfc2VuZFRyYW5zYWN0aW9uIjogW10sICJyZXF1ZXN0L3BlcnNvbmFsX3NpZ24iOiBbXX19fQ=="
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I further authorize the stated URI to perform the following actions on my behalf: (1) 'request': 'eth_sendTransaction', 'personal_sign' for 'eip155'.
URI: https://service.invalid/login
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- urn:recap:eyJhdHQiOiB7ImVpcDE1NSI6IHsicmVxdWVzdC9ldGhfc2VuZFRyYW5zYWN0aW9uIjogW10sICJyZXF1ZXN0L3BlcnNvbmFsX3NpZ24iOiBbXX19fQ==
"""
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(statement: nil, resources: [validRecapUrn])
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
func testWithSignAndNotifyRecaps() throws {
let recap1 = "urn:recap:ewogICAiYXR0Ijp7CiAgICAgICJlaXAxNTUiOnsKICAgICAgICAgInJlcXVlc3QvZXRoX3NlbmRUcmFuc2FjdGlvbiI6IFt7fV0sCiAgICAgICAgICJyZXF1ZXN0L3BlcnNvbmFsX3NpZ24iOiBbe31dCiAgICAgIH0KICAgfQp9"
let recap2 = "urn:recap:ewogICAiYXR0Ijp7CiAgICAgICJodHRwczovL25vdGlmeS53YWxsZXRjb25uZWN0LmNvbS9hbGwtYXBwcyI6ewogICAgICAgICAiY3J1ZC9ub3RpZmljYXRpb25zIjogW3t9XSwKICAgICAgICAgImNydWQvc3Vic2NyaXB0aW9ucyI6IFt7fV0KICAgICAgfQogICB9Cn0"
let expectedMessage =
"""
service.invalid wants you to sign in with your Ethereum account:
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
I further authorize the stated URI to perform the following actions on my behalf: (1) 'request': 'eth_sendTransaction', 'personal_sign' for 'eip155'. (2) 'crud': 'notifications', 'subscriptions' for 'https://notify.walletconnect.com/all-apps'.
URI: https://service.invalid?walletconnect_notify_key=did:key:z6MktW4hKdsvcXgt9wXmYbSD5sH4NCk5GmNZnokP9yh2TeCf
Version: 1
Chain ID: 1
Nonce: 32891756
Issued At: 2021-09-30T16:25:24Z
Resources:
- urn:recap:eyJhdHQiOnsiZWlwMTU1Ijp7InJlcXVlc3RcL2V0aF9zZW5kVHJhbnNhY3Rpb24iOlt7fV0sInJlcXVlc3RcL3BlcnNvbmFsX3NpZ24iOlt7fV19LCJodHRwczpcL1wvbm90aWZ5LndhbGxldGNvbm5lY3QuY29tXC9hbGwtYXBwcyI6eyJjcnVkXC9ub3RpZmljYXRpb25zIjpbe31dLCJjcnVkXC9zdWJzY3JpcHRpb25zIjpbe31dfX19
"""
let uri = "https://service.invalid?walletconnect_notify_key=did:key:z6MktW4hKdsvcXgt9wXmYbSD5sH4NCk5GmNZnokP9yh2TeCf"
let cacaoPayload = try CacaoPayloadBuilder.makeCacaoPayload(
authPayload: AuthPayload.stub(
requestParams: AuthRequestParams.stub(uri: uri, statement: nil, resources: [recap1, recap2])
),
account: Account.stub())
let message = try sut.formatMessage(from: cacaoPayload)
XCTAssertEqual(message, expectedMessage)
}
// MARK: - EIP-4361 single-line field integrity
func testRejectsDomainWithEmbeddedNewline() throws {
let payload = try smugglingPayload(domain: "service.invalid\nURI: https://evil.invalid")
XCTAssertThrowsError(try sut.formatMessage(from: payload)) { error in
XCTAssertEqual(
error as? SIWEFromCacaoPayloadFormatter.Errors,
.invalidSingleLineField("domain")
)
}
}
func testRejectsStatementWithEmbeddedCarriageReturn() throws {
let payload = try smugglingPayload(statement: "I accept\rURI: https://evil.invalid")
XCTAssertThrowsError(try sut.formatMessage(from: payload)) { error in
XCTAssertEqual(
error as? SIWEFromCacaoPayloadFormatter.Errors,
.invalidSingleLineField("statement")
)
}
}
func testRejectsAudWithEmbeddedNewline() throws {
let payload = try smugglingPayload(aud: "https://service.invalid/login\nNonce: forged")
XCTAssertThrowsError(try sut.formatMessage(from: payload)) { error in
XCTAssertEqual(
error as? SIWEFromCacaoPayloadFormatter.Errors,
.invalidSingleLineField("aud")
)
}
}
private func smugglingPayload(
domain: String = "service.invalid",
aud: String = "https://service.invalid/login",
statement: String? = "I accept the ServiceOrg Terms of Service: https://service.invalid/tos"
) throws -> CacaoPayload {
let account = Account.stub()
return CacaoPayload(
iss: account.did,
domain: domain,
aud: aud,
version: "1",
nonce: "32891756",
iat: "2021-09-30T16:25:24Z",
nbf: nil,
exp: nil,
statement: statement,
requestId: nil,
resources: nil
)
}
}