From 6d39d18cb162f17b2c65191e8bd96c6de9907e56 Mon Sep 17 00:00:00 2001 From: Abishek Bashyal Date: Fri, 2 Jun 2023 11:43:03 +0545 Subject: [PATCH] refactor:Push Prefix added --- README.md | 7 +- Sources/Chat/Conversation.swift | 2 +- Sources/Chat/Inbox.swift | 2 +- Sources/Chat/Requests.swift | 13 ++++ Sources/Chat/Send.swift | 92 +++++++++++++++++++++++-- Sources/Endpoints/ChatEndpoint.swift | 9 +++ Sources/Helpers/Chat/GetInboxList.swift | 6 +- Sources/User/CreateUser.swift | 10 +-- Sources/User/DecryptPgp.swift | 2 +- Sources/User/GetFeeds.swift | 2 +- Sources/User/User.swift | 10 +-- Tests/Chat/ConversationTests.swift | 4 +- Tests/Chat/GetChatsTests.swift | 14 ++-- Tests/Chat/SendTests.swift | 87 +++++++++++++++++++---- Tests/Helper/PgpTests.swift | 28 ++++++++ Tests/User/CreateUserTests.swift | 6 +- Tests/User/GetFeedsTests.swift | 4 +- Tests/User/GetTests.swift | 10 +-- 18 files changed, 252 insertions(+), 56 deletions(-) create mode 100644 Sources/Chat/Requests.swift diff --git a/README.md b/README.md index cf59f49..7bc7de3 100644 --- a/README.md +++ b/README.md @@ -543,6 +543,7 @@ let message = try await Chats.Latest( ### **Fetching chat history between two users** + ```swift const chatHistory:[Message] = await Chat.History( threadhash: String; @@ -692,4 +693,8 @@ let messages:[Message] = try await Chats.History( ------ \ No newline at end of file + + + + +----- diff --git a/Sources/Chat/Conversation.swift b/Sources/Chat/Conversation.swift index cdc0452..90f0a1f 100644 --- a/Sources/Chat/Conversation.swift +++ b/Sources/Chat/Conversation.swift @@ -1,6 +1,6 @@ import Foundation -extension Chats { +extension PushChat { struct Hash: Codable { var threadHash: String? } public static func ConversationHash( diff --git a/Sources/Chat/Inbox.swift b/Sources/Chat/Inbox.swift index 71c2ab1..ce8f3a7 100644 --- a/Sources/Chat/Inbox.swift +++ b/Sources/Chat/Inbox.swift @@ -54,7 +54,7 @@ enum ChatError: Error { case chatError(String) } -public struct Chats { +public struct PushChat { public static func getChats(options: GetChatsOptions) async throws -> [Feeds] { if !isValidETHAddress(address: options.account) { throw ChatError.invalidAddress diff --git a/Sources/Chat/Requests.swift b/Sources/Chat/Requests.swift new file mode 100644 index 0000000..eca954c --- /dev/null +++ b/Sources/Chat/Requests.swift @@ -0,0 +1,13 @@ +// extension Chats { +// public struct RequestOptionsType: Codable { +// var account: String +// var pgpPrivateKey: String +// var env: ENV +// var toDecrypt: Bool = true +// var page: Int = 1 +// var limit: Int = 10 +// } +// public static func requests() async throws { + +// } +// } diff --git a/Sources/Chat/Send.swift b/Sources/Chat/Send.swift index 5ec817b..b4fe568 100644 --- a/Sources/Chat/Send.swift +++ b/Sources/Chat/Send.swift @@ -1,6 +1,6 @@ import Foundation -extension Chats { +extension PushChat { struct SendIntentAPIOptions { } @@ -116,10 +116,10 @@ extension Chats { try await ConversationHash(conversationId: receiverAddress, account: senderAddress) == nil if isConversationFirst { - return try await Chats.sendIntent(chatOptions) + return try await PushChat.sendIntent(chatOptions) } else { // send regular message - return try await Chats.sendMessage(chatOptions) + return try await PushChat.sendMessage(chatOptions) } } @@ -133,13 +133,13 @@ extension Chats { public static func sendIntent(_ sendOptions: SendOptions) async throws -> Message { // check if user exists - let anotherUser = try await User.get(account: sendOptions.receiverAddress, env: .STAGING) + let anotherUser = try await PushUser.get(account: sendOptions.receiverAddress, env: .STAGING) // else create the user frist and send unencrypted intent message var enctyptMessage = true if anotherUser == nil { - let _ = try await User.createUserEmpty( + let _ = try await PushUser.createUserEmpty( userAddress: sendOptions.receiverAddress, env: sendOptions.env) enctyptMessage = false } @@ -162,6 +162,88 @@ extension Chats { return try await sendIntentService(payload: sendMessagePayload, env: sendOptions.env) } + public static func approve(_ approveOptions: ApproveOptions) async throws { + let acceptIntentPayload = try await getApprovePayload(approveOptions) + print(acceptIntentPayload) + + let url = try PushEndpoint.acceptChatRequest(env: approveOptions.env).url + var request = URLRequest(url: url) + request.httpMethod = "PUT" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = try JSONEncoder().encode(acceptIntentPayload) + print(url) + + let (data, res) = try await URLSession.shared.data(for: request) + + print(String(data: data, encoding: .utf8)!) + guard let httpResponse = res as? HTTPURLResponse else { + throw URLError(.badServerResponse) + } + + guard (200...299).contains(httpResponse.statusCode) else { + throw URLError(.badServerResponse) + } + + // do { + // return try JSONDecoder().decode(Message.self, from: data) + // } catch { + // print("[Push SDK] - API \(error.localizedDescription)") + // throw error + // } + + } + + static func getApprovePayload(_ approveOptions: ApproveOptions) async throws + -> ApproveRequestPayload + { + struct AcceptHashData: Encodable { + var fromDID: String + var toDID: String + var status: String + } + + let apiData = AcceptHashData( + fromDID: approveOptions.fromDID, + toDID: approveOptions.toDID, status: "Approved") + + let hash = generateSHA256Hash( + msg: + String(data: try JSONEncoder().encode(apiData), encoding: .utf8)! + ) + + let sig = try Pgp.sign(message: hash, privateKey: approveOptions.privateKey) + print("mess",String(data: try JSONEncoder().encode(apiData), encoding: .utf8)!) + print("hash",hash) + print("sig",sig) + + return ApproveRequestPayload( + fromDID: approveOptions.fromDID, toDID: approveOptions.toDID, signature: sig, + status: "Approved", sigType: "pgp", verificationProof: "pgp:\(sig)") + } + + public struct ApproveOptions { + var fromDID: String + var toDID: String + var privateKey: String + var env: ENV + + public init(fromAddress: String, toAddress: String, privateKey: String, env: ENV) { + self.fromDID = walletToPCAIP10(account: fromAddress) + self.toDID = walletToPCAIP10(account: toAddress) + self.privateKey = privateKey + self.env = env + } + } + + struct ApproveRequestPayload: Codable { + var fromDID: String + var toDID: String + var signature: String + var status: String = "Approved" + var sigType: String + var verificationProof: String + } + } // let bodyToBeHashed = "{\"fromDID\":\"\(sendOptions.account)\",\"toDID\":\"\(sendOptions.receiverAddress)\",\"messageContent\":\"\(sendOptions.messageContent)\",\"messageType\":\"\(sendOptions.messageType)\"}" diff --git a/Sources/Endpoints/ChatEndpoint.swift b/Sources/Endpoints/ChatEndpoint.swift index 71e70ba..3126b07 100644 --- a/Sources/Endpoints/ChatEndpoint.swift +++ b/Sources/Endpoints/ChatEndpoint.swift @@ -69,4 +69,13 @@ extension PushEndpoint { ) } + static func acceptChatRequest( + env: ENV + ) throws -> Self { + return PushEndpoint( + env: env, + path: "chat/request/accept" + ) + } + } diff --git a/Sources/Helpers/Chat/GetInboxList.swift b/Sources/Helpers/Chat/GetInboxList.swift index ad95d41..849ddbd 100644 --- a/Sources/Helpers/Chat/GetInboxList.swift +++ b/Sources/Helpers/Chat/GetInboxList.swift @@ -40,7 +40,7 @@ private func decryptAndVerifySignature( private func decryptFeeds( feeds: [Feeds], - connectedUser: User, + connectedUser: PushUser, pgpPrivateKey: String?, env: ENV ) async throws -> [Feeds] { @@ -57,7 +57,7 @@ private func decryptFeeds( throw ChatError.decryptedPrivateKeyNecessary } - let decryptedMsg = try Chats.decryptMessage( + let decryptedMsg = try PushChat.decryptMessage( message: currentFeed.msg!, privateKeyArmored: pgpPrivateKey!) currentFeed.msg?.messageContent = decryptedMsg } @@ -73,7 +73,7 @@ public func getInboxLists( pgpPrivateKey: String?, env: ENV ) async throws -> [Feeds] { - let connectedUser = try await User.get(account: user, env: env) + let connectedUser = try await PushUser.get(account: user, env: env) if connectedUser == nil { throw ChatError.invalidAddress } diff --git a/Sources/User/CreateUser.swift b/Sources/User/CreateUser.swift index 7110d9e..ec7cdf6 100644 --- a/Sources/User/CreateUser.swift +++ b/Sources/User/CreateUser.swift @@ -71,8 +71,8 @@ public struct CreateUserResponse: Decodable { public var allowedNumMsg: Int } -extension User { - public static func create(options: CreateUserOptions) async throws -> User { +extension PushUser { + public static func create(options: CreateUserOptions) async throws -> PushUser { do { let wallet = try await Wallet(signer: options.signer) let address = wallet.account @@ -169,7 +169,7 @@ extension User { } // orut - let createdUser = try JSONDecoder().decode(User.self, from: data) + let createdUser = try JSONDecoder().decode(PushUser.self, from: data) options.progressHook?( ProgressHookType( @@ -193,7 +193,7 @@ extension User { } } - public static func createUserEmpty(userAddress: String, env: ENV) async throws -> User { + public static func createUserEmpty(userAddress: String, env: ENV) async throws -> PushUser { let caip10 = walletToPCAIP10(account: userAddress) let updatedData = CreateUserAPIOptions( @@ -224,7 +224,7 @@ extension User { } // orut - let createdUser = try JSONDecoder().decode(User.self, from: data) + let createdUser = try JSONDecoder().decode(PushUser.self, from: data) return createdUser } } diff --git a/Sources/User/DecryptPgp.swift b/Sources/User/DecryptPgp.swift index a599b05..f11d54a 100644 --- a/Sources/User/DecryptPgp.swift +++ b/Sources/User/DecryptPgp.swift @@ -1,6 +1,6 @@ import Foundation -extension User { +extension PushUser { struct EncryptedPrivateKey: Codable { var ciphertext: String var version: String diff --git a/Sources/User/GetFeeds.swift b/Sources/User/GetFeeds.swift index 1016cfe..8cf9e26 100644 --- a/Sources/User/GetFeeds.swift +++ b/Sources/User/GetFeeds.swift @@ -129,7 +129,7 @@ public struct FeedsOptionsType { } } -extension User { +extension PushUser { public static func getFeeds( options: FeedsOptionsType ) async throws -> FeedResponse { diff --git a/Sources/User/User.swift b/Sources/User/User.swift index 13cafca..97c1e8a 100644 --- a/Sources/User/User.swift +++ b/Sources/User/User.swift @@ -1,6 +1,6 @@ import Foundation -public struct User: Decodable { +public struct PushUser: Decodable { public let about: String? public let name: String? public let allowedNumMsg: Int @@ -19,11 +19,11 @@ public struct User: Decodable { public let nfts: [String]? } -extension User { +extension PushUser { public static func get( account userAddress: String, env: ENV - ) async throws -> User? { + ) async throws -> PushUser? { let caipAddress = walletToPCAIP10(account: userAddress) let url = PushEndpoint.user( account: caipAddress, @@ -45,13 +45,13 @@ extension User { return nil } - let userProfile = try JSONDecoder().decode(User.self, from: data) + let userProfile = try JSONDecoder().decode(PushUser.self, from: data) return userProfile } public static func userProfileCreated(account: String, env: ENV) async throws -> Bool { - let userInfo = try await User.get(account: account, env: env) + let userInfo = try await PushUser.get(account: account, env: env) return userInfo != nil } } diff --git a/Tests/Chat/ConversationTests.swift b/Tests/Chat/ConversationTests.swift index 1902967..aaab413 100644 --- a/Tests/Chat/ConversationTests.swift +++ b/Tests/Chat/ConversationTests.swift @@ -5,11 +5,11 @@ class ConversationTests: XCTestCase { func testConversationHash() async throws { let userAddress = "0xD26A7BF7fa0f8F1f3f73B056c9A67565A6aFE63c" - let converationHash = try await Chats.ConversationHash( + let converationHash = try await PushChat.ConversationHash( conversationId: "0xACEe0D180d0118FD4F3027Ab801cc862520570d1", account: userAddress)! // For empty conversation return nil - let converationHashNil = try await Chats.ConversationHash( + let converationHashNil = try await PushChat.ConversationHash( conversationId: "0xACFe0D180d0118FD4F3027Ab801cc862520570d1", account: userAddress) XCTAssertEqual(converationHash, "bafyreib3kfifq4qcxr634xlyxlf5gs3fmmaffokyew7tet6acqj7m3zdxu") diff --git a/Tests/Chat/GetChatsTests.swift b/Tests/Chat/GetChatsTests.swift index c16f815..b6411a2 100644 --- a/Tests/Chat/GetChatsTests.swift +++ b/Tests/Chat/GetChatsTests.swift @@ -8,11 +8,11 @@ class GetChatsTests: XCTestCase { privateKey: "c39d17b1575c8d5e6e615767e19dc285d1f803d21882fb0c60f7f5b7edb759b2") let userAddress = try await signer.getAddress() - let user = try await User.get(account: userAddress, env: .STAGING)! - let pgpPrivateKey = try await User.DecryptPGPKey( + let user = try await PushUser.get(account: userAddress, env: .STAGING)! + let pgpPrivateKey = try await PushUser.DecryptPGPKey( encryptedPrivateKey: user.encryptedPrivateKey, signer: signer) - let chats = try await Push.Chats.getChats( + let chats = try await Push.PushChat.getChats( options: GetChatsOptions( account: userAddress, pgpPrivateKey: pgpPrivateKey, @@ -32,14 +32,14 @@ class GetChatsTests: XCTestCase { let signer = try SignerPrivateKey( privateKey: "c39d17b1575c8d5e6e615767e19dc285d1f803d21882fb0c60f7f5b7edb759b2") let userAddress = try await signer.getAddress() - let user = try await User.get(account: userAddress, env: .STAGING)! - let pgpPrivateKey = try await User.DecryptPGPKey( + let user = try await PushUser.get(account: userAddress, env: .STAGING)! + let pgpPrivateKey = try await PushUser.DecryptPGPKey( encryptedPrivateKey: user.encryptedPrivateKey, signer: signer) - let converationHash = try await Chats.ConversationHash( + let converationHash = try await PushChat.ConversationHash( conversationId: "0x4D5bE92D510300ceF50a2FC03534A95b60028950", account: userAddress)! - let messages = try await Chats.History( + let messages = try await PushChat.History( threadHash: converationHash, limit: 5, pgpPrivateKey: pgpPrivateKey, env: .STAGING) for msg in messages { diff --git a/Tests/Chat/SendTests.swift b/Tests/Chat/SendTests.swift index c85539c..1a95667 100644 --- a/Tests/Chat/SendTests.swift +++ b/Tests/Chat/SendTests.swift @@ -10,8 +10,8 @@ class SendChatsTests: XCTestCase { let messageToSen = "Hello user \(recipientAddress)" - let _ = try await Push.Chats.sendIntent( - Chats.SendOptions( + let _ = try await Push.PushChat.sendIntent( + PushChat.SendOptions( messageContent: messageToSen, messageType: "Text", receiverAddress: recipientAddress, @@ -19,7 +19,7 @@ class SendChatsTests: XCTestCase { pgpPrivateKey: senderPgpPk )) - let latestFeed = try await Chats.getChats( + let latestFeed = try await PushChat.getChats( options: GetChatsOptions( account: senderAddress, pgpPrivateKey: senderPgpPk, toDecrypt: true, page: 1, limit: 1, env: .STAGING)) @@ -35,8 +35,8 @@ class SendChatsTests: XCTestCase { let messageToSen = "Hello user \(recipientAddress)" - let _ = try await Push.Chats.sendIntent( - Chats.SendOptions( + let _ = try await Push.PushChat.sendIntent( + PushChat.SendOptions( messageContent: messageToSen, messageType: "Text", receiverAddress: recipientAddress, @@ -44,8 +44,8 @@ class SendChatsTests: XCTestCase { pgpPrivateKey: senderPgpPk )) - let _ = try await Push.Chats.sendMessage( - Chats.SendOptions( + let _ = try await Push.PushChat.sendMessage( + PushChat.SendOptions( messageContent: messageToSen, messageType: "Text", receiverAddress: recipientAddress, @@ -53,7 +53,7 @@ class SendChatsTests: XCTestCase { pgpPrivateKey: senderPgpPk )) - let latestFeed = try await Chats.getChats( + let latestFeed = try await PushChat.getChats( options: GetChatsOptions( account: senderAddress, pgpPrivateKey: senderPgpPk, toDecrypt: true, page: 1, limit: 1, env: .STAGING)) @@ -70,8 +70,8 @@ class SendChatsTests: XCTestCase { let messageToSen1 = "Hello user --- Intent \(recipientAddress)" let messageToSen2 = "Hello user --- Message \(recipientAddress)" - let _ = try await Push.Chats.send( - Chats.SendOptions( + let _ = try await Push.PushChat.send( + PushChat.SendOptions( messageContent: messageToSen1, messageType: "Text", receiverAddress: recipientAddress, @@ -79,14 +79,14 @@ class SendChatsTests: XCTestCase { pgpPrivateKey: senderPgpPk )) - let latestFeed1 = try await Chats.getChats( + let latestFeed1 = try await PushChat.getChats( options: GetChatsOptions( account: senderAddress, pgpPrivateKey: senderPgpPk, toDecrypt: true, page: 1, limit: 1, env: .STAGING)) let latestMessage1 = latestFeed1[0].msg!.messageContent - let _ = try await Push.Chats.send( - Chats.SendOptions( + let _ = try await Push.PushChat.send( + PushChat.SendOptions( messageContent: messageToSen2, messageType: "Text", receiverAddress: recipientAddress, @@ -94,7 +94,7 @@ class SendChatsTests: XCTestCase { pgpPrivateKey: senderPgpPk )) - let latestFeed2 = try await Chats.getChats( + let latestFeed2 = try await PushChat.getChats( options: GetChatsOptions( account: senderAddress, pgpPrivateKey: senderPgpPk, toDecrypt: true, page: 1, limit: 1, env: .STAGING)) @@ -104,6 +104,32 @@ class SendChatsTests: XCTestCase { XCTAssertEqual(latestMessage2, messageToSen2) } + func testApproveIntent() async throws { + let reqAddress = generateRandomEthereumAddress() + + // let res = try await User.create(options: CreateUserOptions(env: .STAGING, signer: Signer, version: PGP,) + + let userAddress = UserAddress + let userAddressPgpPk = UserPrivateKey + + let messageToSen1 = "Hello user --- Intent \(reqAddress)" + let messageToSen2 = "Hello user --- Message \(reqAddress)" + + try await _ = PushUser.createUserEmpty(userAddress: reqAddress, env: .STAGING) + + let _ = try await Push.PushChat.send( + PushChat.SendOptions( + messageContent: messageToSen1, + messageType: "Text", + receiverAddress: userAddress, + account: reqAddress, + pgpPrivateKey: "" + )) + + try await Push.PushChat.approve( + PushChat.ApproveOptions(fromAddress: reqAddress, toAddress: userAddress, privateKey:UserPrivateKey, env: .STAGING)) + + } } let UserAddress = "0xD26A7BF7fa0f8F1f3f73B056c9A67565A6aFE63c" @@ -167,3 +193,36 @@ let UserPrivateKey = """ =aUD6 -----END PGP PRIVATE KEY BLOCK----- """ + +let UserPublicKey = """ + -----BEGIN PGP PUBLIC KEY BLOCK----- + Version: openpgp-mobile + + xsBNBGM0d6MBCACxjqDjA0lpIETCEYjgg5IwnXzm3Kp1ruKiNcsGV5O682ywYhYK + lSeAA4U5fQUAhyzlHUmTXnLZv8TgNlQSenkuLBRJgJ6xAMKcgKVz5RP3d6hkQpnX + BygVL6mr7ZycGEYda0VwZk8zD6a6hBdv4+hlVrhz+8hVsC6LhTprhCaO7qheotG/ + seG4sv+0OV1yN7QB1gyQusz93Cui7f9VJ4cf3h981nGEFZmncel9SIZ9r75p9s+L + TXAzr3pj3HOUGig9ZdVldQw++reW7RJSipNOM8tuYcyi08Q8ZYpAX0VZwpQeWHNS + /rdhnr9ig0tbDF7lDMvKESP36yjsSC7qvi7JABEBAAHNAMLAiAQTAQgAPAUCYzR3 + owmQTKJxiypD3PwWIQTYKfOVquMJtYIiiRFMonGLKkPc/AIbAwIeAQIZAQILBwIV + CAIWAAIiAQAA93cH/AxI78v+dcgOUazw5o8VKTzDvh1i/q6Xrolu6miYmuepjosE + lPMlhXVL3woznbMxMRqh4stOgB+VvsRjZ0mVTmEYFew5Vlk3h4jW6Am9kqoLKR3v + TDRcQY8fHThgpQkW+OrSPGanba0XhqVmuc8BnLr1RyrFBnEq5mcEbQLhXm1/Hvky + K0cbhG91CIF6+yOt1bCe7Vop0rvrEx8w+si9uMQ11zwHyUa5SWKjPN1AzUhdu6v7 + deihTBdGxiDUMwlh0tNiYQmqhLysCrqblBQVczWw4vxmSR2hZ9+qfoL6PPu49kVj + VxIhowAW4EuvrMzmc+JOJj264W5es0T8CVnI5wrOwE0EYzR3owEIAMj5cXvC3UUg + kFb/WgfCvbXZdcPNUUOfNo3ABrfQolT5AMRxuEI6564IDAqIHbS0YDH6H9ZydGcU + YKJcC7m3FApHsJKP5RQlHdFlzsDU4HhOPc8Zv8k1fY+8fcbJ4dqcLZSLo4rP2X4S + Wob7ub8b3f6TJvhMETZ2/1M2UvFNV36UIVcmUM0H9/Z23ze1YJX+0Ocn+m9NGdJD + QXNieRMAoevU1dJIcebctmh6zyNaHeIW8T6HYsaduNfsn2E+rNHtVFH1UZEkN9tg + VQtw7boSMZq6M0k8AO+ttkgCRixKQJ7DRzNW9ZFer2G0jp5RnqcZaA9WLsOhN0rw + ubjg2ezMD+sAEQEAAcLAdgQYAQgAKgUCYzR3owmQTKJxiypD3PwWIQTYKfOVquMJ + tYIiiRFMonGLKkPc/AIbDAAAJFcH/RWxYt53nhU/enOaT7NXRqOnkxC7qppB8ITb + DbDLUrfgnKZDAr3+1yL2lOauqSwhiLVITWtVAQ/fiFYU6B2c7emv4SwRXt0vzodX + vASSvPcqnXS5vetKXetQY69z9kGtETzeV5ww3gf92nsHoMkT9helVa64nZJC5CPa + E9aL7drb4L/4ZQ9ZvKilPrfpkbXbRfNub1wJKCQVFnXxtXB83hlyq/tqrzeXjZai + 8+HQfOVr7NE7e20Vtat7P51yzZTBCPfOsPdHPRdJeWrIS76DmfKF0ATKOw0PNfWB + EIXKzpU+pdxSjyFbgg9NGOczMtYUTkheIQeBerPjFWsoCEtHMcE= + =5cZE + -----END PGP PUBLIC KEY BLOCK-----" + """ diff --git a/Tests/Helper/PgpTests.swift b/Tests/Helper/PgpTests.swift index e03bb3f..550c2ff 100644 --- a/Tests/Helper/PgpTests.swift +++ b/Tests/Helper/PgpTests.swift @@ -59,6 +59,34 @@ class PgpTests: XCTestCase { } + struct AcceptHashData: Encodable { + var fromDID: String + var toDID: String + var status: String + } + + func testPgpSig() async throws { + let message = "af0e555cc21ad40b2eec3b8453e4f206a43483be95accfe320e657941d63c77a" + + let fromDID = walletToPCAIP10(account: "0xD26A7BF7fa0f8F1f3f73B056c9A67565A6aFE63c") + let toDID = walletToPCAIP10(account: "0xA7226C6227A2afe0556020Eb02F1524e41fc4F9c") + + let apiData = AcceptHashData( + fromDID: fromDID, + toDID: toDID, status: "Approved") + + let hash = generateSHA256Hash( + msg: + String(data: try JSONEncoder().encode(apiData), encoding: .utf8)! + ) + + let signature = try Pgp.sign(message: hash, privateKey: secKey) + + XCTAssertEqual(message, hash) + + print(signature) + + } } let pubKey = """ diff --git a/Tests/User/CreateUserTests.swift b/Tests/User/CreateUserTests.swift index 715d70c..fbf0087 100644 --- a/Tests/User/CreateUserTests.swift +++ b/Tests/User/CreateUserTests.swift @@ -8,7 +8,7 @@ class CreateUserTests: XCTestCase { func testUserCreateFailsIfAlreadyExists() async throws { let expectation = XCTestExpectation(description: "Creates user successfully with account") do { - let _ = try await User.create( + let _ = try await PushUser.create( options: CreateUserOptions( env: ENV.STAGING, signer: SignerPrivateKey( @@ -31,7 +31,7 @@ class CreateUserTests: XCTestCase { let addrs = try await signer.getAddress() let userCAIPAddress = walletToPCAIP10(account: addrs) - let user = try await User.create( + let user = try await PushUser.create( options: CreateUserOptions( env: ENV.STAGING, signer: SignerPrivateKey( @@ -49,7 +49,7 @@ class CreateUserTests: XCTestCase { let userAddress = generateRandomEthereumAddress() let userCAIPAddress = walletToPCAIP10(account: userAddress) - let user = try await User.createUserEmpty(userAddress: userAddress, env: .STAGING) + let user = try await PushUser.createUserEmpty(userAddress: userAddress, env: .STAGING) XCTAssertEqual(user.did, userCAIPAddress) XCTAssertEqual(user.wallets, userCAIPAddress) diff --git a/Tests/User/GetFeedsTests.swift b/Tests/User/GetFeedsTests.swift index 5a93967..8c0f719 100644 --- a/Tests/User/GetFeedsTests.swift +++ b/Tests/User/GetFeedsTests.swift @@ -6,7 +6,7 @@ class GetFeedsTests: XCTestCase { func testUserFeedsReturns() async throws { let user = "eip155:0x3aae65DF8424b0Bb80C1f74dD480b04dbEA54213" - let res = try await User.getFeeds( + let res = try await PushUser.getFeeds( options: Push.FeedsOptionsType( user: user, @@ -21,7 +21,7 @@ class GetFeedsTests: XCTestCase { let user = "eip155:0x3aae65DF8424b0B" let expectation = XCTestExpectation(description: "Throws error") do { - let _ = try await User.getFeeds( + let _ = try await PushUser.getFeeds( options: Push.FeedsOptionsType( user: user, diff --git a/Tests/User/GetTests.swift b/Tests/User/GetTests.swift index 185e5a5..b26f4ad 100644 --- a/Tests/User/GetTests.swift +++ b/Tests/User/GetTests.swift @@ -7,7 +7,7 @@ class GetUserTests: XCTestCase { let nonEixstingUser = "eip155:0x265422EcbC75BEb6f3BedDf0C3480Eb6072d021d" let env = ENV.STAGING - let res = try await User.get(account: nonEixstingUser, env: env) + let res = try await PushUser.get(account: nonEixstingUser, env: env) XCTAssertNil(res, "Expected the User to be nil.") } @@ -16,7 +16,7 @@ class GetUserTests: XCTestCase { let account = "eip155:0x03fAD591aEb926bFD95FE1E38D51811167a5ad5c" let env = ENV.STAGING - let user = try await User.get(account: account, env: env)! + let user = try await PushUser.get(account: account, env: env)! XCTAssertNotNil(user, "Expected the User not to be nil.") XCTAssertEqual(account, user.did, "Expected the User DID to match.") @@ -31,7 +31,7 @@ class GetUserTests: XCTestCase { func testUserGetReturnsUserWithEthAddress() async throws { let account = "0x03fAD591aEb926bFD95FE1E38D51811167a5ad5c" let env = ENV.STAGING - let user = try await User.get(account: account, env: env)! + let user = try await PushUser.get(account: account, env: env)! XCTAssertNotNil(user, "Expected the User not to be nil.") XCTAssertTrue( @@ -44,8 +44,8 @@ class GetUserTests: XCTestCase { let existingAccount = "0x03fAD591aEb926bFD95FE1E38D51811167a5ad5c" let notExistingAccount = generateRandomEthereumAddress() - let res1 = try await User.userProfileCreated(account: existingAccount, env: .STAGING) - let res2 = try await User.userProfileCreated(account: notExistingAccount, env: .STAGING) + let res1 = try await PushUser.userProfileCreated(account: existingAccount, env: .STAGING) + let res2 = try await PushUser.userProfileCreated(account: notExistingAccount, env: .STAGING) XCTAssertTrue(res1) XCTAssertFalse(res2)