Skip to content

Commit

Permalink
More small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Aug 15, 2023
1 parent bf59404 commit cadc3a7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 71 deletions.
117 changes: 61 additions & 56 deletions packages/common/src/web3ProofTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ type IndexDetails = {

export type VerifiableCredentialSubject = {
type: 'object';
properties:
| {
id: IndexDetails;
}
| Record<string, PropertyDetails>;
properties: {
id: IndexDetails;
attributes: {
properties: Record<string, PropertyDetails>;
};
};
required: string[];
};

Expand All @@ -70,57 +71,61 @@ export const IDENTITY_SUBJECT_SCHEMA: VerifiableCredentialSubject = {
type: 'string',
description: 'Credential subject identifier',
},
firstName: {
title: 'first name',
type: 'string',
},
lastName: {
title: 'last name',
type: 'string',
},
sex: {
title: 'sex',
type: 'string',
},
dob: {
title: 'date of birth',
type: 'string',
},
countryOfResidence: {
title: 'last name',
type: 'string',
},
nationality: {
title: 'last name',
type: 'string',
},
idDocType: {
title: 'last name',
type: 'string',
},
idDocNo: {
title: 'last name',
type: 'string',
},
idDocIssuer: {
title: 'last name',
type: 'string',
},
idDocIssuedAt: {
title: 'last name',
type: 'string',
},
idDocExpiresAt: {
title: 'last name',
type: 'string',
},
nationalIdNo: {
title: 'last name',
type: 'string',
},
taxIdNo: {
title: 'last name',
type: 'string',
attributes: {
properties: {
firstName: {
title: 'first name',
type: 'string',
},
lastName: {
title: 'last name',
type: 'string',
},
sex: {
title: 'sex',
type: 'string',
},
dob: {
title: 'date of birth',
type: 'string',
},
countryOfResidence: {
title: 'last name',
type: 'string',
},
nationality: {
title: 'last name',
type: 'string',
},
idDocType: {
title: 'last name',
type: 'string',
},
idDocNo: {
title: 'last name',
type: 'string',
},
idDocIssuer: {
title: 'last name',
type: 'string',
},
idDocIssuedAt: {
title: 'last name',
type: 'string',
},
idDocExpiresAt: {
title: 'last name',
type: 'string',
},
nationalIdNo: {
title: 'last name',
type: 'string',
},
taxIdNo: {
title: 'last name',
type: 'string',
},
},
},
},
required: [],
Expand Down
35 changes: 20 additions & 15 deletions packages/common/src/web3Proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ import { stringify } from 'json-bigint';

function verifyRangeStatement(
statement: RangeStatementV2,
properties: PropertyDetails
properties?: PropertyDetails
) {
if (statement.lower === undefined) {
throw new Error('Range statements must contain a lower field');
}
if (statement.upper === undefined) {
throw new Error('Range statements must contain an upper field');
}
if (properties.type === 'string') {
if (properties?.type === 'string') {
if (typeof statement.lower != 'string') {
throw new Error(
properties.title +
Expand All @@ -61,7 +61,7 @@ function verifyRangeStatement(
' is a string property and therefore the upper end of a range statement must be a string'
);
}
} else if (properties.type === 'integer') {
} else if (properties?.type === 'integer') {
if (typeof statement.lower != 'bigint') {
throw new Error(
properties.title +
Expand All @@ -83,7 +83,7 @@ function verifyRangeStatement(
function verifySetStatement(
statement: MembershipStatementV2 | NonMembershipStatementV2,
typeName: string,
properties: PropertyDetails
properties?: PropertyDetails
) {
if (statement.set === undefined) {
throw new Error(typeName + 'statements must contain a lower field');
Expand All @@ -93,7 +93,7 @@ function verifySetStatement(
}

if (
properties.type === 'string' &&
properties?.type === 'string' &&
!statement.set.every((value) => typeof value == 'string')
) {
throw new Error(
Expand All @@ -102,7 +102,7 @@ function verifySetStatement(
);
}
if (
properties.type === 'integer' &&
properties?.type === 'integer' &&
!statement.set.every((value) => typeof value == 'bigint')
) {
throw new Error(
Expand All @@ -115,7 +115,7 @@ function verifySetStatement(
function verifyAtomicStatement(
statement: AtomicStatementV2,
existingStatements: AtomicStatementV2[],
schema: VerifiableCredentialSubject
schema?: VerifiableCredentialSubject
) {
if (statement.type === undefined) {
throw new Error('Statements must contain a type field');
Expand All @@ -135,21 +135,26 @@ function verifyAtomicStatement(
throw new Error('id is a reserved attribute name');
}

if (!Object.keys(schema.properties).includes(statement.attributeTag)) {
if (
schema &&
!Object.keys(schema.properties.attributes.properties).includes(
statement.attributeTag
)
) {
throw new Error('Unknown attributeTag: ' + statement.attributeTag);
}
// TODO Improve type to remove typecast?
const properties = (schema.properties as Record<string, PropertyDetails>)[
statement.attributeTag
];

const property =
schema &&
schema.properties.attributes.properties[statement.attributeTag];

switch (statement.type) {
case StatementTypes.AttributeInRange:
return verifyRangeStatement(statement, properties);
return verifyRangeStatement(statement, property);
case StatementTypes.AttributeInSet:
return verifySetStatement(statement, 'membership', properties);
return verifySetStatement(statement, 'membership', property);
case StatementTypes.AttributeNotInSet:
return verifySetStatement(statement, 'non-membership', properties);
return verifySetStatement(statement, 'non-membership', property);
case StatementTypes.RevealAttribute:
return;
default:
Expand Down

0 comments on commit cadc3a7

Please sign in to comment.