-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(proofs): sort credentials based on revocation (#1225)
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
25b2bcf
commit 0f6d231
Showing
3 changed files
with
115 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ages/core/src/modules/proofs/formats/indy/util/__tests__/sortRequestedCredentials.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { RequestedAttribute } from '../../models' | ||
import { sortRequestedCredentials } from '../sortRequestedCredentials' | ||
|
||
const credentials = [ | ||
new RequestedAttribute({ | ||
credentialId: '1', | ||
revealed: true, | ||
revoked: true, | ||
}), | ||
new RequestedAttribute({ | ||
credentialId: '2', | ||
revealed: true, | ||
revoked: undefined, | ||
}), | ||
new RequestedAttribute({ | ||
credentialId: '3', | ||
revealed: true, | ||
revoked: false, | ||
}), | ||
new RequestedAttribute({ | ||
credentialId: '4', | ||
revealed: true, | ||
revoked: false, | ||
}), | ||
new RequestedAttribute({ | ||
credentialId: '5', | ||
revealed: true, | ||
revoked: true, | ||
}), | ||
new RequestedAttribute({ | ||
credentialId: '6', | ||
revealed: true, | ||
revoked: undefined, | ||
}), | ||
] | ||
|
||
describe('sortRequestedCredentials', () => { | ||
test('sorts the credentials', () => { | ||
expect(sortRequestedCredentials(credentials)).toEqual([ | ||
credentials[1], | ||
credentials[5], | ||
credentials[2], | ||
credentials[3], | ||
credentials[0], | ||
credentials[4], | ||
]) | ||
}) | ||
}) |
33 changes: 33 additions & 0 deletions
33
packages/core/src/modules/proofs/formats/indy/util/sortRequestedCredentials.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { RequestedAttribute, RequestedPredicate } from '../models' | ||
|
||
/** | ||
* Sort requested attributes and predicates by `revoked` status. The order is: | ||
* - first credentials with `revoked` set to undefined, this means no revocation status is needed for the credentials | ||
* - then credentials with `revoked` set to false, this means the credentials are not revoked | ||
* - then credentials with `revoked` set to true, this means the credentials are revoked | ||
*/ | ||
export function sortRequestedCredentials<Requested extends Array<RequestedAttribute> | Array<RequestedPredicate>>( | ||
credentials: Requested | ||
) { | ||
const staySame = 0 | ||
const credentialGoUp = -1 | ||
const credentialGoDown = 1 | ||
|
||
// Clone as sort is in place | ||
const credentialsClone = [...credentials] | ||
|
||
return credentialsClone.sort((credential, compareTo) => { | ||
// Nothing needs to happen if values are the same | ||
if (credential.revoked === compareTo.revoked) return staySame | ||
|
||
// Undefined always is at the top | ||
if (credential.revoked === undefined) return credentialGoUp | ||
if (compareTo.revoked === undefined) return credentialGoDown | ||
|
||
// Then revoked | ||
if (credential.revoked === false) return credentialGoUp | ||
|
||
// It means that compareTo is false and credential is true | ||
return credentialGoDown | ||
}) | ||
} |