@turnkey/core
Version:
A core JavaScript web and React Native package for interfacing with Turnkey's infrastructure.
101 lines • 5.05 kB
TypeScript
import { Hex } from "viem";
import { EthereumWalletInterface, SignIntent, SwitchableChain, WalletInterfaceType, WalletProvider } from "../../../__types__";
/**
* Abstract base class for Ethereum wallet implementations.
*
* Provides shared logic for:
* - Provider discovery via EIP-6963 (request/announce events)
* - Connecting/disconnecting via EIP-1193
* - Recovering compressed secp256k1 public keys from EIP-191 signatures
*/
export declare abstract class BaseEthereumWallet implements EthereumWalletInterface {
readonly interfaceType = WalletInterfaceType.Ethereum;
/**
* Signs a message using the specified wallet provider.
*
* @param message - The message to be signed, as a string or hex.
* @param provider - The wallet provider to use for signing.
* @param intent - The intent of the signature (e.g. message signing or transaction sending).
* @returns A promise resolving to a hex-encoded signature string.
*/
abstract sign(payload: string | Hex, provider: WalletProvider, intent: SignIntent): Promise<Hex>;
/**
* Retrieves the compressed secp256k1 public key by signing a known message.
*
* - Signs the fixed string "GET_PUBLIC_KEY" (EIP-191) and recovers the key.
* - Returns the compressed public key as a hex string (no 0x prefix).
*
* @param provider - The wallet provider to use.
* @returns A promise that resolves to the compressed public key (hex-encoded).
*/
getPublicKey: (provider: WalletProvider) => Promise<string>;
/**
* Discovers EIP-1193 providers using the EIP-6963 standard.
*
* - Dispatches "eip6963:requestProvider" and listens for "eip6963:announceProvider".
* - For each discovered provider, attempts to read `eth_accounts` and `eth_chainId`
* (silently ignored if unavailable), defaulting to chainId "0x1".
* - Returns providers discovered during this discovery cycle; may be empty.
*
* @returns A promise that resolves to a list of available wallet providers.
*/
getProviders: () => Promise<WalletProvider[]>;
/**
* Requests or verifies account connection via `eth_requestAccounts`.
*
* - If the wallet is already connected, resolves immediately (no prompt).
* - If not connected, the wallet will typically prompt the user to authorize.
*
* @param provider - The wallet provider to use.
* @returns A promise that resolves with the connected wallet's address.
* @throws {Error} If the wallet returns no accounts after the request.
*/
connectWalletAccount: (provider: WalletProvider) => Promise<string>;
/**
* Attempts to disconnect the wallet by revoking `eth_accounts` permission.
*
* - Calls `wallet_revokePermissions` with `{ eth_accounts: {} }`.
* - Provider behavior is wallet-specific: some implement it, some ignore it,
* and others (e.g., Phantom) throw “method not supported”.
*
* @param provider - The wallet provider to disconnect.
* @returns A promise that resolves once the request completes (or rejects if the provider errors).
*/
disconnectWalletAccount: (provider: WalletProvider) => Promise<void>;
/**
* Switches to a new EVM chain, with add-then-switch fallback.
*
* - Tries `wallet_switchEthereumChain` first.
* - If the wallet returns error code 4902 (unknown chain):
* - If `chainOrId` is a string (hex chainId), throws and asks the caller to pass metadata.
* - If `chainOrId` is a `SwitchableChain`, calls `wallet_addEthereumChain` and then retries the switch.
*
* @param provider - The wallet provider to use.
* @param chainOrId - Hex chain ID string or full `SwitchableChain` metadata.
* @throws {Error} If provider is non-EVM, adding/switching fails, or metadata is missing.
*/
switchChain(provider: WalletProvider, chainOrId: string | SwitchableChain): Promise<void>;
}
/**
* EthereumWallet implementation using EIP-1193 compatible providers.
*
* Handles message signing and transaction submission.
*/
export declare class EthereumWallet extends BaseEthereumWallet {
/**
* Signs a message or sends a transaction depending on intent.
*
* - `SignMessage` → `personal_sign` (hex signature).
* - `SignAndSendTransaction` → `eth_sendTransaction` (tx hash).
* - For transactions, `message` must be a raw tx that `Transaction.from(...)` can parse.
* - May prompt the user (account access, signing, or send).
*
* @param payload - The payload or raw transaction to be signed/sent.
* @param provider - The wallet provider to use.
* @param intent - Signing intent (SignMessage or SignAndSendTransaction).
* @returns A promise that resolves to a hex string (signature or tx hash).
* @throws {Error} If the intent is unsupported or the wallet rejects the request.
*/
sign: (payload: string, provider: WalletProvider, intent: SignIntent) => Promise<Hex>;
}
//# sourceMappingURL=ethereum.d.ts.map