From 1a5c4c9827fdc98338395311d7b176ebd09332f3 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Fri, 6 Mar 2020 11:33:40 +0200 Subject: [PATCH] fix: Removing context entities --- examples/send-vc/test.ts | 82 +++++++++++-------- packages/daf-core/src/entities/claim.ts | 1 + .../src/entities/credential-context.ts | 10 --- .../daf-core/src/entities/credential-type.ts | 10 --- packages/daf-core/src/entities/credential.ts | 45 ++++++++-- .../src/entities/presentation-context.ts | 10 --- .../src/entities/presentation-type.ts | 10 --- .../daf-core/src/entities/presentation.ts | 10 +-- packages/daf-core/src/index.ts | 4 - 9 files changed, 87 insertions(+), 95 deletions(-) delete mode 100644 packages/daf-core/src/entities/credential-context.ts delete mode 100644 packages/daf-core/src/entities/credential-type.ts delete mode 100644 packages/daf-core/src/entities/presentation-context.ts delete mode 100644 packages/daf-core/src/entities/presentation-type.ts diff --git a/examples/send-vc/test.ts b/examples/send-vc/test.ts index 82b69e2a7..409e7d8ab 100644 --- a/examples/send-vc/test.ts +++ b/examples/send-vc/test.ts @@ -1,19 +1,6 @@ -import { - Identity, - Key, - OMessage, - MessageMetaData, - Credential, - Presentation, - CredentialContext, - CredentialType, - PresentationContext, - PresentationType, - Claim, - Action, -} from 'daf-core' +import { Identity, Key, OMessage, MessageMetaData, Credential, Presentation, Claim, Action } from 'daf-core' import { core } from './setup' -import { createConnection } from 'typeorm' +import { createConnection, Like } from 'typeorm' const main = async () => { await createConnection({ @@ -29,22 +16,9 @@ const main = async () => { password: '', database: 'simonas', - synchronize: false, + synchronize: true, logging: true, - entities: [ - Key, - Identity, - OMessage, - MessageMetaData, - Credential, - PresentationType, - PresentationContext, - Presentation, - CredentialType, - CredentialContext, - Claim, - Action, - ], + entities: [Key, Identity, OMessage, MessageMetaData, Credential, Presentation, Claim, Action], }) let identity1: Identity @@ -59,12 +33,50 @@ const main = async () => { identity2 = ((await core.identityManager.createIdentity(identityProviders[0].type)) as any) as Identity } - console.log(identity1) - console.log(identity2) + // console.log(identity1) + // console.log(identity2) - const vc = new Credential() - vc.issuer = identity1 - vc.subject = identity2 + // const vc = new Credential() + // vc.issuer = identity1 + // vc.subject = identity2 + // vc.issuedAt = 1235456 + // vc.notBefore = 1232424 + // vc.expiresAt = 32423423 + // vc.raw = '4123123123' + // vc.context = ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'] + // vc.type = ['VerifiableCredential', 'PublicProfile'] + // vc.setCredentialSubject({ + // name: 'Simonas', + // profilePicture: 'https://simons.com/a.png', + // address: { + // street: 'some', + // house: 1 + // } + // }) + + // try { + // await vc.save() + // } catch(e) { + // console.log(e.message) + // } + + // console.log(vc) + + // const res = await Claim.find({ + // relations: ['issuer', 'subject', 'credential'], + // where: { + // issuer: identity1, + // type: 'name' + // } + // }) + + const res = await Credential.find({ + where: { + type: Like('%Public%'), + }, + }) + + console.dir(res, { depth: 10 }) } main().catch(console.log) diff --git a/packages/daf-core/src/entities/claim.ts b/packages/daf-core/src/entities/claim.ts index 4efa9302c..b91f0ecbb 100644 --- a/packages/daf-core/src/entities/claim.ts +++ b/packages/daf-core/src/entities/claim.ts @@ -7,6 +7,7 @@ import { PrimaryGeneratedColumn, OneToMany, ManyToMany, + BeforeInsert, } from 'typeorm' import { Identity } from './identity' import { Credential } from './credential' diff --git a/packages/daf-core/src/entities/credential-context.ts b/packages/daf-core/src/entities/credential-context.ts deleted file mode 100644 index 32312dc8e..000000000 --- a/packages/daf-core/src/entities/credential-context.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm' - -@Entity() -export class CredentialContext extends BaseEntity { - @PrimaryGeneratedColumn() - id: string - - @Column() - context: string -} diff --git a/packages/daf-core/src/entities/credential-type.ts b/packages/daf-core/src/entities/credential-type.ts deleted file mode 100644 index 0cf961fda..000000000 --- a/packages/daf-core/src/entities/credential-type.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm' - -@Entity() -export class CredentialType extends BaseEntity { - @PrimaryGeneratedColumn() - id: string - - @Column() - type: string -} diff --git a/packages/daf-core/src/entities/credential.ts b/packages/daf-core/src/entities/credential.ts index 125bee4cc..e9e47929c 100644 --- a/packages/daf-core/src/entities/credential.ts +++ b/packages/daf-core/src/entities/credential.ts @@ -1,3 +1,4 @@ +import { blake2bHex } from 'blakejs' import { Entity, Column, @@ -7,12 +8,12 @@ import { PrimaryColumn, OneToMany, ManyToMany, + BeforeInsert, + AfterInsert, } from 'typeorm' import { Identity } from './identity' import { Message } from './message' import { Presentation } from './presentation' -import { CredentialContext } from './credential-context' -import { CredentialType } from './credential-type' import { Claim } from './claim' @Entity() @@ -20,6 +21,30 @@ export class Credential extends BaseEntity { @PrimaryColumn() hash: string + @BeforeInsert() + async updateHash() { + this.hash = blake2bHex(this.raw) + } + + setCredentialSubject(credentialSubject: object) { + this.credentialSubject = credentialSubject + this.claims = [] + for (const type in this.credentialSubject) { + if (this.credentialSubject.hasOwnProperty(type)) { + const value = this.credentialSubject[type] + const isObj = typeof value === 'function' || (typeof value === 'object' && !!value) + const claim = new Claim() + claim.type = type + claim.value = value + claim.isObj = isObj + claim.issuer = this.issuer + claim.subject = this.subject + claim.credential = this + this.claims.push(claim) + } + } + } + @ManyToOne( type => Identity, identity => identity.issuedCredentials, @@ -44,17 +69,21 @@ export class Credential extends BaseEntity { @Column() raw: string - @ManyToMany(type => CredentialContext) - @JoinTable() - context: CredentialContext[] + @Column('simple-array') + context: string[] + + @Column('simple-array') + type: string[] - @ManyToMany(type => CredentialType) - @JoinTable() - type: CredentialType[] + @Column('simple-json') + credentialSubject: object @OneToMany( type => Claim, claim => claim.credential, + { + cascade: true, + }, ) claims: Claim[] diff --git a/packages/daf-core/src/entities/presentation-context.ts b/packages/daf-core/src/entities/presentation-context.ts deleted file mode 100644 index ecfa9da06..000000000 --- a/packages/daf-core/src/entities/presentation-context.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm' - -@Entity() -export class PresentationContext extends BaseEntity { - @PrimaryGeneratedColumn() - id: string - - @Column() - context: string -} diff --git a/packages/daf-core/src/entities/presentation-type.ts b/packages/daf-core/src/entities/presentation-type.ts deleted file mode 100644 index 482ffb88e..000000000 --- a/packages/daf-core/src/entities/presentation-type.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm' - -@Entity() -export class PresentationType extends BaseEntity { - @PrimaryGeneratedColumn() - id: string - - @Column() - type: string -} diff --git a/packages/daf-core/src/entities/presentation.ts b/packages/daf-core/src/entities/presentation.ts index ca1632c9f..ff2e9a783 100644 --- a/packages/daf-core/src/entities/presentation.ts +++ b/packages/daf-core/src/entities/presentation.ts @@ -10,8 +10,6 @@ import { } from 'typeorm' import { Identity } from './identity' import { Message } from './message' -import { PresentationContext } from './presentation-context' -import { PresentationType } from './presentation-type' import { Credential } from './credential' @Entity() @@ -43,13 +41,9 @@ export class Presentation extends BaseEntity { @Column() raw: string - @ManyToMany(type => PresentationContext) - @JoinTable() - context: PresentationContext[] + context: string[] - @ManyToMany(type => PresentationType) - @JoinTable() - type: PresentationType[] + type: string[] @ManyToMany( type => Credential, diff --git a/packages/daf-core/src/index.ts b/packages/daf-core/src/index.ts index f10a54d41..8a0cb00bd 100644 --- a/packages/daf-core/src/index.ts +++ b/packages/daf-core/src/index.ts @@ -24,8 +24,4 @@ export { MessageMetaData } from './entities/message-meta-data' export { Action } from './entities/action' export { Claim } from './entities/claim' export { Credential } from './entities/credential' -export { CredentialContext } from './entities/credential-context' -export { CredentialType } from './entities/credential-type' export { Presentation } from './entities/presentation' -export { PresentationContext } from './entities/presentation-context' -export { PresentationType } from './entities/presentation-type'