Skip to content
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

fix: fix issue with block user #23

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions Sources/User/UpdateProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/User/UpdateUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down