|
1 |
| -import { DID, getChainId } from '@iden3/js-iden3-core'; |
| 1 | +import { DID } from '@iden3/js-iden3-core'; |
2 | 2 | import { IDataStorage } from '../storage/interfaces';
|
3 | 3 | import {
|
4 | 4 | W3CCredential,
|
@@ -266,101 +266,23 @@ export class CredentialWallet implements ICredentialWallet {
|
266 | 266 | if (!schema.$metadata.uris['jsonLdContext']) {
|
267 | 267 | throw new Error('jsonLdContext is missing is the schema');
|
268 | 268 | }
|
269 |
| - request.context = request.context ?? []; |
| 269 | + // do copy of request to avoid mutation |
| 270 | + const r = { ...request }; |
| 271 | + r.context = r.context ?? []; |
270 | 272 | if (
|
271 |
| - request.displayMethod?.type === DisplayMethodType.Iden3BasicDisplayMethodV1 && |
272 |
| - !request.context.includes(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD) |
| 273 | + r.displayMethod?.type === DisplayMethodType.Iden3BasicDisplayMethodV1 && |
| 274 | + !r.context.includes(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD) |
273 | 275 | ) {
|
274 |
| - request.context.push(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD); |
| 276 | + r.context.push(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD); |
275 | 277 | }
|
276 |
| - const context = [ |
277 |
| - VerifiableConstants.JSONLD_SCHEMA.W3C_CREDENTIAL_2018, |
278 |
| - ...request.context, |
279 |
| - VerifiableConstants.JSONLD_SCHEMA.IDEN3_CREDENTIAL, |
280 |
| - schema.$metadata.uris['jsonLdContext'] |
281 |
| - ]; |
| 278 | + r.context.push(schema.$metadata.uris['jsonLdContext']); |
| 279 | + r.expiration = r.expiration ? r.expiration * 1000 : undefined; |
| 280 | + r.id = r.id ? r.id : `urn:${uuid.v4()}`; |
| 281 | + r.issuanceDate = r.issuanceDate ? r.issuanceDate * 1000 : Date.now(); |
282 | 282 |
|
283 |
| - const credentialType = [ |
284 |
| - VerifiableConstants.CREDENTIAL_TYPE.W3C_VERIFIABLE_CREDENTIAL, |
285 |
| - request.type |
286 |
| - ]; |
287 |
| - |
288 |
| - const expirationDate = |
289 |
| - !request.expiration || request.expiration == 0 ? null : request.expiration; |
290 |
| - |
291 |
| - const credentialSubject = request.credentialSubject; |
292 |
| - credentialSubject['type'] = request.type; |
293 |
| - |
294 |
| - const cr = new W3CCredential(); |
295 |
| - cr.id = `urn:${uuid.v4()}`; |
296 |
| - cr['@context'] = context; |
297 |
| - cr.type = credentialType; |
298 |
| - cr.expirationDate = expirationDate ? new Date(expirationDate * 1000).toISOString() : undefined; |
299 |
| - cr.refreshService = request.refreshService; |
300 |
| - cr.displayMethod = request.displayMethod; |
301 |
| - cr.issuanceDate = new Date().toISOString(); |
302 |
| - cr.credentialSubject = credentialSubject; |
303 |
| - cr.issuer = issuer.string(); |
304 |
| - cr.credentialSchema = { |
305 |
| - id: request.credentialSchema, |
306 |
| - type: VerifiableConstants.JSON_SCHEMA_VALIDATOR |
307 |
| - }; |
308 |
| - |
309 |
| - cr.credentialStatus = this.buildCredentialStatus(request, issuer); |
310 |
| - |
311 |
| - return cr; |
| 283 | + return W3CCredential.fromCredentialRequest(issuer, r); |
312 | 284 | };
|
313 | 285 |
|
314 |
| - /** |
315 |
| - * Builds credential status |
316 |
| - * @param {CredentialRequest} request |
317 |
| - * @returns `CredentialStatus` |
318 |
| - */ |
319 |
| - private buildCredentialStatus(request: CredentialRequest, issuer: DID): CredentialStatus { |
320 |
| - const credentialStatus: CredentialStatus = { |
321 |
| - id: request.revocationOpts.id, |
322 |
| - type: request.revocationOpts.type, |
323 |
| - revocationNonce: request.revocationOpts.nonce |
324 |
| - }; |
325 |
| - |
326 |
| - switch (request.revocationOpts.type) { |
327 |
| - case CredentialStatusType.SparseMerkleTreeProof: |
328 |
| - return { |
329 |
| - ...credentialStatus, |
330 |
| - id: `${credentialStatus.id.replace(/\/$/, '')}/${credentialStatus.revocationNonce}` |
331 |
| - }; |
332 |
| - case CredentialStatusType.Iden3ReverseSparseMerkleTreeProof: |
333 |
| - return { |
334 |
| - ...credentialStatus, |
335 |
| - id: request.revocationOpts.issuerState |
336 |
| - ? `${credentialStatus.id.replace(/\/$/, '')}/node?state=${ |
337 |
| - request.revocationOpts.issuerState |
338 |
| - }` |
339 |
| - : `${credentialStatus.id.replace(/\/$/, '')}` |
340 |
| - }; |
341 |
| - case CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023: { |
342 |
| - const issuerId = DID.idFromDID(issuer); |
343 |
| - const chainId = getChainId(DID.blockchainFromId(issuerId), DID.networkIdFromId(issuerId)); |
344 |
| - const searchParams = [ |
345 |
| - ['revocationNonce', request.revocationOpts.nonce?.toString() || ''], |
346 |
| - ['contractAddress', `${chainId}:${request.revocationOpts.id}`], |
347 |
| - ['state', request.revocationOpts.issuerState || ''] |
348 |
| - ] |
349 |
| - .filter(([, value]) => Boolean(value)) |
350 |
| - .map(([key, value]) => `${key}=${value}`) |
351 |
| - .join('&'); |
352 |
| - |
353 |
| - return { |
354 |
| - ...credentialStatus, |
355 |
| - // `[did]:[methodid]:[chain]:[network]:[id]/credentialStatus?(revocationNonce=value)&[contractAddress=[chainID]:[contractAddress]]&(state=issuerState)` |
356 |
| - id: `${issuer.string()}/credentialStatus?${searchParams}` |
357 |
| - }; |
358 |
| - } |
359 |
| - default: |
360 |
| - return credentialStatus; |
361 |
| - } |
362 |
| - } |
363 |
| - |
364 | 286 | /**
|
365 | 287 | * {@inheritDoc ICredentialWallet.findById}
|
366 | 288 | */
|
|
0 commit comments