|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * TRAIL Protocol - Verifiable Credential Example |
| 5 | + * |
| 6 | + * Demonstrates how to: |
| 7 | + * 1. Create a self-signed Verifiable Credential (VC 2.0) |
| 8 | + * 2. Inspect the DataIntegrityProof (eddsa-jcs-2023) |
| 9 | + * 3. Verify the credential cryptographically |
| 10 | + * 4. Detect tampering (negative test) |
| 11 | + * |
| 12 | + * Prerequisites: |
| 13 | + * npm install @trailprotocol/core |
| 14 | + * |
| 15 | + * Run: |
| 16 | + * node vc-verification.js |
| 17 | + */ |
| 18 | + |
| 19 | +const { |
| 20 | + generateKeyPair, |
| 21 | + createSelfDid, |
| 22 | + createSelfSignedCredential, |
| 23 | + verifyCredential, |
| 24 | +} = require('@trailprotocol/core'); |
| 25 | + |
| 26 | +async function main() { |
| 27 | + // ------------------------------------------------------- |
| 28 | + // Step 1: Setup - generate keys and create a DID |
| 29 | + // ------------------------------------------------------- |
| 30 | + const issuerKeys = generateKeyPair(); |
| 31 | + const issuerDid = createSelfDid(issuerKeys.publicKeyMultibase); |
| 32 | + |
| 33 | + console.log('=== Setup ==='); |
| 34 | + console.log('Issuer DID:', issuerDid); |
| 35 | + console.log(); |
| 36 | + |
| 37 | + // ------------------------------------------------------- |
| 38 | + // Step 2: Issue a self-signed Verifiable Credential |
| 39 | + // ------------------------------------------------------- |
| 40 | + // createSelfSignedCredential(issuerDid, subjectDid, claims, privateKeyBytes) |
| 41 | + // |
| 42 | + // This creates a VC 2.0 credential with a DataIntegrityProof |
| 43 | + // using the eddsa-jcs-2023 cryptosuite (Ed25519 + JCS RFC 8785). |
| 44 | + // For self-signed VCs, issuer and subject are the same DID. |
| 45 | + const credential = createSelfSignedCredential( |
| 46 | + issuerDid, |
| 47 | + issuerDid, // subject = issuer (self-signed) |
| 48 | + { |
| 49 | + type: 'TrailTrustAttestation', |
| 50 | + aiSystemType: 'agent', |
| 51 | + capabilities: ['text-generation', 'tool-use'], |
| 52 | + }, |
| 53 | + issuerKeys.privateKeyBytes |
| 54 | + ); |
| 55 | + |
| 56 | + console.log('=== Issued Credential ==='); |
| 57 | + console.log(JSON.stringify(credential, null, 2)); |
| 58 | + console.log(); |
| 59 | + |
| 60 | + // ------------------------------------------------------- |
| 61 | + // Step 3: Inspect the proof |
| 62 | + // ------------------------------------------------------- |
| 63 | + console.log('=== Proof Details ==='); |
| 64 | + console.log(' Type:', credential.proof.type); |
| 65 | + console.log(' Cryptosuite:', credential.proof.cryptosuite); |
| 66 | + console.log(' Verification Method:', credential.proof.verificationMethod); |
| 67 | + console.log(' Created:', credential.proof.created); |
| 68 | + console.log(' Proof Purpose:', credential.proof.proofPurpose); |
| 69 | + console.log(' Proof Value (first 40 chars):', credential.proof.proofValue.substring(0, 40) + '...'); |
| 70 | + console.log(); |
| 71 | + |
| 72 | + // ------------------------------------------------------- |
| 73 | + // Step 4: Verify the credential |
| 74 | + // ------------------------------------------------------- |
| 75 | + // verifyCredential(vc, publicKeyBytes) checks: |
| 76 | + // - Required VC fields (@context, type, issuer, issuanceDate, credentialSubject) |
| 77 | + // - DataIntegrityProof signature validity |
| 78 | + console.log('=== Verification ==='); |
| 79 | + const result = verifyCredential(credential, issuerKeys.publicKeyBytes); |
| 80 | + console.log('Credential valid:', result.valid); |
| 81 | + console.log('Errors:', result.errors.length === 0 ? 'none' : result.errors); |
| 82 | + console.log(); |
| 83 | + |
| 84 | + // ------------------------------------------------------- |
| 85 | + // Step 5: Tamper detection (negative test) |
| 86 | + // ------------------------------------------------------- |
| 87 | + // Modify the credential and verify again - should fail |
| 88 | + // because the signature no longer matches the content. |
| 89 | + console.log('=== Tamper Detection ==='); |
| 90 | + const tampered = JSON.parse(JSON.stringify(credential)); |
| 91 | + tampered.credentialSubject.trailTrustTier = 2; // Attacker tries to upgrade trust tier |
| 92 | + |
| 93 | + const tamperedResult = verifyCredential(tampered, issuerKeys.publicKeyBytes); |
| 94 | + console.log('Tampered credential valid:', tamperedResult.valid, '(expected: false)'); |
| 95 | + console.log('Errors:', tamperedResult.errors); |
| 96 | + console.log(); |
| 97 | + |
| 98 | + console.log('Done. All examples completed successfully.'); |
| 99 | +} |
| 100 | + |
| 101 | +main().catch(console.error); |
0 commit comments