EVM-compatible blockchain adapter for CryptForge SDK (Ethereum, Sonic, Polygon, etc.)
npm install @cryptforge/blockchain-evmEVM-compatible blockchain adapter for the CryptForge SDK. This package provides key derivation and wallet operations for all EVM-compatible blockchains (Ethereum, Sonic, Polygon, BSC, Arbitrum, etc.) using the ethers.js library.
``bash`
npm install @cryptforge/blockchain-evmor
pnpm add @cryptforge/blockchain-evmor
yarn add @cryptforge/blockchain-evm
For common EVM chains, use the pre-configured adapters:
#### With Electron
`javascript
import { setupCryptForgeHandlers } from '@cryptforge/transport-electron/main';
import { EthereumAdapter, SonicAdapter } from '@cryptforge/blockchain-evm';
setupCryptForgeHandlers({
blockchainAdapters: {
Ethereum: EthereumAdapter,
Sonic: SonicAdapter,
},
});
`
Pre-configured adapters include both mainnet and testnet configurations:
`javascript
import { SonicAdapter } from '@cryptforge/blockchain-evm';
// Get current network (defaults to mainnet)
const network = SonicAdapter.getNetwork();
console.log(network?.rpcUrl); // "https://rpc.sonic.soniclabs.com"
console.log(network?.chainId); // 146
// Switch to testnet
SonicAdapter.setNetwork('testnet');
const testnet = SonicAdapter.getNetwork();
console.log(testnet?.rpcUrl); // "https://rpc.blaze.soniclabs.com"
console.log(testnet?.chainId); // 57054
// Check available networks
const networks = SonicAdapter.getAvailableNetworks();
console.log(networks); // ['mainnet', 'testnet']
// Get current network key
console.log(SonicAdapter.getCurrentNetworkKey()); // 'testnet'
`
For custom or unlisted EVM chains, use the configurable EVMAdapter:
`javascript
import { EVMAdapter } from '@cryptforge/blockchain-evm';
const PolygonAdapter = new EVMAdapter({
chainData: {
name: "Polygon",
symbol: "MATIC",
cmc_id: 3890
},
coinType: 60,
standardDecimals: 18,
networks: {
mainnet: {
name: "Polygon Mainnet",
rpcUrl: "https://polygon-rpc.com",
explorerUrl: "https://polygonscan.com",
chainId: 137,
},
testnet: {
name: "Polygon Amoy Testnet",
rpcUrl: "https://rpc-amoy.polygon.technology",
explorerUrl: "https://amoy.polygonscan.com",
chainId: 80002,
},
},
defaultNetwork: 'mainnet',
});
`
#### Custom RPC URLs
Override RPC URLs for Ethereum with your own provider:
`javascript
import { EVMAdapter } from '@cryptforge/blockchain-evm';
const CustomEthereum = new EVMAdapter({
chainData: {
name: "Ethereum",
symbol: "ETH",
cmc_id: 1027
},
coinType: 60,
standardDecimals: 18,
networks: {
mainnet: {
name: "Ethereum Mainnet",
rpcUrl: https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY},https://sepolia.infura.io/v3/${process.env.INFURA_API_KEY}
explorerUrl: "https://etherscan.io",
chainId: 1,
},
testnet: {
name: "Ethereum Sepolia",
rpcUrl: ,`
explorerUrl: "https://sepolia.etherscan.io",
chainId: 11155111,
},
},
});
- Universal EVM Support: Works with all EVM-compatible blockchains
- Pre-configured Chains: Ready-to-use adapters for Ethereum and Sonic
- Network Management: Built-in mainnet/testnet support with easy switching
- Configurable: Easily create custom adapters for any EVM chain
- Custom RPC URLs: Override default RPC providers with your own
- BIP44 Standard: Uses standard derivation paths (default: m/44'/60'/0'/0/0)
- ethers.js: Built on the battle-tested ethers.js v6 library
- Type Safe: Full TypeScript support with type definitions
- Checksummed Addresses: Returns EIP-55 checksummed addresses
#### EthereumAdapterm/44'/60'/0'/0/0
Pre-configured adapter for Ethereum.
- Chain: Ethereum
- Symbol: ETH
- Path: YOUR_INFURA_API_KEY
- Networks: Mainnet (Chain ID: 1), Sepolia Testnet (Chain ID: 11155111)
- Note: Replace in RPC URLs with your actual Infura API key
#### SonicAdapterm/44'/60'/0'/0/0
Pre-configured adapter for Sonic (EVM Layer 2).
- Chain: Sonic
- Symbol: S
- Path:
- Networks: Mainnet (Chain ID: 146), Testnet (Chain ID: 57054)
Create custom EVM chain adapters.
#### Constructor
`typescript`
new EVMAdapter(config: EVMAdapterConfig)
#### EVMAdapterConfig
`typescript
interface NetworkConfig {
name: string; // Network name (e.g., "Ethereum Mainnet")
rpcUrl: string; // RPC endpoint URL
explorerUrl?: string; // Block explorer URL (optional)
chainId: number; // Chain ID for the network
}
interface EVMAdapterConfig {
chainData: {
name: string; // Chain name (e.g., "Polygon")
symbol: string; // Token symbol (e.g., "MATIC")
cmc_id: number; // CoinMarketCap ID
};
coinType?: number; // BIP44 coin type (default: 60)
standardDecimals?: number; // Token decimals (default: 18)
networks?: { // Network configurations
mainnet?: NetworkConfig;
testnet?: NetworkConfig;
[key: string]: NetworkConfig | undefined;
};
defaultNetwork?: string; // Default network key (default: 'mainnet')
account?: number; // BIP44 account (default: 0)
change?: number; // BIP44 change (default: 0)
addressIndex?: number; // BIP44 address index (default: 0)
}
`
#### Methods
##### Key Derivation
- deriveKeys(mnemonic: string): Promise: Derives keys from a BIP39 mnemonic phrase
##### Network Management
- getNetwork(): NetworkConfig | undefined: Get current network configurationgetCurrentNetworkKey(): string
- : Get current network key (e.g., 'mainnet', 'testnet')setNetwork(networkKey: string): void
- : Switch to a different networkgetAvailableNetworks(): string[]
- : Get list of configured network keyshasNetwork(networkKey: string): boolean
- : Check if a network is configured
##### Properties
- chainData: ChainData: Chain metadata (name, symbol, CMC ID)standardDecimals: number
- : Standard decimals for the chain's native token
`typescript
import { createAuthClient } from '@cryptforge/auth';
import { EthereumAdapter } from '@cryptforge/blockchain-evm';
const auth = createAuthClient();
auth.registerAdapter('ethereum', EthereumAdapter);
// Unlock wallet
await auth.unlock({ password: 'password', chainId: 'ethereum' });
// Sign and send transaction
const { signedTransaction, signature } = await auth.signTransaction({
transaction: {
to: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
value: ethers.parseEther('0.1'), // 0.1 ETH
gasLimit: 21000,
},
});
console.log('Signed Transaction:', signedTransaction);
console.log('Transaction Hash:', signature);
`
`typescript
// Unlock wallet first
await auth.unlock({ password: 'password', chainId: 'ethereum' });
// Sign a message
const { signature, address, publicKey } = await auth.signMessage({
message: 'Hello Ethereum!',
});
console.log('Signature:', signature);
console.log('Signed by:', address);
// Verify signature
const isValid = await auth.verifySignature(
'Hello Ethereum!',
signature,
publicKey
);
console.log('Valid:', isValid);
`
`typescript
const adapter = EthereumAdapter;
// Get native balance
const balance = await adapter.getNativeBalance(
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
);
console.log('ETH Balance:', ethers.formatEther(balance));
// Get all token balances
const tokens = await adapter.getTokenBalances(
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
);
tokens.forEach(token => {
console.log(${token.symbol}: ${token.balance});`
});
`typescript
const adapter = EthereumAdapter;
// Get recent transactions
const transactions = await adapter.getTransactions(
'0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
{ limit: 10 }
);
transactions.forEach(tx => {
console.log('Hash:', tx.hash);
console.log('From:', tx.from);
console.log('To:', tx.to);
console.log('Value:', ethers.formatEther(tx.value));
});
`
`typescript
// Get addresses at different indices
const addresses = await auth.getAddresses('ethereum', 0, 5);
addresses.forEach(addr => {
console.log(Index ${addr.index}: ${addr.address});Path: ${addr.path}
console.log();
});
// Output:
// Index 0: 0xabc... Path: m/44'/60'/0'/0/0
// Index 1: 0xdef... Path: m/44'/60'/0'/0/1
// ...
`
The EVM adapter supports pluggable data providers for fetching blockchain data.
`typescript
import { EVMAdapter, AlchemyProvider } from '@cryptforge/blockchain-evm';
const adapter = new EVMAdapter({
chainData: {
name: 'Ethereum',
symbol: 'ETH',
cmc_id: 1027,
},
coinType: 60,
networks: {
mainnet: {
name: 'Ethereum Mainnet',
rpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY',
chainId: 1,
},
},
});
// Alchemy provider automatically detects Alchemy RPC URLs
// and provides enhanced data fetching capabilities
`
`typescript
import { EVMAdapter, EtherscanProvider } from '@cryptforge/blockchain-evm';
const adapter = new EVMAdapter({
chainData: {
name: 'Ethereum',
symbol: 'ETH',
cmc_id: 1027,
},
coinType: 60,
networks: {
mainnet: {
name: 'Ethereum Mainnet',
rpcUrl: 'https://mainnet.infura.io/v3/YOUR_KEY',
explorerUrl: 'https://etherscan.io',
chainId: 1,
},
},
});
// Configure Etherscan provider
const etherscanProvider = new EtherscanProvider({
apiKey: 'YOUR_ETHERSCAN_API_KEY',
network: 'mainnet',
});
`
This package is 100% browser-compatible:
- ✅ Chrome, Firefox, Safari, Edge
- ✅ Node.js (v16+)
- ✅ Electron (main and renderer)
- ✅ React Native (with polyfills)
- ✅ Web Workers
Uses ethers.js which is fully browser-safe.
See working examples in:
- examples/vue-electron-example/ - Complete wallet applicationexamples/vue-web-example/` - Web-based wallet
-
- @cryptforge/auth - Authentication and key management
- @cryptforge/core - Core types and interfaces
- @cryptforge/blockchain-btc - Bitcoin blockchain adapter
ISC