veriglob 1.0.1
veriglob: ^1.0.1 copied to clipboard
Veriglob decentralized identity protocol - Dart implementation
import 'dart:io';
import 'dart:typed_data';
import 'package:veriglob/veriglob.dart';
void main() async {
// Generate Ed25519 key pair
final keyPair = generateEd25519KeyPair();
print('Generated key pair');
print('Public key: ${keyPair.publicKey.length} bytes');
// Create DID from public key
final did = createDIDKey(keyPair.publicKey);
print('DID: ${did.did}');
// Issue a credential
final subject = IdentitySubject(
id: did.did,
givenName: 'John',
familyName: 'Doe',
dateOfBirth: '1990-01-15',
nationality: 'US',
);
final vcToken = issueVC(
did.did,
did.did,
keyPair.privateKey,
subject,
credentialId: generateCredentialId(),
);
print('Issued VC token: ${vcToken.substring(0, 50)}...');
// Verify the credential
final claims = verifyVC(vcToken, keyPair.publicKey);
print('Verified! Issuer: ${claims.issuer}');
print('Subject: ${claims.vc.credentialSubject}');
// Create a presentation
final nonce = generateNonce();
final vpToken = createPresentation(
did.did,
keyPair.privateKey,
[vcToken],
'did:key:verifier123',
nonce,
);
print('Created presentation token');
// Verify presentation
final vpClaims = verifyPresentation(
vpToken,
keyPair.publicKey,
expectedNonce: nonce,
);
print('Verified presentation! Holder: ${vpClaims.vp.holder}');
// Derive keys from mnemonic
const mnemonic =
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
final derivedKeys = deriveKeysFromMnemonic(mnemonic);
final derivedDid = createDIDKey(derivedKeys.publicKey);
print('Derived DID from mnemonic: ${derivedDid.did}');
// ============================================================
// WALLET & CREDENTIAL FILE STORAGE EXAMPLE
// ============================================================
print('\n--- Wallet & Credential File Storage ---\n');
// Define paths for wallet and credential storage
final walletPath = '${Directory.systemTemp.path}/veriglob_example/wallet.enc';
final credentialPath =
'${Directory.systemTemp.path}/veriglob_example/credentials/identity.enc';
// Create or open a wallet
Wallet wallet;
if (await Wallet.exists(walletPath)) {
// Open existing wallet
wallet = await Wallet.open(walletPath, mnemonic);
print('Opened existing wallet');
} else {
// Create new wallet
wallet = await Wallet.create(walletPath, mnemonic);
print('Created new wallet at: $walletPath');
}
// Create a StoredCredential from the VC we issued earlier
final storedCredential = StoredCredential.create(
id: generateCredentialId(),
name: 'Identity Credential',
issuer: 'Example Issuer',
issuerDid: did.did,
issuerPublicKey: did.did,
typeString: 'IdentityCredential',
token: vcToken,
issuedAt: DateTime.now().toUtc(),
expiresAt: DateTime.now().toUtc().add(const Duration(days: 365)),
description: 'A sample identity credential for demonstration',
credentialSubject: {
'givenName': 'John',
'familyName': 'Doe',
'dateOfBirth': '1990-01-15',
},
);
// Store credential to a file (encrypted with wallet keys)
await wallet.storeCredentialToFile(storedCredential, credentialPath);
print('Stored encrypted credential to: $credentialPath');
// Load credential back from file
final loadedCredential = await wallet.loadCredentialFromFile(credentialPath);
print('Loaded credential from file:');
print(' - ID: ${loadedCredential.id}');
print(' - Name: ${loadedCredential.name}');
print(' - Type: ${loadedCredential.typeLabel}');
print(' - Status: ${loadedCredential.statusLabel}');
print(' - Issuer: ${loadedCredential.issuerName}');
// You can also add the credential to the wallet's internal storage
await wallet.addCredential(storedCredential);
print('Added credential to wallet internal storage');
// List all credentials in wallet
final allCredentials = wallet.listCredentials();
print('Wallet contains ${allCredentials.length} credential(s)');
// Clean up example files (optional)
// await File(walletPath).delete();
// await File(credentialPath).delete();
print('\n--- Example Complete ---');
}