Skip to content

Commit 74a433b

Browse files
committed
add logs
1 parent e673fc4 commit 74a433b

File tree

1 file changed

+127
-33
lines changed

1 file changed

+127
-33
lines changed

Example/IntegrationTests/Sign/SignClientTests.swift

Lines changed: 127 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ final class SignClientTests: XCTestCase {
2525

2626

2727
static private func makeClients(name: String, linkModeUniversalLink: String? = "https://x.com", supportLinkMode: Bool = false) -> (PairingClient, SignClient, RuntimeKeyValueStorage, RelayClient) {
28-
let logger = ConsoleLogger(prefix: name, loggingLevel: .debug)
28+
let loggingLevel: LoggingLevel = .debug
29+
let logger = ConsoleLogger(prefix: name, loggingLevel: loggingLevel)
2930
let keychain = KeychainStorageMock()
3031
let keyValueStorage = RuntimeKeyValueStorage()
3132
let relayClient = RelayClientFactory.create(
@@ -55,7 +56,7 @@ final class SignClientTests: XCTestCase {
5556

5657
let client = SignClientFactory.create(
5758
metadata: metadata,
58-
logger: logger,
59+
logger: ConsoleLogger(prefix: "\(name) 📜", loggingLevel: loggingLevel),
5960
keyValueStorage: keyValueStorage,
6061
keychainStorage: keychain,
6162
pairingClient: pairingClient,
@@ -868,11 +869,18 @@ final class SignClientTests: XCTestCase {
868869
}
869870

870871
func testEIP1271SessionAuthenticated() async throws {
872+
print("🧪TEST: Starting testEIP1271SessionAuthenticated()")
871873

874+
// Step 1: Prepare EIP1271 data and expectation
875+
print("🧪TEST: Step 1 - Preparing account, signature, and expectation.")
872876
let account = Account(chainIdentifier: "eip155:1", address: "0x6DF3d14554742D67068BB7294C80107a3c655A56")!
873877
let eip1271Signature = "0xb518b65724f224f8b12dedeeb06f8b278eb7d3b42524959bed5d0dfa49801bd776c7ee05de396eadc38ee693c917a04d93b20981d68c4a950cbc42ea7f4264bc1c"
878+
print("🧪TEST: Using account: \(account.description), signature: \(eip1271Signature)")
874879

875880
let responseExpectation = expectation(description: "successful response delivered")
881+
882+
// Step 2: Dapp tries to authenticate
883+
print("🧪TEST: Step 2 - Dapp calls dapp.authenticate(...)")
876884
let uri = try! await dapp.authenticate(AuthRequestParams(
877885
domain: "etherscan.io",
878886
chains: ["eip155:1"],
@@ -885,23 +893,49 @@ final class SignClientTests: XCTestCase {
885893
resources: nil,
886894
methods: nil
887895
))!
896+
print("🧪TEST: Received URI from dapp.authenticate(...): \(uri)")
888897

898+
// Step 3: Wallet pairs with the URI
899+
print("🧪TEST: Step 3 - Pairing on wallet with URI: \(uri)")
889900
try await walletPairingClient.pair(uri: uri)
890901

902+
// Step 4: Wallet handles authenticate requests
903+
print("🧪TEST: Step 4 - Subscribing to wallet.authenticateRequestPublisher...")
891904
wallet.authenticateRequestPublisher.sink { [unowned self] (request, _) in
905+
print("🧪TEST: Wallet received authenticate request. Building EIP1271 cacao and approving...")
906+
892907
Task(priority: .high) {
893-
let signature = CacaoSignature(t: .eip1271, s: eip1271Signature)
894-
let cacao = try! wallet.buildSignedAuthObject(authPayload: request.payload, signature: signature, account: account)
895-
_ = try await wallet.approveSessionAuthenticate(requestId: request.id, auths: [cacao])
908+
do {
909+
let signature = CacaoSignature(t: .eip1271, s: eip1271Signature)
910+
let cacao = try wallet.buildSignedAuthObject(authPayload: request.payload, signature: signature, account: account)
911+
print("🧪TEST: EIP1271 cacao built successfully. Approving session authenticate...")
912+
_ = try await wallet.approveSessionAuthenticate(requestId: request.id, auths: [cacao])
913+
print("🧪TEST: Session authenticate approved for requestId: \(request.id)")
914+
} catch {
915+
XCTFail("Failed to approve session authenticate with EIP1271: \(error)")
916+
}
896917
}
897918
}
898919
.store(in: &publishers)
920+
921+
// Step 5: Dapp listens for auth response
922+
print("🧪TEST: Step 5 - Subscribing to dapp.authResponsePublisher...")
899923
dapp.authResponsePublisher.sink { (_, result) in
900-
guard case .success = result else { XCTFail(); return }
924+
print("🧪TEST: Dapp received auth response.")
925+
guard case .success = result else {
926+
XCTFail("Dapp authResponsePublisher received failure.")
927+
return
928+
}
929+
print("🧪TEST: Dapp auth response succeeded. Fulfilling expectation.")
901930
responseExpectation.fulfill()
902931
}
903932
.store(in: &publishers)
933+
934+
// Step 6: Wait for the result
935+
print("🧪TEST: Step 6 - Waiting for response expectation (timeout = \(InputConfig.defaultTimeout) seconds)...")
904936
await fulfillment(of: [responseExpectation], timeout: InputConfig.defaultTimeout)
937+
938+
print("🧪TEST: Finished testEIP1271SessionAuthenticated() ✅")
905939
}
906940

907941
func testEIP191SessionAuthenticateSignatureVerificationFailed() async {
@@ -1328,66 +1362,126 @@ final class SignClientTests: XCTestCase {
13281362
}
13291363

13301364
func testUpgradeSessionToLinkModeAndSendRequestOverLinkMode() async throws {
1365+
print("🧪TEST: Starting testUpgradeSessionToLinkModeAndSendRequestOverLinkMode...")
13311366

1367+
// Step 1: Set up the Dapp for link mode
1368+
print("🧪TEST: Step 1 - Calling setUpDappForLinkMode()")
13321369
try await setUpDappForLinkMode()
1370+
print("🧪TEST: Finished setUpDappForLinkMode()")
1371+
13331372
let requestMethod = "personal_sign"
13341373
let requestParams = [EthSendTransaction.stub()]
1374+
let responseParams = "0xdeadbeef"
13351375
let sessionResponseOnLinkModeExpectation = expectation(description: "Dapp expects to receive a response")
13361376

1337-
let responseParams = "0xdeadbeef"
1377+
// We'll use this semaphore to ensure correct ordering of tasks
13381378
let semaphore = DispatchSemaphore(value: 0)
13391379

1380+
print("🧪TEST: Subscribing to wallet.authenticateRequestPublisher...")
13401381
wallet.authenticateRequestPublisher.sink { [unowned self] (request, _) in
1382+
print("🧪TEST: Received authenticate request from wallet.authenticateRequestPublisher. Processing...")
13411383

13421384
Task(priority: .high) {
1343-
let signerFactory = DefaultSignerFactory()
1344-
let signer = MessageSignerFactory(signerFactory: signerFactory).create()
1345-
1346-
let supportedAuthPayload = try! wallet.buildAuthPayload(payload: request.payload, supportedEVMChains: [Blockchain("eip155:1")!, Blockchain("eip155:137")!], supportedMethods: ["eth_signTransaction", "personal_sign"])
1347-
1348-
let siweMessage = try! wallet.formatAuthMessage(payload: supportedAuthPayload, account: walletAccount)
1349-
1350-
let signature = try signer.sign(
1351-
message: siweMessage,
1352-
privateKey: prvKey,
1353-
type: .eip191)
1354-
1355-
let auth = try wallet.buildSignedAuthObject(authPayload: supportedAuthPayload, signature: signature, account: walletAccount)
1385+
do {
1386+
let signerFactory = DefaultSignerFactory()
1387+
let signer = MessageSignerFactory(signerFactory: signerFactory).create()
13561388

1357-
_ = try! await wallet.approveSessionAuthenticate(requestId: request.id, auths: [auth])
1358-
semaphore.signal()
1389+
let supportedAuthPayload = try wallet.buildAuthPayload(
1390+
payload: request.payload,
1391+
supportedEVMChains: [Blockchain("eip155:1")!, Blockchain("eip155:137")!],
1392+
supportedMethods: ["eth_signTransaction", "personal_sign"]
1393+
)
1394+
let siweMessage = try wallet.formatAuthMessage(payload: supportedAuthPayload, account: walletAccount)
1395+
let signature = try signer.sign(message: siweMessage, privateKey: prvKey, type: .eip191)
1396+
let auth = try wallet.buildSignedAuthObject(
1397+
authPayload: supportedAuthPayload,
1398+
signature: signature,
1399+
account: walletAccount
1400+
)
1401+
1402+
print("🧪TEST: Approving session authenticate on wallet...")
1403+
_ = try await wallet.approveSessionAuthenticate(requestId: request.id, auths: [auth])
1404+
print("🧪TEST: Wallet approved session authenticate. Signaling semaphore.")
1405+
semaphore.signal()
1406+
} catch {
1407+
XCTFail("Failed to approve session authenticate: \(error)")
1408+
semaphore.signal()
1409+
}
13591410
}
13601411
}
13611412
.store(in: &publishers)
1413+
1414+
print("🧪TEST: Subscribing to dapp.authResponsePublisher...")
13621415
dapp.authResponsePublisher.sink { [unowned self] (_, result) in
1416+
print("🧪TEST: Dapp received auth response. Waiting on semaphore...")
13631417
semaphore.wait()
1418+
1419+
// After the wallet’s auth, we force block publishing to simulate link mode usage only
1420+
print("🧪TEST: Blocking relay publishing to use link mode exclusively...")
13641421
dappRelayClient.blockPublishing = true
13651422
walletRelayClient.blockPublishing = true
1366-
guard case .success(let (session, _)) = result,
1367-
let session = session else { XCTFail(); return }
13681423

1369-
Task { [unowned self] in
1370-
let request = try! Request(id: RPCID(0), topic: session.topic, method: requestMethod, params: requestParams, chainId: Blockchain("eip155:1")!)
1371-
let requestEnvelope = try! await self.dapp.requestLinkMode(params: request)!
1372-
try! self.wallet.dispatchEnvelope(requestEnvelope)
1424+
guard case .success(let (session, _)) = result, let session = session else {
1425+
XCTFail("Auth response did not return a valid session.")
1426+
return
1427+
}
1428+
print("🧪TEST: Auth responded with a valid session. Topic: \(session.topic)")
1429+
1430+
Task(priority: .high) {
1431+
do {
1432+
print("🧪TEST: Sending link-mode request from dapp to wallet...")
1433+
let request = try Request(
1434+
id: RPCID(0),
1435+
topic: session.topic,
1436+
method: requestMethod,
1437+
params: requestParams,
1438+
chainId: Blockchain("eip155:1")!
1439+
)
1440+
let requestEnvelope = try await self.dapp.requestLinkMode(params: request)!
1441+
try self.wallet.dispatchEnvelope(requestEnvelope)
1442+
print("🧪TEST: Dispatched the request envelope to the wallet over link mode.")
1443+
} catch {
1444+
XCTFail("Failed to dispatch link-mode request: \(error)")
1445+
}
13731446
}
13741447
}
13751448
.store(in: &publishers)
13761449

1450+
print("🧪TEST: Subscribing to wallet.sessionRequestPublisher...")
13771451
wallet.sessionRequestPublisher.sink { [unowned self] (sessionRequest, _) in
1452+
print("🧪TEST: Wallet received a session request. Preparing link-mode response...")
1453+
13781454
Task(priority: .high) {
1379-
let envelope = try! await wallet.respondLinkMode(topic: sessionRequest.topic, requestId: sessionRequest.id, response: .response(AnyCodable(responseParams)))!
1380-
try! dapp.dispatchEnvelope(envelope)
1455+
do {
1456+
let envelope = try await wallet.respondLinkMode(
1457+
topic: sessionRequest.topic,
1458+
requestId: sessionRequest.id,
1459+
response: .response(AnyCodable(responseParams))
1460+
)!
1461+
try dapp.dispatchEnvelope(envelope)
1462+
print("🧪TEST: Responded to request and dispatched the response envelope back to Dapp.")
1463+
} catch {
1464+
XCTFail("Failed to respond or dispatch envelope: \(error)")
1465+
}
13811466
}
1382-
}.store(in: &publishers)
1467+
}
1468+
.store(in: &publishers)
13831469

1470+
print("🧪TEST: Subscribing to dapp.sessionResponsePublisher...")
13841471
dapp.sessionResponsePublisher.sink { response in
1472+
print("🧪TEST: Dapp received session response. Fulfilling expectation...")
13851473
sessionResponseOnLinkModeExpectation.fulfill()
1386-
}.store(in: &publishers)
1474+
}
1475+
.store(in: &publishers)
13871476

1477+
print("🧪TEST: Starting normal authenticate over universal link: \(walletLinkModeUniversalLink)")
13881478
let uri = try await dapp.authenticate(AuthRequestParams.stub(), walletUniversalLink: walletLinkModeUniversalLink)!
1479+
print("🧪TEST: Pairing on wallet with URI: \(uri)")
13891480
try await walletPairingClient.pair(uri: uri)
1481+
1482+
print("🧪TEST: Waiting for sessionResponseOnLinkModeExpectation (timeout = \(InputConfig.defaultTimeout) seconds)...")
13901483
await fulfillment(of: [sessionResponseOnLinkModeExpectation], timeout: InputConfig.defaultTimeout)
1391-
}
13921484

1485+
print("🧪TEST: Finished testUpgradeSessionToLinkModeAndSendRequestOverLinkMode ✅")
1486+
}
13931487
}

0 commit comments

Comments
 (0)