PHP code example of nadun / eth-wallet-creator

1. Go to this page and download the library: Download nadun/eth-wallet-creator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nadun / eth-wallet-creator example snippets


return [
    // Path to Node.js executable
    'node_path' => env('ETH_WALLET_NODE_PATH', 'node'),
    
    // Default BIP44 derivation path for Ethereum
    'derivation_path' => env('ETH_WALLET_DERIVATION_PATH', "m/44'/60'/0'/0/0"),
    
    // Mnemonic strength: 128 (12 words) or 256 (24 words)
    'mnemonic_strength' => env('ETH_WALLET_MNEMONIC_STRENGTH', 128),
    
    // Timeout for Node.js process
    'timeout' => env('ETH_WALLET_TIMEOUT', 30),
];

use Nadun\EthWallet\Facades\EthWallet;

// Generate a new wallet with 12-word mnemonic
$wallet = EthWallet::generate();

/* Returns:
[
    'address' => '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
    'privateKey' => '0x...',
    'publicKey' => '0x...',
    'mnemonic' => 'word1 word2 word3 ... word12',
    'derivationPath' => "m/44'/60'/0'/0/0",
    'path' => "m/44'/60'/0'/0/0"
]
*/

// 256 bits = 24 words
$wallet = EthWallet::generate(256);

$mnemonic = "your twelve word mnemonic phrase goes here for wallet recovery";

$wallet = EthWallet::restoreFromMnemonic($mnemonic);

$mnemonic = "your mnemonic phrase here";

// Derive wallet at index 0
$wallet0 = EthWallet::deriveChildWallet($mnemonic, 0);

// Derive wallet at index 1
$wallet1 = EthWallet::deriveChildWallet($mnemonic, 1);

// Each child has a unique address but shares the same mnemonic

$isValid = EthWallet::validateMnemonic("your mnemonic phrase");

if ($isValid) {
    echo "Valid mnemonic!";
}

$data = EthWallet::getAddressFromPrivateKey('0x1234567890abcdef...');

/* Returns:
[
    'address' => '0x...',
    'publicKey' => '0x...'
]
*/

// Use custom BIP44 path
$wallet = EthWallet::generate(128, "m/44'/60'/0'/0/5");

// Or set globally
EthWallet::setDerivationPath("m/44'/60'/1'/0/0");
$wallet = EthWallet::generate();

namespace App\Http\Controllers;

use Nadun\EthWallet\Facades\EthWallet;
use Illuminate\Http\Request;

class WalletController extends Controller
{
    public function createWallet()
    {
        try {
            $wallet = EthWallet::generate();
            
            // ⚠️ IMPORTANT: In production, encrypt and securely store:
            // - $wallet['privateKey']
            // - $wallet['mnemonic']
            
            return response()->json([
                'success' => true,
                'address' => $wallet['address'],
                // Never return private key or mnemonic to client!
            ]);
            
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'error' => $e->getMessage()
            ], 500);
        }
    }
    
    public function restoreWallet(Request $request)
    {
        $request->validate([
            'mnemonic' => '

use Nadun\EthWallet\WalletService;

class MyService
{
    protected $walletService;
    
    public function __construct(WalletService $walletService)
    {
        $this->walletService = $walletService;
    }
    
    public function generateWallet()
    {
        return $this->walletService->generate();
    }
}

use Illuminate\Support\Facades\Crypt;

// Generate wallet
$wallet = EthWallet::generate();

// Encrypt sensitive data before storing
$encryptedPrivateKey = Crypt::encryptString($wallet['privateKey']);
$encryptedMnemonic = Crypt::encryptString($wallet['mnemonic']);

// Store in database
DB::table('wallets')->insert([
    'address' => $wallet['address'],
    'encrypted_private_key' => $encryptedPrivateKey,
    'encrypted_mnemonic' => $encryptedMnemonic,
    'created_at' => now()
]);

// Later, decrypt when needed
$privateKey = Crypt::decryptString($encryptedPrivateKey);

use Nadun\EthWallet\Exceptions\WalletGenerationException;
use Nadun\EthWallet\Exceptions\NodeBridgeException;

try {
    $wallet = EthWallet::generate();
} catch (WalletGenerationException $e) {
    // Wallet generation failed
    Log::error('Wallet generation failed: ' . $e->getMessage());
} catch (NodeBridgeException $e) {
    // Node.js bridge communication error
    Log::error('Node bridge error: ' . $e->getMessage());
}

'node_path' => 'C:\Program Files\nodejs\node.exe'

public function generateMultipleWallets(int $count)
{
    $wallets = [];
    
    for ($i = 0; $i < $count; $i++) {
        $wallets[] = EthWallet::generate();
    }
    
    return $wallets;
}

// Generate master mnemonic
$masterWallet = EthWallet::generate();
$mnemonic = $masterWallet['mnemonic'];

// Derive multiple addresses from same mnemonic
$addresses = [];
for ($i = 0; $i < 10; $i++) {
    $child = EthWallet::deriveChildWallet($mnemonic, $i);
    $addresses[] = $child['address'];
}

// All these addresses can be recovered with the single mnemonic!
bash
php artisan vendor:publish --tag=eth-wallet-config
bash
# Generate wallet with 12 words
php artisan eth-wallet:generate

# Generate with 24 words
php artisan eth-wallet:generate --strength=256

# Custom derivation path
php artisan eth-wallet:generate --path="m/44'/60'/1'/0/0"

# JSON output
php artisan eth-wallet:generate --json
bash
# Restore main wallet
php artisan eth-wallet:restore "your twelve word mnemonic phrase here"

# Restore child wallet
php artisan eth-wallet:restore "your mnemonic" --index=5

# JSON output
php artisan eth-wallet:restore "your mnemonic" --json
bash
php artisan eth-wallet:validate "your mnemonic phrase here"