/
authentication-template.ts
42 lines (39 loc) · 1.7 KB
/
authentication-template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { AuthenticationTemplate } from '../lib.js';
import { ajvStandaloneJsonParse } from './ajv/ajv-utils.js';
// eslint-disable-next-line import/no-internal-modules
import AuthenticationTemplateValidator from './ajv/validate-authentication-template.js';
/**
* Safely parse and validate an authentication template, returning either an
* error message as a string or a valid {@link AuthenticationTemplate}. The
* template may be provided either as an untrusted JSON string or as a
* pre-parsed object.
*
* This method validates both the structure and the contents of a template:
* - All properties and sub-properties are verified to be of the expected type.
* - The template contains no unknown properties.
* - The ID of each entity, script, and scenario is confirmed to be unique.
* - Script IDs referenced by entities and other scripts (via `unlocks`) are
* confirmed to exist.
* - The derivation paths of each HdKey are validated against each other.
*
* This method does not validate the CashAssembly contents of scripts (by
* attempting compilation, evaluating {@link AuthenticationTemplateScriptTest}s,
* or testing scenario generation).
*
* @param untrustedJsonOrObject - the JSON string or object to validate as an
* authentication template
*/
export const importAuthenticationTemplate = (
untrustedJsonOrObject: unknown
): AuthenticationTemplate | string => {
const errorPrefix = `Authentication template import failed:`;
const template = ajvStandaloneJsonParse<AuthenticationTemplate>(
untrustedJsonOrObject,
AuthenticationTemplateValidator
);
if (typeof template === 'string') {
return `${errorPrefix}${template}`;
}
// TODO: add back other validation
return template;
};