@turnkey/core
Version:
A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.
166 lines • 7.91 kB
TypeScript
import { WalletInterfaceType, WalletProvider, SignIntent, WalletConnectInterface, SwitchableChain } from "@types";
import type { WalletConnectClient } from "./client";
export declare class WalletConnectWallet implements WalletConnectInterface {
private client;
readonly interfaceType = WalletInterfaceType.WalletConnect;
private ethereumNamespaces;
private solanaNamespaces;
private ethChain;
private solChain;
private uri?;
private changeListeners;
private addChangeListener;
private notifyChange;
/**
* Constructs a WalletConnectWallet bound to a WalletConnect client.
*
* - Subscribes to session deletions and automatically re-initiates pairing,
* updating `this.uri` so the UI can present a fresh QR/deeplink.
*
* @param client - The low-level WalletConnect client used for session/RPC.
*/
constructor(client: WalletConnectClient);
/**
* Initializes WalletConnect pairing flow with the specified namespaces.
*
* - Saves the requested chain namespaces (e.g., `["eip155:1", "eip155:137", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"]`).
* - If an active session already has connected accounts, pairing is skipped.
* - Otherwise initiates a pairing and stores the resulting URI.
*
* @param opts.ethereumNamespaces - List of EVM CAIP IDs (e.g., "eip155:1").
* @param opts.solanaNamespaces - List of Solana CAIP IDs (e.g., "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp").
* @throws {Error} If no namespaces are provided for either chain.
*/
init(opts: {
ethereumNamespaces: string[];
solanaNamespaces: string[];
}): Promise<void>;
/**
* Returns WalletConnect providers with associated chain/account metadata.
*
* - Builds an EVM provider (if Ethereum namespaces are enabled).
* - Builds a Solana provider (if Solana namespaces are enabled).
*
* @returns A promise resolving to an array of WalletProvider objects.
*/
getProviders(): Promise<WalletProvider[]>;
/**
* Approves the session if needed and ensures at least one account is available.
*
* - Calls `approve()` on the underlying client when pairing is pending.
* - Throws if the approved session contains no connected accounts.
*
* @param _provider - Unused (present for interface compatibility).
* @throws {Error} If the session contains no accounts.
*/
connectWalletAccount(_provider: WalletProvider): Promise<void>;
/**
* Switches the user’s WalletConnect session to a new EVM chain.
*
* - Ethereum-only: only supported for providers on the Ethereum namespace.
* - No add-then-switch: WalletConnect cannot add chains mid-session. The target chain
* must be present in `ethereumNamespaces` negotiated at pairing time. To support a new chain,
* you must include it in the walletConfig.
* - Accepts a hex chain ID (e.g., "0x1"). If a `SwitchableChain` is passed, only its `id`
* (hex chain ID) is used; metadata is ignored for WalletConnect.
*
* @param provider - The WalletProvider returned by `getProviders()`.
* @param chainOrId - Hex chain ID (e.g., "0x1") or a `SwitchableChain` (its `id` is used).
* @returns A promise that resolves when the switch completes.
* @throws {Error} If no active session, provider is non-EVM, the chain is not in `ethereumNamespaces`,
* or the switch RPC fails.
*/
switchChain(provider: WalletProvider, chainOrId: string | SwitchableChain): Promise<void>;
/**
* Signs a message or transaction using the specified wallet provider and intent.
*
* - Ensures an active WalletConnect session:
* - If a pairing is in progress (URI shown but not yet approved), this call will
* wait for the user to approve the session and may appear stuck until they do.
* - If no pairing is in progress, this will throw (e.g., "call pair() before approve()").
* - Ethereum:
* - `SignMessage` → `personal_sign` (returns hex signature).
* - `SignAndSendTransaction` → `eth_sendTransaction` (returns tx hash).
* - Solana:
* - `SignMessage` → `solana_signMessage` (returns hex signature).
* - `SignTransaction` → `solana_signTransaction` (returns hex signature).
* - `SignAndSendTransaction` → `solana_sendTransaction` (returns hex signature of the submitted tx).
*
* @param payload - Payload or serialized transaction to sign.
* @param provider - The WalletProvider to use.
* @param intent - The signing intent.
* @returns A hex string (signature or transaction hash, depending on intent).
* @throws {Error} If no account is available, no pairing is in progress, or the intent is unsupported.
*/
sign(payload: string, provider: WalletProvider, intent: SignIntent): Promise<string>;
/**
* Retrieves the public key of the connected wallet.
*
* - Ethereum: signs a fixed challenge and recovers the compressed secp256k1 public key.
* - Solana: decodes the base58-encoded address to raw bytes.
*
* @param provider - The WalletProvider to fetch the key from.
* @returns A compressed public key as a hex string.
* @throws {Error} If no account is available or the namespace is unsupported.
*/
getPublicKey(provider: WalletProvider): Promise<string>;
/**
* Disconnects the current session and re-initiates a fresh pairing URI.
*
* - Calls `disconnect()` on the client, then `pair()` with current namespaces.
* - Updates `this.uri` so the UI can present a new QR/deeplink.
*/
disconnectWalletAccount(_provider: WalletProvider): Promise<void>;
/**
* Builds a lightweight provider interface for the given chain.
*
* @param chainId - Namespace chain ID (e.g., "eip155:1" or "solana:101").
* @returns A WalletConnect-compatible provider that proxies JSON-RPC via WC.
*/
private makeProvider;
/**
* Ensures there is an active WalletConnect session, initiating approval if necessary.
*
* - If a session exists, returns it immediately.
* - If no session exists but a pairing is in progress, awaits `approve()` —
* this will block until the user approves (or rejects) in their wallet.
* - If no session exists and no pairing is in progress, throws; the caller
* must have initiated pairing via `pair()` elsewhere.
*
* @returns The active WalletConnect session.
* @throws {Error} If approval is rejected, completes without establishing a session,
* or no pairing is in progress.
*/
private ensureSession;
/**
* Builds a WalletProvider descriptor for an EVM chain.
*
* - Extracts the connected address (if any) and current chain ID.
* - Includes the pairing `uri` if available.
*
* @param session - Current WalletConnect session (or null).
* @param info - Provider branding info (name, icon).
* @returns A WalletProvider object for Ethereum.
*/
private buildEthProvider;
/**
* Builds a WalletProvider descriptor for Solana.
*
* - Extracts the connected address (if any).
* - Includes the fresh pairing `uri` if available.
*
* @param session - Current WalletConnect session (or null).
* @param info - Provider branding info (name, icon).
* @returns A WalletProvider object for Solana.
*/
private buildSolProvider;
/**
* Builds the requested WalletConnect namespaces from the current config.
*
* - Includes methods and events for Ethereum and/or Solana based on enabled namespaces.
*
* @returns A namespaces object suitable for `WalletConnectClient.pair()`.
*/
private buildNamespaces;
}
//# sourceMappingURL=base.d.ts.map