@veramo/data-store
Version:
Veramo data storage plugin based on TypeORM database drivers
132 lines (110 loc) • 3.35 kB
text/typescript
import { VerifiableCredential, VerifiablePresentation } from '@veramo/core-types'
import {
BaseEntity,
Column,
Entity,
JoinTable,
ManyToMany,
ManyToOne,
PrimaryColumn,
Relation,
} from 'typeorm'
import { Identifier } from './identifier.js'
import { Message } from './message.js'
import { createCredentialEntity, Credential } from './credential.js'
import { asArray, computeEntryHash } from '@veramo/utils'
import { normalizeCredential } from 'did-jwt-vc'
/**
* Represents some common properties of a Verifiable Presentation that are stored in a TypeORM database for querying.
*
* @see {@link @veramo/core-types#IDataStoreORM.dataStoreORMGetVerifiablePresentations | dataStoreORMGetVerifiablePresentations} for the interface defining how this can be queried.
*
* @see {@link @veramo/data-store#DataStoreORM | DataStoreORM} for the implementation of the query interface.
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
('presentation')
export class Presentation extends BaseEntity {
()
// @ts-ignore
hash: string
// @ts-ignore
private _raw: IVerifiablePresentation
set raw(raw: VerifiablePresentation) {
this._raw = raw
this.hash = computeEntryHash(raw)
}
({ type: 'simple-json' })
get raw(): VerifiablePresentation {
return this._raw
}
((type) => Identifier, (identifier) => identifier.issuedPresentations, {
cascade: ['insert'],
eager: true,
onDelete: 'CASCADE',
})
// @ts-ignore
holder: Relation<Identifier>
((type) => Identifier, (identifier) => identifier?.receivedPresentations, {
cascade: ['insert'],
eager: true,
nullable: true,
})
()
// @ts-ignore
verifier?: Relation<Identifier[]>
({ nullable: true })
id?: String
()
// @ts-ignore
issuanceDate: Date
({ nullable: true })
expirationDate?: Date
('simple-array')
// @ts-ignore
context: string[]
('simple-array')
// @ts-ignore
type: string[]
((type) => Credential, (credential) => credential.presentations, {
cascade: true,
})
()
// @ts-ignore
credentials: Relation<Credential[]>
((type) => Message, (message) => message.presentations)
// @ts-ignore
messages: Relation<Message[]>
}
export const createPresentationEntity = (vpi: VerifiablePresentation): Presentation => {
const vp = vpi
const presentation = new Presentation()
presentation.context = asArray(vp['@context'])
presentation.type = asArray(vp.type || [])
presentation.id = vp.id
if (vp.issuanceDate) {
presentation.issuanceDate = new Date(vp.issuanceDate)
}
if (vp.expirationDate) {
presentation.expirationDate = new Date(vp.expirationDate)
}
const holder = new Identifier()
holder.did = vp.holder
presentation.holder = holder
presentation.verifier = asArray(vp.verifier || []).map((verifierDid) => {
const id = new Identifier()
id.did = verifierDid
return id
})
presentation.raw = vpi
presentation.credentials = (vp.verifiableCredential || [])
.map((cred) => {
if (typeof cred === 'string') {
return normalizeCredential(cred)
} else {
return <VerifiableCredential>cred
}
})
.map(createCredentialEntity)
return presentation
}