Skip to content

Commit

Permalink
feat: deprecate CType meta schema draft-01 (#778)
Browse files Browse the repository at this point in the history
* feat: deprecate CType meta schema draft-01
* fix: default to log level warn in non-production environments
  • Loading branch information
rflechtner authored Jul 13, 2023
1 parent 544519f commit 5be1875
Show file tree
Hide file tree
Showing 3 changed files with 40 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
17 changes: 11 additions & 6 deletions packages/config/src/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ 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 (typeof process !== 'undefined') {
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
27 changes: 27 additions & 0 deletions packages/core/src/ctype/CType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ import {
CTypeModelV1,
} from './CType.schemas.js'

let notifyDeprecated: (cTypeId: ICType['$id']) => void = () => {
// do nothing
}
if (
typeof process !== 'undefined' &&
process.env?.NODE_ENV &&
process.env.NODE_ENV !== 'production'
) {
const logger = ConfigService.LoggingFactory.getLogger('deprecated')
const alreadyNotified = new Set<ICType['$id']>()
notifyDeprecated = (cTypeId) => {
if (alreadyNotified.has(cTypeId)) {
return
}
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.`
)
alreadyNotified.add(cTypeId)
}
}

/**
* 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 +157,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 +186,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 5be1875

Please sign in to comment.