UNPKG

@synet/identity

Version:

Simple and secure identity management library for Verifiable Identity

99 lines (77 loc) • 3.53 kB
/** * Basic test for Identity Unit Operator */ import { Identity } from '../src/identity'; async function testIdentity() { console.log('šŸš€ Testing Identity Unit Operator...'); try { // Test 1: Generate new identity console.log('\n1. Testing Identity.generate()...'); const result = await Identity.generate('test-user'); if (!result.isSuccess) { throw new Error(result.errorMessage || 'Failed to generate identity'); } const identity = result.value; console.log('āœ… Identity generated successfully'); console.log(' Alias:', identity.getAlias()); console.log(' DID:', identity.getDid()); console.log(' Public Key:', identity.getPublicKeyHex()); console.log(' Provider:', identity.getProvider()); // Test 2: Access units console.log('\n2. Testing unit access...'); const signer = identity.signer(); const key = identity.key(); const didUnit = identity.did(); const credentialUnit = identity.credential(); console.log('āœ… All units accessible'); console.log(' Signer:', signer.whoami()); console.log(' Key:', key?.whoami() || 'null'); console.log(' DID:', didUnit.whoami()); console.log(' Credential:', credentialUnit.whoami()); // Test 3: Use signer console.log('\n3. Testing signer operations...'); const message = 'Hello, World!'; const signature = await signer.sign(message); const isValid = await signer.verify(message, signature); console.log('āœ… Signer operations work'); console.log(' Signature:', signature); console.log(' Valid:', isValid); // Test 4: Export data console.log('\n4. Testing data export...'); const json = identity.toJSON(); const publicData = identity.public(); console.log('āœ… Data export works'); console.log(' Full JSON has private key:', !!json.privateKeyHex); console.log(' Public data has private key:', !!('privateKeyHex' in publicData)); // Test 5: Create from data (skip if no private key) console.log('\n5. Testing Identity.create()...'); const recreatedResult = Identity.create(json); if (!recreatedResult.isSuccess) { console.log('āš ļø Identity recreation skipped:', recreatedResult.errorMessage); console.log('\nšŸŽ‰ Core tests passed!'); return; } const recreated = recreatedResult.value; console.log('āœ… Identity recreated successfully'); console.log(' Same alias:', recreated.getAlias() === identity.getAlias()); console.log(' Same DID:', recreated.getDid() === identity.getDid()); // Test 6: Use recreated signer (might not work without proper private key) console.log('\n6. Testing recreated signer...'); try { const recreatedSigner = recreated.signer(); const signature2 = await recreatedSigner.sign(message); const isValid2 = await recreatedSigner.verify(message, signature2); console.log('āœ… Recreated signer works'); console.log(' Signature:', signature2); console.log(' Valid:', isValid2); } catch (error) { console.log('āš ļø Recreated signer test skipped (private key issue)'); console.log(' Error:', error instanceof Error ? error.message : String(error)); } console.log('\nšŸŽ‰ All tests passed!'); } catch (error) { console.error('āŒ Test failed:', error instanceof Error ? error.message : String(error)); //process.exit(1); } } testIdentity();