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

feat: user info from dict to struct type #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions Sources/mpc-core-kit-swift/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ public extension Web3AuthNetwork {
return convertWeb3AuthNetworkToTorusNetWork(network: self)
}
}


func dictionaryToJsonData(_ dictionary: [String: Any]) -> Data? {
return try? JSONSerialization.data(withJSONObject: dictionary, options: [])
}
Comment on lines +84 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func dictionaryToJsonData(_ dictionary: [String: Any]) -> Data? {
return try? JSONSerialization.data(withJSONObject: dictionary, options: [])
}
func dictionaryToJsonData(_ dictionary: [String: Any]) -> Data {
return try JSONSerialization.data(withJSONObject: dictionary, options: [])
}


func convertUserDataType(dictionary: [String: Any]) throws -> UserInfo? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not return an optional:
func convertUserDataType(dictionary: [String: Any]) throws -> UserInfo

if let jsonData = dictionaryToJsonData(dictionary) {
do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This do {} catch {} can be removed.

i.e

            let decoder = JSONDecoder()
            let userInfo = try decoder.decode(UserInfo.self, from: jsonData)
            return userInfo

let decoder = JSONDecoder()
let userInfo = try decoder.decode(UserInfo.self, from: jsonData)
return userInfo
} catch {
print("Error decoding JSON: \(error)")
throw "Invalid UserInfo Data structure";
}
} else {
throw "Invalid UserInfo Data structure";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be of the form:

throw SomeError.SomeCase("Invalid UserInfo Data structure")

Even better is it doesn't accept a message, then it fits nicely in the error handling:

 throw UserInfoError.InvalidData

}
}
91 changes: 91 additions & 0 deletions Sources/mpc-core-kit-swift/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,94 @@ public struct enableMFARecoveryFactor {
self.additionalMetadata = additionalMetadata
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the structs declared here will be in the next version of customauth.

I'd suggest we wait with this PR for that and then update the dependency instead and use it from there.

public enum LoginType: String, Codable {
case google = "google"
case facebook = "facebook"
case reddit = "reddit"
case discord = "discord"
case twitch = "twitch"
case apple = "apple"
case line = "line"
case github = "github"
case kakao = "kakao"
case linkedin = "linkedin"
case twitter = "twitter"
case weibo = "weibo"
case wechat = "wechat"
case farcaster = "farcaster"
case emailPasswordless = "email_passwordless"
case smsPasswordless = "sms_passwordless"
case webauthn = "webauthn"
case jwt = "jwt"
}

public struct WebAuthnExtraParams: Codable {
public let authenticatorAttachment: String?
public let attestation: String?
public let userVerification: String?
}

public struct TorusGenericObject: Codable {
public let type: String?
public let verifier: String?
public let verifierIdField: String?
}

public struct TorusVerifierResponse: Codable {
public let email: String
public let name: String
public let profileImage: String
public let aggregateVerifier: String?
public let verifier: String
public let verifierId: String
public let typeOfLogin: LoginType
public let ref: String?
public let registerOnly: Bool?
public let extraVerifierParams: WebAuthnExtraParams?
}

public struct LoginWindowResponse: Codable {
public let accessToken: String
public let idToken: String?
public let ref: String?
public let extraParams: String?
public let extraParamsPassed: String?
public let state: TorusGenericObject
}

public struct UserInfo: Codable {
public let email: String?
public let name: String?
public let profileImage: String?
public let aggregateVerifier: String?
public let verifier: String
public let verifierId: String
public let typeOfLogin: LoginType?
public let ref: String?
public let registerOnly: Bool?
public let extraVerifierParams: WebAuthnExtraParams?
public let accessToken: String?
public let idToken: String?
public let extraParams: String?
public let extraParamsPassed: String?
public let state: TorusGenericObject?
}

enum UserInfoConversionError: Error, LocalizedError {
case missingData
case invalidData
case decodingError(Error)

var errorDescription: String? {
switch self {
case .missingData:
return "UserInfo dictionary is missing."
case .invalidData:
return "UserInfo dictionary is invalid."
case .decodingError(let error):
return "Error decoding UserInfo: \(error.localizedDescription)"
}
}
}

22 changes: 18 additions & 4 deletions Sources/mpc-core-kit-swift/mpcCoreKitSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,25 @@ public struct MpcCoreKit {
return try await self.login(userData: TorusKeyData(torusKey: torusKey, userInfo: modUserInfo))
}

public func getUserInfo() throws -> [String: Any] {
guard let userInfo = self.userInfo else {
throw ("user is not logged in.")
public func getUserInfo() throws -> UserInfo {
do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The do {} catch {} can be removed here.

guard let userInfoDict = self.userInfo else {
throw UserInfoConversionError.missingData
}

guard let userInfoStruct = try convertUserDataType(dictionary: userInfoDict) else {
throw UserInfoConversionError.missingData
}
return userInfoStruct
} catch UserInfoConversionError.missingData {
throw("Error: UserInfo dictionary is missing.")
} catch UserInfoConversionError.invalidData {
throw("Error: UserInfo dictionary is invalid.")
} catch UserInfoConversionError.decodingError(let error) {
throw("Error decoding UserInfo: \(error)")
} catch {
throw("An unexpected error occurred: \(error)")
}
return userInfo
}

public func getKeyDetails() async throws -> MpcKeyDetails {
Expand Down
9 changes: 3 additions & 6 deletions Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,9 @@ final class mpc_kit_swiftTests: XCTestCase {
XCTAssertEqual(getKeyDetails.requiredFactors, 0);

let userInfo = try coreKitInstance2.getUserInfo();
if let verifierId = userInfo["verifierId"] as? String {
XCTAssertEqual(verifierId, email);
} else {
XCTFail("Verifier ID not matching.")
}

let verifierId = userInfo.verifierId
XCTAssertEqual(verifierId, email);

let hash2 = try Data(hex: "010203040506").sha3(varient: Variants.KECCAK256)
let signatures2 = try await coreKitInstance2.tssSign(message: hash2)
}
Expand Down
Loading