Skip to content

Commit

Permalink
feat: Added id and tag fields
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Apr 15, 2020
1 parent f491371 commit 0b62eaa
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/daf-core/src/entities/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class Credential extends BaseEntity {
)
subject?: Identity

@Column({ nullable: true })
id?: String

@Column()
issuanceDate: Date

Expand Down
13 changes: 11 additions & 2 deletions packages/daf-core/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions packages/daf-core/src/entities/presentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Presentation extends BaseEntity {
identity => identity.issuedPresentations,
{
cascade: ['insert'],
eager: true
eager: true,
},
)
issuer: Identity
Expand All @@ -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

Expand Down
24 changes: 22 additions & 2 deletions packages/daf-w3c/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface CredentialInput {
'@context'?: string[]
context?: string[]
type: string[]
id?: string
issuer: string
expirationDate?: string
credentialSubject: CredentialSubjectInput
Expand All @@ -102,6 +103,7 @@ export interface PresentationInput {
'@context'?: string[]
context?: string[]
type: string[]
id?: string
issuer: string
audience: string
tag?: string
Expand All @@ -115,28 +117,46 @@ 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,
credentialSubject,
},
}

if (input.expirationDate) {
result['exp'] = new Date(input.expirationDate).getUTCSeconds()
}

if (input.id) {
result['jti'] = input.id
}

return result
}

const transformPresentationInput = (input: PresentationInput): PresentationPayload => {
// TODO validate input
const result: PresentationPayload = {
aud: input.audience,
// exp: input.expirationDate,
vp: {
'@context': input['@context'] || input['context'],
type: input.type,
verifiableCredential: input.verifiableCredential,
},
}

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
}
3 changes: 3 additions & 0 deletions packages/daf-w3c/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ export const typeDefs = `
scalar CredentialSubject
input SignCredentialInput {
id: String
issuer: String!
context: [String]!
type: [String]!
credentialSubject: CredentialSubject!
}
input SignPresentationInput {
id: String
tag: String
issuer: String!
audience: String!
context: [String]!
Expand Down
8 changes: 8 additions & 0 deletions packages/daf-w3c/src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 0b62eaa

Please sign in to comment.