Initializing tKey JS SDK
After installation, the next step to using Web3Auth Core Kit tKey SDK is to initialize the SDK. The initialization takes a few steps, including initiating the tKey SDK with the service provider and modules.
- Configuration of Service Provider
- Configuration of Storage Layer
- Configuration of Modules
- Instantiation of tKey
These are the most critical steps where you must pass on different parameters according to the need of your project.
Configuring Service Provider
Service Provider in tKey
is used for generating a Share A, i.e. the private
key share managed by a wallet service provider via their authentication flows. This share in our
wallet infrastructure refers to the social login aspect, where we associate a
private key share with the user's social login, enabling the seamless login experience.
@tkey/service-provider-sfa
In the Web3Auth Core Kit tKey SDK setup, you have to use the Web3Auth SFA Service Provider
(@tkey/service-provider-sfa
), which is basically a wrapper around the
Single Factor Auth JS library implementing Web3Auth's Social Login feature as a
service provider and enabling you to associate the social login of the user as one of the shares.
Web3AuthOptions
- Table
- Interface
Parameter | Description |
---|---|
clientId | Web3Auth Client ID. Obtain your clientId from the Web3Auth Developer Dashboard. |
network | Web3Auth network to use for login, takes TORUS_NETWORK_TYPE as a value. |
export interface Web3AuthOptions {
/**
* Client id for web3auth.
* You can obtain your client id from the web3auth developer dashboard.
* You can set any random string for this on localhost.
*/
clientId: string;
/**
* Web3Auth network to use for login
*/
network: TORUS_NETWORK_TYPE;
}
Please note that the some of the parameters mentioned in the type description within the SFA JS SDK are not valid for the service provider. We have only mentioned the ones that are applicable.
Usage
const web3AuthOptions: any = {
// Get your Client ID from the Web3Auth Dashboard
cliendId: "YOUR_WEB3AUTH_CLIENT_ID",
network: TORUS_SAPPHIRE_NETWORK.SAPPHIRE_MAINNET,
};
// Configuration of Service Provider
const serviceProvider = new SFAServiceProvider({ web3AuthOptions });
Configuring Storage Layer
The Storage Layer refers to the Metadata Storage, which is used to store the metadata information of the shares generated by the tKey SDK.
@tkey/storage-layer-torus
By default, we recommend using the Web3Auth (Torus) Metadata Storage URL as your default storage. However, if you want to own the storage layer, you can contact our team to help you set that up.
TorusStorageLayerArgs
This TorusStorageLayer
constructor takes an object with TorusStorageLayerArgs
as input. It
contains the following parameters:
- Table
- Type Reference
Parameter | Description |
---|---|
enableLogging? | Enables logging on the console. |
hostUrl? | URL for connecting to your metadata server. Use https://metadata.tor.us for connecting to the Torus Metadata Server. |
serverTimeOffset? | Time offset for letting user connect to metadata server. |
declare type TorusStorageLayerArgs = {
enableLogging?: boolean;
// Use `https://metadata.tor.us` for connecting to the Torus Metadata Server
hostUrl?: string;
serverTimeOffset?: number;
};
Usage
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
const storageLayer = new TorusStorageLayer({ hostUrl: "https://metadata.tor.us" });
Configuring Modules
Modules extend the tKey SDK's capability to provide additional functionality for managing your tKey shares. Out of the multiple modules available, you can choose the module you want to include in your implementation. Here is the list of available modules:
Storage Modules (Share B)
Storage Modules are used to store the tKey shares in the user's device storage. You can choose any of the following modules:
Web Storage Module
Adds the capability to add or remove a share from the local and file storage.
@tkey/web-storage
import { WebStorageModule } from "@tkey/web-storage";
const webStorageModule = new WebStorageModule();
React Native Storage Module
Adds the capability to add or remove a share from the React Native Encrypted Store/ Expo Secure Store.
@tkey/react-native-storage
- Bare React Native
- Expo React Native
import { ReactNativeStorageModule } from "@tkey/react-native-storage";
import EncryptedStorage from "react-native-encrypted-storage";
const reactNativeStorageModule = new ReactNativeStorageModule(EncryptedStorage);
import { ReactNativeStorageModule } from "@tkey/react-native-storage";
import * as SecureStore from "expo-secure-store";
const reactNativeStorageModule = new ReactNativeStorageModule(SecureStore);
Chrome Storage Module
Adds the capability to add or remove a share from the Chrome Extension storage.
@tkey/chrome-storage
import { ChromeExtensionStorageModule } from "@tkey/chrome-storage";
const chromeStorageModule = new ChromeExtensionStorageModule();
Recovery Modules (Share C)
Recovery Modules are used to recover the tKey shares in the case user doesn't have access to their device/storage or needs additional security.
Security Questions Module
Adds the capability to add or remove a question and password as a share.
@tkey/security-questions
import { SecurityQuestionsModule } from "@tkey/security-questions";
const securityQuestionsModule = new SecurityQuestionsModule();
Share Transfer Module
Adds the capability to transfer a share to another device.
@tkey/share-transfer
import { ShareTransferModule } from "@tkey/share-transfer";
const shareTransferModule = new ShareTransferModule();
Share Serialization Module
Adds the capability to serialize/ deserialize a BN (private key) into a mnemonic.
@tkey/share-serialization
import { ShareSerializationModule } from "@tkey/share-serialization";
const shareSerializationModule = new ShareSerializationModule();
Additional Modules
These modules provide extra capability to the tKey SDK, like importing user's existing private keys or seedphrases.
Seed Phrase Module
Adds the capability to store and use seed phrases on the metadata.
@tkey/seed-phrase
import { SeedPhraseModule } from "@tkey/seed-phrase";
const seedPhraseModule = new SeedPhraseModule();
Private Key Module
Adds the capability to store extra private keys on the metadata.
@tkey/private-keys
import { PrivateKeyModule } from "@tkey/private-keys";
const privateKeyModule = new PrivateKeyModule();
Instantiating tKey
Import the TKey
class from @tkey/core
import { TKey } from "@tkey/core";
Assign the TKey
class to a variable
const tKey = new TKey(TKeyArgs);
This ThresholdKey constructor takes an object with TKeyArgs
as input.
Parameters
TKeyArgs
- Table
- Type Declaration
Parameter | Description |
---|---|
enableLogging? | This option is used to specify whether to enable logging. |
modules? | An object mapping of the modules, needed to be configured with their instances within the app. |
serviceProvider? | Takes in the Service Provider Instance to authenticate the user using social or custom login. |
storageLayer? | Takes in the Storage Layer Instance. |
customAuthArgs? | Optional, you can pass the custom authentication arguments from Web3Auth Service Provider here as well. |
manualSync? | Helps you turn on manual sync, which helps you configure tKey to not sync with the server for every request. You can sync at your convenience. |
serverTimeOffset? | Time offset for letting user connect to metadata server. |
declare type TKeyArgs = {
enableLogging?: boolean;
// Pass over the modules you want to use
modules?: ModuleMap;
// Pass over the service provider variable you instantiated
serviceProvider?: IServiceProvider;
// Pass over the storage layer variable you instantiated
storageLayer?: IStorageLayer;
// You need to pass the customAuthArgs from Web3Auth Service Provider here.
customAuthArgs?: CustomAuthArgs;
manualSync?: boolean;
serverTimeOffset?: number;
};
export declare type ModuleMap = {
[moduleName: string]: IModule;
};
Example
- Web
- Bare React Native
- Expo React Native
import { TKey } from "@tkey/core";
import { SecurityQuestionsModule } from "@tkey/security-questions";
import SFAServiceProvider from "@tkey/service-provider-sfa";
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
import { ShareSerializationModule } from "@tkey/share-serialization";
import { WebStorageModule } from "@tkey/web-storage";
const web3AuthOptions: any = {
clientId: "YOUR_WEB3AUTH_CLIENT_ID", // Get your Client ID from the Web3Auth Dashboard
network: "sapphire_mainnet", // ["sapphire_mainnet", "testnet"]
};
const serviceProvider = new SFAServiceProvider({ web3AuthOptions });
// Configuration of Storage Layer
const storageLayer = new TorusStorageLayer({
hostUrl: "https://metadata.tor.us",
});
// Configuration of Modules
const webStorageModule = new WebStorageModule();
const shareSerializationModule = new ShareSerializationModule();
const securityQuestionsModule = new SecurityQuestionsModule();
// Instantiation of tKey
export const tKeyInstance = new TKey({
serviceProvider,
storageLayer,
modules: {
securityQuestions: securityQuestionsModule,
shareSerialization: shareSerializationModule,
webStorage: webStorageModule,
},
});
import { TKey } from "@tkey/core";
import { SecurityQuestionsModule } from "@tkey/security-questions";
import SFAServiceProvider from "@tkey/service-provider-sfa";
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
import { ShareSerializationModule } from "@tkey/share-serialization";
import { WebStorageModule } from "@tkey/web-storage";
import { ReactNativeStorageModule } from "@tkey/react-native-storage";
import EncryptedStorage from "react-native-encrypted-storage";
const web3AuthOptions: any = {
clientId: "YOUR_WEB3AUTH_CLIENT_ID", // Get your Client ID from the Web3Auth Dashboard
network: "sapphire_mainnet", // ["sapphire_mainnet", "testnet"]
};
const serviceProvider = new SFAServiceProvider({ web3AuthOptions });
// Configuration of Storage Layer
const storageLayer = new TorusStorageLayer({
hostUrl: "https://metadata.tor.us",
});
// Configuration of Modules
const reactNativeStorageModule = new ReactNativeStorageModule(EncryptedStorage);
const shareSerializationModule = new ShareSerializationModule();
const securityQuestionsModule = new SecurityQuestionsModule();
// Instantiation of tKey
export const tKeyInstance = new TKey({
serviceProvider,
storageLayer,
modules: {
securityQuestions: securityQuestionsModule,
shareSerialization: shareSerializationModule,
reactNativeStorage: reactNativeStorageModule,
},
});
import { TKey } from "@tkey/core";
import { SecurityQuestionsModule } from "@tkey/security-questions";
import SFAServiceProvider from "@tkey/service-provider-sfa";
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
import { ShareSerializationModule } from "@tkey/share-serialization";
import { ReactNativeStorageModule } from "@tkey/react-native-storage";
import * as SecureStore from "expo-secure-store";
const web3AuthOptions: any = {
clientId: "YOUR_WEB3AUTH_CLIENT_ID", // Get your Client ID from the Web3Auth Dashboard
network: "testnet", // ["sapphire_mainnet", "testnet"]
};
const serviceProvider = new SFAServiceProvider({ web3AuthOptions });
// Configuration of Storage Layer
const storageLayer = new TorusStorageLayer({
hostUrl: "https://metadata.tor.us",
});
// Configuration of Modules
const reactNativeStorageModule = new ReactNativeStorageModule(SecureStore);
const shareSerializationModule = new ShareSerializationModule();
const securityQuestionsModule = new SecurityQuestionsModule();
// Instantiation of tKey
export const tKeyInstance = new TKey({
serviceProvider,
storageLayer,
modules: {
securityQuestions: securityQuestionsModule,
shareSerialization: shareSerializationModule,
reactNativeStorage: reactNativeStorageModule,
},
});