From 0b62eaa30ceb2f79192a476ff9f6809cbe5b1ccf Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Wed, 15 Apr 2020 15:44:11 +0300 Subject: [PATCH] feat: Added id and tag fields --- packages/daf-core/src/entities/credential.ts | 3 +++ packages/daf-core/src/entities/message.ts | 13 ++++++++-- .../daf-core/src/entities/presentation.ts | 7 ++++-- packages/daf-w3c/src/action-handler.ts | 24 +++++++++++++++++-- packages/daf-w3c/src/graphql.ts | 3 +++ packages/daf-w3c/src/message-handler.ts | 8 +++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/packages/daf-core/src/entities/credential.ts b/packages/daf-core/src/entities/credential.ts index c77d3b5bc..a52ed2119 100644 --- a/packages/daf-core/src/entities/credential.ts +++ b/packages/daf-core/src/entities/credential.ts @@ -74,6 +74,9 @@ export class Credential extends BaseEntity { ) subject?: Identity + @Column({ nullable: true }) + id?: String + @Column() issuanceDate: Date diff --git a/packages/daf-core/src/entities/message.ts b/packages/daf-core/src/entities/message.ts index 1dff7df65..2b3879594 100644 --- a/packages/daf-core/src/entities/message.ts +++ b/packages/daf-core/src/entities/message.ts @@ -4,11 +4,13 @@ import { BaseEntity, ManyToOne, ManyToMany, - PrimaryGeneratedColumn, + PrimaryColumn, JoinTable, CreateDateColumn, UpdateDateColumn, + BeforeInsert, } from 'typeorm' +import { blake2bHex } from 'blakejs' import { Identity } from './identity' import { Presentation } from './presentation' import { Credential } from './credential' @@ -30,7 +32,14 @@ export class Message extends BaseEntity { } } - @PrimaryGeneratedColumn('uuid') + @BeforeInsert() + setId() { + if (!this.id) { + this.id = blake2bHex(this.raw) + } + } + + @PrimaryColumn() id: string @CreateDateColumn() diff --git a/packages/daf-core/src/entities/presentation.ts b/packages/daf-core/src/entities/presentation.ts index ad1984625..3f375819e 100644 --- a/packages/daf-core/src/entities/presentation.ts +++ b/packages/daf-core/src/entities/presentation.ts @@ -35,7 +35,7 @@ export class Presentation extends BaseEntity { identity => identity.issuedPresentations, { cascade: ['insert'], - eager: true + eager: true, }, ) issuer: Identity @@ -45,11 +45,14 @@ export class Presentation extends BaseEntity { identity => identity.receivedPresentations, { cascade: ['insert'], - eager: true + eager: true, }, ) audience: Identity + @Column({ nullable: true }) + id?: String + @Column() issuanceDate: Date diff --git a/packages/daf-w3c/src/action-handler.ts b/packages/daf-w3c/src/action-handler.ts index d7906db2c..bb657b182 100644 --- a/packages/daf-w3c/src/action-handler.ts +++ b/packages/daf-w3c/src/action-handler.ts @@ -93,6 +93,7 @@ export interface CredentialInput { '@context'?: string[] context?: string[] type: string[] + id?: string issuer: string expirationDate?: string credentialSubject: CredentialSubjectInput @@ -102,6 +103,7 @@ export interface PresentationInput { '@context'?: string[] context?: string[] type: string[] + id?: string issuer: string audience: string tag?: string @@ -115,7 +117,6 @@ const transformCredentialInput = (input: CredentialInput): VerifiableCredentialP delete credentialSubject.id const result: VerifiableCredentialPayload = { sub: input.credentialSubject.id, - // exp: input.expirationDate, vc: { '@context': input['@context'] || input['context'], type: input.type, @@ -123,6 +124,14 @@ const transformCredentialInput = (input: CredentialInput): VerifiableCredentialP }, } + if (input.expirationDate) { + result['exp'] = new Date(input.expirationDate).getUTCSeconds() + } + + if (input.id) { + result['jti'] = input.id + } + return result } @@ -130,7 +139,6 @@ const transformPresentationInput = (input: PresentationInput): PresentationPaylo // TODO validate input const result: PresentationPayload = { aud: input.audience, - // exp: input.expirationDate, vp: { '@context': input['@context'] || input['context'], type: input.type, @@ -138,5 +146,17 @@ const transformPresentationInput = (input: PresentationInput): PresentationPaylo }, } + if (input.expirationDate) { + result['exp'] = new Date(input.expirationDate).getUTCSeconds() + } + + if (input.id) { + result['jti'] = input.id + } + + if (input.tag) { + result['tag'] = input.tag + } + return result } diff --git a/packages/daf-w3c/src/graphql.ts b/packages/daf-w3c/src/graphql.ts index 691027c67..2731a3feb 100644 --- a/packages/daf-w3c/src/graphql.ts +++ b/packages/daf-w3c/src/graphql.ts @@ -50,6 +50,7 @@ export const typeDefs = ` scalar CredentialSubject input SignCredentialInput { + id: String issuer: String! context: [String]! type: [String]! @@ -57,6 +58,8 @@ export const typeDefs = ` } input SignPresentationInput { + id: String + tag: String issuer: String! audience: String! context: [String]! diff --git a/packages/daf-w3c/src/message-handler.ts b/packages/daf-w3c/src/message-handler.ts index 738c06d5f..bb4204dc9 100644 --- a/packages/daf-w3c/src/message-handler.ts +++ b/packages/daf-w3c/src/message-handler.ts @@ -94,6 +94,10 @@ export function createCredential(payload: VerifiableCredentialPayload, jwt: stri vc.raw = jwt + if (payload.jti) { + vc.id = payload.jti + } + if (payload.nbf || payload.iat) { vc.issuanceDate = timestampToDate(payload.nbf || payload.iat) } @@ -125,6 +129,10 @@ export function createPresentation( vp.raw = jwt + if (payload.jti) { + vp.id = payload.jti + } + if (payload.nbf || payload.iat) { vp.issuanceDate = timestampToDate(payload.nbf || payload.iat) }