Skip to content

Commit

Permalink
feat: sort requested credentials (#1839)
Browse files Browse the repository at this point in the history
Signed-off-by: wadeking98 <[email protected]>
  • Loading branch information
wadeking98 authored Apr 26, 2024
1 parent 9c3b950 commit b46c7fa
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ describe('AnonCredsRsServices', () => {
revocationRegistryId: null,
credentialRevocationId: null,
methodName: 'inMemory',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

const proofRequest: AnonCredsProofRequest = {
Expand Down Expand Up @@ -410,6 +412,8 @@ describe('AnonCredsRsServices', () => {
revocationRegistryId: null,
credentialRevocationId: null,
methodName: 'inMemory',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

const proofRequest: AnonCredsProofRequest = {
Expand Down
2 changes: 2 additions & 0 deletions packages/anoncreds/src/anoncreds-rs/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export async function createCredentialForHolder(options: {
methodName: 'inMemory',
credentialRevocationId: null,
revocationRegistryId: null,
createdAt: new Date('2024-01-01T00:00:00Z'),
updatedAt: new Date('2024-01-01T00:00:00Z'),
}
const returnObj = {
credential: w3cJsonLdCredential,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ describe('Legacy indy format services', () => {
revocationRegistryId: null,
credentialRevocationId: null,
methodName: 'inMemory',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

expect(holderCredentialRecord.metadata.data).toEqual({
Expand Down
2 changes: 2 additions & 0 deletions packages/anoncreds/src/models/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface AnonCredsCredentialInfo {
revocationRegistryId: string | null
credentialRevocationId: string | null
methodName: string
createdAt: Date
updatedAt: Date
linkSecretId: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import type { AnonCredsCredentialInfo, AnonCredsRequestedAttributeMatch } from '

import { sortRequestedCredentialsMatches } from '../sortRequestedCredentialsMatches'

const credentialInfo = {} as unknown as AnonCredsCredentialInfo
const credentialInfo = {
updatedAt: new Date('2024-01-01T00:00:00Z'),
createdAt: new Date('2024-01-01T00:00:00Z'),
} as unknown as AnonCredsCredentialInfo

const credentials: AnonCredsRequestedAttributeMatch[] = [
{
credentialId: '1',
revealed: true,
revoked: true,
credentialInfo,
credentialInfo: { ...credentialInfo, updatedAt: new Date('2024-01-01T00:00:01Z') },
},
{
credentialId: '2',
revealed: true,
revoked: undefined,
credentialInfo,
credentialInfo: { ...credentialInfo, updatedAt: new Date('2024-01-01T00:00:01Z') },
},
{
credentialId: '3',
revealed: true,
revoked: false,
credentialInfo,
credentialInfo: { ...credentialInfo, updatedAt: new Date('2024-01-01T00:00:01Z') },
},
{
credentialId: '4',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { AnonCredsRequestedAttributeMatch, AnonCredsRequestedPredicateMatch
export function sortRequestedCredentialsMatches<
Requested extends Array<AnonCredsRequestedAttributeMatch> | Array<AnonCredsRequestedPredicateMatch>
>(credentials: Requested) {
const staySame = 0
const credentialGoUp = -1
const credentialGoDown = 1

Expand All @@ -18,7 +17,8 @@ export function sortRequestedCredentialsMatches<

return credentialsClone.sort((credential, compareTo) => {
// Nothing needs to happen if values are the same
if (credential.revoked === compareTo.revoked) return staySame
if (credential.revoked === compareTo.revoked)
return compareTo.credentialInfo.updatedAt.getTime() - credential.credentialInfo.updatedAt.getTime()

// Undefined always is at the top
if (credential.revoked === undefined) return credentialGoUp
Expand Down
4 changes: 4 additions & 0 deletions packages/anoncreds/src/utils/w3cAnonCredsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ function anonCredsCredentialInfoFromW3cRecord(w3cCredentialRecord: W3cCredential
revocationRegistryId: anonCredsTags.anonCredsRevocationRegistryId ?? null,
methodName: anonCredsCredentialMetadata.methodName,
linkSecretId: anonCredsCredentialMetadata.linkSecretId,
createdAt: w3cCredentialRecord.createdAt,
updatedAt: w3cCredentialRecord.updatedAt ?? w3cCredentialRecord.createdAt,
}
}

Expand All @@ -89,6 +91,8 @@ function anonCredsCredentialInfoFromAnonCredsRecord(
revocationRegistryId: anonCredsCredentialRecord.credential.rev_reg_id ?? null,
methodName: anonCredsCredentialRecord.methodName,
linkSecretId: anonCredsCredentialRecord.linkSecretId,
createdAt: anonCredsCredentialRecord.createdAt,
updatedAt: anonCredsCredentialRecord.updatedAt ?? anonCredsCredentialRecord.createdAt,
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/anoncreds/tests/anoncreds-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ async function anonCredsFlowTest(options: { issuerId: string; revocable: boolean
revocationRegistryId: revocable ? revocationRegistryDefinitionId : null,
credentialRevocationId: revocable ? '1' : null,
methodName: 'inMemory',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

const expectedCredentialMetadata = revocable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ async function anonCredsFlowTest(options: { issuerId: string; revocable: boolean
revocationRegistryId: revocable ? revocationRegistryDefinitionId : null,
credentialRevocationId: revocable ? '1' : null,
methodName: 'inMemory',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

const expectedCredentialMetadata = revocable
Expand Down
2 changes: 2 additions & 0 deletions packages/anoncreds/tests/indy-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ describe('Legacy indy format services using anoncreds-rs', () => {
credentialRevocationId: null,
methodName: 'inMemory',
linkSecretId: 'linkSecretId',
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
})

expect(holderCredentialRecord.metadata.data).toEqual({
Expand Down

0 comments on commit b46c7fa

Please sign in to comment.