Skip to content

Commit

Permalink
Merge pull request #373 from WalletConnect/#347-Extend-pairing-on-app…
Browse files Browse the repository at this point in the history
…rove-Proposal

#347 extend pairing on approve proposal
  • Loading branch information
llbartekll authored Jul 28, 2022
2 parents 0614b88 + 05b082c commit bd965c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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 {
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)
}

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

0 comments on commit bd965c0

Please sign in to comment.