@veramo/data-store
Version:
Veramo data storage plugin based on TypeORM database drivers
142 lines (121 loc) • 3.66 kB
text/typescript
import {
Entity,
Column,
Connection,
PrimaryColumn,
BaseEntity,
OneToMany,
ManyToMany,
Index,
BeforeInsert,
BeforeUpdate,
} from 'typeorm'
import { Key } from './key.js'
import { Service } from './service.js'
import { Message } from './message.js'
import { Presentation } from './presentation.js'
import { Credential } from './credential.js'
import { Claim } from './claim.js'
/**
* Represents some properties and relationships of an {@link @veramo/core-types#IIdentifier} that are stored in a TypeORM
* database for the purpose of keeping track of keys and services associated with a DID managed by a Veramo agent.
*
* @see {@link @veramo/data-store#DIDStore | DIDStore} for the implementation used by the
* {@link @veramo/did-manager#DIDManager | DIDManager}.
* @see {@link @veramo/data-store#DataStoreORM | DataStoreORM} for the implementation of the query interface.
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
('identifier')
(['alias', 'provider'], { unique: true })
export class Identifier extends BaseEntity {
()
// @ts-ignore
did: string
({ nullable: true })
// @ts-ignore
provider?: string
({ nullable: true })
// @ts-ignore
alias?: string
()
setSaveDate() {
this.saveDate = new Date()
this.updateDate = new Date()
}
()
setUpdateDate() {
this.updateDate = new Date()
}
({ select: false })
// @ts-ignore
saveDate: Date
({ select: false })
// @ts-ignore
updateDate: Date
({ nullable: true })
// @ts-ignore
controllerKeyId?: string
((type) => Key, (key) => key.identifier)
// @ts-ignore
keys: Key[]
((type) => Service, (service) => service.identifier, {
cascade: true,
})
// @ts-ignore
services: Service[]
((type) => Message, (message) => message.from)
// @ts-ignore
sentMessages: Message[]
((type) => Message, (message) => message.to)
// @ts-ignore
receivedMessages: Message[]
((type) => Presentation, (presentation) => presentation.holder)
// @ts-ignore
issuedPresentations: Presentation[]
((type) => Presentation, (presentation) => presentation.verifier)
// @ts-ignore
receivedPresentations: Presentation[]
((type) => Credential, (credential) => credential.issuer)
// @ts-ignore
issuedCredentials: Credential[]
((type) => Credential, (credential) => credential.subject)
// @ts-ignore
receivedCredentials: Credential[]
((type) => Claim, (claim) => claim.issuer)
// @ts-ignore
issuedClaims: Claim[]
((type) => Claim, (claim) => claim.subject)
// @ts-ignore
receivedClaims: Claim[]
/**
* Convenience method to get the most recent information about a subject DID as described by Verifiable Credential
* claims.
*
* Example:
* ```typescript
* // get the latest claim value for credentials containing `credentialSubject.name` and this Identifier as subject.
* const name = await identifier.getLatestClaimValue({type: 'name'})
* ```
*
* @param where - The TypeORM `where` filter to use.
*/
async getLatestClaimValue(
dbConnection: Promise<Connection>,
where: any,
): Promise<string | null | undefined> {
const claim = await (await dbConnection).getRepository(Claim).findOne({
where: {
...where,
subject: this.did,
},
order: {
issuanceDate: 'DESC',
},
})
return claim?.value
}
shortDid() {
return `${this.did.slice(0, 15)}...${this.did.slice(-4)}`
}
}