From cadc3a7ca1e9291923b8aeda16b30492ce18f9fb Mon Sep 17 00:00:00 2001 From: Hjort Date: Tue, 15 Aug 2023 10:42:45 +0200 Subject: [PATCH] More small fixes --- packages/common/src/web3ProofTypes.ts | 117 ++++++++++++++------------ packages/common/src/web3Proofs.ts | 35 ++++---- 2 files changed, 81 insertions(+), 71 deletions(-) diff --git a/packages/common/src/web3ProofTypes.ts b/packages/common/src/web3ProofTypes.ts index 0c5ea9c35..4a8b44ec9 100644 --- a/packages/common/src/web3ProofTypes.ts +++ b/packages/common/src/web3ProofTypes.ts @@ -54,11 +54,12 @@ type IndexDetails = { export type VerifiableCredentialSubject = { type: 'object'; - properties: - | { - id: IndexDetails; - } - | Record; + properties: { + id: IndexDetails; + attributes: { + properties: Record; + }; + }; required: string[]; }; @@ -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: [], diff --git a/packages/common/src/web3Proofs.ts b/packages/common/src/web3Proofs.ts index 5152d6e69..8aaaf6302 100644 --- a/packages/common/src/web3Proofs.ts +++ b/packages/common/src/web3Proofs.ts @@ -40,7 +40,7 @@ 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'); @@ -48,7 +48,7 @@ function verifyRangeStatement( 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 + @@ -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 + @@ -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'); @@ -93,7 +93,7 @@ function verifySetStatement( } if ( - properties.type === 'string' && + properties?.type === 'string' && !statement.set.every((value) => typeof value == 'string') ) { throw new Error( @@ -102,7 +102,7 @@ function verifySetStatement( ); } if ( - properties.type === 'integer' && + properties?.type === 'integer' && !statement.set.every((value) => typeof value == 'bigint') ) { throw new Error( @@ -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'); @@ -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)[ - 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: