-
Notifications
You must be signed in to change notification settings - Fork 172
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
#211 Namespace validation #215
Changes from all commits
9ede85b
cf06ab2
f0115d4
24360fc
c107293
31e514c
d04d0a9
f858656
2ff4871
f084808
7b06353
b954a02
2bd7fd7
90f7b60
eedabee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ final class SessionEngine { | |
var onSessionDelete: ((String, SessionType.Reason)->())? | ||
var onEventReceived: ((String, Session.Event, Blockchain?)->())? | ||
|
||
var settlingProposal: SessionProposal? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could it be overriden? |
||
|
||
private let sessionStore: WCSessionStorage | ||
private let pairingStore: WCPairingStorage | ||
private let sessionToPairingTopic: KeyValueStore<String> | ||
|
@@ -59,6 +61,30 @@ final class SessionEngine { | |
sessionStore.getAcknowledgedSessions().map{$0.publicRepresentation()} | ||
} | ||
|
||
func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace]) throws { | ||
let agreementKeys = try! kms.getAgreementSecret(for: topic)! | ||
|
||
let selfParticipant = Participant(publicKey: agreementKeys.publicKey.hexRepresentation, metadata: metadata) | ||
|
||
let expectedExpiryTimeStamp = Date().addingTimeInterval(TimeInterval(WCSession.defaultTimeToLive)) | ||
guard let relay = proposal.relays.first else {return} | ||
let settleParams = SessionType.SettleParams( | ||
relay: relay, | ||
controller: selfParticipant, | ||
namespaces: namespaces, | ||
expiry: Int64(expectedExpiryTimeStamp.timeIntervalSince1970))//todo - test expiration times | ||
let session = WCSession( | ||
topic: topic, | ||
selfParticipant: selfParticipant, | ||
peerParticipant: proposal.proposer, | ||
settleParams: settleParams, | ||
acknowledged: false) | ||
logger.debug("Sending session settle request") | ||
Task { try? await networkingInteractor.subscribe(topic: topic) } | ||
sessionStore.setSession(session) | ||
networkingInteractor.request(.wcSessionSettle(settleParams), onTopic: topic) | ||
} | ||
|
||
func delete(topic: String, reason: Reason) async throws { | ||
logger.debug("Will delete session for reason: message: \(reason.message) code: \(reason.code)") | ||
try await networkingInteractor.request(.wcSessionDelete(reason.internalRepresentation()), onTopic: topic) | ||
|
@@ -139,34 +165,24 @@ final class SessionEngine { | |
} | ||
}.store(in: &publishers) | ||
} | ||
|
||
func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace]) throws { | ||
try Validator.validate(namespaces) // FIXME: Validation should happen before responding proposal, before settlement | ||
let agreementKeys = try! kms.getAgreementSecret(for: topic)! | ||
|
||
let selfParticipant = Participant(publicKey: agreementKeys.publicKey.hexRepresentation, metadata: metadata) | ||
|
||
let expectedExpiryTimeStamp = Date().addingTimeInterval(TimeInterval(WCSession.defaultTimeToLive)) | ||
guard let relay = proposal.relays.first else {return} | ||
let settleParams = SessionType.SettleParams( | ||
relay: relay, | ||
controller: selfParticipant, | ||
namespaces: namespaces, | ||
expiry: Int64(expectedExpiryTimeStamp.timeIntervalSince1970))//todo - test expiration times | ||
let session = WCSession( | ||
topic: topic, | ||
selfParticipant: selfParticipant, | ||
peerParticipant: proposal.proposer, | ||
settleParams: settleParams, | ||
acknowledged: false) | ||
logger.debug("Sending session settle request") | ||
Task { try? await networkingInteractor.subscribe(topic: topic) } | ||
sessionStore.setSession(session) | ||
networkingInteractor.request(.wcSessionSettle(settleParams), onTopic: topic) | ||
} | ||
|
||
|
||
private func onSessionSettle(payload: WCRequestSubscriptionPayload, settleParams: SessionType.SettleParams) { | ||
logger.debug("Did receive session settle request") | ||
guard let proposedNamespaces = settlingProposal?.requiredNamespaces else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🤔 am I correct? will it work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. splitting engines/methods for controller/non-controller later will really help. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually they both happen on proposer's side, so it will work. The thing is that they happen on different engines. There may be a better solution to this. |
||
// TODO: respond error | ||
return | ||
} | ||
settlingProposal = nil | ||
let sessionNamespaces = settleParams.namespaces | ||
do { | ||
try Namespace.validate(proposedNamespaces) | ||
try Namespace.validate(sessionNamespaces) | ||
try Namespace.validateApproved(sessionNamespaces, against: proposedNamespaces) | ||
} catch { | ||
// TODO: respond error | ||
return | ||
} | ||
|
||
let topic = payload.topic | ||
|
||
let agreementKeys = try! kms.getAgreementSecret(for: topic)! | ||
|
@@ -177,12 +193,12 @@ final class SessionEngine { | |
updatePairingMetadata(topic: pairingTopic, metadata: settleParams.controller.metadata) | ||
} | ||
|
||
// TODO: Validate namespaces | ||
let session = WCSession(topic: topic, | ||
selfParticipant: selfParticipant, | ||
peerParticipant: settleParams.controller, | ||
settleParams: settleParams, | ||
acknowledged: true) | ||
let session = WCSession( | ||
topic: topic, | ||
selfParticipant: selfParticipant, | ||
peerParticipant: settleParams.controller, | ||
settleParams: settleParams, | ||
acknowledged: true) | ||
sessionStore.setSession(session) | ||
networkingInteractor.respondSuccess(for: payload) | ||
onSessionSettle?(session.publicRepresentation()) | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think those two validations should not be in do{}catch{} block but rather throw an error so user can see that minimal requirement(or other requirement) has not been met.
I am also not sure if it make sense to respond with error for them in catch{} block
that is not valid error for a peer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the validation error catch, it has to be thrown. I just used it as a temporary solution to make the validation start working. An issue can be created for that to improve error reading.
The catch respond error already existed before, but only when the key agreement failed. Should this be changed also?