PHP code example of php-core / simple-crypto

1. Go to this page and download the library: Download php-core/simple-crypto 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/ */

    

php-core / simple-crypto example snippets


use PHPCore\SimpleCrypto\Wallet\Mnemonic\BIP39Mnemonic;
use PHPCore\SimpleCrypto\Wallet\KeyPair\BTCKeyPair;
use PHPCore\SimpleCrypto\Wallet\KeyPair\EVMKeyPair;

// Create or import a mnemonic
$mnemonic = new BIP39Mnemonic();
$phrase = $mnemonic->generate(12);
$seed = $mnemonic->toSeed($phrase);

// Create master key pairs
$btcMaster = BTCKeyPair::fromSeed($seed);
$evmMaster = EVMKeyPair::fromSeed($seed);

// Generate Bitcoin addresses
$btcPath = "m/84'/0'/0'/0/0"; // BIP84 for native SegWit
$btcAccount = $btcMaster->derivePath($btcPath);

echo "Bitcoin Legacy: " . $btcAccount->getAddress('legacy') . "\n";
echo "Bitcoin SegWit: " . $btcAccount->getAddress('segwit') . "\n";
echo "Bitcoin Native SegWit: " . $btcAccount->getAddress('native_segwit') . "\n";

// Generate EVM address (works for ETH, MATIC, BSC, etc.)
$evmPath = "m/44'/60'/0'/0/0"; // BIP44 for Ethereum
$evmAccount = $evmMaster->derivePath($evmPath);
echo "EVM Address: " . $evmAccount->getAddress() . "\n";

// Generate multiple Bitcoin addresses
for ($i = 0; $i < 5; $i++) {
    $path = "m/84'/0'/0'/0/{$i}";
    $account = $btcMaster->derivePath($path);
    echo "Address #{$i}: " . $account->getAddress('native_segwit') . "\n";
}

// Generate multiple EVM addresses
for ($i = 0; $i < 5; $i++) {
    $path = "m/44'/60'/0'/0/{$i}";
    $account = $evmMaster->derivePath($path);
    echo "Address #{$i}: " . $account->getAddress() . "\n";
}

// Bitcoin message signing
$btcAccount = BTCKeyPair::fromSeed($seed);
$message = "Hello, Bitcoin!";
$signature = $btcAccount->sign($message);
$isValid = $btcAccount->verify($message, $signature);

// EVM message signing
$evmAccount = EVMKeyPair::fromSeed($seed);
$message = "Hello, Ethereum!";
$signature = $evmAccount->sign($message);
$isValid = $evmAccount->verify($message, $signature);

// Bitcoin paths
$legacyPath = "m/44'/0'/0'/0/0";     // BIP44 legacy
$segwitPath = "m/49'/0'/0'/0/0";      // BIP49 SegWit
$nativePath = "m/84'/0'/0'/0/0";      // BIP84 Native SegWit

// EVM paths
$standardPath = "m/44'/60'/0'/0/0";   // Standard Ethereum
$ledgerPath = "m/44'/60'/0'";         // Ledger Live
$customPath = "m/44'/60'/1'/0/0";     // Custom account

$account = EVMKeyPair::fromSeed($seed);

// Get private key (for import into wallets)
$privateKey = $account->getPrivateKey();

// Get public key
$publicKey = $account->getPublicKey();

// Get compressed public key
$compressedPubKey = $account->getCompressedPublicKey();