# Usage ### ThresholdKey Natively, the instance of tKey, (ie. ThresholdKey) returns many functions, however, we have documented a few relevant ones here. You can check the table below for a list of all relevant functions. | Function | Description | Arguments | Async | return | | ------------------- | ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | `initialize` | Generates a Threshold Key corresponding to your login provider. | `import_share: String`, `input: ShareStore?`, `never_initialize_new_key: Bool`, `include_local_metadata_transitions: Bool` | Yes | `KeyDetails` | `reconstruct` | Reconstructs the user private key. Can only work if the minimum threshold of keys has reached | `void` | Yes | `KeyReconstructionDetails` | `reconstruct_latest_poly` | Returns the latest polynomial from all the available shares (for this pub-poly). We using Lagrange's interpolation to derive the polynomial | `void` | No | `Polynomial` | `get_all_share_stores_for_latest_polynomial` | Get all available ShareStores from latest polynomial | `void` | No | `ShareStoreArray` | `generate_new_share` | Generate a new share for the reconstructed private key. | `void` | Yes | `GenerateShareStoreResult` | `delete_share` | Delete a share details from metadata. The corresponding share won't be valid anymore. | `share_index: String` | Yes | `void` | `delete_tkey` | Deletes a threshold key, all shares will be removed, use with caution | `void` | Yes | `void` | `get_key_details` | Get the details of the keys present for the particular user. | `void` | No | `KeyDetails` | `output_share` | Output a share from the tKey | `shareIndex: String, shareType: String?` | No | `String` | `share_to_share_store` | Convert Share to ShareStore | `share: String` | No | `ShareStore` | `input_share` | Adds an existing share to tkey. | `share: String`, `shareType: String?` | Yes | `void` | `output_share_store` | Output a share store from the tKey | `shareIndex: String, polyId: String?` | No | `ShareStore` | `input_share_store` | Input a share store into the tKey | `shareStore: ShareStore` | Yes | `void` | `get_shares_indexes` | Returns an array of all the share indexes from latest polynomial | `void` | No | `[String]` | `encrypt` | Encrypt a message/data with the provided publicKey. | `msg: String` | No | `String` | `decrypt` | Decrypt a message/data with the provided publicKey. | `msg: String` | No | `String` | `get_tkey_store` | Returns data from tkey store given a module name | `moduleName: String` | No | `[[String:Any]]` | `get_tkey_store_item` | Returns data from tkey store given id and a module name | `moduleName: String`, `id: String` | No | `String` | `get_shares` | get shares from tKey | `void` | No | `ShareStorePolyIdIndexMap` | `get_share_descriptions` | Get a description to a share | `void` | No | `[String: [String]]` | `add_share_description` | Add a description to a share | `key: String, description: String, update_metadata: Bool` | Yes | `void` | `update_share_description` | Update a description to a share | `key: String, oldDescription: String, newDescription: String, update_metadata: Bool` | Yes | `void` | `delete_share_description` | Delete a description to a share | `key: String, description: String, update_metadata: Bool` | Yes | `void` | ### Getting User Information `CustomAuth.triggerLogin()` The function CustomAuth.triggerLogin() returns a Dictionary which contains the user's information and details about the login. You can access the information within it to get the user details from the login provider. The contents of the dictionary may vary depending on the verifier (`single_id_verifier`, `and_aggregate_verifier`, `or_aggregate_verifier` and `single_logins`). `SubVerifierDetails` SubVerifierDetails are the details of each subverifiers to be used. | Parameter | Type | Description | Mandatory | | --------- | ---- | ----------- | --------- | | loginType | SubVerifierType | Type of your login verifier | No | | loginProvider | LoginProviders | | Yes | | clientId | String | Client ID from your login service provider | Yes | | verifier | String | Verifier Name from Web3Auth Dashboard | Yes | | redirectURL | String | | Yes | | browserRedirectURL | String? | | No | | jwtParams | [String: String] | Additional JWT Params | No | | urlSession | URLSession | | No | ```swift import CustomAuth let sub = SubVerifierDetails(loginType: .installed, // default .web //example of login using a google loginProvider: .google, clientId: "<your-client-id>", verifierName: "<verifier-name>", redirectURL: "<your-redirect-url>", browserRedirectURL: "<your-browser-redirect-url>") let tdsdk = CustomAuth(aggregateVerifierType: "<type-of-verifier>", aggregateVerifierName: "<verifier-name>", subVerifierDetails: [sub], network: <etherum-network-to-use>) // controller is used to present a SFSafariViewController. tdsdk.triggerLogin(controller: <UIViewController>?, browserType: <method-of-opening-browser>, modalPresentationStyle: <style-of-modal>).done{ data in print("private key rebuild", data) }.catch{ err in print(err) } ``` ```swift # Example let sub = SubVerifierDetails(loginType: .web, loginProvider: .google, clientId: <your-client-id>, verifierName: "google-pepper", redirectURL: "tdsdk://tdsdk/oauthCallback", browserRedirectURL: "https://scripts.toruswallet.io/redirect.html") let tdsdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifierName: "google-pepper", subVerifierDetails: [sub], network: .ROPSTEN) tdsdk.triggerLogin().done { userdata in print("private key rebuild", userdata) }.catch { err in print(err) } ``` ### Initializing tKey Once you have triggered the login process, you're ready to initialize the tKey. This will generate a Threshold Key corresponding to your login provider. ``` let key_details = try! await threshold_key.initialize(never_initialize_new_key: false, include_local_metadata_transitions: false) ``` - Parameters | Parameter | Type | Description | Mandatory | | --------- | ---- | ----------- | --------- | | import_share | String | Initialise tkey with an existing share store. This allows you to directly initialise tKey without using the service provider login. | No | | input | OpaquePointer? | Import a key into tkey for initialisation. | No | | never_initialize_new_key | Bool | Never initialise using a new key if the shares are already formed| Yes | | include_local_metadata_transitions | Bool | Pass the previous transition metadata| Yes | ### Getting tKey Details `let key_details = try! threshold_key.get_key_details()` The function get_key_details() returns the details of the keys present generated for the specific user. This includes the public key X & Y of the user, alongside the shares details and the threshold. #### Sample Key Details Return ``` [ { pubKey: { x: "471dbccd7e55eb2d24..329b8174f2339e516a3d1728d", y: "3f93da3597ded482fc..b23c5c79011a3deb8ccf8bf50", }, requiredShares: -9, threshold: 2, totalShares: 11, shareDescriptions: { "1": [ '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671429485524}', '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671429560829}', '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671454332687}', '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671454508464}', '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671454832067}', '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671506806091}', ], "94da7ea9b8680ea13d..1de31915c54c1cfa055134308969088": [ '{"module":"webStorage","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36","dateAdded":1671424397242}', ], "9b29f5c69bdcc4c79b..8c573e3001d72dc7a42866e88272866": [ '{"module":"securityQuestions","questions":"whats your password?","dateAdded":1671424430842}', ], }, }, ]; ``` ### Reconstructing User's Private Key The function `reconstruct()` reconstructs the private key of the user from the shares generated. This function returns the private key of the user once the threshold has been met. ``` let reconstructedKeyResult = try! await threshold_key.reconstruct() public final class KeyReconstructionDetails: Codable { public var key: String public var seed_phrase: [String] public var all_keys: [String] } ``` ``` # Example let privateKey = reconstructedKeyResult.key ``` ### Generating a new Share The function `generate_new_share()` generates a new share on the same threshold set by the user. This function returns the new share generated. ``` `let newShare = try! await threshold_key.generate_new_share()` ``` ### Deleting a Share The function `delete_share()` deletes a share from the user's shares. This function returns the updated shareStore after the share has been deleted. ``` let shareStore = try! await threshold_key.delete_share(share_index: idx) ``` ### Using Modules for Further Operations For making advanced operations on tKey and to manipulate the keys, you can use the modules provided by tKey. As mentioned in the initialisation section, you need to configure the modules beforehand to make it work with tKey. Once that is done, the instance of the respective module is available within your tKey instance and can be used for further operations. ### Making Blockchain Calls Once you have generated the private key, you can use it to make blockchain calls. The key generated by tKey is of type secp256k1, which is compatible with EVM-based blockchains like Ethereum, Polygon, and many others that use the same curve. However, you can also convert this key into other curves and utilize it. For example, we have a dedicated package for converting this module to the ed25519 curve for usage in Solana and other blockchains that use this curve. In addition to that, we have dedicated provider packages for EVM and Solana Blockchain libraries. You can check out their respective documentation here: Getting a Ethereum Provider from tKey: Ethereum Provider Getting a Solana Provider from tKey: Solana Provider