Initialization
Create, initialize, and clean up a Torus web3 instance.
Torus
This is the main class of anything related to Torus
const Torus = require("@toruslabs/solana-embed");
Using ES6,
import Torus from "@toruslabs/solana-embed";
Then, create a new instance of Torus.
const torus = new Torus(options);
The Torus constructor takes an object with TorusCtorArgs
as input.
Parameters
options
-TorusCtorArgs
(optional) : The options of the constructormodalZIndex
-number
(optional) : Z-index of the modal and iframe Default values 99999
Returns
Object
: The torus instance with all its methods and events.
Reference
Examples
const torus = new Torus();
init
Initialize the torus object after creation
await torus.init(params);
Parameters
params
-TorusParams
(optional) : The parameters passed to initialize Torus objectbuttonPosition
: "top-left" | "top-right" | "bottom-right" | "bottom-left"; (optional)apiKey
-string
(optional) : Api key - Get yours today at https://developer.tor.usnetwork
-NetworkInterface
(optional) : The network options. Used for setting a default networkbuildEnv
-enum
(optional): The build environment to use. Supported values areproduction
development
staging
testing
enableLogging
-boolean
(optional) : Enables/disables logging. Useful for debuggingshowTorusButton
-boolean
(optional) : Shows/Hides the Torus ButtonuseLocalStorage
-boolean
(optional) : Prefers to use localStorage instead of sessionStorage for torus iframe. Allows longer lived sessions. By default torus iframe uses sessionStorage. Set this param astrue
to use localStorage.whitelabel
-WhiteLabelParams
Params to enable whitelabelling of torus website and widget.
Returns
Promise<void>
: Returns a promise which resolves to void
Reference
export interface TorusParams {
/**
* Determines where the torus widget is visible on the page.
* @default bottom-left
*/
buttonPosition?: BUTTON_POSITION_TYPE;
/**
* Api key
* Get yours today at {@link https://developer.tor.us | Dashboard}
*/
apiKey?: string;
/**
* Torus Network Object
*/
network?: NetworkInterface;
/**
* Build Environment of Torus.
*
* production uses https://solana.tor.us,
*
* development uses http://localhost:8080 (expects torus-website to be run locally),
*
* testing uses https://solana-testing.tor.us (latest internal build)
* @default production
*/
buildEnv?: TORUS_BUILD_ENV_TYPE;
/**
* Enables or disables logging.
*
* Defaults to false in prod and true in other environments
*/
enableLogging?: boolean;
/**
* whether to show/hide torus widget.
*
* Defaults to true
* @default true
*/
showTorusButton?: boolean;
/**
* Prefers to use localStorage instead of sessionStorage for torus iframe. Allows longer lived sessions
*
* Defaults to false
* @default false
*/
useLocalStorage?: boolean;
/**
* Params to enable whitelabelling of torus website and widget
*/
whiteLabel?: WhiteLabelParams;
}
whiteLabel
export interface WhiteLabelParams {
/**
* App name to display in the UI
*/
name?: string;
/**
* App url
*/
url?: string;
/**
* Whitelabel theme
*/
theme: ThemeParams;
/**
* Language of whitelabel.
*
* order of preference: Whitelabel language \> user language (in torus-website) \> browser language
*/
defaultLanguage?: string;
/**
* Logo Url to be used in light mode (dark logo)
*/
logoDark: string;
/**
* Logo Url to be used in dark mode (light logo)
*/
logoLight: string;
/**
* Shows/hides topup option in torus-website/widget.
* Defaults to false
* @defaultValue false
*/
topupHide?: boolean;
/**
* Custom translations. See (examples/vue-app) to configure
*/
customTranslations?: LocaleLinks<unknown>;
}
export interface ThemeParams {
/**
* If true, enables dark mode
* Defaults to false
* @defaultValue false
*/
isDark: boolean;
/**
* Colors object to customize colors in torus theme.
*
* Contact us for whitelabel. Example provided in `examples/vue-app`
*/
colors: Record<string, string>;
}
type Record<K extends keyof any, T> = {
[P in K]: T;
};
export interface LocaleLinks<T> {
/**
* Item corresponding to english
*/
en?: T;
/**
* Item corresponding to japanese
*/
ja?: T;
/**
* Item corresponding to korean
*/
ko?: T;
/**
* Item corresponding to german
*/
de?: T;
/**
* Item corresponding to chinese (simplified)
*/
zh?: T;
/**
* Item corresponding to spanish
*/
es?: T;
}
export interface NetworkInterface {
/**
* Block explorer url for the chain
* @example https://explorer.solana.com/
*/
blockExplorerUrl: string;
/**
* Logo url for the base token
*/
logo: string;
/**
* Name for ticker
* @example 'Solana'
*/
tickerName: string;
/**
* Symbol for ticker
* @example SOL
*/
ticker: string;
/**
* RPC target Url for the chain
* @example https://api.devnet.solana.com/
*/
rpcTarget: string;
/**
* Chain Id parameter(hex with 0x prefix) for the network. Mandatory for all networks. (assign one with a map to network identifier for platforms)
* @example 0x1 for mainnet, 0x2 for testnet, 0x3 for devnet, 'loading' if not connected to anything yet or connection fails
* @defaultValue 'loading'
*/
chainId: string;
/**
* Display name for the network
*/
displayName: string;
}
Example
await torus.init();
await torus.init({
buildEnv: "testing", // uses solana-testing.tor.us
enableLogging: false, // default : false
showTorusButton: false, // default: true
whiteLabel: {
theme: { isDark: true, colors: { torusBrand1: "#00a8ff" } },
logoDark: "https://web3auth.io/images/w3a-L-Favicon-1.svg",
logoLight: "https://web3auth.io/images/w3a-D-Favicon-1.svg",
topupHide: true,
},
});
cleanUp
This cleans up the iframe and buttons created by torus package. If the user is logged in, it logs him out first and then cleans up.
await torus.cleanUp();
Returns
Promise<void>
: Returns a promise which resolves to void.
Examples
await torus.cleanUp();