Embeddable Web3 authentication solution that integrates OAuth logins with Multi Party Computation technology for passwordless wallet access
—
EIP-1193 compliant Ethereum provider for Web3 interactions, RPC requests, and blockchain operations.
Access the Ethereum provider instance for Web3 operations.
/** Main Ethereum provider instance (EIP-1193 compliant) */
readonly provider: TorusInpageProvider;
/** Alias for provider (for compatibility) */
readonly ethereum: TorusInpageProvider;Usage Example:
// Access provider
const provider = torus.provider;
// Use with web3 libraries
import Web3 from "web3";
const web3 = new Web3(provider);
// Use with ethers.js
import { ethers } from "ethers";
const ethersProvider = new ethers.providers.Web3Provider(provider);Primary method for making JSON-RPC requests to the Ethereum network.
/**
* Submit RPC request per EIP-1193 specification
* @param args - Request arguments containing method and parameters
* @returns Promise resolving to the result of the method call
*/
request<T>(args: RequestArguments): Promise<Maybe<T>>;
interface RequestArguments {
/** The RPC method to request */
method: string;
/** The parameters for the RPC method */
params?: unknown[] | Record<string, unknown>;
}
type Maybe<T> = Partial<T> | null | undefined;Usage Examples:
// Get account balance
const balance = await torus.provider.request({
method: "eth_getBalance",
params: [address, "latest"]
});
// Send transaction
const txHash = await torus.provider.request({
method: "eth_sendTransaction",
params: [{
from: address,
to: "0x742d35Cc6765C788200b9aa5FdCFD9A3ebFCF2d7",
value: "0xde0b6b3a7640000", // 1 ETH in wei
gas: "0x5208" // 21000 gas
}]
});
// Sign message
const signature = await torus.provider.request({
method: "personal_sign",
params: ["Hello World", address]
});
// Get network information
const chainId = await torus.provider.request({
method: "eth_chainId"
});
const networkVersion = await torus.provider.request({
method: "net_version"
});Check provider connection status.
/**
* Check if the provider is connected to Torus
* @returns Boolean indicating connection status
*/
isConnected(): boolean;Usage Example:
if (torus.provider.isConnected()) {
console.log("Provider is connected");
// Safe to make RPC requests
} else {
console.log("Provider not connected");
// Need to wait for connection or re-initialize
}For backward compatibility with older dApps.
/**
* Legacy callback-based RPC method (deprecated - use request() instead)
* @param payload - JSON-RPC request object
* @param callback - Error-first callback function
*/
sendAsync(payload: JRPCRequest<unknown>, callback: (error: Error | null, result?: JRPCResponse<unknown>) => void): void;
/**
* Legacy send methods (deprecated - use request() instead)
* Multiple overloads for backward compatibility
*/
send(method: string, params?: unknown[]): Promise<JRPCResponse<unknown>>;
send(payload: JRPCRequest<unknown>, callback: Function): void;
send(payload: SendSyncJsonRpcRequest): JRPCResponse<unknown>;Migration Example:
// Old way (deprecated)
torus.provider.sendAsync({
method: "eth_getBalance",
params: [address, "latest"]
}, (error, result) => {
if (error) {
console.error(error);
} else {
console.log("Balance:", result.result);
}
});
// New way (recommended)
try {
const balance = await torus.provider.request({
method: "eth_getBalance",
params: [address, "latest"]
});
console.log("Balance:", balance);
} catch (error) {
console.error(error);
}Access current provider state information.
/** Current chain ID in hex format (null if not connected) */
readonly chainId: string | null;
/** Currently selected account address (null if not connected/logged in) */
readonly selectedAddress: string | null;
/** Current network version identifier */
readonly networkVersion: string | null;
/** Always true - identifies this as a Torus provider */
readonly isTorus: true;
/** Whether metadata should be sent automatically */
readonly shouldSendMetadata: boolean;
/** Legacy enable function for compatibility */
readonly enable: () => Promise<string[]>;
/** Function for handling preopen requests */
readonly tryPreopenHandle: (payload: UnvalidatedJsonRpcRequest | UnvalidatedJsonRpcRequest[], cb: (...args: unknown[]) => void) => void;Usage Example:
// Check current state
console.log("Provider state:", {
chainId: torus.provider.chainId,
selectedAddress: torus.provider.selectedAddress,
networkVersion: torus.provider.networkVersion,
connected: torus.provider.isConnected()
});
// Legacy enable (for older dApps)
const accounts = await torus.provider.enable();
console.log("Enabled accounts:", accounts);The provider emits events for state changes (extends SafeEventEmitter).
// Event types (standard EIP-1193 events)
provider.on("connect", (connectInfo) => {
console.log("Provider connected:", connectInfo);
});
provider.on("disconnect", (error) => {
console.log("Provider disconnected:", error);
});
provider.on("accountsChanged", (accounts) => {
console.log("Accounts changed:", accounts);
});
provider.on("chainChanged", (chainId) => {
console.log("Chain changed:", chainId);
});
provider.on("message", (message) => {
console.log("Provider message:", message);
});Complete Event Handling Example:
// Set up all provider event handlers
function setupProviderEvents() {
const provider = torus.provider;
provider.on("connect", (connectInfo) => {
console.log("Connected to chain:", connectInfo.chainId);
updateUI({ connected: true });
});
provider.on("disconnect", (error) => {
console.error("Provider disconnected:", error);
updateUI({ connected: false });
});
provider.on("accountsChanged", (accounts) => {
if (accounts.length === 0) {
console.log("User disconnected all accounts");
updateUI({ account: null });
} else {
console.log("Active account:", accounts[0]);
updateUI({ account: accounts[0] });
}
});
provider.on("chainChanged", (chainId) => {
console.log("Network changed to:", chainId);
updateUI({ chainId });
// Reload the page to avoid issues with stale state
window.location.reload();
});
}Frequently used Ethereum JSON-RPC methods.
// Account and balance information
eth_accounts // Get available accounts
eth_getBalance // Get account balance
eth_getTransactionCount // Get account nonce
// Transaction methods
eth_sendTransaction // Send transaction
eth_signTransaction // Sign transaction
eth_getTransactionReceipt // Get transaction receipt
// Message signing
personal_sign // Sign message
eth_signTypedData_v4 // Sign typed data (EIP-712)
// Network information
eth_chainId // Get chain ID
net_version // Get network version
eth_blockNumber // Get latest block number
// Smart contract interaction
eth_call // Call contract method
eth_estimateGas // Estimate gas for transaction
eth_getLogs // Get contract event logs
// Wallet management
wallet_addEthereumChain // Add custom network
wallet_switchEthereumChain // Switch networks
wallet_requestPermissions // Request permissionsProper error handling for provider requests.
try {
const result = await torus.provider.request({
method: "eth_sendTransaction",
params: [transactionObject]
});
console.log("Transaction sent:", result);
} catch (error) {
// Handle different error types
if (error.code === 4001) {
console.log("User rejected the request");
} else if (error.code === -32602) {
console.error("Invalid request parameters");
} else if (error.code === -32603) {
console.error("Internal JSON-RPC error");
} else {
console.error("Unexpected error:", error.message);
}
}Full TypeScript support with proper typing.
interface JRPCRequest<T> {
id?: string | number;
jsonrpc: "2.0";
method: string;
params?: T;
}
interface JRPCResponse<T> {
id: string | number;
jsonrpc: "2.0";
result?: T;
error?: {
code: number;
message: string;
data?: unknown;
};
}
interface UnvalidatedJsonRpcRequest {
id?: string | number;
jsonrpc?: string;
method: string;
params?: unknown;
preopenInstanceId?: string;
}
interface SendSyncJsonRpcRequest extends JRPCRequest<unknown> {
method: "eth_accounts" | "eth_coinbase" | "eth_uninstallFilter" | "net_version";
}Install with Tessl CLI
npx tessl i tessl/npm-toruslabs--torus-embed