Skip to content

Commit

Permalink
Merge pull request #275 from docknetwork/feat/presentation-preview-wi…
Browse files Browse the repository at this point in the history
…th-bound-indicator

feat: add ability to exclude placeholder bounds in pexToBounds
  • Loading branch information
maycon-mello authored Jul 4, 2024
2 parents 8e4b1c5 + 6c000bb commit 9e57f2b
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 12 deletions.
36 changes: 25 additions & 11 deletions packages/wasm/src/services/credential/pex-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,18 @@ function getAttributeName({field, selectedCredentials, index}) {
return attributeName;
}

export function pexToBounds(pexRequest, selectedCredentials = []) {
export function pexToBounds(
pexRequest,
selectedCredentials = [],
isRawBoundies = false,
) {
const descriptorBounds = [];
const [MIN_DATE_ADJ, MAX_DATE_ADJ, MIN_NUMBER_ADJ, MAX_NUMBER_ADJ] = [
MIN_DATE_PLACEHOLDER,
MAX_DATE_PLACEHOLDER,
MIN_NUMBER,
MAX_NUMBER,
].map(value => (isRawBoundies ? undefined : value));

// One list of bounds per descriptor/credential
pexRequest.input_descriptors.forEach((inputDescriptor, index) => {
Expand Down Expand Up @@ -87,32 +97,34 @@ export function pexToBounds(pexRequest, selectedCredentials = []) {

// Get min/max bounds values, if using exclusive we must apply an epsilon so equality isnt true
if (format === 'date-time' || format === 'date') {
max = new Date(max === undefined ? MAX_DATE_PLACEHOLDER : max);
min = new Date(min === undefined ? MIN_DATE_PLACEHOLDER : min);
max = max === undefined ? MAX_DATE_ADJ : max;
max = max ? new Date(max) : undefined;
min = min === undefined ? MIN_DATE_ADJ : min;
min = min ? new Date(min) : undefined;
} else if (type === 'number') {
max =
max === undefined
? MAX_NUMBER
: exclusiveMaximum === undefined
? MAX_NUMBER_ADJ
: exclusiveMaximum === undefined || isRawBoundies
? max
: max - EPSILON_NUMBER;
min =
min === undefined
? MIN_NUMBER
: exclusiveMinimum === undefined
? MIN_NUMBER_ADJ
: exclusiveMinimum === undefined || isRawBoundies
? min
: min + EPSILON_NUMBER;
} else if (type === 'integer') {
max =
max === undefined
? MAX_NUMBER
: exclusiveMaximum === undefined
? MAX_NUMBER_ADJ
: exclusiveMaximum === undefined || isRawBoundies
? max
: max - EPSILON_INT;
min =
min === undefined
? MIN_NUMBER
: exclusiveMinimum === undefined
? MIN_NUMBER_ADJ
: exclusiveMinimum === undefined || isRawBoundies
? min
: min + EPSILON_INT;
} else {
Expand All @@ -131,6 +143,8 @@ export function pexToBounds(pexRequest, selectedCredentials = []) {
attributeName,
min,
max,
type,
format,
});
});

Expand Down
215 changes: 214 additions & 1 deletion packages/wasm/src/services/credential/pex-helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getPexRequiredAttributes} from './pex-helpers';
import {getPexRequiredAttributes, pexToBounds} from './pex-helpers';

describe('pex helpers', () => {
describe('getPexRequiredAttributes', () => {
Expand Down Expand Up @@ -197,4 +197,217 @@ describe('pex helpers', () => {
expect(result.length).toBe(1);
});
});

describe('pexToBounds', () => {
it('should convert pexRequest to bounds with default values', () => {
const pexRequest = {
input_descriptors: [
{
constraints: {
fields: [
{
filter: {
type: 'number',
minimum: 0,
},
path: ['$.age'],
},
{
filter: {
format: 'date',
minimum: '2021-01-01',
},
path: ['$.dateOfBirth'],
},
],
},
},
],
};

const bounds = pexToBounds(pexRequest);

expect(bounds).toEqual([
[
{
attributeName: 'age',
min: 0,
max: 1000000000000000000,
type: 'number',
format: undefined,
},
{
attributeName: 'dateOfBirth',
min: new Date('2021-01-01'),
max: new Date(884541351600000),
type: undefined,
format: 'date',
},
],
]);
});

it('should convert pexRequest to bounds with raw boundaries enabled', () => {
const pexRequest = {
input_descriptors: [
{
constraints: {
fields: [
{
filter: {
type: 'number',
minimum: 0,
},
path: ['$.age'],
},
],
},
},
],
};

const bounds = pexToBounds(pexRequest, [], true);

expect(bounds).toEqual([
[
{
attributeName: 'age',
min: 0,
max: undefined,
type: 'number',
format: undefined,
},
],
]);
});
it('should convert pexRequest to bounds for date-time format', () => {
const pexRequest = {
input_descriptors: [
{
constraints: {
fields: [
{
filter: {
format: 'date-time',
maximum: '2022-01-01T00:00:00Z',
minimum: '2021-01-01T00:00:00Z',
},
path: ['$.expirationDate'],
},
],
},
},
],
};

const bounds = pexToBounds(pexRequest);

expect(bounds).toEqual([
[
{
attributeName: 'expirationDate',
min: new Date('2021-01-01T00:00:00Z'),
max: new Date('2022-01-01T00:00:00Z'),
type: undefined,
format: 'date-time',
},
],
]);
});

it('should convert pexRequest to bounds for number format', () => {
const pexRequest = {
input_descriptors: [
{
constraints: {
fields: [
{
filter: {
type: 'number',
maximum: 100,
minimum: 0,
},
path: ['$.amount'],
},
],
},
},
],
};

const bounds = pexToBounds(pexRequest);

expect(bounds).toEqual([
[
{
attributeName: 'amount',
min: 0,
max: 100,
type: 'number',
format: undefined,
},
],
]);
});

it('should convert pexRequest to bounds for multiple fields', () => {
const pexRequest = {
input_descriptors: [
{
constraints: {
fields: [
{
filter: {
format: 'date',
maximum: '2022-01-01',
minimum: '2021-01-01',
},
path: ['$.startDate'],
},
{
filter: {
type: 'number',
maximum: 100,
minimum: 0,
},
path: ['$.amount'],
},
],
},
},
],
};

const bounds = pexToBounds(pexRequest);

expect(bounds).toEqual([
[
{
attributeName: 'startDate',
min: new Date('2021-01-01'),
max: new Date('2022-01-01'),
type: undefined,
format: 'date',
},
{
attributeName: 'amount',
min: 0,
max: 100,
type: 'number',
format: undefined,
},
],
]);
});

it('should handle empty pexRequest', () => {
const pexRequest = {
input_descriptors: [],
};

const bounds = pexToBounds(pexRequest);

expect(bounds).toEqual([]);
});
});
});

0 comments on commit 9e57f2b

Please sign in to comment.