import { Versioned } from './versioned'; import { Encoder } from '../encoder'; import { CRYPTO_VERSION_STR, FlattenedSchema, SCHEMA_PROPS_STR, SCHEMA_STR, STATUS_STR, SUBJECT_STR } from './types-and-consts'; /** * Rules * 1. Schema must define a top level `credentialSubject` field for the subject, and it can be an object or array of object * 2. Credential status if defined must be present as `credentialStatus` field. * 3. Any top level keys in the schema JSON can be created Some example schemas { '$schema': 'http://json-schema.org/draft-07/schema#', type: 'object', properties: { credentialSubject: { type: 'object', properties: { fname: { type: 'string' }, lname: { type: 'string' }, email: { type: 'string' }, SSN: { '$ref': '#/definitions/encryptableString' }, userId: { '$ref': '#/definitions/encryptableCompString' }, country: { type: 'string' }, city: { type: 'string' }, timeOfBirth: { type: 'integer', minimum: 0 }, height: { type: 'number', minimum: 0, multipleOf: 0.1 }, weight: { type: 'number', minimum: 0, multipleOf: 0.1 }, BMI: { type: 'number', minimum: 0, multipleOf: 0.01 }, score: { type: 'number', minimum: -100, multipleOf: 0.1 }, secret: { type: 'string' } } } }, definitions: { encryptableString: { type: 'string' }, encryptableCompString: { type: 'string' } } } { '$schema': 'http://json-schema.org/draft-07/schema#', type: 'object', properties: { credentialSubject: { type: 'object', properties: { fname: { type: 'string' }, lname: { type: 'string' }, sensitive: { type: 'object', properties: { very: { type: 'object', properties: { secret: { type: 'string' } } }, email: { type: 'string' }, phone: { type: 'string' }, SSN: { '$ref': '#/definitions/encryptableString' } } }, lessSensitive: { type: 'object', properties: { location: { type: 'object', properties: { country: { type: 'string' }, city: { type: 'string' } } }, department: { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } } } }, rank: { type: 'integer', minimum: 0 } } }, credentialStatus: { type: 'object', properties: { id: { type: 'string' }, type: { type: 'string' }, revocationCheck: { type: 'string' }, revocationId: { type: 'string' } } } }, definitions: { encryptableString: { type: 'string' }, encryptableCompString: { type: 'string' } } } { '$schema': 'http://json-schema.org/draft-07/schema#', type: 'object', properties: { credentialSubject: { type: 'array', items: [ { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } }, { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } }, { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } } ] } }, definitions: { encryptableString: { type: 'string' }, encryptableCompString: { type: 'string' } } } { '$schema': 'http://json-schema.org/draft-07/schema#', type: 'object', properties: { credentialSubject: { type: 'array', items: [ { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } }, { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } }, { type: 'object', properties: { name: { type: 'string' }, location: { type: 'object', properties: { name: { type: 'string' }, geo: { type: 'object', properties: { lat: { type: 'number', minimum: -90, multipleOf: 0.001 }, long: { type: 'number', minimum: -180, multipleOf: 0.001 } } } } } } } ] }, issuer: { type: 'object', properties: { name: { type: 'string' }, desc: { type: 'string' }, logo: { type: 'string' } } }, issuanceDate: { type: 'integer', minimum: 0 }, expirationDate: { type: 'integer', minimum: 0 } }, definitions: { encryptableString: { type: 'string' }, encryptableCompString: { type: 'string' } } } */ export declare const META_SCHEMA_STR = "$schema"; export interface ISchema { [CRYPTO_VERSION_STR]: object; [SCHEMA_STR]: object; [SUBJECT_STR]: object | object[]; [STATUS_STR]?: object; [key: string]: object; } export declare enum ValueType { Str = 0, RevStr = 1, PositiveInteger = 2, Integer = 3, PositiveNumber = 4, Number = 5 } export interface StringType { type: ValueType.Str; } export interface ReversibleStringType { type: ValueType.RevStr; compress: boolean; } export interface PositiveIntegerType { type: ValueType.PositiveInteger; } export interface IntegerType { type: ValueType.Integer; minimum: number; } export interface PositiveNumberType { type: ValueType.PositiveNumber; decimalPlaces: number; } export interface NumberType { type: ValueType.Number; minimum: number; decimalPlaces: number; } export type ValueTypes = StringType | ReversibleStringType | PositiveIntegerType | IntegerType | PositiveNumberType | NumberType; export interface IJsonSchemaProperties { [SUBJECT_STR]: object | object[]; [STATUS_STR]?: object; [key: string]: object; } /** * JSON schema that contains the properties */ export interface IEmbeddedJsonSchema { [META_SCHEMA_STR]: string; $id?: string; title?: string; type: string; [SCHEMA_PROPS_STR]: IJsonSchemaProperties; definitions?: { [key: string]: object; }; } /** * JSON schema that does not contain the properties but its $id property can be used to fetch the properties. * Intentionally not allowing `properties` key as reconciliation will be needed in case of conflict with fetched properties */ export interface IJsonSchema { [META_SCHEMA_STR]: string; $id: string; title?: string; type: string; } export interface ISchemaParsingOpts { /** Whether to use the default values or throw error if required parameters are not passed */ useDefaults: boolean; defaultMinimumInteger: number; defaultMinimumDate: number; defaultDecimalPlaces: number; } export declare const DefaultSchemaParsingOpts: ISchemaParsingOpts; export interface ISchemaOverrides { version: string; } export type CredVal = string | number | object | CredVal[]; export declare class CredentialSchema extends Versioned { /** Follows semver and must be updated accordingly when the logic of this class changes or the underlying crypto changes. */ static VERSION: string; private static readonly STR_TYPE; private static readonly STR_REV_TYPE; private static readonly POSITIVE_INT_TYPE; private static readonly BOOLEAN_TYPE; private static readonly INT_TYPE; private static readonly POSITIVE_NUM_TYPE; private static readonly NUM_TYPE; private static readonly DATETIME_TYPE; /** CredentialBuilder subject/claims cannot have any of these names */ static RESERVED_NAMES: Set; /** Implicit fields for schema version < 0.4.0 */ static OLD_IMPLICIT_FIELDS: { cryptoVersion: { type: string; }; credentialSchema: { type: string; }; }; /** Implicit fields for schema version >= 0.4.0 */ static IMPLICIT_FIELDS: { cryptoVersion: { type: string; }; credentialSchema: { id: { type: string; }; type: { type: string; }; version: { type: string; }; details: { type: string; }; }; }; /** Custom definitions for JSON schema syntax */ static JSON_SCHEMA_CUSTOM_DEFS: { encryptableString: { type: string; }; encryptableCompString: { type: string; }; }; /** Custom override definitions for JSON schema syntax. Any refs in the jsonschema that reference these will be overwritten */ static JSON_SCHEMA_OVERRIDE_DEFS: { '#/definitions/encryptableString': { type: string; compress: boolean; }; '#/definitions/encryptableCompString': { type: string; compress: boolean; }; }; /** Keys to ignore from generic validation as they are already validated */ static IGNORE_GENERIC_VALIDATION: Set; static POSSIBLE_TYPES: Set; readonly schema: ISchema; readonly jsonSchema: IEmbeddedJsonSchema | IJsonSchema; readonly parsingOptions: ISchemaParsingOpts; encoder: Encoder; fullJsonSchema?: IEmbeddedJsonSchema; /** * Takes a schema object as per JSON-schema syntax (`IJsonSchema`), validates it and converts it to an internal * representation (`ISchema`) and stores both as the one with JSON-schema syntax is added to the credential representation. * @param jsonSchema - Could be a JSON schema with properties or contain an $id key which is used to fetch them * @param parsingOpts - Options to parse the schema like whether to use defaults and what defaults to use * @param addMissingParsingOpts - Whether to update `parsingOpts` for any missing options with default options. Pass false * when deserializing to get the exact object that was serialized which is necessary when verifying signatures * @param overrides - Override any properties of the schema * @param fullJsonSchema - When `jsonSchema` does not contain the properties, this object is expected to contain them. * @param useConstantTimeEncoder - Set to false when creating legacy schemas */ constructor(jsonSchema: IEmbeddedJsonSchema | IJsonSchema, parsingOpts?: Partial, addMissingParsingOpts?: boolean, overrides?: Partial, fullJsonSchema?: IEmbeddedJsonSchema, useConstantTimeEncoder?: boolean); /** * Initialize the encoder as per the internal representation of schema, i.e. `ISchema` */ initEncoder(useConstantTimeEncoder?: boolean): void; /** * Validates the internal representation of schema * @param schema */ static validate(schema: ISchema): void; static validateGeneric(schema: object, ignoreKeys?: Set): void; typeOfName(name: string, flattenedSchema?: FlattenedSchema): ValueTypes; static typeOfName(name: string, flattenedSchema: FlattenedSchema): ValueTypes; static typeOfValue(value: object): ValueTypes; /** * Essential properties of a non-embedded schema. * @param withDefinitions - add custom definitions as well */ static essential(withDefinitions?: boolean): IEmbeddedJsonSchema; static statusAsJsonSchema(): object; flatten(): FlattenedSchema; hasStatus(): boolean; /** * Older version of toJSON, i.e. versions < 0.4.0 */ toJSONOlder(): object; toJSON(): object; static fromJSON(j: object): CredentialSchema; /** * Similar to this.fromJSON but can load an externally referenced schema if the given schema is not an embedded one. * @param j * @param schemaGetter */ static fromJSONWithPotentiallyExternalSchema(j: object, schemaGetter: (url: string) => Promise): Promise; /** * Convert to a JSON string and the string is deterministic. This is important for signing */ toJsonString(): string; /** * Convert schema JSON to a data URI * @param jsonSchema * @param version - The schema version. This is needed as a different conversion to JSON function was used in * older version and backward compatibility is needed. */ static convertToDataUri(jsonSchema: IEmbeddedJsonSchema | IJsonSchema, version?: string): string; static convertFromDataUri(embedded: string): IEmbeddedJsonSchema | IJsonSchema; /** * Same as the constructor of this class but gets the JSON schema from a callback * @param jsonSchema - The JSON schema that contains the URL to fetch the full JSON schema, i.e. properties * @param schemaGetter - The callback that takes the `$id` field of `jsonSchema` and returns the full JSON schema. * @param parsingOpts * @param addMissingParsingOpts * @param overrides */ static newSchemaFromExternal(jsonSchema: IJsonSchema, schemaGetter: (url: string) => Promise, parsingOpts?: Partial, addMissingParsingOpts?: boolean, overrides?: Partial): Promise; getJsonLdContext(): object; /** * Returns true if the JSON schema provided during the object creation was an embedded one. */ hasEmbeddedJsonSchema(): boolean; /** * Gets the embedded JSON schema either from the one that was provided or the one that was fetched. */ getEmbeddedJsonSchema(): IEmbeddedJsonSchema; getJsonSchemaProperties(): object; static getDummyContextValue(term: string): string; /** * Convert a schema object as per JSON-schema syntax (`IJsonSchema`) to the internal representation (`ISchema`). * Currently, does not check if the needed JSON-schema definitions are actually present but assumes that they will be * already passed. * @param inputNode * @param parsingOpts * @param nodeKeyName - Name of the node, used for throwing more informative error message * @param rootObject */ static convertToInternalSchemaObj(inputNode: any, parsingOpts: ISchemaParsingOpts, nodeKeyName?: string, rootObject?: object): object; static parseIntegerType(node: { minimum?: number; }, parsingOpts: ISchemaParsingOpts, nodeName: string): object; static parseDateType(node: { minimum?: number; }, parsingOpts: ISchemaParsingOpts): object; static parseBooleanType(): object; static parseNumberType(node: { minimum?: number; multipleOf: number; }, parsingOpts: ISchemaParsingOpts, nodeName: string): object; static getDecimalPlaces(d: number): number; static flattenSchemaObj(schema: object, versionGte040?: boolean): FlattenedSchema; /** * Generate a schema based on the credential and with the help of a schema that defines some fields. For fields with a * conflicting types between credential and schema, error will be thrown. For extra keys or items in schema, they will be * removed in the returned schema. The missing keys or items in schema will be added in the returned schema. * @param cred * @param schema */ static generateAppropriateSchema(cred: object, schema: CredentialSchema): CredentialSchema; /** * Get type and format of the credential value * @param value * @param schemaVersion - Schema version for which the parsing rules should apply * @private */ private static getTypeAndFormat; /** * Get sub-schema for the credential value * @param value * @param schemaVersion - Schema version for which the parsing rules should apply * @private */ private static getSubschema; /** * Update given JSON-schema properties based on the given credential object. * @param cred * @param schemaProps - These will be updated based on the credential subject. * @param schemaVersion - Schema version for which the schema generation logic should apply * @private */ private static generateFromCredential; private static validateStringType; /** * Returns true if the given object is an embedded schema, i.e. it has the `properties` key set. * @param obj */ static isEmbeddedJsonSchema(obj: IEmbeddedJsonSchema | IJsonSchema): boolean; } export declare function getTransformedMinMax(name: string, valTyp: ValueTypes, min: number, max: number): [number, number]; //# sourceMappingURL=schema.d.ts.map