Skip to content

Commit

Permalink
fix: fix issue with block user (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Gbogboade Ayomide <[email protected]>
  • Loading branch information
gbogboadePush and Gbogboade1 authored Mar 6, 2024
1 parent f651404 commit 8ec7401
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
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

0 comments on commit 8ec7401

Please sign in to comment.