@kravc/schema 2.8.0-alpha.6 → 2.8.0-alpha.8
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/dist/CredentialFactory.d.ts +5 -318
- package/dist/CredentialFactory.d.ts.map +1 -1
- package/dist/CredentialFactory.js +5 -317
- package/dist/CredentialFactory.js.map +1 -1
- package/dist/Schema.d.ts +15 -420
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +13 -384
- package/dist/Schema.js.map +1 -1
- package/dist/ValidationError.d.ts +3 -49
- package/dist/ValidationError.d.ts.map +1 -1
- package/dist/ValidationError.js +3 -48
- package/dist/ValidationError.js.map +1 -1
- package/dist/Validator.d.ts +6 -470
- package/dist/Validator.d.ts.map +1 -1
- package/dist/Validator.js +50 -478
- package/dist/Validator.js.map +1 -1
- package/dist/helpers/cleanupAttributes.d.ts +3 -32
- package/dist/helpers/cleanupAttributes.d.ts.map +1 -1
- package/dist/helpers/cleanupAttributes.js +1 -30
- package/dist/helpers/cleanupAttributes.js.map +1 -1
- package/dist/helpers/cleanupNulls.d.ts +1 -25
- package/dist/helpers/cleanupNulls.d.ts.map +1 -1
- package/dist/helpers/cleanupNulls.js +2 -61
- package/dist/helpers/cleanupNulls.js.map +1 -1
- package/dist/helpers/createSchemasMap.d.ts +2 -93
- package/dist/helpers/createSchemasMap.d.ts.map +1 -1
- package/dist/helpers/createSchemasMap.js +4 -162
- package/dist/helpers/createSchemasMap.js.map +1 -1
- package/dist/helpers/getReferenceIds.d.ts +1 -165
- package/dist/helpers/getReferenceIds.d.ts.map +1 -1
- package/dist/helpers/getReferenceIds.js +2 -166
- package/dist/helpers/getReferenceIds.js.map +1 -1
- package/dist/helpers/got.d.ts +2 -58
- package/dist/helpers/got.d.ts.map +1 -1
- package/dist/helpers/got.js +1 -57
- package/dist/helpers/got.js.map +1 -1
- package/dist/helpers/mapObjectProperties.d.ts +2 -144
- package/dist/helpers/mapObjectProperties.d.ts.map +1 -1
- package/dist/helpers/mapObjectProperties.js +1 -142
- package/dist/helpers/mapObjectProperties.js.map +1 -1
- package/dist/helpers/normalizeAttributes.d.ts +3 -211
- package/dist/helpers/normalizeAttributes.d.ts.map +1 -1
- package/dist/helpers/normalizeAttributes.js +1 -209
- package/dist/helpers/normalizeAttributes.js.map +1 -1
- package/dist/helpers/normalizeProperties.d.ts +1 -165
- package/dist/helpers/normalizeProperties.d.ts.map +1 -1
- package/dist/helpers/normalizeProperties.js +1 -164
- package/dist/helpers/normalizeProperties.js.map +1 -1
- package/dist/helpers/normalizeRequired.d.ts +1 -153
- package/dist/helpers/normalizeRequired.d.ts.map +1 -1
- package/dist/helpers/normalizeRequired.js +0 -151
- package/dist/helpers/normalizeRequired.js.map +1 -1
- package/dist/helpers/normalizeType.d.ts +1 -77
- package/dist/helpers/normalizeType.d.ts.map +1 -1
- package/dist/helpers/normalizeType.js +11 -139
- package/dist/helpers/normalizeType.js.map +1 -1
- package/dist/helpers/nullifyEmptyValues.d.ts +1 -135
- package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -1
- package/dist/helpers/nullifyEmptyValues.js +13 -143
- package/dist/helpers/nullifyEmptyValues.js.map +1 -1
- package/dist/helpers/removeRequiredAndDefault.d.ts +2 -102
- package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -1
- package/dist/helpers/removeRequiredAndDefault.js +1 -100
- package/dist/helpers/removeRequiredAndDefault.js.map +1 -1
- package/dist/helpers/validateId.d.ts +1 -36
- package/dist/helpers/validateId.d.ts.map +1 -1
- package/dist/helpers/validateId.js +1 -36
- package/dist/helpers/validateId.js.map +1 -1
- package/dist/ld/documentLoader.d.ts +1 -1
- package/dist/ld/documentLoader.d.ts.map +1 -1
- package/dist/ld/documentLoader.js +1 -1
- package/dist/ld/documentLoader.js.map +1 -1
- package/dist/ld/getLinkedDataAttributeType.d.ts +1 -1
- package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -1
- package/dist/ld/getLinkedDataAttributeType.js +1 -1
- package/dist/ld/getLinkedDataAttributeType.js.map +1 -1
- package/dist/ld/getLinkedDataContext.d.ts +1 -1
- package/dist/ld/getLinkedDataContext.d.ts.map +1 -1
- package/dist/ld/getLinkedDataContext.js +1 -1
- package/dist/ld/getLinkedDataContext.js.map +1 -1
- package/package.json +2 -2
- package/src/CredentialFactory.ts +5 -318
- package/src/Schema.ts +17 -427
- package/src/ValidationError.ts +5 -52
- package/src/Validator.ts +19 -483
- package/src/__tests__/CredentialFactory.test.ts +1 -1
- package/src/__tests__/Schema.test.ts +3 -8
- package/src/__tests__/ValidationError.test.ts +4 -2
- package/src/__tests__/Validator.test.ts +21 -4
- package/src/helpers/__tests__/cleanupAttributes.test.ts +3 -1
- package/src/helpers/__tests__/createSchemasMap.test.ts +1 -1
- package/src/helpers/__tests__/mapObjectProperties.test.ts +2 -1
- package/src/helpers/__tests__/normalizeAttributes.test.ts +4 -2
- package/src/helpers/__tests__/normalizeProperties.test.ts +3 -1
- package/src/helpers/__tests__/normalizeRequired.test.ts +4 -9
- package/src/helpers/__tests__/nullifyEmptyValues.test.ts +43 -12
- package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +3 -10
- package/src/helpers/cleanupAttributes.ts +6 -44
- package/src/helpers/cleanupNulls.ts +2 -63
- package/src/helpers/createSchemasMap.ts +4 -163
- package/src/helpers/getReferenceIds.ts +2 -173
- package/src/helpers/got.ts +3 -59
- package/src/helpers/mapObjectProperties.ts +4 -156
- package/src/helpers/normalizeAttributes.ts +7 -211
- package/src/helpers/normalizeProperties.ts +1 -172
- package/src/helpers/normalizeRequired.ts +1 -161
- package/src/helpers/normalizeType.ts +11 -139
- package/src/helpers/nullifyEmptyValues.ts +10 -145
- package/src/helpers/removeRequiredAndDefault.ts +1 -106
- package/src/helpers/validateId.ts +1 -36
- package/src/ld/documentLoader.ts +1 -1
- package/src/ld/getLinkedDataAttributeType.ts +1 -1
- package/src/ld/getLinkedDataContext.ts +1 -1
- package/src/{helpers/JsonSchema.ts → types.d.ts} +12 -16
- package/dist/helpers/JsonSchema.d.ts +0 -99
- package/dist/helpers/JsonSchema.d.ts.map +0 -1
- package/dist/helpers/JsonSchema.js +0 -3
- package/dist/helpers/JsonSchema.js.map +0 -1
package/src/CredentialFactory.ts
CHANGED
|
@@ -2,168 +2,18 @@ import { constants } from 'credentials-context';
|
|
|
2
2
|
|
|
3
3
|
import Validator from './Validator';
|
|
4
4
|
import validateId from './helpers/validateId';
|
|
5
|
-
import type { TargetObject } from './helpers/JsonSchema';
|
|
6
5
|
import Schema, { type LinkedDataType } from './Schema';
|
|
7
6
|
|
|
8
7
|
const { CREDENTIALS_CONTEXT_V1_URL } = constants;
|
|
9
8
|
|
|
10
|
-
/**
|
|
11
|
-
* Factory class for creating Verifiable Credentials with embedded JSON-LD linked data context.
|
|
12
|
-
*
|
|
13
|
-
* **Intent:** Provide a standardized way to generate W3C Verifiable Credentials that are
|
|
14
|
-
* compatible with JSON-LD and semantic web standards. The factory automatically validates
|
|
15
|
-
* credential subjects against provided schemas, generates appropriate JSON-LD contexts, and
|
|
16
|
-
* structures credentials according to the Verifiable Credentials data model.
|
|
17
|
-
*
|
|
18
|
-
* **Use Cases:**
|
|
19
|
-
* - Generate Verifiable Credentials for decentralized identity systems
|
|
20
|
-
* - Create credentials with automatic schema validation and type checking
|
|
21
|
-
* - Build credentials with embedded semantic context (schema.org, custom vocabularies)
|
|
22
|
-
* - Support multi-schema credentials with nested object references
|
|
23
|
-
* - Enable credential interoperability through standardized JSON-LD contexts
|
|
24
|
-
* - Integrate with credential issuance systems (e.g., DID-based identity providers)
|
|
25
|
-
*
|
|
26
|
-
* **Key Features:**
|
|
27
|
-
* - Automatic validation of credential subjects against schema definitions
|
|
28
|
-
* - JSON-LD context generation for semantic web compatibility
|
|
29
|
-
* - Support for multiple schemas with cross-references (`$ref`)
|
|
30
|
-
* - Type-safe credential generation with TypeScript support
|
|
31
|
-
* - Schema.org type mapping for common data formats (date-time, date, etc.)
|
|
32
|
-
*
|
|
33
|
-
* **Example - Simple Credential:**
|
|
34
|
-
* ```typescript
|
|
35
|
-
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
36
|
-
*
|
|
37
|
-
* // Define credential subject schema
|
|
38
|
-
* const accountSchema = new Schema({
|
|
39
|
-
* id: {},
|
|
40
|
-
* username: { required: true },
|
|
41
|
-
* createdAt: { format: 'date-time', required: true },
|
|
42
|
-
* dateOfBirth: { format: 'date' }
|
|
43
|
-
* }, 'Account');
|
|
44
|
-
*
|
|
45
|
-
* // Create factory with credential URI and schemas
|
|
46
|
-
* const factory = new CredentialFactory(
|
|
47
|
-
* 'https://example.com/schema/AccountV1',
|
|
48
|
-
* [accountSchema]
|
|
49
|
-
* );
|
|
50
|
-
*
|
|
51
|
-
* // Generate credential
|
|
52
|
-
* const credential = factory.createCredential(
|
|
53
|
-
* 'https://example.com/credentials/123',
|
|
54
|
-
* 'did:holder:123',
|
|
55
|
-
* {
|
|
56
|
-
* id: 'did:holder:123',
|
|
57
|
-
* username: 'alice',
|
|
58
|
-
* createdAt: new Date().toISOString()
|
|
59
|
-
* }
|
|
60
|
-
* );
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* **Example - Multi-Schema Credential with References:**
|
|
64
|
-
* ```typescript
|
|
65
|
-
* import { Schema, CredentialFactory } from '@kravc/schema';
|
|
66
|
-
*
|
|
67
|
-
* // Define multiple related schemas
|
|
68
|
-
* const playerSchema = new Schema({
|
|
69
|
-
* id: {},
|
|
70
|
-
* hasVideoGameScore: { $ref: 'VideoGameScore', required: true }
|
|
71
|
-
* }, 'Player');
|
|
72
|
-
*
|
|
73
|
-
* const videoGameSchema = new Schema({
|
|
74
|
-
* id: {},
|
|
75
|
-
* name: { type: 'string', required: true },
|
|
76
|
-
* version: { type: 'string', required: true }
|
|
77
|
-
* }, 'VideoGame', 'https://schema.org/');
|
|
78
|
-
*
|
|
79
|
-
* const scoreSchema = new Schema({
|
|
80
|
-
* game: { $ref: 'VideoGame', required: true },
|
|
81
|
-
* wins: { type: 'integer', required: true },
|
|
82
|
-
* bestScore: { type: 'integer', required: true }
|
|
83
|
-
* }, 'VideoGameScore');
|
|
84
|
-
*
|
|
85
|
-
* // Create factory with multiple schemas
|
|
86
|
-
* const factory = new CredentialFactory(
|
|
87
|
-
* 'https://example.com/schema/GameScoreV1',
|
|
88
|
-
* [playerSchema, videoGameSchema, scoreSchema]
|
|
89
|
-
* );
|
|
90
|
-
*
|
|
91
|
-
* // Generate credential with nested data
|
|
92
|
-
* const credential = factory.createCredential(
|
|
93
|
-
* 'https://example.com/credentials/score-456',
|
|
94
|
-
* 'did:player:789',
|
|
95
|
-
* {
|
|
96
|
-
* id: 'did:player:789',
|
|
97
|
-
* hasVideoGameScore: {
|
|
98
|
-
* game: { id: 'did:game:001', name: 'MineSweeper', version: '1.0' },
|
|
99
|
-
* wins: 10,
|
|
100
|
-
* bestScore: 5000
|
|
101
|
-
* }
|
|
102
|
-
* }
|
|
103
|
-
* );
|
|
104
|
-
* ```
|
|
105
|
-
*
|
|
106
|
-
* **Example - Accessing Credential Context:**
|
|
107
|
-
* ```typescript
|
|
108
|
-
* const factory = new CredentialFactory(uri, schemas);
|
|
109
|
-
*
|
|
110
|
-
* // Get credential type (extracted from URI)
|
|
111
|
-
* const type = factory.credentialType; // e.g., 'AccountV1'
|
|
112
|
-
*
|
|
113
|
-
* // Get JSON-LD context for embedding
|
|
114
|
-
* const context = factory.context;
|
|
115
|
-
* // {
|
|
116
|
-
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
117
|
-
* // Account: { '@id': '...', '@context': {...} }
|
|
118
|
-
* // }
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
9
|
+
/** Factory class for creating Verifiable Credentials with embedded JSON-LD linked data context. */
|
|
121
10
|
class CredentialFactory {
|
|
122
11
|
private _uri: string;
|
|
123
12
|
private _types: Schema[];
|
|
124
13
|
private _context: Record<string, LinkedDataType>;
|
|
125
14
|
private _validator: Validator;
|
|
126
15
|
|
|
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
|
-
*/
|
|
16
|
+
/** Creates a new CredentialFactory instance. */
|
|
167
17
|
constructor(uri: string, schemas: Schema[]) {
|
|
168
18
|
validateId('uri', uri);
|
|
169
19
|
|
|
@@ -185,73 +35,14 @@ class CredentialFactory {
|
|
|
185
35
|
}
|
|
186
36
|
}
|
|
187
37
|
|
|
188
|
-
/**
|
|
189
|
-
* Returns the credential type name extracted from the factory URI.
|
|
190
|
-
*
|
|
191
|
-
* **Intent:** Provide a convenient way to access the credential type identifier
|
|
192
|
-
* without manually parsing the URI.
|
|
193
|
-
*
|
|
194
|
-
* **Use Cases:**
|
|
195
|
-
* - Access credential type for logging or debugging
|
|
196
|
-
* - Use type name in credential processing logic
|
|
197
|
-
* - Generate type-specific identifiers or filenames
|
|
198
|
-
*
|
|
199
|
-
* **Behavior:**
|
|
200
|
-
* - Extracts the last segment from the URI path
|
|
201
|
-
* - Example: 'https://example.com/schema/AccountV1' → 'AccountV1'
|
|
202
|
-
*
|
|
203
|
-
* @returns The credential type name (last segment of the URI path)
|
|
204
|
-
*
|
|
205
|
-
* **Example:**
|
|
206
|
-
* ```typescript
|
|
207
|
-
* const factory = new CredentialFactory(
|
|
208
|
-
* 'https://example.com/schema/AccountV1',
|
|
209
|
-
* [schema]
|
|
210
|
-
* );
|
|
211
|
-
*
|
|
212
|
-
* console.log(factory.credentialType); // 'AccountV1'
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
38
|
+
/** Returns the credential type name extracted from the factory URI. */
|
|
215
39
|
get credentialType() {
|
|
216
40
|
const [ credentialType ] = this._uri.split('/').reverse();
|
|
217
41
|
|
|
218
42
|
return credentialType;
|
|
219
43
|
}
|
|
220
44
|
|
|
221
|
-
/**
|
|
222
|
-
* Returns the JSON-LD context object for the credential.
|
|
223
|
-
*
|
|
224
|
-
* **Intent:** Provide the complete JSON-LD context mapping that should be included
|
|
225
|
-
* in the credential's `@context` array for semantic web compatibility.
|
|
226
|
-
*
|
|
227
|
-
* **Use Cases:**
|
|
228
|
-
* - Access context for credential serialization
|
|
229
|
-
* - Inspect context mappings for debugging
|
|
230
|
-
* - Use context in custom credential processing
|
|
231
|
-
* - Embed context in other JSON-LD documents
|
|
232
|
-
*
|
|
233
|
-
* **Structure:**
|
|
234
|
-
* - Contains the credential type mapping (`{ credentialType: { '@id': uri } }`)
|
|
235
|
-
* - Includes all schema-linked data types from the factory's schemas
|
|
236
|
-
* - Each schema's `linkedDataType` is included if the schema was created with a URL
|
|
237
|
-
*
|
|
238
|
-
* @returns Object containing credential type and schema type mappings for JSON-LD context
|
|
239
|
-
*
|
|
240
|
-
* **Example:**
|
|
241
|
-
* ```typescript
|
|
242
|
-
* const factory = new CredentialFactory(uri, schemas);
|
|
243
|
-
* const context = factory.context;
|
|
244
|
-
*
|
|
245
|
-
* // Result structure:
|
|
246
|
-
* // {
|
|
247
|
-
* // AccountV1: { '@id': 'https://example.com/schema/AccountV1' },
|
|
248
|
-
* // Account: {
|
|
249
|
-
* // '@id': 'https://example.com/schema/AccountV1#Account',
|
|
250
|
-
* // '@context': { ... }
|
|
251
|
-
* // }
|
|
252
|
-
* // }
|
|
253
|
-
* ```
|
|
254
|
-
*/
|
|
45
|
+
/** Returns the JSON-LD context object for the credential. */
|
|
255
46
|
get context() {
|
|
256
47
|
return {
|
|
257
48
|
[this.credentialType]: { '@id': this._uri },
|
|
@@ -259,111 +50,7 @@ class CredentialFactory {
|
|
|
259
50
|
};
|
|
260
51
|
}
|
|
261
52
|
|
|
262
|
-
/**
|
|
263
|
-
* Creates a Verifiable Credential for the specified subject.
|
|
264
|
-
*
|
|
265
|
-
* **Intent:** Generate a W3C-compliant Verifiable Credential with validated subject data,
|
|
266
|
-
* proper JSON-LD context, and standardized structure ready for issuance and verification.
|
|
267
|
-
*
|
|
268
|
-
* **Use Cases:**
|
|
269
|
-
* - Issue credentials for user accounts, achievements, certifications
|
|
270
|
-
* - Generate credentials with automatic schema validation
|
|
271
|
-
* - Create credentials with embedded semantic context for interoperability
|
|
272
|
-
* - Build credentials for decentralized identity systems
|
|
273
|
-
* - Generate credentials that can be verified using JSON-LD processors
|
|
274
|
-
*
|
|
275
|
-
* **Behavior:**
|
|
276
|
-
* - Validates that `id` and `holder` are valid URLs/URIs
|
|
277
|
-
* - Validates the subject against the root schema (first schema in the factory)
|
|
278
|
-
* - Applies schema defaults and normalizes data according to schema rules
|
|
279
|
-
* - Generates credential with proper `@context`, `type`, `id`, `holder`, and `credentialSubject`
|
|
280
|
-
* - Returns credential ready for signing (issuer, issuanceDate, proof to be added separately)
|
|
281
|
-
*
|
|
282
|
-
* **Credential Structure:**
|
|
283
|
-
* ```typescript
|
|
284
|
-
* {
|
|
285
|
-
* '@context': [
|
|
286
|
-
* 'https://www.w3.org/2018/credentials/v1',
|
|
287
|
-
* factory.context // JSON-LD context from factory
|
|
288
|
-
* ],
|
|
289
|
-
* id: string, // Credential identifier
|
|
290
|
-
* type: ['VerifiableCredential', credentialType],
|
|
291
|
-
* holder: string, // DID or identifier of credential holder
|
|
292
|
-
* credentialSubject: { // Validated and normalized subject data
|
|
293
|
-
* id: string,
|
|
294
|
-
* type: string, // Root schema ID
|
|
295
|
-
* // ... additional subject properties
|
|
296
|
-
* }
|
|
297
|
-
* }
|
|
298
|
-
* ```
|
|
299
|
-
*
|
|
300
|
-
* @param id - Unique identifier for the credential (must be a valid URL/URI)
|
|
301
|
-
* @param holder - DID or identifier of the credential holder (must be a valid URL/URI)
|
|
302
|
-
* @param subject - Object containing the credential subject data to be validated against
|
|
303
|
-
* the root schema. Properties are validated, defaults are applied,
|
|
304
|
-
* and nested objects are validated against referenced schemas.
|
|
305
|
-
*
|
|
306
|
-
* @returns Verifiable Credential object with validated subject and JSON-LD context
|
|
307
|
-
*
|
|
308
|
-
* @throws Error if `id` is not a valid URL/URI
|
|
309
|
-
* @throws Error if `holder` is not a valid URL/URI
|
|
310
|
-
* @throws ValidationError if subject data doesn't match the schema requirements
|
|
311
|
-
*
|
|
312
|
-
* **Example - Basic Credential:**
|
|
313
|
-
* ```typescript
|
|
314
|
-
* const factory = new CredentialFactory(uri, [accountSchema]);
|
|
315
|
-
*
|
|
316
|
-
* const credential = factory.createCredential(
|
|
317
|
-
* 'https://example.com/credentials/123',
|
|
318
|
-
* 'did:holder:456',
|
|
319
|
-
* {
|
|
320
|
-
* id: 'did:holder:456',
|
|
321
|
-
* username: 'alice',
|
|
322
|
-
* createdAt: '2024-01-01T00:00:00Z'
|
|
323
|
-
* }
|
|
324
|
-
* );
|
|
325
|
-
*
|
|
326
|
-
* // Result:
|
|
327
|
-
* // {
|
|
328
|
-
* // '@context': ['https://www.w3.org/2018/credentials/v1', {...}],
|
|
329
|
-
* // id: 'https://example.com/credentials/123',
|
|
330
|
-
* // type: ['VerifiableCredential', 'AccountV1'],
|
|
331
|
-
* // holder: 'did:holder:456',
|
|
332
|
-
* // credentialSubject: {
|
|
333
|
-
* // id: 'did:holder:456',
|
|
334
|
-
* // username: 'alice',
|
|
335
|
-
* // createdAt: '2024-01-01T00:00:00Z',
|
|
336
|
-
* // type: 'Account'
|
|
337
|
-
* // }
|
|
338
|
-
* // }
|
|
339
|
-
* ```
|
|
340
|
-
*
|
|
341
|
-
* **Example - Credential with Nested Objects:**
|
|
342
|
-
* ```typescript
|
|
343
|
-
* const factory = new CredentialFactory(uri, [playerSchema, gameSchema, scoreSchema]);
|
|
344
|
-
*
|
|
345
|
-
* const credential = factory.createCredential(
|
|
346
|
-
* 'https://example.com/credentials/score-789',
|
|
347
|
-
* 'did:player:123',
|
|
348
|
-
* {
|
|
349
|
-
* id: 'did:player:123',
|
|
350
|
-
* hasVideoGameScore: {
|
|
351
|
-
* game: {
|
|
352
|
-
* id: 'did:game:001',
|
|
353
|
-
* name: 'MineSweeper',
|
|
354
|
-
* version: '1.0'
|
|
355
|
-
* },
|
|
356
|
-
* wins: 10,
|
|
357
|
-
* bestScore: 5000
|
|
358
|
-
* }
|
|
359
|
-
* }
|
|
360
|
-
* );
|
|
361
|
-
* ```
|
|
362
|
-
*
|
|
363
|
-
* **Note:** The returned credential does not include `issuer`, `issuanceDate`, or `proof`
|
|
364
|
-
* fields. These should be added by the credential issuance system (e.g., using
|
|
365
|
-
* [@kravc/identity](http://github.com/alexkravets/identity) or similar libraries).
|
|
366
|
-
*/
|
|
53
|
+
/** Creates a Verifiable Credential for the specified subject. */
|
|
367
54
|
createCredential(id: string, holder: string, subject: TargetObject = {}) {
|
|
368
55
|
validateId('id', id);
|
|
369
56
|
validateId('holder', holder);
|