From 24ad82a9abd1e1a5c3cef91efea7a17637b958c3 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Mon, 27 Apr 2020 15:35:17 +0300 Subject: [PATCH] fix: Keeping app specific payload fields --- packages/daf-w3c/src/action-handler.ts | 109 ++++++++++++++++--------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/packages/daf-w3c/src/action-handler.ts b/packages/daf-w3c/src/action-handler.ts index c25224cef..642d38264 100644 --- a/packages/daf-w3c/src/action-handler.ts +++ b/packages/daf-w3c/src/action-handler.ts @@ -97,6 +97,11 @@ export interface CredentialInput { issuer: string expirationDate?: string credentialSubject: CredentialSubjectInput + credentialStatus?: { + id: string + type: string + } + [x: string]: any } export interface PresentationInput { @@ -109,54 +114,80 @@ export interface PresentationInput { tag?: string expirationDate?: string verifiableCredential: string[] + [x: string]: any } const transformCredentialInput = (input: CredentialInput): VerifiableCredentialPayload => { - // TODO validate input - const credentialSubject = { ...input.credentialSubject } - delete credentialSubject.id - const result: VerifiableCredentialPayload = { - sub: input.credentialSubject.id, - vc: { - '@context': input['@context'] || input['context'], - type: input.type, - credentialSubject, - }, - } - - if (input.expirationDate) { - result['exp'] = Date.parse(input.expirationDate) / 1000 - } - - if (input.id) { - result['jti'] = input.id + + const result = { vc: {} } + + for (const key in input) { + switch (key) { + case 'credentialSubject': + result['sub'] = input[key].id + const credentialSubject = { ...input.credentialSubject } + delete credentialSubject.id + result['vc']['credentialSubject'] = credentialSubject + break + case '@context': + case 'context': + result['vc']['@context'] = input[key] + break + case 'type': + result['vc']['type'] = input[key] + break + case 'expirationDate': + result['exp'] = Date.parse(input[key]) / 1000 + break + case 'id': + result['jti'] = input[key] + break + case 'credentialStatus': + result['status'] = input[key] + break + case 'issuer': + // remove issuer + break + default: + result[key] = input[key] + } } - return result + return result as VerifiableCredentialPayload } const transformPresentationInput = (input: PresentationInput): PresentationPayload => { - // TODO validate input - const result: PresentationPayload = { - aud: input.audience, - vp: { - '@context': input['@context'] || input['context'], - type: input.type, - verifiableCredential: input.verifiableCredential, - }, - } - - if (input.expirationDate) { - result['exp'] = Date.parse(input.expirationDate) / 1000 - } - if (input.id) { - result['jti'] = input.id - } - - if (input.tag) { - result['tag'] = input.tag + const result = { vp: {} } + + for (const key in input) { + switch (key) { + case '@context': + case 'context': + result['vp']['@context'] = input[key] + break + case 'type': + result['vp']['type'] = input[key] + break + case 'expirationDate': + result['exp'] = Date.parse(input[key]) / 1000 + break + case 'id': + result['jti'] = input[key] + break + case 'audience': + result['aud'] = input[key] + break + case 'verifiableCredential': + result['vp']['verifiableCredential'] = input[key] + break + case 'issuer': + // remove issuer + break + default: + result[key] = input[key] + } } - return result + return result as PresentationPayload }