Skip to main content

Torus Solana Wallet UI Plugin

@web3auth/solana-wallet-connector-plugin

Torus Solana Wallet UI Plugin helps you get the functionalities of the Torus Solana Wallet into your Web3Auth Instance. It helps you use your Web3Auth key in the Torus Solana Wallet, making it easier for your end user to make transactions, add money into their wallet and even use it across different applications using Torus Wallet.

After initialisation, this plugin gives you a provider from the Torus Solana Wallet, injecting the Torus Wallet UI into your dApp and helping you make the wallet calls directly through it without the need of making your own wallet UI flow.

In this section we'll explore more about how you can use the Torus Solana Wallet UI Plugin for your dApp.

Installation

@web3auth/solana-wallet-connector-plugin

npm install --save @web3auth/solana-wallet-connector-plugin

Initialisation

Import the SolanaWalletConnectorPlugin class from @web3auth/solana-wallet-connector-plugin.

import { SolanaWalletConnectorPlugin } from "@web3auth/solana-wallet-connector-plugin";

Assign the SolanaWalletConnectorPlugin class to a variable

After creating your Web3Auth instance, you need to initialise the Torus Wallet UI Plugin and add it to a class for further usage.

const torusPlugin = new SolanaWalletConnectorPlugin(torusWalletOpts: {},
walletInitOptions: {
enableLogging: true,
},);

This constructor takes an objects with torusWalletOpts & walletInitOptions as input.

Arguments

While initializing the plugin, you need to pass on the following parameters to the constructor:

ParameterTypeDescriptionMandatory
torusWalletOpts?TorusCtorArgsConfiguration options for Torus WalletNo
walletInitOptionsPartial<TorusParams>Parameters to customise your Torus Wallet Embed UI within the applicationYes

torusWalletOpts

torusWalletOpts is an optional parameter that allows you to pass in the configuration options for Torus Wallet. It takes the TorusCtorArgs object as input which contains the following parameters:

TorusCtorArgs

ParameterTypeDescriptionDefault ValueMandatory
modalZIndex?numberZ-index of the modal and iframe99999No

walletInitOptions

The walletInitOptions from Solana Wallet is a required parameter that allows you to customise your Solana Wallet Embed Modal UI within the application. It takes the objet TorusParams as input which contains multiple parameters mentioned in the Solana Wallet Documentation.

Out of those mentioned parameters in Interface tab, the following 4 parameters are important for whitelabeling the plugin:

walletInitOptions: Partial<TorusParams>

TorusParams

ParameterTypeMandatoryDescriptionDefault
whiteLabelWhiteLabelParamsYesParams to enable whitelabelling of torus website and widget. Know more about whitelabeling options for Solana Wallet here.N/A
showTorusButtonbooleanNoDetermines whether to show/hide torus widget.true
useWalletConnectbooleanNoSetting useWalletConnect to true allows to display wallet connect qr scanner from solana-embed.false
apiKeystringNoGet it from https://dashboard.web3auth.ioN/A

While initializing the plugin, you need to pass on the following parameters to the constructor:

whiteLabel

As you can see, the whiteLabel parameter is a required parameter within the plugin. This is because this plugin needs you to incorporate basic whitelabel to avoid showing the Torus Wallet branding on your website.

The whiteLabel parameter takes WhiteLabelParams object as input which contains the following parameters:

ParameterTypeMandatoryDescriptionDefault
namestringNoApp name to display in the UIN/A
urlstringNoApp urlN/A
themeThemeParams
(contains two params, isDark: boolean &
colors: Record<string, string>)
YesParams to customise the theme for the wallet embed. You can enable darkMode and use the colours of your choice for the modal.N/A
defaultLanguage?stringNoLanguage of whitelabelOrder of preference: Whitelabel language > user language (in torus-website) > browser language
logoDarkstringYesLogo to be shown on dark background (dark theme)N/A
logoLightstringYesLogo to be shown on light background (light theme)N/A
topupHide?booleanNoShows/hides topup option in torus-website/widget.false
customTranslations?stringNoCustom translations.en - English
info

For additional information on the init options for Torus - Solana Wallet, please check the Torus Solana Wallet documentation

Add the torusPlugin class to your Web3Auth instance

After initialising the torusPlugin, use the addPlugin() function to your Web3Auth instance, you can use the Torus Wallet UI Plugin for your dApp.

await web3AuthInstance.addPlugin(torusPlugin);

Example

import { SolanaWalletConnectorPlugin } from "@web3auth/solana-wallet-connector-plugin";

const torusPlugin = new SolanaWalletConnectorPlugin({
torusWalletOpts: {},
walletInitOptions: {
whiteLabel: {
name: "Whitelabel Demo",
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,
defaultLanguage: "en",
},
},
});
await web3auth.addPlugin(torusPlugin);

Using the Torus Solana Wallet Provider

As soon as you add the torusPlugin class to your Web3Auth instance, you have access to the Torus Wallet Provider. You can use this provider to access the Torus Solana Wallet UI within your dApp and make RPC calls through that. To use the provider, use the following command:

Example

const blockhash = (await conn.getRecentBlockhash("finalized")).blockhash;
const TransactionInstruction = SystemProgram.transfer({
fromPubkey: new PublicKey(publicKeys[0]),
toPubkey: new PublicKey(publicKeys[1]),
lamports: 0.1 * LAMPORTS_PER_SOL,
});
const transaction = new Transaction({
recentBlockhash: blockhash,
feePayer: new PublicKey(publicKeys[0]),
}).add(TransactionInstruction);

// torusWalletInstance will be available on torusPlugin once user is logged in.
// this will show transaction confirmation popup.
const res = await torusPlugin.torusWalletInstance.sendTransaction(transaction);

initiateTopup()

Initiates the Top Up flow with selected provider.

Arguments

ParameterTypeDescriptionMandatory
providerPAYMENT_PROVIDER_TYPE ["moonpay", "wyre", "rampnetwork", "xanpool", "mercuryo", "transak"]Payment Provider nameYes
paramsPaymentParamsPayment Params objectYes

PaymentParams

ParameterTypeDescriptionMandatory
selectedAddress?stringAddress to send the funds toNo
selectedCurrency?stringDefault fiat currency for the user to make the payment inNo
fiatValue?numberAmount to buy in the selectedCurrencyNo
selectedCryptoCurrency?stringCryptocurrency to buyNo
cyptoAmount?numberAmount Cryptocurrency to buyNo

Example

import { SolanaWalletConnectorPlugin } from "@web3auth/solana-wallet-connector-plugin";

const torusPlugin = new SolanaWalletConnectorPlugin({
torusWalletOpts: {},
walletInitOptions: {
whiteLabel: {
theme: { isDark: true, colors: { primary: "#00a8ff" } },
logoDark: "https://web3auth.io/images/w3a-L-Favicon-1.svg",
logoLight: "https://web3auth.io/images/w3a-D-Favicon-1.svg",
},
},
});

await web3auth.addPlugin(torusPlugin); // add plugin to web3auth instance

await torusPlugin.initiateTopup("moonpay", {
selectedAddress: "wallet_address",
selectedCurrency: "USD", // Fiat currency
fiatValue: 100, // Fiat Value
selectedCryptoCurrency: "ETH", // Cryptocurreny `SOL`, `MATIC` etc.
});

Additional Reading

Initialiazing the Torus Solana Wallet UI Plugin with a custom provider

You can also initialize the Torus Solana Wallet UI Plugin with a custom provider. If your dApp is already initiating another provider, you can use that provider to initialize the Torus Solana Wallet UI Plugin. This way it will be easier for you to use the Torus Solana Wallet UI Plugin with your dApp.

Example

import { SolanaWalletConnectorPlugin } from "@web3auth/solana-wallet-connector-plugin";

const torusPlugin = new SolanaWalletConnectorPlugin({
torusWalletOpts: {},
walletInitOptions: {
whiteLabel: {
name: "Whitelabel Demo",
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,
defaultLanguage: "en",
},
useWalletConnect: true,
enableLogging: true,
},
});

torusPlugin.initWithProvider(YOUR_CUSTOM_PROVIDER, userInfo);

Know more about how to use the Torus Solana Wallet Instance and provider