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 0bab1ab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
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)
}
}
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

0 comments on commit 0bab1ab

Please sign in to comment.