This feature adds abstract repository support for resolving short form peer DID variant 4 identifiers. Short form DIDs are compact representations that require the original long form DID document to be stored and retrievable for resolution.
An abstract interface that can be implemented with different storage backends:
interface IDIDRepository {
store(shortFormDid: string, longFormDid: string): Promise<void>;
retrieve(shortFormDid: string): Promise<string | null>;
exists(shortFormDid: string): Promise<boolean>;
}A concrete implementation using in-memory storage:
const repository = new InMemoryDIDRepository();The resolve function now accepts an optional repository parameter:
const doc = await resolve(shortFormDid, repository);import {
createNumAlgo4,
resolve,
InMemoryDIDRepository,
storeLongFormDid
} from '@aviarytech/did-peer';
// 1. Create repository
const repository = new InMemoryDIDRepository();
// 2. Create a DID
const longFormDid = await createNumAlgo4([authKey], [encKey]);
// 3. Store in repository
const shortFormDid = await storeLongFormDid(longFormDid, repository);
// 4. Resolve short form (requires repository)
const doc = await resolve(shortFormDid, repository);
// 5. Resolve long form (no repository needed)
const doc2 = await resolve(longFormDid);class DatabaseDIDRepository implements IDIDRepository {
async store(shortFormDid: string, longFormDid: string): Promise<void> {
// Store in your database
await db.collection('dids').insertOne({
shortForm: shortFormDid,
longForm: longFormDid
});
}
async retrieve(shortFormDid: string): Promise<string | null> {
// Retrieve from your database
const result = await db.collection('dids').findOne({
shortForm: shortFormDid
});
return result?.longForm || null;
}
async exists(shortFormDid: string): Promise<boolean> {
const count = await db.collection('dids').countDocuments({
shortForm: shortFormDid
});
return count > 0;
}
}- Backward Compatibility: Existing code continues to work without changes
- Flexible Storage: Repository interface can be implemented with any storage backend
- Proper Error Handling: Clear error messages for missing repositories or DIDs
- Utility Functions: Helper functions for extracting short forms and storing DIDs
- Type Safety: Full TypeScript support with proper interfaces
- Long Form:
did:peer:4zHash:zDocument(self-contained, no repository needed) - Short Form:
did:peer:4zHash(requires repository lookup)
When resolving:
- Long form documents have
alsoKnownAs: [shortFormDid] - Short form documents have
alsoKnownAs: [longFormDid]
- No Repository Provided: Throws error when trying to resolve short form without repository
- DID Not Found: Throws error when short form DID doesn't exist in repository
- Invalid Format: Throws error for malformed DID strings
Comprehensive test suite covers:
- Repository storage and retrieval
- Short form extraction utilities
- Integration with resolve functions
- Error conditions and edge cases
Run tests with:
npm test src/tests/lib/repository.spec.ts