Configure Middleware
Learn how to intercept and enhance wallet operations with middleware.
Use middleware to inspect, decorate, or replace a wallet account as WDK retrieves it.
Register Middleware
Register middleware for the identifier you passed to registerWallet(). WDK attaches the account's protocol methods first, then calls your callbacks in registration order on every indexed or path-based retrieval, including cached accounts.
The second argument is MiddlewareContext. It contains the wallet identifier, the original account's captured full path, and the index if you retrieved the account by index. All callbacks on that retrieval receive the same context. The context path comes from the wallet account, so a relative input to getAccountByPath() is not echoed as the full path.
Return nothing to keep the current account. A non-nullish result replaces it for the next callback and the caller. When returning a replacement, preserve the wallet methods and protocol getters that the caller needs. If your callback only calls a helper, use a block with no return value so a result from that helper cannot replace the account.
Logging
wdk.registerMiddleware('ethereum', async (account, { blockchain, index }) => {
console.log('Accessed account:', blockchain, index, await account.getAddress())
})Register middleware that adds or wraps account methods before attaching Policies. Later middleware receives the governed account when a policy applies. That account rejects function, accessor, and prototype changes. See policy method customization.
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.