@kravc/schema 2.7.6 → 2.8.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -14
- package/dist/CredentialFactory.d.ts +345 -0
- package/dist/CredentialFactory.d.ts.map +1 -0
- package/dist/CredentialFactory.js +381 -0
- package/dist/CredentialFactory.js.map +1 -0
- package/dist/Schema.d.ts +448 -0
- package/dist/Schema.d.ts.map +1 -0
- package/dist/Schema.js +506 -0
- package/dist/Schema.js.map +1 -0
- package/dist/ValidationError.d.ts +70 -0
- package/dist/ValidationError.d.ts.map +1 -0
- package/dist/ValidationError.js +78 -0
- package/dist/ValidationError.js.map +1 -0
- package/dist/Validator.d.ts +483 -0
- package/dist/Validator.d.ts.map +1 -0
- package/dist/Validator.js +570 -0
- package/dist/Validator.js.map +1 -0
- package/dist/helpers/JsonSchema.d.ts +99 -0
- package/dist/helpers/JsonSchema.d.ts.map +1 -0
- package/dist/helpers/JsonSchema.js +3 -0
- package/dist/helpers/JsonSchema.js.map +1 -0
- package/dist/helpers/cleanupAttributes.d.ts +34 -0
- package/dist/helpers/cleanupAttributes.d.ts.map +1 -0
- package/dist/helpers/cleanupAttributes.js +113 -0
- package/dist/helpers/cleanupAttributes.js.map +1 -0
- package/dist/helpers/cleanupNulls.d.ts +27 -0
- package/dist/helpers/cleanupNulls.d.ts.map +1 -0
- package/dist/helpers/cleanupNulls.js +96 -0
- package/dist/helpers/cleanupNulls.js.map +1 -0
- package/dist/helpers/getReferenceIds.d.ts +169 -0
- package/dist/helpers/getReferenceIds.d.ts.map +1 -0
- package/dist/helpers/getReferenceIds.js +241 -0
- package/dist/helpers/getReferenceIds.js.map +1 -0
- package/dist/helpers/got.d.ts +60 -0
- package/dist/helpers/got.d.ts.map +1 -0
- package/dist/helpers/got.js +72 -0
- package/dist/helpers/got.js.map +1 -0
- package/dist/helpers/mapObjectProperties.d.ts +150 -0
- package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
- package/dist/helpers/mapObjectProperties.js +229 -0
- package/dist/helpers/mapObjectProperties.js.map +1 -0
- package/dist/helpers/normalizeAttributes.d.ts +213 -0
- package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
- package/dist/helpers/normalizeAttributes.js +243 -0
- package/dist/helpers/normalizeAttributes.js.map +1 -0
- package/dist/helpers/normalizeProperties.d.ts +168 -0
- package/dist/helpers/normalizeProperties.d.ts.map +1 -0
- package/dist/helpers/normalizeProperties.js +223 -0
- package/dist/helpers/normalizeProperties.js.map +1 -0
- package/dist/helpers/normalizeRequired.d.ts +159 -0
- package/dist/helpers/normalizeRequired.d.ts.map +1 -0
- package/dist/helpers/normalizeRequired.js +206 -0
- package/dist/helpers/normalizeRequired.js.map +1 -0
- package/dist/helpers/normalizeType.d.ts +81 -0
- package/dist/helpers/normalizeType.d.ts.map +1 -0
- package/dist/helpers/normalizeType.js +210 -0
- package/dist/helpers/normalizeType.js.map +1 -0
- package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
- package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
- package/dist/helpers/nullifyEmptyValues.js +191 -0
- package/dist/helpers/nullifyEmptyValues.js.map +1 -0
- package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
- package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
- package/dist/helpers/removeRequiredAndDefault.js +138 -0
- package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
- package/dist/helpers/validateId.d.ts +39 -0
- package/dist/helpers/validateId.d.ts.map +1 -0
- package/dist/helpers/validateId.js +51 -0
- package/dist/helpers/validateId.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/ld/documentLoader.d.ts +8 -0
- package/dist/ld/documentLoader.d.ts.map +1 -0
- package/dist/ld/documentLoader.js +24 -0
- package/dist/ld/documentLoader.js.map +1 -0
- package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
- package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
- package/dist/ld/getLinkedDataAttributeType.js +32 -0
- package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
- package/dist/ld/getLinkedDataContext.d.ts +19 -0
- package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
- package/dist/ld/getLinkedDataContext.js +50 -0
- package/dist/ld/getLinkedDataContext.js.map +1 -0
- package/eslint.config.mjs +32 -52
- package/examples/credentials/createAccountCredential.ts +27 -0
- package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
- package/examples/index.ts +7 -0
- package/examples/schemas/FavoriteItemSchema.ts +27 -0
- package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
- package/examples/schemas/PreferencesSchema.ts +29 -0
- package/examples/schemas/ProfileSchema.ts +91 -0
- package/examples/schemas/Status.yaml +3 -0
- package/examples/schemas/StatusSchema.ts +12 -0
- package/jest.config.mjs +5 -0
- package/package.json +27 -20
- package/src/CredentialFactory.ts +392 -0
- package/src/Schema.ts +583 -0
- package/src/ValidationError.ts +90 -0
- package/src/Validator.ts +603 -0
- package/src/__tests__/CredentialFactory.test.ts +588 -0
- package/src/__tests__/Schema.test.ts +371 -0
- package/src/__tests__/ValidationError.test.ts +235 -0
- package/src/__tests__/Validator.test.ts +787 -0
- package/src/helpers/JsonSchema.ts +119 -0
- package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
- package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
- package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
- package/src/helpers/__tests__/got.test.ts +193 -0
- package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
- package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
- package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
- package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
- package/src/helpers/__tests__/normalizeType.test.ts +772 -0
- package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
- package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
- package/src/helpers/__tests__/validateId.test.ts +118 -0
- package/src/helpers/cleanupAttributes.ts +151 -0
- package/src/helpers/cleanupNulls.ts +106 -0
- package/src/helpers/getReferenceIds.ts +273 -0
- package/src/helpers/got.ts +73 -0
- package/src/helpers/mapObjectProperties.ts +272 -0
- package/src/helpers/normalizeAttributes.ts +247 -0
- package/src/helpers/normalizeProperties.ts +249 -0
- package/src/helpers/normalizeRequired.ts +233 -0
- package/src/helpers/normalizeType.ts +235 -0
- package/src/helpers/nullifyEmptyValues.ts +207 -0
- package/src/helpers/removeRequiredAndDefault.ts +151 -0
- package/src/helpers/validateId.ts +53 -0
- package/src/index.ts +13 -0
- package/src/ld/__tests__/documentLoader.test.ts +57 -0
- package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
- package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
- package/src/ld/documentLoader.ts +28 -0
- package/src/ld/getLinkedDataAttributeType.ts +46 -0
- package/src/ld/getLinkedDataContext.ts +80 -0
- package/tsconfig.json +27 -0
- package/types/credentials-context.d.ts +14 -0
- package/types/security-context.d.ts +6 -0
- package/examples/Status.yaml +0 -3
- package/examples/createAccountCredential.js +0 -27
- package/examples/createMineSweeperScoreCredential.js +0 -63
- package/examples/index.js +0 -9
- package/src/CredentialFactory.js +0 -67
- package/src/CredentialFactory.spec.js +0 -131
- package/src/Schema.js +0 -104
- package/src/Schema.spec.js +0 -172
- package/src/ValidationError.js +0 -31
- package/src/Validator.js +0 -128
- package/src/Validator.spec.js +0 -355
- package/src/helpers/cleanupAttributes.js +0 -71
- package/src/helpers/cleanupNulls.js +0 -42
- package/src/helpers/getReferenceIds.js +0 -71
- package/src/helpers/mapObject.js +0 -65
- package/src/helpers/normalizeAttributes.js +0 -28
- package/src/helpers/normalizeProperties.js +0 -61
- package/src/helpers/normalizeRequired.js +0 -37
- package/src/helpers/normalizeType.js +0 -41
- package/src/helpers/nullifyEmptyValues.js +0 -57
- package/src/helpers/removeRequiredAndDefault.js +0 -30
- package/src/helpers/validateId.js +0 -19
- package/src/index.d.ts +0 -25
- package/src/index.js +0 -8
- package/src/ld/documentLoader.js +0 -25
- package/src/ld/documentLoader.spec.js +0 -12
- package/src/ld/getLinkedDataContext.js +0 -63
- package/src/ld/getLinkedDataType.js +0 -38
- /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
- /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const credentials_context_1 = require("credentials-context");
|
|
7
|
+
const Validator_1 = __importDefault(require("./Validator"));
|
|
8
|
+
const validateId_1 = __importDefault(require("./helpers/validateId"));
|
|
9
|
+
const Schema_1 = __importDefault(require("./Schema"));
|
|
10
|
+
const { CREDENTIALS_CONTEXT_V1_URL } = credentials_context_1.constants;
|
|
11
|
+
/**
|
|
12
|
+
* Factory class for creating Verifiable Credentials with embedded JSON-LD linked data context.
|
|
13
|
+
*
|
|
14
|
+
* **Intent:** Provide a standardized way to generate W3C Verifiable Credentials that are
|
|
15
|
+
* compatible with JSON-LD and semantic web standards. The factory automatically validates
|
|
16
|
+
* credential subjects against provided schemas, generates appropriate JSON-LD contexts, and
|
|
17
|
+
* structures credentials according to the Verifiable Credentials data model.
|
|
18
|
+
*
|
|
19
|
+
* **Use Cases:**
|
|
20
|
+
* - Generate Verifiable Credentials for decentralized identity systems
|
|
21
|
+
* - Create credentials with automatic schema validation and type checking
|
|
22
|
+
* - Build credentials with embedded semantic context (schema.org, custom vocabularies)
|
|
23
|
+
* - Support multi-schema credentials with nested object references
|
|
24
|
+
* - Enable credential interoperability through standardized JSON-LD contexts
|
|
25
|
+
* - Integrate with credential issuance systems (e.g., DID-based identity providers)
|
|
26
|
+
*
|
|
27
|
+
* **Key Features:**
|
|
28
|
+
* - Automatic validation of credential subjects against schema definitions
|
|
29
|
+
* - JSON-LD context generation for semantic web compatibility
|
|
30
|
+
* - Support for multiple schemas with cross-references (`$ref`)
|
|
31
|
+
* - Type-safe credential generation with TypeScript support
|
|
32
|
+
* - Schema.org type mapping for common data formats (date-time, date, etc.)
|
|
33
|
+
*
|
|
34
|
+
* **Example - Simple Credential:**
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
37
|
+
*
|
|
38
|
+
* // Define credential subject schema
|
|
39
|
+
* const accountSchema = new Schema({
|
|
40
|
+
* id: {},
|
|
41
|
+
* username: { required: true },
|
|
42
|
+
* createdAt: { format: 'date-time', required: true },
|
|
43
|
+
* dateOfBirth: { format: 'date' }
|
|
44
|
+
* }, 'Account');
|
|
45
|
+
*
|
|
46
|
+
* // Create factory with credential URI and schemas
|
|
47
|
+
* const factory = new CredentialFactory(
|
|
48
|
+
* 'https://example.com/schema/AccountV1',
|
|
49
|
+
* [accountSchema]
|
|
50
|
+
* );
|
|
51
|
+
*
|
|
52
|
+
* // Generate credential
|
|
53
|
+
* const credential = factory.createCredential(
|
|
54
|
+
* 'https://example.com/credentials/123',
|
|
55
|
+
* 'did:holder:123',
|
|
56
|
+
* {
|
|
57
|
+
* id: 'did:holder:123',
|
|
58
|
+
* username: 'alice',
|
|
59
|
+
* createdAt: new Date().toISOString()
|
|
60
|
+
* }
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* **Example - Multi-Schema Credential with References:**
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
67
|
+
*
|
|
68
|
+
* // Define multiple related schemas
|
|
69
|
+
* const playerSchema = new Schema({
|
|
70
|
+
* id: {},
|
|
71
|
+
* hasVideoGameScore: { $ref: 'VideoGameScore', required: true }
|
|
72
|
+
* }, 'Player');
|
|
73
|
+
*
|
|
74
|
+
* const videoGameSchema = new Schema({
|
|
75
|
+
* id: {},
|
|
76
|
+
* name: { type: 'string', required: true },
|
|
77
|
+
* version: { type: 'string', required: true }
|
|
78
|
+
* }, 'VideoGame', 'https://schema.org/');
|
|
79
|
+
*
|
|
80
|
+
* const scoreSchema = new Schema({
|
|
81
|
+
* game: { $ref: 'VideoGame', required: true },
|
|
82
|
+
* wins: { type: 'integer', required: true },
|
|
83
|
+
* bestScore: { type: 'integer', required: true }
|
|
84
|
+
* }, 'VideoGameScore');
|
|
85
|
+
*
|
|
86
|
+
* // Create factory with multiple schemas
|
|
87
|
+
* const factory = new CredentialFactory(
|
|
88
|
+
* 'https://example.com/schema/GameScoreV1',
|
|
89
|
+
* [playerSchema, videoGameSchema, scoreSchema]
|
|
90
|
+
* );
|
|
91
|
+
*
|
|
92
|
+
* // Generate credential with nested data
|
|
93
|
+
* const credential = factory.createCredential(
|
|
94
|
+
* 'https://example.com/credentials/score-456',
|
|
95
|
+
* 'did:player:789',
|
|
96
|
+
* {
|
|
97
|
+
* id: 'did:player:789',
|
|
98
|
+
* hasVideoGameScore: {
|
|
99
|
+
* game: { id: 'did:game:001', name: 'MineSweeper', version: '1.0' },
|
|
100
|
+
* wins: 10,
|
|
101
|
+
* bestScore: 5000
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* );
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* **Example - Accessing Credential Context:**
|
|
108
|
+
* ```typescript
|
|
109
|
+
* const factory = new CredentialFactory(uri, schemas);
|
|
110
|
+
*
|
|
111
|
+
* // Get credential type (extracted from URI)
|
|
112
|
+
* const type = factory.credentialType; // e.g., 'AccountV1'
|
|
113
|
+
*
|
|
114
|
+
* // Get JSON-LD context for embedding
|
|
115
|
+
* const context = factory.context;
|
|
116
|
+
* // {
|
|
117
|
+
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
118
|
+
* // Account: { '@id': '...', '@context': {...} }
|
|
119
|
+
* // }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
class CredentialFactory {
|
|
123
|
+
_uri;
|
|
124
|
+
_types;
|
|
125
|
+
_context;
|
|
126
|
+
_validator;
|
|
127
|
+
/**
|
|
128
|
+
* Creates a new CredentialFactory instance.
|
|
129
|
+
*
|
|
130
|
+
* **Intent:** Initialize a factory with credential schemas and URI, enabling credential
|
|
131
|
+
* generation with automatic validation and JSON-LD context generation.
|
|
132
|
+
*
|
|
133
|
+
* **Use Cases:**
|
|
134
|
+
* - Set up credential factories for different credential types
|
|
135
|
+
* - Configure factories with single or multiple related schemas
|
|
136
|
+
* - Prepare factories for batch credential generation
|
|
137
|
+
*
|
|
138
|
+
* **Behavior:**
|
|
139
|
+
* - Validates that the provided URI is a valid URL
|
|
140
|
+
* - Processes schemas: if a schema doesn't have a URL, creates a new Schema instance
|
|
141
|
+
* with the factory URI as the base URL
|
|
142
|
+
* - Validates all schemas using the Validator
|
|
143
|
+
* - Builds JSON-LD context from schema linked data types
|
|
144
|
+
*
|
|
145
|
+
* @param uri - The base URI for the credential type (e.g., 'https://example.com/schema/AccountV1')
|
|
146
|
+
* Must be a valid URL. The last segment will be used as the credential type name.
|
|
147
|
+
* @param schemas - Array of Schema instances defining the credential subject structure.
|
|
148
|
+
* The first schema is considered the root schema for validation.
|
|
149
|
+
* Schemas can reference each other using `$ref`.
|
|
150
|
+
*
|
|
151
|
+
* @throws Error if the URI is not a valid URL
|
|
152
|
+
* @throws Error if schema validation fails (e.g., invalid JSON Schema structure)
|
|
153
|
+
*
|
|
154
|
+
* **Example:**
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const accountSchema = new Schema({
|
|
157
|
+
* username: { required: true },
|
|
158
|
+
* email: { format: 'email', required: true }
|
|
159
|
+
* }, 'Account');
|
|
160
|
+
*
|
|
161
|
+
* const factory = new CredentialFactory(
|
|
162
|
+
* 'https://example.com/schema/AccountV1',
|
|
163
|
+
* [accountSchema]
|
|
164
|
+
* );
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
constructor(uri, schemas) {
|
|
168
|
+
(0, validateId_1.default)('uri', uri);
|
|
169
|
+
this._types = schemas
|
|
170
|
+
.map(schema => {
|
|
171
|
+
if (schema.url) {
|
|
172
|
+
return schema;
|
|
173
|
+
}
|
|
174
|
+
return new Schema_1.default(schema, schema.id, uri);
|
|
175
|
+
});
|
|
176
|
+
this._uri = uri;
|
|
177
|
+
this._context = {};
|
|
178
|
+
this._validator = new Validator_1.default(this._types);
|
|
179
|
+
for (const { id, linkedDataType } of this._types) {
|
|
180
|
+
this._context[id] = linkedDataType;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns the credential type name extracted from the factory URI.
|
|
185
|
+
*
|
|
186
|
+
* **Intent:** Provide a convenient way to access the credential type identifier
|
|
187
|
+
* without manually parsing the URI.
|
|
188
|
+
*
|
|
189
|
+
* **Use Cases:**
|
|
190
|
+
* - Access credential type for logging or debugging
|
|
191
|
+
* - Use type name in credential processing logic
|
|
192
|
+
* - Generate type-specific identifiers or filenames
|
|
193
|
+
*
|
|
194
|
+
* **Behavior:**
|
|
195
|
+
* - Extracts the last segment from the URI path
|
|
196
|
+
* - Example: 'https://example.com/schema/AccountV1' → 'AccountV1'
|
|
197
|
+
*
|
|
198
|
+
* @returns The credential type name (last segment of the URI path)
|
|
199
|
+
*
|
|
200
|
+
* **Example:**
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const factory = new CredentialFactory(
|
|
203
|
+
* 'https://example.com/schema/AccountV1',
|
|
204
|
+
* [schema]
|
|
205
|
+
* );
|
|
206
|
+
*
|
|
207
|
+
* console.log(factory.credentialType); // 'AccountV1'
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
get credentialType() {
|
|
211
|
+
const [credentialType] = this._uri.split('/').reverse();
|
|
212
|
+
return credentialType;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Returns the JSON-LD context object for the credential.
|
|
216
|
+
*
|
|
217
|
+
* **Intent:** Provide the complete JSON-LD context mapping that should be included
|
|
218
|
+
* in the credential's `@context` array for semantic web compatibility.
|
|
219
|
+
*
|
|
220
|
+
* **Use Cases:**
|
|
221
|
+
* - Access context for credential serialization
|
|
222
|
+
* - Inspect context mappings for debugging
|
|
223
|
+
* - Use context in custom credential processing
|
|
224
|
+
* - Embed context in other JSON-LD documents
|
|
225
|
+
*
|
|
226
|
+
* **Structure:**
|
|
227
|
+
* - Contains the credential type mapping (`{ credentialType: { '@id': uri } }`)
|
|
228
|
+
* - Includes all schema-linked data types from the factory's schemas
|
|
229
|
+
* - Each schema's `linkedDataType` is included if the schema was created with a URL
|
|
230
|
+
*
|
|
231
|
+
* @returns Object containing credential type and schema type mappings for JSON-LD context
|
|
232
|
+
*
|
|
233
|
+
* **Example:**
|
|
234
|
+
* ```typescript
|
|
235
|
+
* const factory = new CredentialFactory(uri, schemas);
|
|
236
|
+
* const context = factory.context;
|
|
237
|
+
*
|
|
238
|
+
* // Result structure:
|
|
239
|
+
* // {
|
|
240
|
+
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
241
|
+
* // Account: {
|
|
242
|
+
* // '@id': 'https://example.com/schema/AccountV1#Account',
|
|
243
|
+
* // '@context': { ... }
|
|
244
|
+
* // }
|
|
245
|
+
* // }
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
get context() {
|
|
249
|
+
return {
|
|
250
|
+
[this.credentialType]: { '@id': this._uri },
|
|
251
|
+
...this._context
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Creates a Verifiable Credential for the specified subject.
|
|
256
|
+
*
|
|
257
|
+
* **Intent:** Generate a W3C-compliant Verifiable Credential with validated subject data,
|
|
258
|
+
* proper JSON-LD context, and standardized structure ready for issuance and verification.
|
|
259
|
+
*
|
|
260
|
+
* **Use Cases:**
|
|
261
|
+
* - Issue credentials for user accounts, achievements, certifications
|
|
262
|
+
* - Generate credentials with automatic schema validation
|
|
263
|
+
* - Create credentials with embedded semantic context for interoperability
|
|
264
|
+
* - Build credentials for decentralized identity systems
|
|
265
|
+
* - Generate credentials that can be verified using JSON-LD processors
|
|
266
|
+
*
|
|
267
|
+
* **Behavior:**
|
|
268
|
+
* - Validates that `id` and `holder` are valid URLs/URIs
|
|
269
|
+
* - Validates the subject against the root schema (first schema in the factory)
|
|
270
|
+
* - Applies schema defaults and normalizes data according to schema rules
|
|
271
|
+
* - Generates credential with proper `@context`, `type`, `id`, `holder`, and `credentialSubject`
|
|
272
|
+
* - Returns credential ready for signing (issuer, issuanceDate, proof to be added separately)
|
|
273
|
+
*
|
|
274
|
+
* **Credential Structure:**
|
|
275
|
+
* ```typescript
|
|
276
|
+
* {
|
|
277
|
+
* '@context': [
|
|
278
|
+
* 'https://www.w3.org/2018/credentials/v1',
|
|
279
|
+
* factory.context // JSON-LD context from factory
|
|
280
|
+
* ],
|
|
281
|
+
* id: string, // Credential identifier
|
|
282
|
+
* type: ['VerifiableCredential', credentialType],
|
|
283
|
+
* holder: string, // DID or identifier of credential holder
|
|
284
|
+
* credentialSubject: { // Validated and normalized subject data
|
|
285
|
+
* id: string,
|
|
286
|
+
* type: string, // Root schema ID
|
|
287
|
+
* // ... additional subject properties
|
|
288
|
+
* }
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*
|
|
292
|
+
* @param id - Unique identifier for the credential (must be a valid URL/URI)
|
|
293
|
+
* @param holder - DID or identifier of the credential holder (must be a valid URL/URI)
|
|
294
|
+
* @param subject - Object containing the credential subject data to be validated against
|
|
295
|
+
* the root schema. Properties are validated, defaults are applied,
|
|
296
|
+
* and nested objects are validated against referenced schemas.
|
|
297
|
+
*
|
|
298
|
+
* @returns Verifiable Credential object with validated subject and JSON-LD context
|
|
299
|
+
*
|
|
300
|
+
* @throws Error if `id` is not a valid URL/URI
|
|
301
|
+
* @throws Error if `holder` is not a valid URL/URI
|
|
302
|
+
* @throws ValidationError if subject data doesn't match the schema requirements
|
|
303
|
+
*
|
|
304
|
+
* **Example - Basic Credential:**
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const factory = new CredentialFactory(uri, [accountSchema]);
|
|
307
|
+
*
|
|
308
|
+
* const credential = factory.createCredential(
|
|
309
|
+
* 'https://example.com/credentials/123',
|
|
310
|
+
* 'did:holder:456',
|
|
311
|
+
* {
|
|
312
|
+
* id: 'did:holder:456',
|
|
313
|
+
* username: 'alice',
|
|
314
|
+
* createdAt: '2024-01-01T00:00:00Z'
|
|
315
|
+
* }
|
|
316
|
+
* );
|
|
317
|
+
*
|
|
318
|
+
* // Result:
|
|
319
|
+
* // {
|
|
320
|
+
* // '@context': ['https://www.w3.org/2018/credentials/v1', {...}],
|
|
321
|
+
* // id: 'https://example.com/credentials/123',
|
|
322
|
+
* // type: ['VerifiableCredential', 'AccountV1'],
|
|
323
|
+
* // holder: 'did:holder:456',
|
|
324
|
+
* // credentialSubject: {
|
|
325
|
+
* // id: 'did:holder:456',
|
|
326
|
+
* // username: 'alice',
|
|
327
|
+
* // createdAt: '2024-01-01T00:00:00Z',
|
|
328
|
+
* // type: 'Account'
|
|
329
|
+
* // }
|
|
330
|
+
* // }
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* **Example - Credential with Nested Objects:**
|
|
334
|
+
* ```typescript
|
|
335
|
+
* const factory = new CredentialFactory(uri, [playerSchema, gameSchema, scoreSchema]);
|
|
336
|
+
*
|
|
337
|
+
* const credential = factory.createCredential(
|
|
338
|
+
* 'https://example.com/credentials/score-789',
|
|
339
|
+
* 'did:player:123',
|
|
340
|
+
* {
|
|
341
|
+
* id: 'did:player:123',
|
|
342
|
+
* hasVideoGameScore: {
|
|
343
|
+
* game: {
|
|
344
|
+
* id: 'did:game:001',
|
|
345
|
+
* name: 'MineSweeper',
|
|
346
|
+
* version: '1.0'
|
|
347
|
+
* },
|
|
348
|
+
* wins: 10,
|
|
349
|
+
* bestScore: 5000
|
|
350
|
+
* }
|
|
351
|
+
* }
|
|
352
|
+
* );
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* **Note:** The returned credential does not include `issuer`, `issuanceDate`, or `proof`
|
|
356
|
+
* fields. These should be added by the credential issuance system (e.g., using
|
|
357
|
+
* [@kravc/identity](http://github.com/alexkravets/identity) or similar libraries).
|
|
358
|
+
*/
|
|
359
|
+
createCredential(id, holder, subject = {}) {
|
|
360
|
+
(0, validateId_1.default)('id', id);
|
|
361
|
+
(0, validateId_1.default)('holder', holder);
|
|
362
|
+
const type = [
|
|
363
|
+
'VerifiableCredential',
|
|
364
|
+
this.credentialType
|
|
365
|
+
];
|
|
366
|
+
const [rootType] = this._types;
|
|
367
|
+
const credentialSubject = this._validator.validate(subject, rootType.id);
|
|
368
|
+
return {
|
|
369
|
+
'@context': [
|
|
370
|
+
CREDENTIALS_CONTEXT_V1_URL,
|
|
371
|
+
this.context
|
|
372
|
+
],
|
|
373
|
+
id,
|
|
374
|
+
type,
|
|
375
|
+
holder,
|
|
376
|
+
credentialSubject
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
exports.default = CredentialFactory;
|
|
381
|
+
//# sourceMappingURL=CredentialFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CredentialFactory.js","sourceRoot":"","sources":["../src/CredentialFactory.ts"],"names":[],"mappings":";;;;;AAAA,6DAAgD;AAEhD,4DAAoC;AACpC,sEAA8C;AAE9C,sDAAuD;AAEvD,MAAM,EAAE,0BAA0B,EAAE,GAAG,+BAAS,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8GG;AACH,MAAM,iBAAiB;IACb,IAA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