Overview
The@subwallet/extension-dapp package provides a convenient API for dApp developers to interact with SubWallet Extension and other Polkadot-compatible wallets. It wraps the low-level window.injectedWeb3 interface with easy-to-use functions for account access, signing, and provider management.
Purpose and Responsibilities
- Wallet Discovery: Detect and enable installed wallet extensions
- Account Access: Retrieve accounts from all enabled wallets
- Account Subscriptions: Subscribe to account changes
- Provider Selection: Find specific wallet providers by name or address
- RPC Providers: List and use custom RPC providers
- Type Safety: Provides TypeScript types for all interactions
- Utility Functions: Byte wrapping/unwrapping for signing operations
Key Exports
Fromsrc/bundle.ts:
Core Functions
web3Enable
Enables all wallet extensions and returns available providers:originName(required): Your dApp’s name, shown to users during authorizationcompatInits(optional): Array of compatibility initialization functions
InjectedExtension objects, each containing:
name: Extension name (e.g., ‘subwallet-js’)version: Extension versionaccounts: Account access interfacesigner: Transaction signing interfacemetadata: Metadata management interfaceprovider: Optional RPC provider
web3Accounts
Retrieves all accounts from all enabled wallets:web3AccountsSubscribe
Subscribes to account changes across all wallets:- Real-time account list updates
- Detecting when users add/remove accounts
- Multi-wallet account synchronization
web3FromSource
Retrieves a specific wallet extension by name:'subwallet-js': SubWallet Extension'polkadot-js': Polkadot.js Extension'talisman': Talisman Wallet
web3FromAddress
Finds the wallet extension that controls a specific address:web3ListRpcProviders
Lists custom RPC providers exposed by a wallet:ProviderList object or null if not supported:
web3UseRpcProvider
Starts using a specific RPC provider from a wallet:Utility Functions
wrapBytes & unwrapBytes
Fromsrc/wrapBytes.ts:
State Variables
isWeb3Injected
Boolean indicating if any wallet has been injected:web3EnablePromise
The promise from the lastweb3Enable() call:
Complete dApp Integration Example
Error Handling
All functions throw errors with descriptive messages:TypeScript Types
All types are re-exported from@subwallet/extension-inject/types:
Dependencies
- @polkadot/util: Utility functions
- @polkadot/util-crypto: Address encoding/decoding
- @subwallet/extension-inject: Type definitions
- @polkadot/api: For API usage examples
- @polkadot/util: Utility functions
- @polkadot/util-crypto: Cryptographic utilities
Browser Compatibility
Works in all modern browsers that support:- ES6+ JavaScript
- Promises
windowobject- Browser extensions
Best Practices
- Always call
web3Enable()first: Before any other function - Use meaningful dApp names: Users see this during authorization
- Handle missing wallets: Check if extensions array is empty
- Subscribe to account changes: Keep UI in sync with wallet changes
- Cache extension references: Avoid repeated
web3FromSource()calls - Clean up subscriptions: Call unsubscribe functions when done
- Handle errors gracefully: Wrap calls in try-catch blocks
Related Packages
- extension-inject - Underlying injection interfaces
- extension-base - Extension implementation
- extension-koni-ui - Extension UI