Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#347 extend pairing on approve proposal #373

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -82,7 +81,15 @@ final class ApproveEngine {
let proposeResponse = SessionType.ProposeResponse(relay: relay, responderPublicKey: selfPublicKey.hexRepresentation)
let response = JSONRPCResponse<AnyCodable>(id: payload.wcRequest.id, result: AnyCodable(proposeResponse))

guard var pairing = pairingStore.getPairing(forTopic: payload.topic) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we care about pairing is active / inactive ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think on wallet pairing is always active

Copy link
Contributor Author

@llbartekll llbartekll Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly

throw Errors.pairingNotFound
}

try await networkingInteractor.respond(topic: payload.topic, response: .response(response), tag: payload.wcRequest.responseTag)

try pairing.updateExpiry()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also extending pairing in handleProposeResponse. So in approve method we extending for wallet, and in handleProposeResponse we extending for dapp, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

pairingStore.setPairing(pairing)

try await settle(topic: sessionTopic, proposal: proposal, namespaces: sessionNamespaces)
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/WalletConnectSignTests/ApproveEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions Tests/WalletConnectSignTests/Stub/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down