Skip to content

Commit

Permalink
fix: priority sorting for didcomm services (#1555)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Aug 28, 2023
1 parent 8d2057f commit 80c37b3
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('convertToNewDidDocument', () => {
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
priority: 6,
}),
new IndyAgentService({
id: 'did:sov:SKJVx2kn373FNgvff1SbJo#service-2#something-extra',
Expand All @@ -159,7 +159,7 @@ describe('convertToNewDidDocument', () => {
serviceEndpoint: 'did:sov:SKJVx2kn373FNgvff1SbJo',
recipientKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
routingKeys: ['EoGusetSxDJktp493VCyh981nUnzMamTRjvBaHZAy68d'],
priority: 5,
priority: 6,
}),
new IndyAgentService({
id: '#service-2#something-extra',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/connections/models/did/DidDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ export class DidDoc {
>

// Sort services based on indicated priority
return services.sort((a, b) => b.priority - a.priority)
return services.sort((a, b) => a.priority - b.priority)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('Did | DidDoc', () => {
})

it('returns all IndyAgentService and DidCommService instances sorted by priority', async () => {
expect(didDoc.didCommServices).toEqual([didDoc.service[2], didDoc.service[1]])
expect(didDoc.didCommServices).toEqual([didDoc.service[1], didDoc.service[2]])
})
})
})
6 changes: 5 additions & 1 deletion packages/core/src/modules/connections/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export function convertToNewDidDocument(didDoc: DidDoc): DidDocument {
}
})

didDoc.didCommServices.forEach((service) => {
// FIXME: we reverse the didCommServices here, as the previous implementation was wrong
// and we need to keep the same order to not break the did creation process.
// When we implement the migration to did:peer:2 and did:peer:3 according to the
// RFCs we can change it.
didDoc.didCommServices.reverse().forEach((service) => {
const serviceId = normalizeId(service.id)

// For didcommv1, we need to replace the old id with the new ones
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/dids/domain/DidDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class DidDocument {
>

// Sort services based on indicated priority
return services.sort((a, b) => b.priority - a.priority)
return services.sort((a, b) => a.priority - b.priority)
}

// TODO: it would probably be easier if we add a utility to each service so we don't have to handle logic for all service types here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('Did | DidDocument', () => {
it('returns all IndyAgentService and DidCommService instances sorted by priority', async () => {
const services = didDocumentInstance.service ?? []

expect(didDocumentInstance.didCommServices).toEqual([services[2], services[1]])
expect(didDocumentInstance.didCommServices).toEqual([services[1], services[2]])
})
})

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export enum DidCommMimeType {
}

export interface InitConfig {
/**
* Agent public endpoints, sorted by priority (higher priority first)
*/
endpoints?: string[]
label: string
walletConfig?: WalletConfig
Expand Down

0 comments on commit 80c37b3

Please sign in to comment.