@synet/credential
Version:
VC Credentials - Simple, Robust, Unit-based Verifiable Credentials service
164 lines (163 loc) • 4.99 kB
TypeScript
/**
* Universal W3C-compatible Verifiable Credential types
*
* This module provides the foundational types for verifiable credentials
* that are compatible with both W3C standards and any extended functionality.
*
* Key design principles:
* - Universal compatibility (W3C and beyond)
* - Type safety and composability
* - Minimal dependencies
* - Extensible base classes for client implementations
*/
export interface ProofType {
type: string;
proofValue?: string;
created?: string;
verificationMethod?: string;
proofPurpose?: string;
jwt?: string;
[x: string]: unknown;
}
export interface Holder {
id: string;
name?: string;
[key: string]: unknown;
}
export interface VerifiableResource {
ipfsUri: string;
hash: string;
/**
* Optional content mirrors for redundancy and faster resolution.
* @use ar://, ipns://, dat://, sia://, don't use web2 if absolutely necessary
* Avoid using Web2 (http:// or https://) unless absolutely necessary.
* Verifiers must always validate mirror content against the declared hash.
*/
mirrors?: string[];
}
export interface CredentialDelegation {
id: string;
delegatedBy: Holder;
delegatedTo: Holder;
validFrom: string;
validUntil: string;
[key: string]: unknown;
}
export interface BaseCredentialSubject {
holder: Holder;
[key: string]: unknown;
}
/**
* Create a new credential subject type by extending the base credential subject with custom properties
*/
export type CreateCredentialSubject<T> = BaseCredentialSubject & T;
/**
* W3C-compatible Verifiable Credential interface
*
* This is the universal interface that serves as both the W3C standard
* and any extended credential type. By aliasing extended credential types
* to this interface, we create a dependency moat that works with any
* W3C-compatible system while maintaining extended functionality.
*/
export interface W3CVerifiableCredential<S extends BaseCredentialSubject = BaseCredentialSubject> {
"@context": string[];
id: string;
type: string[];
issuer: Holder;
issuanceDate: string;
expirationDate?: string;
credentialSubject: S;
proof: ProofType;
meta?: {
version?: string;
schema?: string;
[key: string]: unknown;
};
}
export interface IdentitySubject extends BaseCredentialSubject {
issuedBy: Holder;
scope?: string[];
}
export interface AuthorizationSubject extends BaseCredentialSubject {
authorizedBy: Holder;
scope?: string[];
metadata?: Record<string, unknown>;
schemaUri?: string;
verifiableResource?: VerifiableResource;
}
export interface AssetSubject extends BaseCredentialSubject {
issuedBy: Holder;
delegated?: CredentialDelegation;
parentAssetId?: string;
schemaUri?: string;
metadata?: Record<string, unknown>;
verifiableResource?: VerifiableResource;
}
export interface GovernanceSubject extends BaseCredentialSubject {
issuedBy: Holder;
metadata?: Record<string, unknown>;
schemaUri?: string;
verifiableResource?: VerifiableResource;
}
export interface DeclarationSubject extends BaseCredentialSubject {
issuedBy: Holder;
metadata?: Record<string, unknown>;
schemaUri?: string;
verifiableResource?: VerifiableResource;
}
export declare const BaseCredentialType: {
readonly Identity: "IdentityCredential";
readonly Authorization: "AuthorizationCredential";
readonly Asset: "AssetCredential";
readonly Governance: "GovernanceCredential";
readonly Declaration: "DeclarationCredential";
};
export type BaseCredentialTypeValues = (typeof BaseCredentialType)[keyof typeof BaseCredentialType];
export declare enum Intelligence {
human = "Human",
ai = "AI",
hybrid = "Hybrid",
swarm = "Swarm",
superintelligent = "Superintelligent"
}
export interface CredentialIssueOptions {
vcId?: string;
context?: string[];
issuanceDate?: string;
expirationDate?: string;
proofFormat?: "jwt" | "jsonld";
meta?: {
version?: string;
schema?: string;
[key: string]: unknown;
};
}
export interface CredentialVerifyOptions {
checkExpiration?: boolean;
checkIssuer?: boolean;
expectedIssuer?: string;
expectedSubject?: string;
allowedProofTypes?: string[];
}
export interface CredentialStorage<T extends W3CVerifiableCredential = W3CVerifiableCredential> {
save(credential: T): Promise<void>;
load(id: string): Promise<T | null>;
delete(id: string): Promise<void>;
list(): Promise<T[]>;
search(query: Record<string, unknown>): Promise<T[]>;
}
export interface CredentialResult<T> {
success: boolean;
data?: T;
error?: string;
details?: Record<string, unknown>;
}
export interface VerificationResult {
verified: boolean;
issuer?: string;
subject?: string;
issuanceDate?: string;
expirationDate?: string;
errors?: string[];
warnings?: string[];
}