diff --git a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift index 0b602f839..ca2d1ed75 100644 --- a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift @@ -71,7 +71,6 @@ final class ApproveEngine { peerPublicKey: proposal.proposer.publicKey ) else { throw Errors.agreementMissingOrInvalid } - // TODO: Extend pairing let sessionTopic = agreementKey.derivedTopic() try kms.setAgreementSecret(agreementKey, topic: sessionTopic) @@ -82,7 +81,15 @@ final class ApproveEngine { let proposeResponse = SessionType.ProposeResponse(relay: relay, responderPublicKey: selfPublicKey.hexRepresentation) let response = JSONRPCResponse(id: payload.wcRequest.id, result: AnyCodable(proposeResponse)) + guard var pairing = pairingStore.getPairing(forTopic: payload.topic) else { + throw Errors.pairingNotFound + } + try await networkingInteractor.respond(topic: payload.topic, response: .response(response), tag: payload.wcRequest.responseTag) + + try pairing.updateExpiry() + pairingStore.setPairing(pairing) + try await settle(topic: sessionTopic, proposal: proposal, namespaces: sessionNamespaces) } diff --git a/Tests/WalletConnectSignTests/ApproveEngineTests.swift b/Tests/WalletConnectSignTests/ApproveEngineTests.swift index ac6098c34..a6590d302 100644 --- a/Tests/WalletConnectSignTests/ApproveEngineTests.swift +++ b/Tests/WalletConnectSignTests/ApproveEngineTests.swift @@ -47,6 +47,8 @@ final class ApproveEngineTests: XCTestCase { func testApproveProposal() async throws { // Client receives a proposal let topicA = String.generateTopic() + let pairing = WCPairing.stub(expiryDate: Date(timeIntervalSinceNow: 10000), topic: topicA) + pairingStorageMock.setPairing(pairing) let proposerPubKey = AgreementPrivateKey().publicKey.hexRepresentation let proposal = SessionProposal.stub(proposerPubKey: proposerPubKey) let request = WCRequest(method: .sessionPropose, params: .sessionPropose(proposal)) @@ -57,9 +59,11 @@ final class ApproveEngineTests: XCTestCase { let topicB = networkingInteractor.subscriptions.last! + let extendedPairing = pairingStorageMock.getPairing(forTopic: topicA)! XCTAssertTrue(networkingInteractor.didCallSubscribe) XCTAssert(cryptoMock.hasAgreementSecret(for: topicB), "Responder must store agreement key for topic B") XCTAssertEqual(networkingInteractor.didRespondOnTopic!, topicA, "Responder must respond on topic A") + XCTAssertEqual(extendedPairing.expiryDate.timeIntervalSince1970, Date(timeIntervalSinceNow: 2_592_000).timeIntervalSince1970, accuracy: 1, "pairing expiry has been extended by 30 days") } func testReceiveProposal() { diff --git a/Tests/WalletConnectSignTests/Stub/Stubs.swift b/Tests/WalletConnectSignTests/Stub/Stubs.swift index 813243610..758488445 100644 --- a/Tests/WalletConnectSignTests/Stub/Stubs.swift +++ b/Tests/WalletConnectSignTests/Stub/Stubs.swift @@ -16,14 +16,14 @@ extension AppMetadata { } extension Pairing { - static func stub(expiryDate: Date = Date(timeIntervalSinceNow: 10000)) -> Pairing { - Pairing(topic: String.generateTopic(), peer: nil, expiryDate: expiryDate) + static func stub(expiryDate: Date = Date(timeIntervalSinceNow: 10000), topic: String = String.generateTopic()) -> Pairing { + Pairing(topic: topic, peer: nil, expiryDate: expiryDate) } } extension WCPairing { - static func stub(expiryDate: Date = Date(timeIntervalSinceNow: 10000), isActive: Bool = true) -> WCPairing { - WCPairing(topic: String.generateTopic(), relay: RelayProtocolOptions.stub(), peerMetadata: AppMetadata.stub(), isActive: isActive, expiryDate: expiryDate) + static func stub(expiryDate: Date = Date(timeIntervalSinceNow: 10000), isActive: Bool = true, topic: String = String.generateTopic()) -> WCPairing { + WCPairing(topic: topic, relay: RelayProtocolOptions.stub(), peerMetadata: AppMetadata.stub(), isActive: isActive, expiryDate: expiryDate) } }