Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate CType meta schema draft-01 #778

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
weichweich marked this conversation as resolved.
Show resolved Hide resolved
})()

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