Skip to content

Commit

Permalink
refactor:decrypt message
Browse files Browse the repository at this point in the history
  • Loading branch information
MdTeach committed Jun 9, 2023
1 parent a5ad794 commit c76a10c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
38 changes: 28 additions & 10 deletions Sources/Chat/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,29 @@ extension PushChat {
}
}

public static func Latest(threadHash: String, pgpPrivateKey: String, env: ENV) async throws
public static func Latest(threadHash: String, pgpPrivateKey: String, toDecrypt: Bool, env: ENV)
async throws
-> Message
{
return try await History(
threadHash: threadHash, limit: 1, pgpPrivateKey: pgpPrivateKey, env: env
threadHash: threadHash, limit: 1, pgpPrivateKey: pgpPrivateKey, toDecrypt: toDecrypt, env: env
).first!
}

public static func History(threadHash: String, limit: Int, pgpPrivateKey: String, env: ENV)
public static func History(
threadHash: String, limit: Int, pgpPrivateKey: String, toDecrypt: Bool, env: ENV
)
async throws -> [Message]
{
do {
var messages = try await getMessagesService(threadHash: threadHash, limit: limit, env: env)

for i in 0..<messages.count {
let decryptedMsg = try decryptMessage(
message: messages[i], privateKeyArmored: pgpPrivateKey)
messages[i].messageContent = decryptedMsg
if toDecrypt {
for i in 0..<messages.count {
let decryptedMsg = try decryptMessage(
message: messages[i], privateKeyArmored: pgpPrivateKey)
messages[i].messageContent = decryptedMsg
}
}

return messages
Expand Down Expand Up @@ -78,11 +83,25 @@ extension PushChat {
if message.encType != "pgp" {
return message.messageContent
}
return try decryptMessage(
message.messageContent, encryptedSecret: message.encryptedSecret,
privateKeyArmored: privateKeyArmored)
} catch {
return "Unable to decrypt message"
}
}

public static func decryptMessage(
_ message: String,
encryptedSecret: String,
privateKeyArmored: String
) throws -> String {
do {

let secretKey = try Pgp.pgpDecrypt(
cipherText: message.encryptedSecret, toPrivateKeyArmored: privateKeyArmored)
cipherText: encryptedSecret, toPrivateKeyArmored: privateKeyArmored)

let userMsg = AESCBCHelper.decrypt(cipherText: message.messageContent, secretKey: secretKey)!
let userMsg = AESCBCHelper.decrypt(cipherText: message, secretKey: secretKey)!
let userMsgStr = String(data: userMsg, encoding: .utf8)

if userMsgStr == nil {
Expand All @@ -94,5 +113,4 @@ extension PushChat {
return "Unable to decrypt message"
}
}

}
3 changes: 2 additions & 1 deletion Tests/Chat/GetChatsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class GetChatsTests: XCTestCase {
conversationId: "0x4D5bE92D510300ceF50a2FC03534A95b60028950", account: userAddress)!

let messages = try await PushChat.History(
threadHash: converationHash, limit: 5, pgpPrivateKey: pgpPrivateKey, env: .STAGING)
threadHash: converationHash, limit: 5, pgpPrivateKey: pgpPrivateKey, toDecrypt: true,
env: .STAGING)

for msg in messages {
XCTAssert(msg.messageContent.count > 0)
Expand Down
8 changes: 4 additions & 4 deletions Tests/Chat/SendTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SendChatsTests: XCTestCase {
let threadHash = try await PushChat.ConversationHash(
conversationId: recipientAddress, account: senderAddress)!
let latestMessage = try await PushChat.History(
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, env: .STAGING
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, toDecrypt: true, env: .STAGING
).first!.messageContent

XCTAssertEqual(latestMessage, messageToSen)
Expand All @@ -55,7 +55,7 @@ class SendChatsTests: XCTestCase {
let threadHash = try await PushChat.ConversationHash(
conversationId: recipientAddress, account: senderAddress)!
let latestMessage = try await PushChat.History(
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, env: .STAGING
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, toDecrypt: true, env: .STAGING
).first!.messageContent

XCTAssertEqual(latestMessage, messageToSen)
Expand All @@ -81,7 +81,7 @@ class SendChatsTests: XCTestCase {
let threadHash = try await PushChat.ConversationHash(
conversationId: recipientAddress, account: senderAddress)!
let latestMessage1 = try await PushChat.History(
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, env: .STAGING
threadHash: threadHash, limit: 1, pgpPrivateKey: senderPgpPk, toDecrypt: true, env: .STAGING
).first!.messageContent

let _ = try await Push.PushChat.send(
Expand All @@ -96,7 +96,7 @@ class SendChatsTests: XCTestCase {
let threadHash2 = try await PushChat.ConversationHash(
conversationId: recipientAddress, account: senderAddress)!
let latestMessage2 = try await PushChat.History(
threadHash: threadHash2, limit: 1, pgpPrivateKey: senderPgpPk, env: .STAGING
threadHash: threadHash2, limit: 1, pgpPrivateKey: senderPgpPk, toDecrypt: true, env: .STAGING
).first!.messageContent

XCTAssertEqual(latestMessage1, messageToSen1)
Expand Down
19 changes: 0 additions & 19 deletions Tests/Chat/temp.swift

This file was deleted.

0 comments on commit c76a10c

Please sign in to comment.