Configure Middleware
Learn how to intercept and enhance wallet operations with middleware.
Middleware allows you to intercept wallet operations. You can use this to add logging, implement retry logic, or route provider failover to the supported failover provider.
Register Middleware
Register middleware for the wallet identifier passed to registerWallet(). WDK runs it on each getAccount() or getAccountByPath() retrieval, including cached accounts, before applying policies.
Add or wrap custom account methods inside this middleware, before WDK returns a governed reference. Assigning functions or defining function/accessor properties through a governed account throws TypeError; changing its prototype also throws. Ordinary data properties remain assignable. See policy method customization.
Logging
This simple middleware logs a message whenever an account is retrieved.
wdk.registerMiddleware('ethereum', async (account) => {
const address = await account.getAddress()
console.log('Accessed Ethereum account:', address)
// You can also attach custom properties or wrap methods here
})Use provider failover
Provider failover is handled outside core middleware. Use the current @tetherto/wdk-failover-provider package when you need provider-level retry and fallback across RPC endpoints.
Install the failover provider package:
npm install @tetherto/wdk-failover-providerImport the provider in your wallet or infrastructure setup:
import FailoverProvider from '@tetherto/wdk-failover-provider'Use this when your app depends on external RPC providers and needs a backup route if the primary provider is slow or unavailable.
See Add provider failover, failover configuration, and the failover API reference for the supported setup.
Next Steps
Learn about error handling and best practices to ensure your application is robust and secure.