Overview
The@subwallet/extension-inject package defines the interface between SubWallet Extension and web pages (dApps). It provides type definitions and injection functions that enable dApps to interact with the wallet through the window.injectedWeb3 object and EVM providers.
Purpose and Responsibilities
- Window Injection: Injects wallet providers into web pages
- Type Definitions: Defines interfaces for dApp-wallet communication
- Multi-Chain Support: Provides injection for Substrate, EVM, Cardano, and Bitcoin
- EIP-6963: Implements EIP-6963 provider discovery for Ethereum
- Provider Interface: Defines standard interfaces for accounts, signing, and metadata
Key Exports
Fromsrc/bundle.ts:
Core Functions
injectExtension
Injects the Substrate wallet provider intowindow.injectedWeb3:
window.injectedWeb3['subwallet-js'] with enable() and version
injectEvmExtension
Injects EVM provider intowindow.ethereum and window.SubWallet:
injectCardanoExtension
Injects Cardano provider intowindow.cardano.subwallet:
injectBitcoinExtension
Injects Bitcoin provider intowindow.SubWalletBitcoin:
inject6963EIP
Implements EIP-6963 provider discovery for Ethereum:Type Definitions
Fromsrc/types.ts:
Core Interfaces
EVM Provider
Cardano Provider
Bitcoin Provider
Metadata Types
Provider Types
EIP-6963 Types
Directory Structure
Dependencies
- @polkadot/rpc-provider: RPC provider interfaces
- @polkadot/types: Type system
- @polkadot/util: Utility functions
- @polkadot/util-crypto: Cryptographic utilities
- @polkadot/x-global: Global environment utilities
- @subwallet/keyring: Keyring types
- web3-core: Web3 core types
- @types/chrome: Chrome extension API types
- @types/firefox-webext-browser: Firefox WebExtensions API types
Integration in Architecture
Theextension-inject package:
- Content Script: Imported and executed in content scripts
- Window Object: Injects providers into the page’s window object
- dApp Communication: Enables dApps to detect and interact with SubWallet
- Multi-Chain: Supports multiple blockchain ecosystems simultaneously
- Standards Compliance: Implements EIP-6963 and other wallet standards
EIP-6963 Provider Discovery
SubWallet implements EIP-6963 for improved wallet discovery:- Multiple wallets can coexist
- dApps can discover all available wallets
- No race conditions for
window.ethereum - Better user experience for wallet selection
Usage Examples
Substrate Injection (Content Script)
EVM Injection
dApp Usage (External)
Related Packages
- extension-base - Implements the injected interfaces
- extension-dapp - Helper utilities for dApp developers
- extension-chains - Uses MetadataDef types