diff --git a/Sources/User/UpdateProfile.swift b/Sources/User/UpdateProfile.swift index b7072c4..1134502 100644 --- a/Sources/User/UpdateProfile.swift +++ b/Sources/User/UpdateProfile.swift @@ -53,7 +53,9 @@ extension PushUser { let verificationProof = "\(sigType):\(signature)" let payload = UpdateUserPayload( - name: newProfile.name, desc: newProfile.desc, picture: newProfile.picture, + name: newProfile.name, + desc: newProfile.desc, + picture: newProfile.picture, blockedUsersList: newProfile.blockedUsersList, verificationProof: verificationProof) @@ -84,41 +86,52 @@ extension PushUser { struct UpdateUserPayload: Codable { var name: String? - var desc: String - var picture: String + var desc: String? + var picture: String? var blockedUsersList: [String] var verificationProof: String + + private enum CodingKeys: String, CodingKey { + case name, desc, picture, blockedUsersList, verificationProof + } + + + func encode(to encoder: Encoder) throws{ + var container = encoder.container(keyedBy: CodingKeys.self) + + try container.encode(name, forKey: .name) + try container.encode(desc, forKey: .desc) + try container.encode(picture, forKey: .picture) + try container.encode(blockedUsersList, forKey: .blockedUsersList) + try container.encode(verificationProof, forKey: .verificationProof) + } } struct UpdateUseProfile: Codable { public var name: String? - public var desc: String - public var picture: String + public var desc: String? + public var picture: String? public var blockedUsersList: [String] } func getUpdateProfileHash(newProfile: PushUser.UserProfile) throws -> ( UpdateUseProfile, String ) { + let newUserProfile = UpdateUseProfile( + name: newProfile.name, + desc: newProfile.desc, + picture: newProfile.picture, + blockedUsersList: newProfile.blockedUsersList!) - let _name = newProfile.name == nil ? "" : newProfile.name! - let _desc = newProfile.desc == nil ? "" : newProfile.desc! - - let name = newProfile.name == nil ? "null" : "\"\(_name)\"" - let desc = newProfile.desc == nil ? "null" : "\"\(_desc)\"" + let name = newProfile.name == nil ? "null" : "\"\(newProfile.name!)\"" + let desc = newProfile.desc == nil ? "null" : "\"\(newProfile.desc!)\"" let picture = "\"\(newProfile.picture)\"" - let blockedUsersList = newProfile.blockedUsersList! + let blockUserAddresses = flatten_address_list(addresses: newProfile.blockedUsersList!) let jsonString = "{\"name\":\(name),\"desc\":\(desc),\"picture\":\(picture),\"blockedUsersList\":\(blockUserAddresses)}" - - let newUserProfile = UpdateUseProfile( - name: name.replacingOccurrences(of: "\"", with: ""), - desc: desc.replacingOccurrences(of: "\"", with: ""), - picture: picture.replacingOccurrences(of: "\"", with: ""), - blockedUsersList: blockedUsersList) - let hash = generateSHA256Hash(msg: jsonString) + let hash = generateSHA256Hash(msg: jsonString) return (newUserProfile, hash) } diff --git a/Tests/User/UpdateUserTests.swift b/Tests/User/UpdateUserTests.swift index 783947e..9ed7721 100644 --- a/Tests/User/UpdateUserTests.swift +++ b/Tests/User/UpdateUserTests.swift @@ -41,6 +41,52 @@ class UpdateUserTests: XCTestCase { env: .STAGING) } + func testGenerateHash() async throws { + + let name = "null" + let desc = "null" + let picture = "\"name\"" + + let blockUserAddresses = flatten_address_list(addresses: []) + let jsonString = + "{\"name\":\(name),\"desc\":\(desc),\"picture\":\(picture),\"blockedUsersList\":\(blockUserAddresses)}" + + + let hash = generateSHA256Hash(msg: jsonString) + print(hash) + } + + func testUserBlockForNewUser() async throws { + let userPk1 = getRandomAccount() + + let signer1 = try SignerPrivateKey(privateKey: userPk1) + + let user1 = try await PushUser.create( + options: PushUser.CreateUserOptions( + env: ENV.DEV, + signer: SignerPrivateKey( + privateKey: userPk1 + ), + progressHook: nil + )) + + + let user1PpgpPk = try await PushUser.DecryptPGPKey( + encryptedPrivateKey: user1.encryptedPrivateKey, signer: signer1) + + + let (a1, a2, a3) = ( + generateRandomEthereumAddress(), generateRandomEthereumAddress(), + generateRandomEthereumAddress() + ) + + let usersToBlock = [a1, a2, a3] + + try await PushUser.blockUsers( + addressesToBlock: usersToBlock, account: user1.did, pgpPrivateKey: user1PpgpPk, + env: .DEV) + } + func testUserUnBlock() async throws { let (a1, a2, a3) = ( generateRandomEthereumAddress(), generateRandomEthereumAddress(),