Skip to main content

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.

note

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

ParameterDescription
clientIdWeb3Auth Client ID. Obtain your clientId from the Web3Auth Developer Dashboard.
networkWeb3Auth network to use for login, takes TORUS_NETWORK_TYPE as a value.
warning

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

info

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:

ParameterDescription
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.

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
import { ReactNativeStorageModule } from "@tkey/react-native-storage";
import EncryptedStorage from "react-native-encrypted-storage";
const reactNativeStorageModule = new ReactNativeStorageModule(EncryptedStorage);

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

ParameterDescription
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.

Example

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,
},
});