Skip to content

Commit

Permalink
feat: deprecate CType meta schema draft-01
Browse files Browse the repository at this point in the history
  • Loading branch information
rflechtner committed Jul 5, 2023
1 parent 544519f commit 14bec34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/config/src/ConfigService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Log Configuration', () => {
it('Tests the default Log Level', () => {
if (process.env.DEBUG === 'true') {
expect(testLogger.getLogLevel()).toEqual(LogLevel.Debug)
} else expect(testLogger.getLogLevel()).toEqual(LogLevel.Error)
} else expect(testLogger.getLogLevel()).toEqual(LogLevel.Warn)
})

it('modifies the Log Level of all Loggers to the desired Level', () => {
Expand All @@ -44,7 +44,7 @@ describe('Log Configuration', () => {

describe('Configuration Service', () => {
it('has configuration Object with default values', () => {
expect(ConfigService.get('logLevel')).toEqual(LogLevel.Error)
expect(ConfigService.get('logLevel')).toEqual(LogLevel.Warn)
expect(() => ConfigService.get('api')).toThrowErrorMatchingInlineSnapshot(
`"The blockchain API is not set. Did you forget to call \`Kilt.connect(…)\` or \`Kilt.init(…)\`?"`
)
Expand Down
15 changes: 9 additions & 6 deletions packages/config/src/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import {
} from 'typescript-logging'
import type { SubscriptionPromise } from '@kiltprotocol/types'

const DEFAULT_DEBUG_LEVEL =
typeof process !== 'undefined' &&
process.env?.DEBUG &&
process.env.DEBUG === 'true'
? LogLevel.Debug
: LogLevel.Error
const DEFAULT_DEBUG_LEVEL = (() => {
if (process?.env?.DEBUG === 'true') {
return LogLevel.Debug
}
if (process?.env?.NODE_ENV && process.env.NODE_ENV !== 'production') {
return LogLevel.Warn
}
return LogLevel.Error
})()

export type configOpts = {
api: ApiPromise
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/ctype/CType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ import {
CTypeModelV1,
} from './CType.schemas.js'

let notifyDeprecated: (cTypeId: ICType['$id']) => void = () => {
// do nothing
}
if (process?.env?.NODE_ENV && process.env.NODE_ENV !== 'production') {
const logger = ConfigService.LoggingFactory.getLogger('deprecated')
notifyDeprecated = (cTypeId) =>
logger.warn(
`Your application has processed the CType '${cTypeId}' which follows the meta schema '${CTypeModelDraft01.$id}'. This class of schemas has known issues that can result in unexpected properties being present in a credential. Consider switching to a CType based on meta schema ${CTypeModelV1.$id} which fixes this issue.`
)
}

/**
* Utility for (re)creating CType hashes. Sorts the schema and strips the $id property (which contains the CType hash) before stringifying.
*
Expand Down Expand Up @@ -136,6 +147,9 @@ export function verifyClaimAgainstSchema(
messages?: string[]
): void {
verifyObjectAgainstSchema(schema, CTypeModel, messages)
if (schema.$schema === CTypeModelDraft01.$id) {
notifyDeprecated(schema.$id)
}
verifyObjectAgainstSchema(claimContents, schema, messages)
}

Expand All @@ -162,6 +176,9 @@ export async function verifyStored(ctype: ICType): Promise<void> {
*/
export function verifyDataStructure(input: ICType): void {
verifyObjectAgainstSchema(input, CTypeModel)
if (input.$schema === CTypeModelDraft01.$id) {
notifyDeprecated(input.$id)
}
const idFromSchema = getIdForSchema(input)
if (idFromSchema !== input.$id) {
throw new SDKErrors.CTypeIdMismatchError(idFromSchema, input.$id)
Expand Down

0 comments on commit 14bec34

Please sign in to comment.