forked from OKEAMAH/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
251 lines (228 loc) · 7.16 KB
/
parser.ts
File metadata and controls
251 lines (228 loc) · 7.16 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
W3CCredential,
getSerializationAttrFromContext,
parseSerializationAttr,
getFieldSlotIndex,
findCredentialType,
CoreClaimCreationOptions,
CoreClaimParsedSlots,
CoreClaimSlotsPaths,
getSerializationAttrFromParsedContext,
parseCoreClaimSlots
} from '../../verifiable';
import { Claim as CoreClaim } from '@iden3/js-iden3-core';
import { Merklizer, Options } from '@iden3/js-jsonld-merklization';
import * as jsonld from 'jsonld/lib';
import * as ldcontext from 'jsonld/lib/context';
/**
*
* @deprecated The interface should not be used. Use CoreClaimParsingOptions from verifiable package instead.
* CoreClaimOptions is params for core claim parsing
*
* @public
* @interface CoreClaimOptions
*/
export type CoreClaimOptions = CoreClaimCreationOptions;
/**
* @deprecated The interface should not be used. Use CoreClaimParsedSlots from verifiable package instead.
* Parsed slots of core.Claim
*
* @public
* @interface ParsedSlots
*/
export type ParsedSlots = CoreClaimParsedSlots;
/**
* @deprecated The interface should not be used. Use CoreClaimSlotsPaths from verifiable package instead.
*/
export type SlotsPaths = CoreClaimSlotsPaths;
/**
* Serialization of data slots for the fields non-merklized claims
*
* @public
* @interface SerializationSchema
*/
export interface SerializationSchema {
indexDataSlotA: string;
indexDataSlotB: string;
valueDataSlotA: string;
valueDataSlotB: string;
}
/**
* schema metadata in the json credential schema
*
* @public
* @interface SchemaMetadata
*/
export interface SchemaMetadata {
uris: { [key: string]: string };
serialization?: SerializationSchema;
}
/**
* JSON credential Schema
*
* @public
* @interface Schema
*/
export interface JSONSchema {
$metadata: SchemaMetadata;
$schema: string;
type: string;
}
/**
* Parser can parse claim and schema data according to specification
*
* @public
* @class Parser
*/
export class Parser {
/**
* @deprecated The method should not be used. Use credential.toCoreClaim instead.
* ParseClaim creates core.Claim object from W3CCredential
*
* @param {W3CCredential} credential - Verifiable Credential
* @param {CoreClaimOptions} [opts] - options to parse core claim
* @returns `Promise<CoreClaim>`
*/
static async parseClaim(credential: W3CCredential, opts?: CoreClaimOptions): Promise<CoreClaim> {
return credential.toCoreClaim(opts);
}
/**
* @deprecated The method should not be used. Use findCredentialType from verifiable.
*/
static findCredentialType(mz: Merklizer): string {
return findCredentialType(mz);
}
/**
* @deprecated The method should not be used. Use credential.getSerializationAttr instead.
*
* Get `iden3_serialization` attr definition from context document either using
* type name like DeliverAddressMultiTestForked or by type id like
* urn:uuid:ac2ede19-b3b9-454d-b1a9-a7b3d5763100.
* */
static async getSerializationAttr(
credential: W3CCredential,
opts: Options,
tp: string
): Promise<string> {
const ldCtx = await jsonld.processContext(
ldcontext.getInitialContext({}),
credential['@context'],
opts
);
return Parser.getSerializationAttrFromParsedContext(ldCtx, tp);
}
/**
* @deprecated The method should not be used. Use getSerializationAttrFromContext from verifiable.
*
* Get `iden3_serialization` attr definition from context document either using
* type name like DeliverAddressMultiTestForked or by type id like
* urn:uuid:ac2ede19-b3b9-454d-b1a9-a7b3d5763100.
*
*/
static async getSerializationAttrFromContext(
context: object,
opts: Options,
tp: string
): Promise<string> {
return getSerializationAttrFromContext(context, opts, tp);
}
/**
* @deprecated The method should not be used. Use getSerializationAttrFromParsedContext from verifiable.
*
* */
static async getSerializationAttrFromParsedContext(
ldCtx: { mappings: Map<string, Record<string, unknown>> },
tp: string
): Promise<string> {
return getSerializationAttrFromParsedContext(ldCtx, tp);
}
/**
* @deprecated The method should not be used. Use parseSerializationAttr from verifiable.
*
*/
static parseSerializationAttr(serAttr: string): SlotsPaths {
return parseSerializationAttr(serAttr);
}
/**
*
* @deprecated The method should not be used. Use credential.parseSlots instead.
* ParseSlots converts payload to claim slots using provided schema
*
* @param {Merklizer} mz - Merklizer
* @param {W3CCredential} credential - Verifiable Credential
* @param {string} credentialType - credential type
* @returns `ParsedSlots`
*/
static async parseSlots(
mz: Merklizer,
credential: W3CCredential,
credentialType: string
): Promise<{ slots: ParsedSlots; nonMerklized: boolean }> {
const ldCtx = await jsonld.processContext(
ldcontext.getInitialContext({}),
credential['@context'],
mz.options
);
return parseCoreClaimSlots(ldCtx, mz, credentialType);
}
/**
* @deprecated The method should not be used. Use getFieldSlotIndex from verifiable.
*
* GetFieldSlotIndex return index of slot from 0 to 7 (each claim has by default 8 slots) for non-merklized claims
*
* @param {string} field - field name
* @param {Uint8Array} schemaBytes -json schema bytes
* @returns `number`
*/
static async getFieldSlotIndex(
field: string,
typeName: string,
schemaBytes: Uint8Array
): Promise<number> {
return getFieldSlotIndex(field, typeName, schemaBytes);
}
/**
* ExtractCredentialSubjectProperties return credential subject types from JSON schema
*
* @param {string | JSON} schema - JSON schema
* @returns `Promise<Array<string>>`
*/
static async extractCredentialSubjectProperties(schema: string): Promise<Array<string>> {
const parsedSchema = typeof schema === 'string' ? JSON.parse(schema) : schema;
const props = parsedSchema.properties?.credentialSubject?.properties;
if (!props) {
throw new Error('properties.credentialSubject.properties is not set');
}
// drop @id field
delete props['id'];
return Object.keys(props);
}
// /**
// * GetLdPrefixesByJSONSchema return possible credential types for JSON schema
// *
// * @param {string} schema - JSON schema
// * @returns `Promise<Map<string, string>>`
// */
// public static async getLdPrefixesByJSONSchema(schema: string): Promise<Map<string, string>> {
// const metadata = Parser.extractMetadata(schema);
// const ldURL = metadata.uris['jsonLdContext'];
// if (!ldURL) {
// throw new Error('jsonLdContext is not set');
// }
// const props = await Parser.extractCredentialSubjectProperties(schema);
// let jsonLdContext;
// try {
// const response = await fetch(ldURL);
// jsonLdContext = await response.json();
// } catch (e) {
// throw new Error(`failed to fetch jsonLdContext ${e}`);
// }
// let prefixes;
// try {
// prefixes = await LDParser.getPrefixes(jsonLdContext, false, props);
// } catch (e) {
// throw new Error(`failed to extract terms from jsonLdContext ${e}`);
// }
// return prefixes;
// }
}