raphaelcangucu/laravel-crypto-address-validator

Laravel package for validating cryptocurrency wallet addresses across multiple coins

v1.0.0 2025-06-25 21:16 UTC

This package is auto-updated.

Last update: 2025-06-25 21:21:09 UTC


README

Latest Version on Packagist Tests Total Downloads

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.