Skip to content

Commit

Permalink
Merge pull request #40 from Web3Auth/refactor_enums
Browse files Browse the repository at this point in the history
refactor: correct enum case and extensions for helpers
  • Loading branch information
himanshuchawla009 authored Feb 3, 2025
2 parents abddafd + 8b1e28e commit abd520b
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 73 deletions.
82 changes: 42 additions & 40 deletions Sources/mpc-core-kit-swift/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,50 @@ import Foundation
import tssClientSwift
#endif

public class FactorDescription: Codable {
public let module: FactorType
public let tssIndex: TssShareType
public let description: String?
public let dateAdded: Int

public init(module: FactorType, tssIndex: TssShareType, description: String?, dateAdded: Int) {
self.module = module
self.tssIndex = tssIndex
self.description = description
self.dateAdded = dateAdded
}
}

public func createCoreKitFactorDescription(module: FactorType, tssIndex: TssShareType, dateAdded: Int, description: String? = nil) -> FactorDescription {
return FactorDescription(module: module, tssIndex: tssIndex, description: description, dateAdded: dateAdded)
}

func factorDescriptionToJsonStr(dataObj: FactorDescription) throws -> String {
let json = try JSONEncoder().encode(dataObj)
guard let jsonStr = String(data: json, encoding: .utf8) else {
throw CoreKitError.invalidResult
extension MpcCoreKit {
public class FactorDescription: Codable {
public let module: FactorType
public let tssIndex: TssShareType
public let description: String?
public let dateAdded: Int

public init(module: FactorType, tssIndex: TssShareType, description: String?, dateAdded: Int) {
self.module = module
self.tssIndex = tssIndex
self.description = description
self.dateAdded = dateAdded
}
}
return jsonStr
}

// To remove reset account function
public func resetAccount(coreKitInstance: MpcCoreKit) async throws {
guard let postboxKey = coreKitInstance.postboxKey else {
throw CoreKitError.notLoggedIn
}

guard let _ = coreKitInstance.metadataHostUrl else {
throw CoreKitError.invalidMetadataUrl

public func createCoreKitFactorDescription(module: FactorType, tssIndex: TssShareType, dateAdded: Int, description: String? = nil) -> FactorDescription {
return FactorDescription(module: module, tssIndex: tssIndex, description: description, dateAdded: dateAdded)
}

guard let tkey = coreKitInstance.tkey else {
throw CoreKitError.invalidTKey

func factorDescriptionToJsonStr(dataObj: FactorDescription) throws -> String {
let json = try JSONEncoder().encode(dataObj)
guard let jsonStr = String(data: json, encoding: .utf8) else {
throw CoreKitError.invalidResult
}
return jsonStr
}

try await tkey.storage_layer_set_metadata(private_key: postboxKey, json: "{ \"message\": \"KEY_NOT_FOUND\" }")

// reset state
try await coreKitInstance.resetDeviceFactorStore()
// To remove reset account function
public func resetAccount() async throws {
guard let postboxKey = postboxKey else {
throw CoreKitError.notLoggedIn
}

guard let _ = metadataHostUrl else {
throw CoreKitError.invalidMetadataUrl
}

guard let tkey = tkey else {
throw CoreKitError.invalidTKey
}

try await tkey.storage_layer_set_metadata(private_key: postboxKey, json: "{ \"message\": \"KEY_NOT_FOUND\" }")

// reset state
try await resetDeviceFactorStore()
}
}
6 changes: 3 additions & 3 deletions Sources/mpc-core-kit-swift/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public struct FactorKeyData : Codable {
}

public enum CoreKitStatus {
case NotInitialized
case RequireFactor
case LoggedIn
case notInitialized
case requireFactor
case loggedIn
}
12 changes: 6 additions & 6 deletions Sources/mpc-core-kit-swift/MpcCoreKitSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ public class MpcCoreKit {
public func status() -> CoreKitStatus {

guard let tkey = self.tkey else {
return CoreKitStatus.NotInitialized
return CoreKitStatus.notInitialized
}

do {
if try tkey.get_key_details().required_shares > 0 {
return CoreKitStatus.RequireFactor
return CoreKitStatus.requireFactor
}

return CoreKitStatus.LoggedIn
return CoreKitStatus.loggedIn
} catch {
return CoreKitStatus.NotInitialized
return CoreKitStatus.notInitialized
}
}

Expand Down Expand Up @@ -319,12 +319,12 @@ public class MpcCoreKit {

if option.disableHashedFactorKey == false {
factorKey = hashFactorKey
descriptionTypeModule = FactorType.HashedShare
descriptionTypeModule = FactorType.hashedShare

} else {
// random generate
factorKey = try curveSecp256k1.SecretKey().serialize()
descriptionTypeModule = FactorType.DeviceShare
descriptionTypeModule = FactorType.deviceShare

// delete exisiting hashFactor backupshare if available
try await deleteMetadataShareBackup(factorKey: hashFactorKey)
Expand Down
4 changes: 2 additions & 2 deletions Sources/mpc-core-kit-swift/Tss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension MpcCoreKit {
try TssModule.backup_share_with_factor_key(threshold_key: thresholdKey, shareIndex: shareIndex, factorKey: newFactor)

// update description
let description = createCoreKitFactorDescription(module: FactorType.HashedShare, tssIndex: tssShareIndex, dateAdded: Int(Date().timeIntervalSince1970))
let description = createCoreKitFactorDescription(module: FactorType.hashedShare, tssIndex: tssShareIndex, dateAdded: Int(Date().timeIntervalSince1970))
let jsonStr = try factorDescriptionToJsonStr(dataObj: description)
let factorPub = try curveSecp256k1.SecretKey(hex: newFactor).toPublic().serialize(compressed: true)
try await thresholdKey.add_share_description(key: factorPub, description: jsonStr)
Expand Down Expand Up @@ -228,7 +228,7 @@ extension MpcCoreKit {
]
#endif

let deviceFactor = try await createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .DeviceShare, additionalMetadata: additionalDeviceMetadata)
let deviceFactor = try await createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .deviceShare, additionalMetadata: additionalDeviceMetadata)

// store to device
try await setDeviceFactor(factorKey: deviceFactor)
Expand Down
16 changes: 9 additions & 7 deletions Sources/mpc-core-kit-swift/Utilities/FactorSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import Foundation
import tkey
#endif

public class FactorSerialization {
public static func mnemonicToKey(tkey: ThresholdKey, shareMnemonic: String) throws -> String {
return try ShareSerializationModule.deserialize_share(threshold_key: tkey, share: shareMnemonic)
}

public static func keyToMnemonic(tkey: ThresholdKey, shareHex: String) throws -> String {
return try ShareSerializationModule.serialize_share(threshold_key: tkey, share: shareHex)
extension MpcCoreKit {
public class FactorSerialization {
public static func mnemonicToKey(tkey: ThresholdKey, shareMnemonic: String) throws -> String {
return try ShareSerializationModule.deserialize_share(threshold_key: tkey, share: shareMnemonic)
}

public static func keyToMnemonic(tkey: ThresholdKey, shareHex: String) throws -> String {
return try ShareSerializationModule.serialize_share(threshold_key: tkey, share: shareHex)
}
}
}
14 changes: 7 additions & 7 deletions Sources/mpc-core-kit-swift/core/FactorType.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Foundation

public enum FactorType: String, Codable {
case HashedShare = "hashedShare"
case SecurityQuestions = "tssSecurityQuestions"
case DeviceShare = "deviceShare"
case SeedPhrase = "seedPhrase"
case PasswordShare = "passwordShare"
case SocialShare = "socialShare"
case Other
case hashedShare = "hashedShare"
case securityQuestions = "tssSecurityQuestions"
case deviceShare = "deviceShare"
case seedPhrase = "seedPhrase"
case passwordShare = "passwordShare"
case socialShare = "socialShare"
case other
}
2 changes: 1 addition & 1 deletion Sources/mpc-core-kit-swift/core/MFARecoveryFactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class MFARecoveryFactor {
public var factorTypeDescription: FactorType
public var additionalMetadata: [String: Codable]

public init(factorKey: String? = nil, factorTypeDescription: FactorType = .Other, additionalMetadata: [String: Codable] = [:]) {
public init(factorKey: String? = nil, factorTypeDescription: FactorType = .other, additionalMetadata: [String: Codable] = [:]) {
self.factorKey = factorKey
self.factorTypeDescription = factorTypeDescription
self.additionalMetadata = additionalMetadata
Expand Down
4 changes: 1 addition & 3 deletions Sources/mpc-core-kit-swift/core/Web3AuthOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ public class CoreKitWeb3AuthOptions {
public let manualSync: Bool
public let web3AuthNetwork: Web3AuthNetwork
public let storageKey: String?
public let sessionTime: Int?
public let disableHashedFactorKey: Bool
public let storage: IStorage
// It is used to define the URL for the Metadata to be used in the SDK.
// This is for internal testing only, and shouldn't be used.
public let overwriteMetadataUrl: String?
public let hashedFactorNonce: String

public init(web3AuthClientId: String, manualSync: Bool = false, web3AuthNetwork: Web3AuthNetwork = .SAPPHIRE_MAINNET, storage: IStorage, storageKey: String? = "local", sessionTime: Int? = 86000, disableHashedFactorKey: Bool = false, overwriteMetadataUrl: String? = nil, hashedFactorNonce: String? = nil) {
public init(web3AuthClientId: String, manualSync: Bool = false, web3AuthNetwork: Web3AuthNetwork = .SAPPHIRE_MAINNET, storage: IStorage, storageKey: String? = "local", disableHashedFactorKey: Bool = false, overwriteMetadataUrl: String? = nil, hashedFactorNonce: String? = nil) {
self.web3AuthClientId = web3AuthClientId
self.manualSync = manualSync
self.web3AuthNetwork = web3AuthNetwork
self.storageKey = storageKey
self.sessionTime = sessionTime
self.storage = storage
self.disableHashedFactorKey = disableHashedFactorKey
self.overwriteMetadataUrl = overwriteMetadataUrl
Expand Down
8 changes: 4 additions & 4 deletions Tests/mpc-kit-swiftTests/mpc_kit_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class mpc_kit_swiftTests: XCTestCase {
let token = data

let _ = try await coreKitInstance.loginWithJwt(verifier: verifier, verifierId: email, idToken: token)
try await resetAccount(coreKitInstance: coreKitInstance)
try await coreKitInstance.resetAccount()
}

// this test is testing account from mpc web
Expand All @@ -123,7 +123,7 @@ final class mpc_kit_swiftTests: XCTestCase {
let hash = try keccak256(data: Data(hexString: "010203040506")!)
_ = try await coreKitInstance.tssSign(message: hash)

let newFactor = try await coreKitInstance.createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .DeviceShare, additionalMetadata: ["my": "mymy"])
let newFactor = try await coreKitInstance.createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .deviceShare, additionalMetadata: ["my": "mymy"])

let deleteFactorPub = try curveSecp256k1.SecretKey(hex: newFactor).toPublic().serialize(compressed: true)
try await coreKitInstance.deleteFactor(deleteFactorPub: deleteFactorPub, deleteFactorKey: newFactor)
Expand Down Expand Up @@ -156,7 +156,7 @@ final class mpc_kit_swiftTests: XCTestCase {
XCTAssertEqual(keyDetails2.requiredFactors, 1)

try await coreKitInstance2.inputFactorKey(factorKey: recoveryFactor)
_ = try await coreKitInstance.createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .DeviceShare)
_ = try await coreKitInstance.createFactor(tssShareIndex: .device, factorKey: nil, factorDescription: .deviceShare)

let getKeyDetails = try await coreKitInstance2.getKeyDetails()
XCTAssertEqual(getKeyDetails.requiredFactors, 0)
Expand Down Expand Up @@ -194,7 +194,7 @@ final class mpc_kit_swiftTests: XCTestCase {
print(sig)
print(syncSig2)

let factor = try await coreKitInstance.createFactor(tssShareIndex: .recovery, factorKey: nil, factorDescription: .Other)
let factor = try await coreKitInstance.createFactor(tssShareIndex: .recovery, factorKey: nil, factorDescription: .other)
print(factor)

let syncSig3 = try coreKitInstance.tssSignSync(message: hash)
Expand Down

0 comments on commit abd520b

Please sign in to comment.