PHP code example of cardano-php / cip8-verifier

1. Go to this page and download the library: Download cardano-php/cip8-verifier 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/ */

    

cardano-php / cip8-verifier example snippets




ardanoPhp\CIP8Verifier\CIP8Verifier;
use CardanoPhp\CIP8Verifier\DTO\VerificationRequest;
use CardanoPhp\CIP8Verifier\Exception\CIP8VerificationException;

try {

    // Your verification data
    $request = new VerificationRequest(
        signatureCbor: "84582aa201276761646472657373581de07a9647d2048870a0726f78621863e03797dc17b946473a35ded45f75a166686173686564f4582431633364353630312d386563632d343264662d623162302d3061323934643061346564355840d40e65ebb258bd48d04092f485b845a6c0c9b1728e896c8364e51e1b6d67cd2c36dc17ad52409671a8ac8e2376e3bf138869621d03c28841a50cd68bc34fa108",
        signatureKey: "a4010103272006215820eb59d52fbd257d3f8f8f51dd59b2013092763fc9cbc109d32d837920be5e62be",
        challengeHex: "31633364353630312d386563632d343264662d623162302d306132393464306134656435",
        expectedSignerStakeAddress: "stake_test1upafv37jqjy8pgrjdauxyxrruqme0hqhh9ryww34mm297agc0f3vc",
        networkMode: 0
    );
    
    // Verify the signature
    $verifier = CIP8Verifier::create();
    $result = $verifier->verify($request);
    
    // Check results
    if ($result->isValid) {
        echo "✅ Signature is valid!\n";
    } else {
        echo "❌ Signature verification failed\n";
    }
    echo "Stake address matches: " . ($result->stakeAddressMatches ? 'Yes' : 'No') . "\n";
    echo "Challenge matches: " . ($result->challengeMatches ? 'Yes' : 'No') . "\n";
    echo "Signature validates: " . ($result->signatureValidates ? 'Yes' : 'No') . "\n";
    
} catch (CIP8VerificationException $e) {

    echo "❌ Verification error: " . $e->getMessage() . "\n";
    
} catch (Throwable $e) {

    echo "❌ Unexpected error: " . $e->getMessage() . "\n";
    
}

// Create a new verifier instance
$verifier = CIP8Verifier::create();

// Verify with type-safe DTO
$result = $verifier->verify(VerificationRequest $request): VerificationResult

new VerificationRequest(
    string $signatureCbor,                // CBOR-encoded signature data
    string $signatureKey,                 // Public key in CBOR format
    string $challengeHex,                 // Challenge message to verify
    string $expectedSignerStakeAddress,   // Expected stake address
    int $networkMode                      // Network mode (0=testnet, 1=mainnet)
)

// Factory method from an array
VerificationRequest::fromArray(array $data): VerificationRequest

readonly class VerificationResult {
    public bool $isValid;              // Overall validation result
    public bool $stakeAddressMatches;  // Stake address validation
    public bool $challengeMatches;     // Challenge content validation  
    public bool $signatureValidates;   // Cryptographic signature validation
}

// Convert to array
$result->toArray(): array

// Factory method
VerificationResult::createValid(bool $stakeAddressMatches, bool $challengeMatches, bool $signatureValidates): VerificationResult

CardanoPhp\CIP8Verifier\Exception\CIP8VerificationException
├── InvalidSignatureLengthException
└── InvalidPublicKeyLengthException

use CardanoPhp\CIP8Verifier\CIP8Verifier;
use CardanoPhp\CIP8Verifier\DTO\VerificationRequest;
use CardanoPhp\CIP8Verifier\Exception\CIP8VerificationException;

try {
    $verifier = CIP8Verifier::create();
    $result = $verifier->verify($request);
    
    if (!$result->isValid) {
        // Handle validation failure (invalid signature, mismatched wallet, etc.)
        error_log("CIP8 verification failed");
    }
} catch (CIP8VerificationException $e) {
    // Handle specific CIP8 errors (invalid input format, parsing errors, etc.)
    error_log("CIP8 error: " . $e->getMessage());
} catch (Throwable $e) {
    // Handle unexpected errors
    error_log("Unexpected error: " . $e->getMessage());
}

use CardanoPhp\CIP8Verifier\CIP8Verifier;
use CardanoPhp\CIP8Verifier\Service\*;

// Manual dependency injection
$bech32Encoder = new Bech32Encoder();
$stakeAddressGenerator = new StakeAddressGenerator($bech32Encoder);
$publicKeyExtractor = new PublicKeyExtractor();
$coseParser = new CoseParser();
$signatureVerifier = new SignatureVerifier();

$verifier = new CIP8Verifier(
    $publicKeyExtractor,
    $stakeAddressGenerator,
    $coseParser,
    $signatureVerifier
);
bash
composer