raphaelcangucu / laravel-crypto-address-validator
Laravel package for validating cryptocurrency wallet addresses across multiple coins
Requires
- php: ^8.1
- illuminate/contracts: ^10.0||^11.0||^12.0
- raphaelcangucu/multicoin-address-validator: ^1.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
README
A Laravel package for validating cryptocurrency wallet addresses across multiple blockchains. Built on top of the multicoin-address-validator library.
Features
- 25+ Cryptocurrencies Supported: Bitcoin, Ethereum, Cardano, Solana, XRP, Litecoin, Bitcoin Cash, and many more
- Network Support: Mainnet, testnet, and stagenet validation
- Laravel Integration: Service provider, facade, and dependency injection support
- Flexible Configuration: Customizable validation rules and options
- High Performance: Optimized for speed and accuracy
- Comprehensive Testing: 100% test coverage with extensive test suite
Supported Cryptocurrencies
Bitcoin (BTC), Ethereum (ETH), Cardano (ADA), Solana (SOL), XRP, Litecoin (LTC), Bitcoin Cash (BCH), Monero (XMR), TRON (TRX), Polkadot (DOT), Dogecoin (DOGE), Chainlink (LINK), Polygon (MATIC), Avalanche (AVAX), Cosmos (ATOM), Tezos (XTZ), VeChain (VET), Algorand (ALGO), Hedera (HBAR), NEAR Protocol (NEAR), Filecoin (FIL), The Graph (GRT), Stellar (XLM), EOS, IOTA, and all ERC-20 tokens.
Installation
You can install the package via composer:
composer require raphaelcangucu/laravel-crypto-address-validator
The package will automatically register its service provider.
You can publish the config file with:
php artisan vendor:publish --tag="crypto-address-validator-config"
This is the contents of the published config file:
<?php // config for CryptoAddressValidator return [ /* |-------------------------------------------------------------------------- | Default Currency |-------------------------------------------------------------------------- | | The default currency to use when validating addresses if no currency | is specified. This can be either a currency symbol (e.g., 'btc') or | the full currency name (e.g., 'bitcoin'). | */ 'default_currency' => 'btc', /* |-------------------------------------------------------------------------- | Default Network Type |-------------------------------------------------------------------------- | | The default network type to use for validation. Options include: | 'prod' (mainnet), 'testnet', 'stagenet' (for supported currencies) | */ 'default_network_type' => 'prod', /* |-------------------------------------------------------------------------- | Strict Mode |-------------------------------------------------------------------------- | | When enabled, strict mode will enforce additional validation rules | such as EIP-55 checksum validation for Ethereum addresses. | When disabled, the validator is more permissive. | */ 'strict_mode' => false, ];
Usage
Facade Usage
use CryptoAddressValidator\Facades\CryptoAddressValidator; // Validate a Bitcoin address $isValid = CryptoAddressValidator::validate('1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2', 'btc'); // Returns: true // Validate an Ethereum address $isValid = CryptoAddressValidator::validate('0x742d35Cc6339C4532CE58b5D3Ea8d5A8d6F6395C', 'eth'); // Returns: true // Validate with options $isValid = CryptoAddressValidator::validate( 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', 'btc', ['networkType' => 'testnet'] ); // Returns: true // Get all supported currencies $currencies = CryptoAddressValidator::getCurrencies(); // Find a specific currency $currency = CryptoAddressValidator::findCurrency('btc'); // Returns: ['symbol' => 'btc', 'name' => 'Bitcoin', ...] // Check if a currency is supported $isSupported = CryptoAddressValidator::isSupported('btc'); // Returns: true
Dependency Injection
use CryptoAddressValidator\CryptoAddressValidator; class PaymentController extends Controller { public function validateAddress(CryptoAddressValidator $validator, Request $request) { $address = $request->input('address'); $currency = $request->input('currency'); if ($validator->validate($address, $currency)) { return response()->json(['valid' => true]); } return response()->json(['valid' => false, 'message' => 'Invalid address']); } }
Validation in Form Requests
use CryptoAddressValidator\Facades\CryptoAddressValidator; use Illuminate\Foundation\Http\FormRequest; class PaymentRequest extends FormRequest { public function rules() { return [ 'address' => [ 'required', 'string', function ($attribute, $value, $fail) { if (!CryptoAddressValidator::validate($value, $this->currency)) { $fail('The '.$attribute.' is not a valid cryptocurrency address.'); } }, ], 'currency' => 'required|string', ]; } }
Custom Validation Rule
You can create a custom validation rule:
use CryptoAddressValidator\Facades\CryptoAddressValidator; use Illuminate\Contracts\Validation\Rule; class CryptoAddress implements Rule { protected $currency; protected $options; public function __construct($currency, $options = []) { $this->currency = $currency; $this->options = $options; } public function passes($attribute, $value) { return CryptoAddressValidator::validate($value, $this->currency, $this->options); } public function message() { return 'The :attribute must be a valid ' . strtoupper($this->currency) . ' address.'; } }
Usage:
$request->validate([ 'btc_address' => ['required', new CryptoAddress('btc')], 'eth_address' => ['required', new CryptoAddress('eth')], 'testnet_address' => ['required', new CryptoAddress('btc', ['networkType' => 'testnet'])], ]);
Advanced Usage
Validation Options
// Validate with specific network CryptoAddressValidator::validate($address, 'btc', [ 'networkType' => 'testnet' // 'prod', 'testnet', 'stagenet' ]); // Ethereum with strict checksum validation CryptoAddressValidator::validate($address, 'eth', [ 'validateChecksum' => true ]); // Custom validation options CryptoAddressValidator::validate($address, 'xrp', [ 'validateTag' => true ]);
Getting Currency Information
// Get all supported currencies $currencies = CryptoAddressValidator::getCurrencies(); /* Returns array like: [ [ 'symbol' => 'btc', 'name' => 'Bitcoin', 'networkTypes' => ['prod', 'testnet'] ], // ... more currencies ] */ // Find specific currency $bitcoin = CryptoAddressValidator::findCurrency('bitcoin'); $ethereum = CryptoAddressValidator::findCurrency('eth'); // Check support $isSupported = CryptoAddressValidator::isSupported('btc'); // true $isSupported = CryptoAddressValidator::isSupported('unknown'); // false
Access Underlying Validator
// Get the underlying multicoin validator instance $validator = CryptoAddressValidator::getValidator(); // Use advanced features directly $result = $validator->validateWithDetails($address, $currency);
Testing
composer test
Performance
The package is optimized for high performance:
- Fast validation: Uses native cryptocurrency validation algorithms
- No external dependencies: All validation is done locally
- Minimal dependencies: Built on a lightweight foundation
- Memory efficient: Low memory footprint even with large validation sets
Error Handling
The package handles errors gracefully:
try { $isValid = CryptoAddressValidator::validate($address, $currency); } catch (\Exception $e) { // Handle validation errors Log::error('Address validation failed: ' . $e->getMessage()); $isValid = false; }
Security
- Input validation: All inputs are properly validated and sanitized
- No external API calls: All validation is done locally for security and performance
- Safe error handling: Errors don't expose sensitive information
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.