-
Notifications
You must be signed in to change notification settings - Fork 191
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
[Chat] sync chat sdk with kotlin implementation and add reject method #322
Changes from 12 commits
8a47f9f
3739c4e
61c00e6
3ab42dc
4ab7d33
8b00b2c
7ca77e3
4862d0e
0de54ce
bb87e42
46f0d60
dbd9cce
14b8d1e
0ffedc8
08b49ee
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
import Foundation | ||
|
||
class LeaveService { | ||
func leave(topic: String) { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ class MessagingService { | |
// TODO - manage author account | ||
let thread = await threadStore.first {$0.topic == topic} | ||
guard let authorAccount = thread?.selfAccount else { throw Errors.threadDoNotExist} | ||
let message = Message(topic: topic, message: messageString, authorAccount: authorAccount, timestamp: JsonRpcID.generate()) | ||
let timestamp = Int64(Date().timeIntervalSince1970 * 1000) | ||
let message = Message(topic: topic, message: messageString, authorAccount: authorAccount, timestamp: timestamp) | ||
let request = JSONRPCRequest<ChatRequestParams>(params: .message(message)) | ||
try await networkingInteractor.request(request, topic: topic, envelopeType: .type0) | ||
Task(priority: .background) { | ||
|
@@ -52,7 +53,8 @@ class MessagingService { | |
private func setUpRequestHandling() { | ||
networkingInteractor.requestPublisher.sink { [unowned self] subscriptionPayload in | ||
switch subscriptionPayload.request.params { | ||
case .message(let message): | ||
case .message(var message): | ||
message.topic = subscriptionPayload.topic | ||
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. Why we need to do that? Is it possible to avoid struct mutation? 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.
|
||
handleMessage(message) | ||
default: | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ class InvitationHandlingService { | |
|
||
let selfThreadPubKey = try kms.createX25519KeyPair() | ||
|
||
let inviteResponse = InviteResponse(pubKey: selfThreadPubKey.hexRepresentation) | ||
let inviteResponse = InviteResponse(publicKey: selfThreadPubKey.hexRepresentation) | ||
|
||
let response = JsonRpcResult.response(JSONRPCResponse<AnyCodable>(id: payload.request.id, result: AnyCodable(inviteResponse))) | ||
|
||
|
@@ -52,15 +52,15 @@ class InvitationHandlingService { | |
|
||
try await networkingInteractor.respond(topic: responseTopic, response: response) | ||
|
||
let threadAgreementKeys = try kms.performKeyAgreement(selfPublicKey: selfThreadPubKey, peerPublicKey: invite.pubKey) | ||
let threadAgreementKeys = try kms.performKeyAgreement(selfPublicKey: selfThreadPubKey, peerPublicKey: invite.publicKey) | ||
|
||
let threadTopic = threadAgreementKeys.derivedTopic() | ||
|
||
try kms.setSymmetricKey(threadAgreementKeys.sharedKey, for: threadTopic) | ||
|
||
try await networkingInteractor.subscribe(topic: threadTopic) | ||
|
||
logger.debug("Accepting an invite") | ||
logger.debug("Accepting an invite on topic: \(threadTopic)") | ||
|
||
// TODO - derive account | ||
let selfAccount = Account("eip155:56:0xe5EeF1368781911d265fDB6946613dA61915a501")! | ||
|
@@ -70,6 +70,23 @@ class InvitationHandlingService { | |
onNewThread?(thread) | ||
} | ||
|
||
func reject(inviteId: String) async throws { | ||
|
||
guard let payload = try invitePayloadStore.get(key: inviteId) else { throw Error.inviteForIdNotFound } | ||
|
||
guard case .invite(let invite) = payload.request.params else {return} | ||
|
||
let responseTopic = try getInviteResponseTopic(payload, invite) | ||
|
||
//TODO - error not in specs yet | ||
let error = JSONRPCErrorResponse.Error(code: 0, message: "user rejected") | ||
let response = JsonRpcResult.error(JSONRPCErrorResponse(id: payload.request.id, error: error)) | ||
|
||
try await networkingInteractor.respond(topic: responseTopic, response: response) | ||
|
||
invitePayloadStore.delete(forKey: inviteId) | ||
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. gj, need to do the same for approve |
||
} | ||
|
||
private func setUpRequestHandling() { | ||
networkingInteractor.requestPublisher.sink { [unowned self] subscriptionPayload in | ||
switch subscriptionPayload.request.params { | ||
|
@@ -87,7 +104,7 @@ class InvitationHandlingService { | |
|
||
private func handleInvite(_ invite: Invite, _ payload: RequestSubscriptionPayload) throws { | ||
logger.debug("did receive an invite") | ||
invitePayloadStore.set(payload, forKey: invite.pubKey) | ||
invitePayloadStore.set(payload, forKey: invite.publicKey) | ||
onInvite?(invite) | ||
} | ||
|
||
|
@@ -101,7 +118,7 @@ class InvitationHandlingService { | |
|
||
let selfPubKey = try AgreementPublicKey(hex: selfPubKeyHex) | ||
|
||
let agreementKeysI = try kms.performKeyAgreement(selfPublicKey: selfPubKey, peerPublicKey: invite.pubKey) | ||
let agreementKeysI = try kms.performKeyAgreement(selfPublicKey: selfPubKey, peerPublicKey: invite.publicKey) | ||
|
||
// agreement keys already stored by serializer | ||
let responseTopic = agreementKeysI.derivedTopic() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,13 +25,16 @@ class InviteService { | |
} | ||
|
||
var peerAccount: Account! | ||
|
||
func invite(peerPubKey: String, peerAccount: Account, openingMessage: String, account: Account) async throws { | ||
// TODO ad storage | ||
self.peerAccount = peerAccount | ||
let selfPubKeyY = try kms.createX25519KeyPair() | ||
let invite = Invite(message: openingMessage, account: account, pubKey: selfPubKeyY.hexRepresentation) | ||
let invite = Invite(message: openingMessage, account: account, publicKey: selfPubKeyY.hexRepresentation) | ||
let symKeyI = try kms.performKeyAgreement(selfPublicKey: selfPubKeyY, peerPublicKey: peerPubKey) | ||
let inviteTopic = try AgreementPublicKey(hex: peerPubKey).rawRepresentation.sha256().toHexString() | ||
|
||
// overrides on invite toipic | ||
try kms.setSymmetricKey(symKeyI.sharedKey, for: inviteTopic) | ||
|
||
let request = JSONRPCRequest<ChatRequestParams>(params: .invite(invite)) | ||
|
@@ -42,7 +45,6 @@ class InviteService { | |
try kms.setSymmetricKey(symKeyI.sharedKey, for: responseTopic) | ||
|
||
try await networkingInteractor.subscribe(topic: responseTopic) | ||
|
||
try await networkingInteractor.request(request, topic: inviteTopic, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation)) | ||
|
||
logger.debug("invite sent on topic: \(inviteTopic)") | ||
|
@@ -67,7 +69,7 @@ class InviteService { | |
let inviteResponse = try jsonrpc.result.get(InviteResponse.self) | ||
logger.debug("Invite has been accepted") | ||
guard case .invite(let inviteParams) = response.requestParams else { return } | ||
Task { try await createThread(selfPubKeyHex: inviteParams.pubKey, peerPubKey: inviteResponse.pubKey, account: inviteParams.account, peerAccount: peerAccount)} | ||
Task { try await createThread(selfPubKeyHex: inviteParams.publicKey, peerPubKey: inviteResponse.publicKey, account: inviteParams.account, peerAccount: peerAccount)} | ||
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. let's use priority .background everywhere? |
||
} catch { | ||
logger.debug("Handling invite response has failed") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,46 @@ import Foundation | |
import WalletConnectUtils | ||
|
||
enum ChatRequestParams: Codable, Equatable { | ||
enum Errors: Error { | ||
case decoding | ||
} | ||
case invite(Invite) | ||
case message(Message) | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case invite | ||
case message | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
switch self { | ||
case .invite(let invite): | ||
try invite.encode(to: encoder) | ||
case .message(let message): | ||
try message.encode(to: encoder) | ||
} | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
if let invite = try? Invite(from: decoder) { | ||
self = .invite(invite) | ||
} else if let massage = try? Message(from: decoder) { | ||
self = .message(massage) | ||
} else { | ||
throw Errors.decoding | ||
} | ||
|
||
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. new line |
||
} | ||
} | ||
|
||
extension JSONRPCRequest { | ||
init(id: Int64 = JsonRpcID.generate(), params: T) where T == ChatRequestParams { | ||
var method: String! | ||
switch params { | ||
case .invite: | ||
method = "invite" | ||
method = "wc_chatInvite" | ||
case .message: | ||
method = "message" | ||
method = "wc_chatMessage" | ||
} | ||
self.init(id: id, method: method, params: params) | ||
} | ||
|
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.
Unused?