Skip to content

Commit

Permalink
feat(sd-jwt): include repository commands in the API
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <blu3beri@proton.me>
  • Loading branch information
berendsliedrecht committed Oct 30, 2023
1 parent 2061b83 commit 75569fe
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
24 changes: 24 additions & 0 deletions packages/sd-jwt/src/SdJwtApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SdJwtCreateOptions, SdJwtPresentOptions, SdJwtReceiveOptions, SdJwtVerifyOptions } from './SdJwtOptions'
import type { SdJwtVcVerificationResult } from './SdJwtService'
import type { SdJwtRecord } from './repository'
import type { Query } from '@aries-framework/core'

import { AgentContext, injectable } from '@aries-framework/core'

Expand Down Expand Up @@ -64,4 +65,27 @@ export class SdJwtApi {
): Promise<{ sdJwtRecord: SdJwtRecord<Header, Payload>; validation: SdJwtVcVerificationResult }> {
return await this.sdJwtService.verify<Header, Payload>(this.agentContext, sdJwtCompact, options)
}

public async getCredentialRecordByIdM<
Header extends Record<string, unknown> = Record<string, unknown>,
Payload extends Record<string, unknown> = Record<string, unknown>
>(id: string): Promise<SdJwtRecord<Header, Payload>> {
return await this.sdJwtService.getCredentialRecordById<Header, Payload>(this.agentContext, id)
}

public async getAllCredentialRecords(): Promise<Array<SdJwtRecord>> {
return await this.sdJwtService.getAllCredentialRecords(this.agentContext)
}

public async findCredentialRecordsByQuery(query: Query<SdJwtRecord>): Promise<Array<SdJwtRecord>> {
return await this.sdJwtService.findCredentialRecordsByQuery(this.agentContext, query)
}

public async removeCredentialRecord(id: string) {
return await this.sdJwtService.removeCredentialRecord(this.agentContext, id)
}

public async updateCredentialRecord(sdJwtRecord: SdJwtRecord) {
return await this.sdJwtService.updateCredentialRecord(this.agentContext, sdJwtRecord)
}
}
28 changes: 27 additions & 1 deletion packages/sd-jwt/src/SdJwtService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SdJwtCreateOptions, SdJwtPresentOptions, SdJwtReceiveOptions, SdJwtVerifyOptions } from './SdJwtOptions'
import type { AgentContext, JwkJson } from '@aries-framework/core'
import type { AgentContext, JwkJson, Query } from '@aries-framework/core'
import type { Signer, SdJwtVcVerificationResult, Verifier, HasherAndAlgorithm } from 'jwt-sd'

import {
Expand Down Expand Up @@ -306,4 +306,30 @@ export class SdJwtService {
validation: verificationResult,
}
}

public async getCredentialRecordById<
Header extends Record<string, unknown> = Record<string, unknown>,
Payload extends Record<string, unknown> = Record<string, unknown>
>(agentContext: AgentContext, id: string): Promise<SdJwtRecord<Header, Payload>> {
return (await this.sdJwtRepository.getById(agentContext, id)) as SdJwtRecord<Header, Payload>
}

public async getAllCredentialRecords(agentContext: AgentContext): Promise<Array<SdJwtRecord>> {
return await this.sdJwtRepository.getAll(agentContext)
}

public async findCredentialRecordsByQuery(
agentContext: AgentContext,
query: Query<SdJwtRecord>
): Promise<Array<SdJwtRecord>> {
return await this.sdJwtRepository.findByQuery(agentContext, query)
}

public async removeCredentialRecord(agentContext: AgentContext, id: string) {
await this.sdJwtRepository.deleteById(agentContext, id)
}

public async updateCredentialRecord(agentContext: AgentContext, sdJwtRecord: SdJwtRecord) {
await this.sdJwtRepository.update(agentContext, sdJwtRecord)
}
}
4 changes: 3 additions & 1 deletion packages/sd-jwt/src/__tests__/SdJwtModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { DependencyManager } from '@aries-framework/core'
import { SdJwtApi } from '../SdJwtApi'
import { SdJwtModule } from '../SdJwtModule'
import { SdJwtService } from '../SdJwtService'
import { SdJwtRepository } from '../repository'

const dependencyManager = {
registerInstance: jest.fn(),
Expand All @@ -19,7 +20,8 @@ describe('SdJwtModule', () => {
expect(dependencyManager.registerContextScoped).toHaveBeenCalledTimes(1)
expect(dependencyManager.registerContextScoped).toHaveBeenCalledWith(SdJwtApi)

expect(dependencyManager.registerSingleton).toHaveBeenCalledTimes(1)
expect(dependencyManager.registerSingleton).toHaveBeenCalledTimes(2)
expect(dependencyManager.registerSingleton).toHaveBeenCalledWith(SdJwtService)
expect(dependencyManager.registerSingleton).toHaveBeenCalledWith(SdJwtRepository)
})
})
6 changes: 6 additions & 0 deletions packages/sd-jwt/src/repository/__tests__/SdJwtRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { SdJwtVc, SignatureAndEncryptionAlgorithm } from 'jwt-sd'
import { SdJwtRecord } from '../SdJwtRecord'

describe('SdJwtRecord', () => {
const holderDidUrl = 'did:key:zUC74VEqqhEHQcgv4zagSPkqFJxuNWuoBPKjJuHETEUeHLoSqWt92viSsmaWjy82y'

test('sets the values passed in the constructor on the record', () => {
const createdAt = new Date()
const sdJwtRecord = new SdJwtRecord({
Expand All @@ -16,6 +18,7 @@ describe('SdJwtRecord', () => {
header: { alg: SignatureAndEncryptionAlgorithm.EdDSA },
payload: { iss: 'did:key:123' },
signature: new Uint8Array(32).fill(42),
holderDidUrl,
},
})

Expand All @@ -29,6 +32,7 @@ describe('SdJwtRecord', () => {
header: { alg: SignatureAndEncryptionAlgorithm.EdDSA },
payload: { iss: 'did:key:123' },
signature: new Uint8Array(32).fill(42),
holderDidUrl,
})
})

Expand All @@ -44,6 +48,7 @@ describe('SdJwtRecord', () => {
header: { alg: SignatureAndEncryptionAlgorithm.EdDSA },
payload: { iss: 'did:key:123' },
signature: new Uint8Array(32).fill(42),
holderDidUrl,
},
})

Expand Down Expand Up @@ -93,6 +98,7 @@ describe('SdJwtRecord', () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature: sdJwt.signature!,
disclosures: sdJwt.disclosures?.map((d) => d.decoded),
holderDidUrl,
},
})

Expand Down
1 change: 0 additions & 1 deletion packages/sd-jwt/tests/sdJwtVc.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ describe('sd-jwt-vc end to end test', () => {
}

const presentation = await holder.modules.sdJwt.present(sdJwtRecord, {
holderDidUrl,
verifierMetadata,
includedDisclosureIndices: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
})
Expand Down

0 comments on commit 75569fe

Please sign in to comment.