Skip to content

Commit

Permalink
Merge pull request #240 from Sphereon-Opensource/fix/SDK-31
Browse files Browse the repository at this point in the history
fix/SDK-31
  • Loading branch information
BtencateSphereon authored Oct 4, 2024
2 parents 437122d + bd16a42 commit cf36c38
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FindArgs, TAgent, TCredentialColumns } from '@veramo/core'
import * as fs from 'fs'
import { IVerifiableCredential } from '@sphereon/ssi-types'
import { CredentialCorrelationType, CredentialRole, CredentialStateType, DigitalCredential } from '@sphereon/ssi-sdk.data-store'
import * as fs from 'fs'
import {
AddDigitalCredential,
credentialIdOrHashFilter,
Expand All @@ -9,7 +10,6 @@ import {
ICredentialStore,
UniqueDigitalCredential,
} from '../../src'
import { IVerifiableCredential } from '@sphereon/ssi-types'

type ConfiguredAgent = TAgent<ICredentialStore>

Expand Down
49 changes: 28 additions & 21 deletions packages/credential-store/src/agent/CredentialStore.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {
AbstractDigitalCredentialStore,
DigitalCredential,
UpdateCredentialStateArgs,
DocumentType,
parseRawDocument,
defaultHasher
} from '@sphereon/ssi-sdk.data-store'
import { IVerifiableCredential } from '@sphereon/ssi-types'
import { IAgentPlugin } from '@veramo/core'
import { schema, logger } from '../index'
import { credentialIdOrHashFilter } from '../utils/filters'
import {
AddCredentialArgs,
credentialIdOrHashFilter,
DeleteCredentialArgs,
DeleteCredentialsArgs,
DocumentType,
GetCredentialArgs,
GetCredentialsArgs,
GetCredentialsByClaimsArgs,
GetCredentialsByIdOrHashArgs,
ICredentialStore,
logger,
OptionalUniqueDigitalCredential,
schema,
TClaimsColumns,
UniqueDigitalCredential,
} from '../index'
import { AbstractDigitalCredentialStore, DigitalCredential, parseRawDocument, UpdateCredentialStateArgs } from '@sphereon/ssi-sdk.data-store'
import { IVerifiableCredential } from '@sphereon/ssi-types'
import { defaultHasher } from '@sphereon/ssi-sdk.data-store'
} from '../types/ICredentialStore'
import { TClaimsColumns} from '../types/claims'

// Exposing the methods here for any REST implementation
export const credentialStoreMethods: Array<string> = [
Expand Down Expand Up @@ -58,52 +62,56 @@ export class CredentialStore implements IAgentPlugin {
this.store = options.store
}

/** {@inheritDoc ICRManager.crmAddCredential} */
/** {@inheritDoc ICredentialStore.crsAddCredential} */
private async crsAddCredential(args: AddCredentialArgs): Promise<DigitalCredential> {
return await this.store.addCredential({ ...args.credential, opts: { ...args.opts, hasher: args.opts?.hasher ?? defaultHasher } })
}

/** {@inheritDoc ICRManager.updateCredentialState} */
/** {@inheritDoc ICredentialStore.crsUpdateCredentialState} */
private async crsUpdateCredentialState(args: UpdateCredentialStateArgs): Promise<DigitalCredential> {
return await this.store.updateCredentialState(args)
}

/** {@inheritDoc ICRManager.crmGetCredential} */
/** {@inheritDoc ICredentialStore.crsGetCredential} */
private async crsGetCredential(args: GetCredentialArgs): Promise<DigitalCredential> {
const { id } = args
return await this.store.getCredential({ id })

return this.store.getCredential({ id })
}

/** {@inheritDoc ICRManager.crmGetCredentials} */
/** {@inheritDoc ICredentialStore.crsGetCredentials} */
private async crsGetCredentials(args: GetCredentialsArgs): Promise<Array<DigitalCredential>> {
const { filter } = args
const credentials = await this.store.getCredentials({ filter })

return credentials.data
}

/** {@inheritDoc ICRManager.crmGetUniqueCredentialByIdOrHash} */
/** {@inheritDoc ICredentialStore.crsGetUniqueCredentialByIdOrHash} */
private async crsGetUniqueCredentialByIdOrHash(args: GetCredentialsByIdOrHashArgs): Promise<OptionalUniqueDigitalCredential> {
const credentials = await this.crsGetCredentials({ filter: credentialIdOrHashFilter(args.credentialRole, args.idOrHash) })
if (credentials.length === 0) {
return undefined
} else if (credentials.length > 1) {
logger.warning('Duplicate credentials detected in crsGetUniqueCredentialByIdOrHash', args)
}

return this.toUniqueCredentials(credentials)[0]
}

/** {@inheritDoc ICRManager.crmGetUniqueCredentials} */
/** {@inheritDoc ICredentialStore.crsGetUniqueCredentials} */
private async crsGetUniqueCredentials(args: GetCredentialsArgs): Promise<Array<UniqueDigitalCredential>> {
const credentials = await this.crsGetCredentials(args)

return this.toUniqueCredentials(credentials)
}

/** {@inheritDoc ICRManager.crmDeleteCredential} */
/** {@inheritDoc ICredentialStore.crsDeleteCredential} */
private async crsDeleteCredential(args: DeleteCredentialArgs): Promise<boolean> {
return this.store.removeCredential(args)
}

/** {@inheritDoc ICRManager.crmDeleteCredentials} */
/** {@inheritDoc ICredentialStore.crsDeleteCredentials} */
private async crsDeleteCredentials(args: DeleteCredentialsArgs): Promise<number> {
const credentials = await this.crsGetCredentials(args)
let count = 0
Expand All @@ -113,6 +121,7 @@ export class CredentialStore implements IAgentPlugin {
count++
}
}

return count
}

Expand All @@ -138,7 +147,7 @@ export class CredentialStore implements IAgentPlugin {
})

// This a copy of how Veramo did this. TODO Use GraphQL in the future?
const claimFilteredCredentials: UniqueDigitalCredential[] = digitalCredentials.filter((uniqueVC) => {
return digitalCredentials.filter((uniqueVC) => {
if (!uniqueVC.uniformVerifiableCredential) {
return false
}
Expand Down Expand Up @@ -178,8 +187,6 @@ export class CredentialStore implements IAgentPlugin {
}) ?? true
)
})

return claimFilteredCredentials
}

private getValueFromCredential(credential: IVerifiableCredential, column: TClaimsColumns): any {
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export {
} from '@sphereon/ssi-sdk.data-store'
export * from './types/ICredentialStore'
export * from './types/claims'
export * from './types/filters'
export * from './utils/filters'
28 changes: 3 additions & 25 deletions packages/data-store/src/eventLogger/EventLoggerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuditLoggingEvent } from '@sphereon/ssi-sdk.core'
import { OrPromise } from '@sphereon/ssi-types'
import { AbstractEventLoggerStore } from './AbstractEventLoggerStore'
import { AuditEventEntity, auditEventEntityFrom } from '../entities/eventLogger/AuditEventEntity'
import { auditEventFrom } from '../utils/eventLogger/MappingUtils'
import { GetAuditEventsArgs, StoreAuditEventArgs } from '../types'

const debug: Debugger = Debug('sphereon:ssi-sdk:event-store')
Expand All @@ -23,7 +24,7 @@ export class EventLoggerStore extends AbstractEventLoggerStore {
...(args?.filter && { where: args?.filter }),
})

return result.map((event: AuditEventEntity) => this.auditEventFrom(event))
return result.map((event: AuditEventEntity) => auditEventFrom(event))
}

storeAuditEvent = async (args: StoreAuditEventArgs): Promise<AuditLoggingEvent> => {
Expand All @@ -34,29 +35,6 @@ export class EventLoggerStore extends AbstractEventLoggerStore {
debug('Storing audit event', auditEventEntity)
const createdResult: AuditEventEntity = await connection.getRepository(AuditEventEntity).save(auditEventEntity)

return this.auditEventFrom(createdResult)
}

private auditEventFrom = (event: AuditEventEntity): AuditLoggingEvent => {
return {
id: event.id,
description: event.description,
timestamp: event.timestamp,
level: event.level,
correlationId: event.correlationId,
actionType: event.actionType,
actionSubType: event.actionSubType,
initiatorType: event.initiatorType,
partyAlias: event.partyAlias,
partyCorrelationId: event.partyCorrelationId,
partyCorrelationType: event.partyCorrelationType,
subSystemType: event.subSystemType,
system: event.system,
systemAlias: event.systemAlias,
systemCorrelationId: event.systemCorrelationId,
systemCorrelationIdType: event.systemCorrelationIdType,
...(event.data && { data: JSON.parse(event.data) }),
...(event.diagnosticData && { diagnosticData: JSON.parse(event.diagnosticData) }),
}
return auditEventFrom(createdResult)
}
}
76 changes: 14 additions & 62 deletions packages/data-store/src/issuanceBranding/IssuanceBrandingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IssuerLocaleBrandingEntity, issuerLocaleBrandingEntityFrom } from '../e
import { BaseLocaleBrandingEntity } from '../entities/issuanceBranding/BaseLocaleBrandingEntity'
import { TextAttributesEntity } from '../entities/issuanceBranding/TextAttributesEntity'
import { AbstractIssuanceBrandingStore } from './AbstractIssuanceBrandingStore'
import { credentialBrandingFrom, issuerBrandingFrom, localeBrandingFrom } from '../utils/issuanceBranding/MappingUtils'
import {
IAddCredentialBrandingArgs,
IAddCredentialLocaleBrandingArgs,
Expand All @@ -30,7 +31,6 @@ import {
IIssuerBrandingFilter,
IIssuerLocaleBranding,
IIssuerLocaleBrandingFilter,
ILocaleBranding,
IRemoveCredentialBrandingArgs,
IRemoveCredentialLocaleBrandingArgs,
IRemoveIssuerBrandingArgs,
Expand All @@ -42,6 +42,7 @@ import {
ICredentialBrandingFilter,
} from '../types'


const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:issuance-branding-store')

export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
Expand Down Expand Up @@ -71,7 +72,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Adding credential branding', credentialBrandingEntity)
const createdResult: CredentialBrandingEntity = await repository.save(credentialBrandingEntity)

return this.credentialBrandingFrom(createdResult)
return credentialBrandingFrom(createdResult)
}

public getCredentialBranding = async (args?: IGetCredentialBrandingArgs): Promise<Array<ICredentialBranding>> => {
Expand All @@ -89,7 +90,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
...(filter && { where: filter }),
})

return result.map((credentialBranding: CredentialBrandingEntity) => this.credentialBrandingFrom(credentialBranding))
return result.map((credentialBranding: CredentialBrandingEntity) => credentialBrandingFrom(credentialBranding))
}

public removeCredentialBranding = async (args: IRemoveCredentialBrandingArgs): Promise<void> => {
Expand Down Expand Up @@ -133,7 +134,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Updating credential branding', branding)
const result: CredentialBrandingEntity = await repository.save(branding, { transaction: true })

return this.credentialBrandingFrom(result)
return credentialBrandingFrom(result)
}

public addCredentialLocaleBranding = async (args: IAddCredentialLocaleBrandingArgs): Promise<ICredentialBranding> => {
Expand Down Expand Up @@ -188,7 +189,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
return Promise.reject(Error('Unable to get updated credential branding'))
}

return this.credentialBrandingFrom(result)
return credentialBrandingFrom(result)
}

public getCredentialLocaleBranding = async (args?: IGetCredentialLocaleBrandingArgs): Promise<Array<ICredentialLocaleBranding>> => {
Expand All @@ -211,7 +212,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
return credentialBrandingLocale
? credentialBrandingLocale.map(
(credentialLocaleBranding: CredentialLocaleBrandingEntity) =>
this.localeBrandingFrom(credentialLocaleBranding) as ICredentialLocaleBranding,
localeBrandingFrom(credentialLocaleBranding) as ICredentialLocaleBranding,
)
: []
}
Expand Down Expand Up @@ -259,7 +260,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Updating credential locale branding', localeBranding)
const updatedResult: CredentialLocaleBrandingEntity = await repository.save(localeBranding, { transaction: true })

return this.localeBrandingFrom(updatedResult) as ICredentialLocaleBranding
return localeBrandingFrom(updatedResult) as ICredentialLocaleBranding
}

public addIssuerBranding = async (args: IAddIssuerBrandingArgs): Promise<IIssuerBranding> => {
Expand All @@ -281,7 +282,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Adding issuer branding', issuerBrandingEntity)
const createdResult: IssuerBrandingEntity = await repository.save(issuerBrandingEntity)

return this.issuerBrandingFrom(createdResult)
return issuerBrandingFrom(createdResult)
}

public getIssuerBranding = async (args?: IGetIssuerBrandingArgs): Promise<Array<IIssuerBranding>> => {
Expand All @@ -299,7 +300,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
...(filter && { where: filter }),
})

return result.map((issuerBranding: IssuerBrandingEntity) => this.issuerBrandingFrom(issuerBranding))
return result.map((issuerBranding: IssuerBrandingEntity) => issuerBrandingFrom(issuerBranding))
}

public removeIssuerBranding = async (args: IRemoveIssuerBrandingArgs): Promise<void> => {
Expand Down Expand Up @@ -343,7 +344,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Updating issuer branding', branding)
const result: IssuerBrandingEntity = await repository.save(branding, { transaction: true })

return this.issuerBrandingFrom(result)
return issuerBrandingFrom(result)
}

public addIssuerLocaleBranding = async (args: IAddIssuerLocaleBrandingArgs): Promise<IIssuerBranding> => {
Expand Down Expand Up @@ -394,7 +395,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
return Promise.reject(Error('Unable to get updated issuer branding'))
}

return this.issuerBrandingFrom(result)
return issuerBrandingFrom(result)
}

public getIssuerLocaleBranding = async (args?: IGetIssuerLocaleBrandingArgs): Promise<Array<IIssuerLocaleBranding>> => {
Expand All @@ -416,7 +417,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {

return issuerLocaleBranding
? issuerLocaleBranding.map(
(issuerLocaleBranding: IssuerLocaleBrandingEntity) => this.localeBrandingFrom(issuerLocaleBranding) as IIssuerLocaleBranding,
(issuerLocaleBranding: IssuerLocaleBrandingEntity) => localeBrandingFrom(issuerLocaleBranding) as IIssuerLocaleBranding,
)
: []
}
Expand Down Expand Up @@ -462,56 +463,7 @@ export class IssuanceBrandingStore extends AbstractIssuanceBrandingStore {
debug('Updating issuer locale branding', localeBranding)
const updatedResult: IssuerLocaleBrandingEntity = await repository.save(localeBranding, { transaction: true })

return this.localeBrandingFrom(updatedResult) as IIssuerLocaleBranding
}

private credentialBrandingFrom = (credentialBranding: CredentialBrandingEntity): ICredentialBranding => {
const result: ICredentialBranding = {
...credentialBranding,
localeBranding: credentialBranding.localeBranding.map((localeBranding: BaseLocaleBrandingEntity) => this.localeBrandingFrom(localeBranding)),
}

return this.replaceNullWithUndefined(result)
}

private issuerBrandingFrom = (issuerBranding: IssuerBrandingEntity): IIssuerBranding => {
const result: IIssuerBranding = {
...issuerBranding,
localeBranding: issuerBranding.localeBranding.map((localeBranding: BaseLocaleBrandingEntity) => this.localeBrandingFrom(localeBranding)),
}

return this.replaceNullWithUndefined(result)
}

private localeBrandingFrom = (localeBranding: BaseLocaleBrandingEntity): ILocaleBranding => {
const result: ILocaleBranding = {
...localeBranding,
locale: localeBranding.locale === '' ? undefined : localeBranding.locale,
}

return this.replaceNullWithUndefined(result)
}

private replaceNullWithUndefined(obj: any): any {
if (obj === null) {
return undefined
}

if (typeof obj !== 'object' || obj instanceof Date) {
return obj
}

if (Array.isArray(obj)) {
return obj.map((value: any) => this.replaceNullWithUndefined(value))
}

const result: any = {}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = this.replaceNullWithUndefined(obj[key])
}
}
return result
return localeBrandingFrom(updatedResult) as IIssuerLocaleBranding
}

private hasDuplicateLocales = async (localeBranding: Array<IBasicCredentialLocaleBranding | IBasicIssuerLocaleBranding>): Promise<boolean> => {
Expand Down
Loading

0 comments on commit cf36c38

Please sign in to comment.