@extrimian/agent 0.12.0-1 → 0.12.0-11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dist/agent.d.ts +77 -77
  2. package/dist/agent.js +173 -173
  3. package/dist/agent.js.map +1 -1
  4. package/dist/data-share-behaviours/data-share-behavior.d.ts +4 -4
  5. package/dist/data-share-behaviours/data-share-behavior.js +2 -2
  6. package/dist/data-share-behaviours/identity-data-share-behavior.d.ts +16 -16
  7. package/dist/data-share-behaviours/identity-data-share-behavior.js +2 -2
  8. package/dist/data-share-behaviours/identity-plaintext-data-share-behavior.d.ts +5 -5
  9. package/dist/data-share-behaviours/identity-plaintext-data-share-behavior.js +15 -15
  10. package/dist/data-share-behaviours/vcs/vc-share-behavior.d.ts +12 -12
  11. package/dist/data-share-behaviours/vcs/vc-share-behavior.js +2 -2
  12. package/dist/data-share-behaviours/vcs/vc-share-didcomm-behavior.d.ts +20 -20
  13. package/dist/data-share-behaviours/vcs/vc-share-didcomm-behavior.js +29 -29
  14. package/dist/did/did-services.d.ts +14 -14
  15. package/dist/did/did-services.js +14 -14
  16. package/dist/exceptions/agent-plugin-not-found.d.ts +2 -2
  17. package/dist/exceptions/agent-plugin-not-found.js +6 -6
  18. package/dist/exceptions/vc-protocol-not-found.d.ts +3 -3
  19. package/dist/exceptions/vc-protocol-not-found.js +11 -11
  20. package/dist/index.d.ts +32 -32
  21. package/dist/index.js +60 -60
  22. package/dist/messaging/messaging.d.ts +41 -41
  23. package/dist/messaging/messaging.js +90 -88
  24. package/dist/messaging/messaging.js.map +1 -1
  25. package/dist/models/agent-identity.d.ts +86 -86
  26. package/dist/models/agent-identity.js +399 -399
  27. package/dist/models/agent-identity.js.map +1 -1
  28. package/dist/models/agent-kms.d.ts +42 -42
  29. package/dist/models/agent-kms.js +78 -78
  30. package/dist/models/agent-pbk.d.ts +11 -11
  31. package/dist/models/agent-pbk.js +11 -11
  32. package/dist/models/agent-registry.d.ts +95 -94
  33. package/dist/models/agent-registry.js +130 -129
  34. package/dist/models/agent-registry.js.map +1 -1
  35. package/dist/models/agent-resolver-cache.d.ts +21 -21
  36. package/dist/models/agent-resolver-cache.js +47 -47
  37. package/dist/models/agent-resolver.d.ts +19 -19
  38. package/dist/models/agent-resolver.js +33 -33
  39. package/dist/models/agent-secure-storage.d.ts +3 -3
  40. package/dist/models/agent-secure-storage.js +2 -2
  41. package/dist/models/agent-secure-storages/filesystem-agent-secure-storage.d.ts +14 -14
  42. package/dist/models/agent-secure-storages/filesystem-agent-secure-storage.js +48 -48
  43. package/dist/models/agent-storage.d.ts +9 -9
  44. package/dist/models/agent-storage.js +2 -2
  45. package/dist/models/did.d.ts +19 -19
  46. package/dist/models/did.js +63 -63
  47. package/dist/models/transports/ami-transport.d.ts +30 -30
  48. package/dist/models/transports/ami-transport.js +183 -183
  49. package/dist/models/transports/connectable-transport.d.ts +25 -25
  50. package/dist/models/transports/connectable-transport.js +25 -25
  51. package/dist/models/transports/dwn-transport.d.ts +32 -32
  52. package/dist/models/transports/dwn-transport.js +191 -191
  53. package/dist/models/transports/messaging-transport.d.ts +3 -3
  54. package/dist/models/transports/messaging-transport.js +2 -2
  55. package/dist/models/transports/transport.d.ts +30 -30
  56. package/dist/models/transports/transport.js +7 -7
  57. package/dist/models/transports/websocket-client-transport.d.ts +5 -5
  58. package/dist/models/transports/websocket-client-transport.js +33 -33
  59. package/dist/models/transports/websocket-server-transport.d.ts +7 -7
  60. package/dist/models/transports/websocket-server-transport.js +21 -21
  61. package/dist/models/transports/websocket-transport.d.ts +30 -30
  62. package/dist/models/transports/websocket-transport.js +84 -84
  63. package/dist/models/vc-storage.d.ts +8 -8
  64. package/dist/models/vc-storage.js +2 -2
  65. package/dist/plugins/iplugin.d.ts +17 -17
  66. package/dist/plugins/iplugin.js +2 -2
  67. package/dist/plugins/istatus-list-plugin.d.ts +14 -14
  68. package/dist/plugins/istatus-list-plugin.js +2 -2
  69. package/dist/plugins/ivc-interceptor-plugin.d.ts +9 -9
  70. package/dist/plugins/ivc-interceptor-plugin.js +2 -2
  71. package/dist/plugins/plugin-dispatcher.d.ts +6 -6
  72. package/dist/plugins/plugin-dispatcher.js +19 -19
  73. package/dist/transport/transport.d.ts +43 -43
  74. package/dist/transport/transport.js +109 -109
  75. package/dist/utils/index.d.ts +1 -1
  76. package/dist/utils/index.js +14 -14
  77. package/dist/utils/lite-event.d.ts +25 -25
  78. package/dist/utils/lite-event.js +36 -36
  79. package/dist/vc/models/credentia-flow.d.ts +4 -4
  80. package/dist/vc/models/credentia-flow.js +8 -8
  81. package/dist/vc/models/credential-request.d.ts +3 -3
  82. package/dist/vc/models/credential-request.js +2 -2
  83. package/dist/vc/models/vc-create-key-request.d.ts +4 -4
  84. package/dist/vc/models/vc-create-key-request.js +2 -2
  85. package/dist/vc/models/waci-message.d.ts +3 -3
  86. package/dist/vc/models/waci-message.js +7 -7
  87. package/dist/vc/protocols/openid-protocol.d.ts +161 -161
  88. package/dist/vc/protocols/openid-protocol.js +125 -125
  89. package/dist/vc/protocols/vc-protocol.d.ts +106 -106
  90. package/dist/vc/protocols/vc-protocol.js +36 -36
  91. package/dist/vc/protocols/waci-protocol.d.ts +183 -183
  92. package/dist/vc/protocols/waci-protocol.js +552 -552
  93. package/dist/vc/vc.d.ts +225 -225
  94. package/dist/vc/vc.js +328 -328
  95. package/package.json +19 -20
  96. package/readme.md +392 -392
  97. package/dist/vc/vc-setup/vc-setup.d.ts +0 -0
@@ -1,553 +1,553 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.WACICredentialOfferSucceded = exports.WACICredentialOfferRejected = exports.CredentialApplicationResponse = exports.WACICredentialOfferResult = exports.CredentialRequestedEventArg = exports.WACIEventArg = exports.SelectiveDisclosure = exports.InternalStorageEnum = exports.WACIRequest = exports.WACIProtocol = void 0;
27
- const waci_1 = require("@extrimian/waci");
28
- const base_64_1 = require("base-64");
29
- const jsonpath = __importStar(require("jsonpath"));
30
- const jsonschema = __importStar(require("jsonschema"));
31
- const did_1 = require("../../models/did");
32
- const utils_1 = require("../../utils");
33
- const credentia_flow_1 = require("../models/credentia-flow");
34
- const vc_protocol_1 = require("./vc-protocol");
35
- const waci_2 = require("@extrimian/waci");
36
- class WACIProtocol extends vc_protocol_1.VCProtocol {
37
- constructor(params) {
38
- var _a, _b, _c, _d, _e, _f, _g;
39
- super();
40
- this.validateSchema = (vc, inputDescriptor) => {
41
- var _a;
42
- for (const field of inputDescriptor.constraints.fields) {
43
- const fieldValues = (_a = field.path) === null || _a === void 0 ? void 0 : _a.map((path) => {
44
- return jsonpath.value(vc, path);
45
- });
46
- for (const value of fieldValues) {
47
- if (!value)
48
- return false;
49
- if (field.filter) {
50
- const { errors } = jsonschema.validate(value, field.filter);
51
- if (errors.length) {
52
- return false;
53
- }
54
- }
55
- }
56
- }
57
- return true;
58
- };
59
- this.issueCredentials = (_a = params === null || params === void 0 ? void 0 : params.issuer) === null || _a === void 0 ? void 0 : _a.issueCredentials;
60
- this.issuerVerificationRules = (_b = params === null || params === void 0 ? void 0 : params.issuer) === null || _b === void 0 ? void 0 : _b.issuerVerificationRules;
61
- this.selectVcToPresent = (_c = params === null || params === void 0 ? void 0 : params.holder) === null || _c === void 0 ? void 0 : _c.selectVcToPresent;
62
- this.presentationDefinition = (_d = params === null || params === void 0 ? void 0 : params.verifier) === null || _d === void 0 ? void 0 : _d.presentationDefinition;
63
- this.credentialApplication = (_e = params === null || params === void 0 ? void 0 : params.holder) === null || _e === void 0 ? void 0 : _e.credentialApplication;
64
- this.credentialApplicationAsync = (_f = params === null || params === void 0 ? void 0 : params.holder) === null || _f === void 0 ? void 0 : _f.credentialApplicationAsync;
65
- this.businessVerificationRules = (_g = params.verifier) === null || _g === void 0 ? void 0 : _g.businessVerificationRules;
66
- this.storage = params === null || params === void 0 ? void 0 : params.storage;
67
- }
68
- initialize(params) {
69
- this.agent = params.agent;
70
- this.waciInterpreter = new waci_1.WACIInterpreter();
71
- if (this.issueCredentials) {
72
- this.waciInterpreter.setUpFor({
73
- getCredentialManifest: async (p) => {
74
- try {
75
- const result = await this.issueCredentials(p.invitationId, p.holderDid);
76
- if (result.result == WACICredentialOfferResult.Succeded) {
77
- const currentDID = !p.message ? null : this.agent.identity.getDIDs().find(x => x == p.message.to ||
78
- (Array.isArray(p.message.to) && p.message.to.some(y => y == x)));
79
- return {
80
- issuerDid: currentDID,
81
- issuerName: result.credentialManifest.issuer.name,
82
- output: result.credentialManifest.credentials.map(x => ({
83
- outputDescriptor: x.outputDescriptor,
84
- verifiableCredential: x.credential,
85
- format: "ldp_vc"
86
- })),
87
- issuerStyles: result.credentialManifest.issuer.styles,
88
- input: result.credentialManifest.inputDescriptors,
89
- frame: result.credentialManifest.frame,
90
- };
91
- }
92
- else if (result.result == WACICredentialOfferResult.Failed) {
93
- return {
94
- result: waci_1.OfferCredentialMessageResult.Failed,
95
- errorMessage: result.rejectMsg
96
- };
97
- }
98
- }
99
- catch (ex) {
100
- return {
101
- result: waci_1.OfferCredentialMessageResult.Failed,
102
- errorMessage: "Error: " + ex.message
103
- };
104
- }
105
- },
106
- signCredential: async (args) => {
107
- const data = await this.storage.get(args.message.thid);
108
- let invitationId = data && data.length > 0 && data[0].pthid ? data[0].pthid : null;
109
- const currentDID = !(args === null || args === void 0 ? void 0 : args.message) ? null : this.agent.identity.getDIDs().find(x => x == args.message.to ||
110
- (Array.isArray(args.message.to) && args.message.to.some(y => y == x)));
111
- const vc = await this.agent.vc.signVC({
112
- credential: args.vc,
113
- did: currentDID ? did_1.DID.from(currentDID) : null
114
- });
115
- this.onCredentialIssued.trigger({ vc: vc, toDID: did_1.DID.from(vc.credentialSubject.id), invitationId });
116
- return vc;
117
- },
118
- credentialVerificationResult: async (p) => {
119
- const data = await this.storage.get(p.thid);
120
- const m = data[0];
121
- const invitationId = data[0].pthid;
122
- let issuerVerification = null;
123
- if (this.issuerVerificationRules) {
124
- issuerVerification = await this.issuerVerificationRules(invitationId, m.from);
125
- }
126
- const verified = (!issuerVerification && p.result) || (issuerVerification.verified && p.result);
127
- if (p.vcs && p.vcs.length > 0) {
128
- this.onPresentationVerified.trigger({
129
- invitationId: invitationId,
130
- rejectMsg: verified ? null : (p.error || issuerVerification.rejectMsg),
131
- verified: verified,
132
- thid: p.thid,
133
- vcs: p.vcs,
134
- messageId: p.message.id,
135
- });
136
- }
137
- },
138
- verifyCredential: async (vc) => await this.agent.vc.verifyVC({
139
- vc: vc
140
- }),
141
- handleIssuanceAck: async (p) => {
142
- const data = await this.storage.get(p.thid);
143
- const invitationId = data[0].pthid;
144
- this.onAckCompleted.trigger({
145
- invitationId,
146
- status: p.status,
147
- messageId: p.message.id,
148
- role: vc_protocol_1.ActorRole.Issuer,
149
- thid: p.thid,
150
- });
151
- },
152
- verifyPresentation: async (vc) => await this.agent.vc.verifyPresentation({
153
- challenge: vc.challenge,
154
- presentation: vc.presentation
155
- }),
156
- }, waci_1.Actor.Issuer);
157
- }
158
- if (this.credentialApplication || this.selectVcToPresent || this.credentialApplicationAsync) {
159
- this.waciInterpreter.setUpFor({
160
- getHolderDID: async (p) => {
161
- const currentDID = !p.message ? null : this.agent.identity.getDIDs().find(x => x == p.message.to ||
162
- (Array.isArray(p.message.to) && p.message.to.some(y => y == x)));
163
- return currentDID || this.agent.identity.getOperationalDID().value;
164
- },
165
- getCredentialApplication: async (p) => {
166
- var _a, _b, _c, _d, _e, _f;
167
- if (this.credentialApplication || this.credentialApplicationAsync) {
168
- // Map the credential descriptors to the actual credentials
169
- const credential_manifests = await this.storage.get(InternalStorageEnum.CredentialManifests);
170
- if (!(credential_manifests === null || credential_manifests === void 0 ? void 0 : credential_manifests.find(x => x.id == p.manifest.data.json.credential_manifest.id))) {
171
- await this.storage.add(InternalStorageEnum.CredentialManifests, credential_manifests ? [...credential_manifests, p.manifest.data.json.credential_manifest] : [p.manifest.data.json.credential_manifest]);
172
- }
173
- // Map the credential descriptors to the actual credentials
174
- const credentialsToReceive = p.manifest.data.json.credential_manifest.output_descriptors.map((descriptor) => {
175
- const credentialDescriptor = p.fulfillment.data.json.credential_fulfillment.descriptor_map.find((map) => map.id === descriptor.id);
176
- return {
177
- data: jsonpath.value(p.fulfillment.data.json, credentialDescriptor.path),
178
- styles: descriptor.styles,
179
- display: descriptor.display
180
- };
181
- });
182
- // Get the credentials from the agent
183
- const credentials = await this.agent.vc.getVerifiableCredentialsWithInfo();
184
- // Filter the credentials based on the input descriptors
185
- const inputs = (((_a = p.manifest.data.json.credential_manifest.presentation_definition) === null || _a === void 0 ? void 0 : _a.input_descriptors) || []).map((descriptor) => {
186
- return {
187
- descriptor,
188
- credentials: (credentials || []).reduce((acc, credential) => {
189
- if (this.validateSchema(credential.data, descriptor)) {
190
- acc.push(credential);
191
- }
192
- return acc;
193
- }, [])
194
- };
195
- });
196
- const cs = inputs.flat();
197
- const output_descriptors = cs.map(x => x.credentials).flat();
198
- const selectiveDisclosure = !((_b = p.manifest.data.json.credential_manifest.presentation_definition) === null || _b === void 0 ? void 0 : _b.frame)
199
- && p.manifest.data.json.credential_manifest.output_descriptors ? null :
200
- SelectiveDisclosure.from(p.manifest.data.json.credential_manifest.presentation_definition.frame, output_descriptors.map(x => x));
201
- if (this.credentialApplicationAsync) {
202
- const data = await this.storage.get(p.message.thid);
203
- const invitationId = data[0].pthid;
204
- data.push(p.message);
205
- await this.storage.add(invitationId, p.message);
206
- await this.storage.update(p.message.thid, data);
207
- this.credentialApplicationAsync(inputs, invitationId, selectiveDisclosure, p.message);
208
- return waci_2.CredentialPresentationResponse.AsyncProcess;
209
- }
210
- // Apply the credential application
211
- let credentialsToPresent = await this.credentialApplication(inputs, selectiveDisclosure, p.message, p.manifest.data.json.credential_manifest.issuer, credentialsToReceive);
212
- if ((_c = p.manifest.data.json.credential_manifest.presentation_definition) === null || _c === void 0 ? void 0 : _c.frame) {
213
- const derivedVc = new Array();
214
- //Recorro todas las credenciales a presentar y las derivo (Se aplica selective disclosure)
215
- for (let vc of credentialsToPresent) {
216
- derivedVc.push(await this.agent.vc.deriveVC({
217
- vc: vc,
218
- deriveProofFrame: (_d = p.manifest.data.json.credential_manifest.presentation_definition) === null || _d === void 0 ? void 0 : _d.frame
219
- }));
220
- }
221
- credentialsToPresent = derivedVc;
222
- }
223
- return {
224
- credentialsToPresent: credentialsToPresent,
225
- presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"],
226
- };
227
- }
228
- else {
229
- if (!((_f = (_e = p.manifest.data.json.credential_manifest) === null || _e === void 0 ? void 0 : _e.presentation_definition) === null || _f === void 0 ? void 0 : _f.input_descriptors)) {
230
- return {
231
- credentialsToPresent: [],
232
- presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"]
233
- };
234
- }
235
- let credentials = await this.agent.vc.getVerifiableCredentials();
236
- credentials = credentials.filter(vc => {
237
- var _a, _b;
238
- return (0, waci_1.validateVcByInputDescriptor)(vc, (_b = (_a = p.manifest.data.json.credential_manifest) === null || _a === void 0 ? void 0 : _a.presentation_definition) === null || _b === void 0 ? void 0 : _b.input_descriptors[0]);
239
- });
240
- const credentialsToPresent = await this.selectVcToPresent(credentials);
241
- return {
242
- credentialsToPresent: credentialsToPresent,
243
- presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"],
244
- };
245
- }
246
- },
247
- getCredentialPresentation: async (p) => {
248
- if (this.credentialApplication || this.credentialApplicationAsync) {
249
- // Get the credentials from the agent
250
- const credentials = await this.agent.vc.getVerifiableCredentialsWithInfo();
251
- // Filter the credentials based on the input descriptors
252
- const inputs = (p.inputDescriptors || []).map((descriptor) => {
253
- return {
254
- descriptor,
255
- credentials: (credentials || []).reduce((acc, credential) => {
256
- if (this.validateSchema(credential.data, descriptor)) {
257
- acc.push(credential);
258
- }
259
- return acc;
260
- }, [])
261
- };
262
- });
263
- const cs = inputs.flat();
264
- const output_descriptors = cs.map(x => x.credentials).flat();
265
- const selectiveDisclosure = !(p === null || p === void 0 ? void 0 : p.frame) ? null :
266
- SelectiveDisclosure.from(p.frame, output_descriptors.map(x => x));
267
- if (this.credentialApplicationAsync) {
268
- const data = await this.storage.get(p.message.thid);
269
- const invitationId = data[0].pthid;
270
- data.push(p.message);
271
- await this.storage.add(invitationId, p.message);
272
- await this.storage.update(p.message.thid, data);
273
- this.credentialApplicationAsync(inputs, invitationId, selectiveDisclosure, p.message);
274
- return waci_2.CredentialPresentationResponse.AsyncProcess;
275
- }
276
- let result = await this.credentialApplication(inputs, selectiveDisclosure, p.message);
277
- if (p.frame) {
278
- const derivedVc = new Array();
279
- //Recorro todas las credenciales a presentar y las derivo (Se aplica selective disclosure)
280
- for (let vc of result) {
281
- derivedVc.push(await this.agent.vc.deriveVC({
282
- vc: vc,
283
- deriveProofFrame: p === null || p === void 0 ? void 0 : p.frame
284
- }));
285
- }
286
- result = derivedVc;
287
- }
288
- return { credentialsToPresent: result };
289
- }
290
- else {
291
- let credentials = await this.agent.vc.getVerifiableCredentials();
292
- credentials = credentials.filter(vc => (0, waci_1.validateVcByInputDescriptor)(vc, p.inputDescriptors[0]));
293
- const credentialsToPresent = await this.selectVcToPresent(credentials);
294
- return {
295
- credentialsToPresent: credentialsToPresent,
296
- };
297
- }
298
- },
299
- handleCredentialFulfillment: async (p) => {
300
- var _a;
301
- const credentialManifests = await this.storage.get(InternalStorageEnum.CredentialManifests);
302
- const credentialManifest = credentialManifests.find(x => x.id === p.credentialFulfillment[0].data.json.credential_fulfillment.manifest_id);
303
- await this.storage.add(InternalStorageEnum.CredentialManifests, credentialManifests.filter(x => x.id !== p.credentialFulfillment[0].data.json.credential_fulfillment.manifest_id));
304
- const credentials = credentialManifest.output_descriptors.map((descriptor) => {
305
- const credentialDescriptor = p.credentialFulfillment[0].data.json.credential_fulfillment.descriptor_map.find((map) => map.id === descriptor.id);
306
- return {
307
- data: jsonpath.value(p.credentialFulfillment[0].data.json, credentialDescriptor.path),
308
- styles: descriptor.styles,
309
- display: descriptor.display,
310
- };
311
- });
312
- this.onVcArrived.trigger({ credentials, issuer: credentialManifest.issuer, messageId: (_a = p.message) === null || _a === void 0 ? void 0 : _a.id });
313
- return true;
314
- },
315
- handlePresentationAck: async (p) => {
316
- if (p.message.type !== "https://didcomm.org/report-problem/2.0/problem-report") {
317
- const data = await this.storage.get(p.message.thid);
318
- // const ot = await this.storage.get(data[0].thid);
319
- // const ot2 = await this.storage.get(data[0].id);
320
- const invitationId = data[0].pthid;
321
- this.onAckCompleted.trigger({
322
- status: p.status,
323
- role: vc_protocol_1.ActorRole.Holder,
324
- messageId: p.message.id,
325
- thid: p.message.thid,
326
- invitationId,
327
- });
328
- }
329
- },
330
- signPresentation: async (p) => {
331
- const signature = await this.agent.vc.signPresentation({
332
- contentToSign: p.contentToSign,
333
- challenge: p.challenge,
334
- domain: p.domain,
335
- });
336
- return signature;
337
- },
338
- }, waci_1.Actor.Holder);
339
- }
340
- if (this.presentationDefinition) {
341
- this.waciInterpreter.setUpFor({
342
- getPresentationDefinition: async (p) => {
343
- const pDef = await this.presentationDefinition(p.invitationId);
344
- return {
345
- inputDescriptors: pDef.inputDescriptors,
346
- frame: pDef.frame
347
- };
348
- // return this.presentationDefinition(p.invitationId);
349
- },
350
- credentialVerificationResult: async (p) => {
351
- var _a;
352
- const data = await this.storage.get(p.thid);
353
- const invitationId = data[0].pthid;
354
- this.onPresentationVerified.trigger({
355
- invitationId: invitationId,
356
- verified: p.result,
357
- thid: p.thid,
358
- vcs: p.vcs,
359
- messageId: (_a = p.message) === null || _a === void 0 ? void 0 : _a.id
360
- });
361
- },
362
- verifyCredential: async (vc) => {
363
- const result = await this.agent.vc.verifyVC({ vc: vc });
364
- this.onVcVerified.trigger({
365
- verified: result.result,
366
- presentationVerified: true,
367
- vc: vc,
368
- });
369
- return result;
370
- },
371
- verifyPresentation: async (p) => {
372
- return {
373
- result: true
374
- };
375
- // const data = await this.storage.get(p.message.thid);
376
- // const invitationId = data[0].pthid;
377
- // let result = await this.agent.vc.verifyPresentation({
378
- // presentation: p.presentation,
379
- // challenge: p.challenge,
380
- // });
381
- // if (result.result && this.businessVerificationRules) {
382
- // let bvrResult = await this.businessVerificationRules(invitationId, DID.from(p.holderDid), p.presentation);
383
- // if (bvrResult.result == false) {
384
- // result = { result: false, error: { name: "businessVerificationRules", description: bvrResult.rejectMessage, code: null } }
385
- // }
386
- // }
387
- // if (!result.result) {
388
- // this.onVcVerified.trigger({
389
- // verified: false,
390
- // presentationVerified: false,
391
- // vc: p.presentation,
392
- // });
393
- // }
394
- // return result;
395
- },
396
- }, waci_1.Actor.Verifier);
397
- }
398
- }
399
- async presentationProceed(invitationId, credentialsToPresent) {
400
- const message = await this.storage.get(invitationId);
401
- const data = await this.storage.get(message.thid);
402
- const response = await this.waciInterpreter.presentationProceed(data, credentialsToPresent, ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"]);
403
- return {
404
- to: did_1.DID.from(response.target),
405
- from: did_1.DID.from(response.message.from),
406
- message: (await this.agent.messaging.packMessage({
407
- message: response.message,
408
- to: did_1.DID.from(response.target),
409
- messageManagerCompatible: false,
410
- })).packedMessage
411
- };
412
- }
413
- async processMessage(message, context, did) {
414
- var _a, _b;
415
- if (typeof message == "string" && (0, utils_1.getSearchParam)('_oob', message)) {
416
- const oob = this.decodeMessage((0, utils_1.getSearchParam)('_oob', message));
417
- if (typeof oob === "string") {
418
- message = JSON.parse(oob);
419
- }
420
- }
421
- const waciMessage = message;
422
- let messages = waciMessage.thid ? await this.storage.get(waciMessage.thid) || new Array() : new Array();
423
- messages.push(waciMessage);
424
- const response = await this.waciInterpreter.processMessage(messages);
425
- if (response) {
426
- this.storage.add(response.message.id, [response.message]);
427
- }
428
- if (response) {
429
- response.message.from = (did === null || did === void 0 ? void 0 : did.value) || response.message.from;
430
- if (response === null || response === void 0 ? void 0 : response.message.thid) {
431
- messages.push(response.message);
432
- this.storage.update(response.message.thid, messages);
433
- }
434
- return {
435
- to: did_1.DID.from(response.target),
436
- message: (await this.agent.messaging.packMessage({
437
- message: response.message,
438
- to: did_1.DID.from(response.target),
439
- messageManagerCompatible: context === null || context === void 0 ? void 0 : context.messageManagerCompatible,
440
- })).packedMessage
441
- };
442
- }
443
- if (messages[messages.length - 1].type == waci_1.WACIMessageType.ProblemReport) {
444
- const problemReportMessage = messages[messages.length - 1];
445
- const data = await this.storage.get(problemReportMessage.thid);
446
- const ot = await this.storage.get(data[0].thid);
447
- const invitationId = ot ? ot[0].pthid : data.pthid;
448
- this.onProblemReport.trigger({
449
- did: did_1.DID.from(problemReportMessage.from),
450
- code: (_a = problemReportMessage.body) === null || _a === void 0 ? void 0 : _a.code,
451
- codeMessage: (_b = problemReportMessage.body) === null || _b === void 0 ? void 0 : _b.comment,
452
- messageId: waciMessage.id,
453
- invitationId: invitationId,
454
- });
455
- }
456
- }
457
- async createOBBInvitation(goalCode, did) {
458
- if (!did)
459
- throw new Error("You need set a did to createOOBInvitation");
460
- return await this.waciInterpreter.createOOBInvitation(did.value, goalCode);
461
- }
462
- async createInvitationMessage(flow, did) {
463
- return await this.createOBBInvitation(flow == credentia_flow_1.CredentialFlow.Issuance ? waci_1.GoalCode.Issuance : waci_1.GoalCode.Presentation, did);
464
- }
465
- async isProtocolMessage(message) {
466
- if (typeof message == "string" && (0, utils_1.getSearchParam)('_oob', message)) {
467
- const oob = this.decodeMessage((0, utils_1.getSearchParam)('_oob', message));
468
- if (typeof oob === "string") {
469
- message = JSON.parse(oob);
470
- }
471
- }
472
- return this.waciInterpreter.isWACIMessage(message);
473
- }
474
- decodeMessage(message) {
475
- try {
476
- return (0, base_64_1.decode)(message);
477
- }
478
- catch (error) {
479
- return null;
480
- }
481
- }
482
- }
483
- exports.WACIProtocol = WACIProtocol;
484
- var WACIRequest;
485
- (function (WACIRequest) {
486
- WACIRequest[WACIRequest["CredentialManifestRequested"] = 0] = "CredentialManifestRequested";
487
- })(WACIRequest = exports.WACIRequest || (exports.WACIRequest = {}));
488
- var InternalStorageEnum;
489
- (function (InternalStorageEnum) {
490
- InternalStorageEnum["CredentialManifests"] = "CredentialManifests";
491
- })(InternalStorageEnum = exports.InternalStorageEnum || (exports.InternalStorageEnum = {}));
492
- class SelectiveDisclosure {
493
- constructor() {
494
- }
495
- static from(frame, outputDescriptors) {
496
- const allFieldsToReveal = [];
497
- const credentialSubjectFieldsToReveal = [];
498
- // Para allFieldsToReveal
499
- for (const key in frame) {
500
- if (key !== '@context' && key !== 'credentialSubject') {
501
- allFieldsToReveal.push(key);
502
- }
503
- }
504
- // Para credentialSubjectFieldsToReveal
505
- for (const key in frame.credentialSubject) {
506
- if (key !== '@explicit' && key !== 'type') {
507
- const descriptor = outputDescriptors.find(descriptor => { var _a, _b; return (_b = (_a = descriptor.display) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.some(prop => prop.path.includes(`$.credentialSubject.${key}`)); });
508
- if (descriptor) {
509
- const property = descriptor.display.properties.find(prop => prop.path.includes(`$.credentialSubject.${key}`));
510
- credentialSubjectFieldsToReveal.push(property.label);
511
- }
512
- else {
513
- credentialSubjectFieldsToReveal.push(key); // usar la key directamente si no encontramos la descripción
514
- }
515
- }
516
- }
517
- const sd = new SelectiveDisclosure();
518
- sd.allFieldsToReveal = allFieldsToReveal;
519
- sd.credentialSubjectFieldsToReveal = credentialSubjectFieldsToReveal;
520
- return sd;
521
- }
522
- }
523
- exports.SelectiveDisclosure = SelectiveDisclosure;
524
- class WACIEventArg {
525
- }
526
- exports.WACIEventArg = WACIEventArg;
527
- class CredentialRequestedEventArg extends WACIEventArg {
528
- }
529
- exports.CredentialRequestedEventArg = CredentialRequestedEventArg;
530
- var WACICredentialOfferResult;
531
- (function (WACICredentialOfferResult) {
532
- WACICredentialOfferResult[WACICredentialOfferResult["Succeded"] = 0] = "Succeded";
533
- WACICredentialOfferResult[WACICredentialOfferResult["Failed"] = 1] = "Failed";
534
- WACICredentialOfferResult[WACICredentialOfferResult["AsyncProcess"] = 2] = "AsyncProcess";
535
- })(WACICredentialOfferResult = exports.WACICredentialOfferResult || (exports.WACICredentialOfferResult = {}));
536
- var CredentialApplicationResponse;
537
- (function (CredentialApplicationResponse) {
538
- CredentialApplicationResponse[CredentialApplicationResponse["AsyncProcess"] = 0] = "AsyncProcess";
539
- })(CredentialApplicationResponse = exports.CredentialApplicationResponse || (exports.CredentialApplicationResponse = {}));
540
- class WACICredentialOfferRejected {
541
- constructor() {
542
- this.result = WACICredentialOfferResult.Failed;
543
- }
544
- }
545
- exports.WACICredentialOfferRejected = WACICredentialOfferRejected;
546
- class WACICredentialOfferSucceded {
547
- constructor(credentialManifest) {
548
- this.result = WACICredentialOfferResult.Succeded;
549
- this.credentialManifest = credentialManifest;
550
- }
551
- }
552
- exports.WACICredentialOfferSucceded = WACICredentialOfferSucceded;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.WACICredentialOfferSucceded = exports.WACICredentialOfferRejected = exports.CredentialApplicationResponse = exports.WACICredentialOfferResult = exports.CredentialRequestedEventArg = exports.WACIEventArg = exports.SelectiveDisclosure = exports.InternalStorageEnum = exports.WACIRequest = exports.WACIProtocol = void 0;
27
+ const waci_1 = require("@extrimian/waci");
28
+ const base_64_1 = require("base-64");
29
+ const jsonpath = __importStar(require("jsonpath"));
30
+ const jsonschema = __importStar(require("jsonschema"));
31
+ const did_1 = require("../../models/did");
32
+ const utils_1 = require("../../utils");
33
+ const credentia_flow_1 = require("../models/credentia-flow");
34
+ const vc_protocol_1 = require("./vc-protocol");
35
+ const waci_2 = require("@extrimian/waci");
36
+ class WACIProtocol extends vc_protocol_1.VCProtocol {
37
+ constructor(params) {
38
+ var _a, _b, _c, _d, _e, _f, _g;
39
+ super();
40
+ this.validateSchema = (vc, inputDescriptor) => {
41
+ var _a;
42
+ for (const field of inputDescriptor.constraints.fields) {
43
+ const fieldValues = (_a = field.path) === null || _a === void 0 ? void 0 : _a.map((path) => {
44
+ return jsonpath.value(vc, path);
45
+ });
46
+ for (const value of fieldValues) {
47
+ if (!value)
48
+ return false;
49
+ if (field.filter) {
50
+ const { errors } = jsonschema.validate(value, field.filter);
51
+ if (errors.length) {
52
+ return false;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ return true;
58
+ };
59
+ this.issueCredentials = (_a = params === null || params === void 0 ? void 0 : params.issuer) === null || _a === void 0 ? void 0 : _a.issueCredentials;
60
+ this.issuerVerificationRules = (_b = params === null || params === void 0 ? void 0 : params.issuer) === null || _b === void 0 ? void 0 : _b.issuerVerificationRules;
61
+ this.selectVcToPresent = (_c = params === null || params === void 0 ? void 0 : params.holder) === null || _c === void 0 ? void 0 : _c.selectVcToPresent;
62
+ this.presentationDefinition = (_d = params === null || params === void 0 ? void 0 : params.verifier) === null || _d === void 0 ? void 0 : _d.presentationDefinition;
63
+ this.credentialApplication = (_e = params === null || params === void 0 ? void 0 : params.holder) === null || _e === void 0 ? void 0 : _e.credentialApplication;
64
+ this.credentialApplicationAsync = (_f = params === null || params === void 0 ? void 0 : params.holder) === null || _f === void 0 ? void 0 : _f.credentialApplicationAsync;
65
+ this.businessVerificationRules = (_g = params.verifier) === null || _g === void 0 ? void 0 : _g.businessVerificationRules;
66
+ this.storage = params === null || params === void 0 ? void 0 : params.storage;
67
+ }
68
+ initialize(params) {
69
+ this.agent = params.agent;
70
+ this.waciInterpreter = new waci_1.WACIInterpreter();
71
+ if (this.issueCredentials) {
72
+ this.waciInterpreter.setUpFor({
73
+ getCredentialManifest: async (p) => {
74
+ try {
75
+ const result = await this.issueCredentials(p.invitationId, p.holderDid);
76
+ if (result.result == WACICredentialOfferResult.Succeded) {
77
+ const currentDID = !p.message ? null : this.agent.identity.getDIDs().find(x => x == p.message.to ||
78
+ (Array.isArray(p.message.to) && p.message.to.some(y => y == x)));
79
+ return {
80
+ issuerDid: currentDID,
81
+ issuerName: result.credentialManifest.issuer.name,
82
+ output: result.credentialManifest.credentials.map(x => ({
83
+ outputDescriptor: x.outputDescriptor,
84
+ verifiableCredential: x.credential,
85
+ format: "ldp_vc"
86
+ })),
87
+ issuerStyles: result.credentialManifest.issuer.styles,
88
+ input: result.credentialManifest.inputDescriptors,
89
+ frame: result.credentialManifest.frame,
90
+ };
91
+ }
92
+ else if (result.result == WACICredentialOfferResult.Failed) {
93
+ return {
94
+ result: waci_1.OfferCredentialMessageResult.Failed,
95
+ errorMessage: result.rejectMsg
96
+ };
97
+ }
98
+ }
99
+ catch (ex) {
100
+ return {
101
+ result: waci_1.OfferCredentialMessageResult.Failed,
102
+ errorMessage: "Error: " + ex.message
103
+ };
104
+ }
105
+ },
106
+ signCredential: async (args) => {
107
+ const data = await this.storage.get(args.message.thid);
108
+ let invitationId = data && data.length > 0 && data[0].pthid ? data[0].pthid : null;
109
+ const currentDID = !(args === null || args === void 0 ? void 0 : args.message) ? null : this.agent.identity.getDIDs().find(x => x == args.message.to ||
110
+ (Array.isArray(args.message.to) && args.message.to.some(y => y == x)));
111
+ const vc = await this.agent.vc.signVC({
112
+ credential: args.vc,
113
+ did: currentDID ? did_1.DID.from(currentDID) : null
114
+ });
115
+ this.onCredentialIssued.trigger({ vc: vc, toDID: did_1.DID.from(vc.credentialSubject.id), invitationId });
116
+ return vc;
117
+ },
118
+ credentialVerificationResult: async (p) => {
119
+ const data = await this.storage.get(p.thid);
120
+ const m = data[0];
121
+ const invitationId = data[0].pthid;
122
+ let issuerVerification = null;
123
+ if (this.issuerVerificationRules) {
124
+ issuerVerification = await this.issuerVerificationRules(invitationId, m.from);
125
+ }
126
+ const verified = (!issuerVerification && p.result) || (issuerVerification.verified && p.result);
127
+ if (p.vcs && p.vcs.length > 0) {
128
+ this.onPresentationVerified.trigger({
129
+ invitationId: invitationId,
130
+ rejectMsg: verified ? null : (p.error || issuerVerification.rejectMsg),
131
+ verified: verified,
132
+ thid: p.thid,
133
+ vcs: p.vcs,
134
+ messageId: p.message.id,
135
+ });
136
+ }
137
+ },
138
+ verifyCredential: async (vc) => await this.agent.vc.verifyVC({
139
+ vc: vc
140
+ }),
141
+ handleIssuanceAck: async (p) => {
142
+ const data = await this.storage.get(p.thid);
143
+ const invitationId = data[0].pthid;
144
+ this.onAckCompleted.trigger({
145
+ invitationId,
146
+ status: p.status,
147
+ messageId: p.message.id,
148
+ role: vc_protocol_1.ActorRole.Issuer,
149
+ thid: p.thid,
150
+ });
151
+ },
152
+ verifyPresentation: async (vc) => await this.agent.vc.verifyPresentation({
153
+ challenge: vc.challenge,
154
+ presentation: vc.presentation
155
+ }),
156
+ }, waci_1.Actor.Issuer);
157
+ }
158
+ if (this.credentialApplication || this.selectVcToPresent || this.credentialApplicationAsync) {
159
+ this.waciInterpreter.setUpFor({
160
+ getHolderDID: async (p) => {
161
+ const currentDID = !p.message ? null : this.agent.identity.getDIDs().find(x => x == p.message.to ||
162
+ (Array.isArray(p.message.to) && p.message.to.some(y => y == x)));
163
+ return currentDID || this.agent.identity.getOperationalDID().value;
164
+ },
165
+ getCredentialApplication: async (p) => {
166
+ var _a, _b, _c, _d, _e, _f;
167
+ if (this.credentialApplication || this.credentialApplicationAsync) {
168
+ // Map the credential descriptors to the actual credentials
169
+ const credential_manifests = await this.storage.get(InternalStorageEnum.CredentialManifests);
170
+ if (!(credential_manifests === null || credential_manifests === void 0 ? void 0 : credential_manifests.find(x => x.id == p.manifest.data.json.credential_manifest.id))) {
171
+ await this.storage.add(InternalStorageEnum.CredentialManifests, credential_manifests ? [...credential_manifests, p.manifest.data.json.credential_manifest] : [p.manifest.data.json.credential_manifest]);
172
+ }
173
+ // Map the credential descriptors to the actual credentials
174
+ const credentialsToReceive = p.manifest.data.json.credential_manifest.output_descriptors.map((descriptor) => {
175
+ const credentialDescriptor = p.fulfillment.data.json.credential_fulfillment.descriptor_map.find((map) => map.id === descriptor.id);
176
+ return {
177
+ data: jsonpath.value(p.fulfillment.data.json, credentialDescriptor.path),
178
+ styles: descriptor.styles,
179
+ display: descriptor.display
180
+ };
181
+ });
182
+ // Get the credentials from the agent
183
+ const credentials = await this.agent.vc.getVerifiableCredentialsWithInfo();
184
+ // Filter the credentials based on the input descriptors
185
+ const inputs = (((_a = p.manifest.data.json.credential_manifest.presentation_definition) === null || _a === void 0 ? void 0 : _a.input_descriptors) || []).map((descriptor) => {
186
+ return {
187
+ descriptor,
188
+ credentials: (credentials || []).reduce((acc, credential) => {
189
+ if (this.validateSchema(credential.data, descriptor)) {
190
+ acc.push(credential);
191
+ }
192
+ return acc;
193
+ }, [])
194
+ };
195
+ });
196
+ const cs = inputs.flat();
197
+ const output_descriptors = cs.map(x => x.credentials).flat();
198
+ const selectiveDisclosure = !((_b = p.manifest.data.json.credential_manifest.presentation_definition) === null || _b === void 0 ? void 0 : _b.frame)
199
+ && p.manifest.data.json.credential_manifest.output_descriptors ? null :
200
+ SelectiveDisclosure.from(p.manifest.data.json.credential_manifest.presentation_definition.frame, output_descriptors.map(x => x));
201
+ if (this.credentialApplicationAsync) {
202
+ const data = await this.storage.get(p.message.thid);
203
+ const invitationId = data[0].pthid;
204
+ data.push(p.message);
205
+ await this.storage.add(invitationId, p.message);
206
+ await this.storage.update(p.message.thid, data);
207
+ this.credentialApplicationAsync(inputs, invitationId, selectiveDisclosure, p.message);
208
+ return waci_2.CredentialPresentationResponse.AsyncProcess;
209
+ }
210
+ // Apply the credential application
211
+ let credentialsToPresent = await this.credentialApplication(inputs, selectiveDisclosure, p.message, p.manifest.data.json.credential_manifest.issuer, credentialsToReceive);
212
+ if ((_c = p.manifest.data.json.credential_manifest.presentation_definition) === null || _c === void 0 ? void 0 : _c.frame) {
213
+ const derivedVc = new Array();
214
+ //Recorro todas las credenciales a presentar y las derivo (Se aplica selective disclosure)
215
+ for (let vc of credentialsToPresent) {
216
+ derivedVc.push(await this.agent.vc.deriveVC({
217
+ vc: vc,
218
+ deriveProofFrame: (_d = p.manifest.data.json.credential_manifest.presentation_definition) === null || _d === void 0 ? void 0 : _d.frame
219
+ }));
220
+ }
221
+ credentialsToPresent = derivedVc;
222
+ }
223
+ return {
224
+ credentialsToPresent: credentialsToPresent,
225
+ presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"],
226
+ };
227
+ }
228
+ else {
229
+ if (!((_f = (_e = p.manifest.data.json.credential_manifest) === null || _e === void 0 ? void 0 : _e.presentation_definition) === null || _f === void 0 ? void 0 : _f.input_descriptors)) {
230
+ return {
231
+ credentialsToPresent: [],
232
+ presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"]
233
+ };
234
+ }
235
+ let credentials = await this.agent.vc.getVerifiableCredentials();
236
+ credentials = credentials.filter(vc => {
237
+ var _a, _b;
238
+ return (0, waci_1.validateVcByInputDescriptor)(vc, (_b = (_a = p.manifest.data.json.credential_manifest) === null || _a === void 0 ? void 0 : _a.presentation_definition) === null || _b === void 0 ? void 0 : _b.input_descriptors[0]);
239
+ });
240
+ const credentialsToPresent = await this.selectVcToPresent(credentials);
241
+ return {
242
+ credentialsToPresent: credentialsToPresent,
243
+ presentationProofTypes: ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"],
244
+ };
245
+ }
246
+ },
247
+ getCredentialPresentation: async (p) => {
248
+ if (this.credentialApplication || this.credentialApplicationAsync) {
249
+ // Get the credentials from the agent
250
+ const credentials = await this.agent.vc.getVerifiableCredentialsWithInfo();
251
+ // Filter the credentials based on the input descriptors
252
+ const inputs = (p.inputDescriptors || []).map((descriptor) => {
253
+ return {
254
+ descriptor,
255
+ credentials: (credentials || []).reduce((acc, credential) => {
256
+ if (this.validateSchema(credential.data, descriptor)) {
257
+ acc.push(credential);
258
+ }
259
+ return acc;
260
+ }, [])
261
+ };
262
+ });
263
+ const cs = inputs.flat();
264
+ const output_descriptors = cs.map(x => x.credentials).flat();
265
+ const selectiveDisclosure = !(p === null || p === void 0 ? void 0 : p.frame) ? null :
266
+ SelectiveDisclosure.from(p.frame, output_descriptors.map(x => x));
267
+ if (this.credentialApplicationAsync) {
268
+ const data = await this.storage.get(p.message.thid);
269
+ const invitationId = data[0].pthid;
270
+ data.push(p.message);
271
+ await this.storage.add(invitationId, p.message);
272
+ await this.storage.update(p.message.thid, data);
273
+ this.credentialApplicationAsync(inputs, invitationId, selectiveDisclosure, p.message);
274
+ return waci_2.CredentialPresentationResponse.AsyncProcess;
275
+ }
276
+ let result = await this.credentialApplication(inputs, selectiveDisclosure, p.message);
277
+ if (p.frame) {
278
+ const derivedVc = new Array();
279
+ //Recorro todas las credenciales a presentar y las derivo (Se aplica selective disclosure)
280
+ for (let vc of result) {
281
+ derivedVc.push(await this.agent.vc.deriveVC({
282
+ vc: vc,
283
+ deriveProofFrame: p === null || p === void 0 ? void 0 : p.frame
284
+ }));
285
+ }
286
+ result = derivedVc;
287
+ }
288
+ return { credentialsToPresent: result };
289
+ }
290
+ else {
291
+ let credentials = await this.agent.vc.getVerifiableCredentials();
292
+ credentials = credentials.filter(vc => (0, waci_1.validateVcByInputDescriptor)(vc, p.inputDescriptors[0]));
293
+ const credentialsToPresent = await this.selectVcToPresent(credentials);
294
+ return {
295
+ credentialsToPresent: credentialsToPresent,
296
+ };
297
+ }
298
+ },
299
+ handleCredentialFulfillment: async (p) => {
300
+ var _a;
301
+ const credentialManifests = await this.storage.get(InternalStorageEnum.CredentialManifests);
302
+ const credentialManifest = credentialManifests.find(x => x.id === p.credentialFulfillment[0].data.json.credential_fulfillment.manifest_id);
303
+ await this.storage.add(InternalStorageEnum.CredentialManifests, credentialManifests.filter(x => x.id !== p.credentialFulfillment[0].data.json.credential_fulfillment.manifest_id));
304
+ const credentials = credentialManifest.output_descriptors.map((descriptor) => {
305
+ const credentialDescriptor = p.credentialFulfillment[0].data.json.credential_fulfillment.descriptor_map.find((map) => map.id === descriptor.id);
306
+ return {
307
+ data: jsonpath.value(p.credentialFulfillment[0].data.json, credentialDescriptor.path),
308
+ styles: descriptor.styles,
309
+ display: descriptor.display,
310
+ };
311
+ });
312
+ this.onVcArrived.trigger({ credentials, issuer: credentialManifest.issuer, messageId: (_a = p.message) === null || _a === void 0 ? void 0 : _a.id });
313
+ return true;
314
+ },
315
+ handlePresentationAck: async (p) => {
316
+ if (p.message.type !== "https://didcomm.org/report-problem/2.0/problem-report") {
317
+ const data = await this.storage.get(p.message.thid);
318
+ // const ot = await this.storage.get(data[0].thid);
319
+ // const ot2 = await this.storage.get(data[0].id);
320
+ const invitationId = data[0].pthid;
321
+ this.onAckCompleted.trigger({
322
+ status: p.status,
323
+ role: vc_protocol_1.ActorRole.Holder,
324
+ messageId: p.message.id,
325
+ thid: p.message.thid,
326
+ invitationId,
327
+ });
328
+ }
329
+ },
330
+ signPresentation: async (p) => {
331
+ const signature = await this.agent.vc.signPresentation({
332
+ contentToSign: p.contentToSign,
333
+ challenge: p.challenge,
334
+ domain: p.domain,
335
+ });
336
+ return signature;
337
+ },
338
+ }, waci_1.Actor.Holder);
339
+ }
340
+ if (this.presentationDefinition) {
341
+ this.waciInterpreter.setUpFor({
342
+ getPresentationDefinition: async (p) => {
343
+ const pDef = await this.presentationDefinition(p.invitationId);
344
+ return {
345
+ inputDescriptors: pDef.inputDescriptors,
346
+ frame: pDef.frame
347
+ };
348
+ // return this.presentationDefinition(p.invitationId);
349
+ },
350
+ credentialVerificationResult: async (p) => {
351
+ var _a;
352
+ const data = await this.storage.get(p.thid);
353
+ const invitationId = data[0].pthid;
354
+ this.onPresentationVerified.trigger({
355
+ invitationId: invitationId,
356
+ verified: p.result,
357
+ thid: p.thid,
358
+ vcs: p.vcs,
359
+ messageId: (_a = p.message) === null || _a === void 0 ? void 0 : _a.id
360
+ });
361
+ },
362
+ verifyCredential: async (vc) => {
363
+ const result = await this.agent.vc.verifyVC({ vc: vc });
364
+ this.onVcVerified.trigger({
365
+ verified: result.result,
366
+ presentationVerified: true,
367
+ vc: vc,
368
+ });
369
+ return result;
370
+ },
371
+ verifyPresentation: async (p) => {
372
+ return {
373
+ result: true
374
+ };
375
+ // const data = await this.storage.get(p.message.thid);
376
+ // const invitationId = data[0].pthid;
377
+ // let result = await this.agent.vc.verifyPresentation({
378
+ // presentation: p.presentation,
379
+ // challenge: p.challenge,
380
+ // });
381
+ // if (result.result && this.businessVerificationRules) {
382
+ // let bvrResult = await this.businessVerificationRules(invitationId, DID.from(p.holderDid), p.presentation);
383
+ // if (bvrResult.result == false) {
384
+ // result = { result: false, error: { name: "businessVerificationRules", description: bvrResult.rejectMessage, code: null } }
385
+ // }
386
+ // }
387
+ // if (!result.result) {
388
+ // this.onVcVerified.trigger({
389
+ // verified: false,
390
+ // presentationVerified: false,
391
+ // vc: p.presentation,
392
+ // });
393
+ // }
394
+ // return result;
395
+ },
396
+ }, waci_1.Actor.Verifier);
397
+ }
398
+ }
399
+ async presentationProceed(invitationId, credentialsToPresent) {
400
+ const message = await this.storage.get(invitationId);
401
+ const data = await this.storage.get(message.thid);
402
+ const response = await this.waciInterpreter.presentationProceed(data, credentialsToPresent, ["JsonWebSignature2020", "EcdsaSecp256k1Signature2019"]);
403
+ return {
404
+ to: did_1.DID.from(response.target),
405
+ from: did_1.DID.from(response.message.from),
406
+ message: (await this.agent.messaging.packMessage({
407
+ message: response.message,
408
+ to: did_1.DID.from(response.target),
409
+ messageManagerCompatible: false,
410
+ })).packedMessage
411
+ };
412
+ }
413
+ async processMessage(message, context, did) {
414
+ var _a, _b;
415
+ if (typeof message == "string" && (0, utils_1.getSearchParam)('_oob', message)) {
416
+ const oob = this.decodeMessage((0, utils_1.getSearchParam)('_oob', message));
417
+ if (typeof oob === "string") {
418
+ message = JSON.parse(oob);
419
+ }
420
+ }
421
+ const waciMessage = message;
422
+ let messages = waciMessage.thid ? await this.storage.get(waciMessage.thid) || new Array() : new Array();
423
+ messages.push(waciMessage);
424
+ const response = await this.waciInterpreter.processMessage(messages);
425
+ if (response) {
426
+ this.storage.add(response.message.id, [response.message]);
427
+ }
428
+ if (response) {
429
+ response.message.from = (did === null || did === void 0 ? void 0 : did.value) || response.message.from;
430
+ if (response === null || response === void 0 ? void 0 : response.message.thid) {
431
+ messages.push(response.message);
432
+ this.storage.update(response.message.thid, messages);
433
+ }
434
+ return {
435
+ to: did_1.DID.from(response.target),
436
+ message: (await this.agent.messaging.packMessage({
437
+ message: response.message,
438
+ to: did_1.DID.from(response.target),
439
+ messageManagerCompatible: context === null || context === void 0 ? void 0 : context.messageManagerCompatible,
440
+ })).packedMessage
441
+ };
442
+ }
443
+ if (messages[messages.length - 1].type == waci_1.WACIMessageType.ProblemReport) {
444
+ const problemReportMessage = messages[messages.length - 1];
445
+ const data = await this.storage.get(problemReportMessage.thid);
446
+ const ot = await this.storage.get(data[0].thid);
447
+ const invitationId = ot ? ot[0].pthid : data.pthid;
448
+ this.onProblemReport.trigger({
449
+ did: did_1.DID.from(problemReportMessage.from),
450
+ code: (_a = problemReportMessage.body) === null || _a === void 0 ? void 0 : _a.code,
451
+ codeMessage: (_b = problemReportMessage.body) === null || _b === void 0 ? void 0 : _b.comment,
452
+ messageId: waciMessage.id,
453
+ invitationId: invitationId,
454
+ });
455
+ }
456
+ }
457
+ async createOBBInvitation(goalCode, did) {
458
+ if (!did)
459
+ throw new Error("You need set a did to createOOBInvitation");
460
+ return await this.waciInterpreter.createOOBInvitation(did.value, goalCode);
461
+ }
462
+ async createInvitationMessage(flow, did) {
463
+ return await this.createOBBInvitation(flow == credentia_flow_1.CredentialFlow.Issuance ? waci_1.GoalCode.Issuance : waci_1.GoalCode.Presentation, did);
464
+ }
465
+ async isProtocolMessage(message) {
466
+ if (typeof message == "string" && (0, utils_1.getSearchParam)('_oob', message)) {
467
+ const oob = this.decodeMessage((0, utils_1.getSearchParam)('_oob', message));
468
+ if (typeof oob === "string") {
469
+ message = JSON.parse(oob);
470
+ }
471
+ }
472
+ return this.waciInterpreter.isWACIMessage(message);
473
+ }
474
+ decodeMessage(message) {
475
+ try {
476
+ return (0, base_64_1.decode)(message);
477
+ }
478
+ catch (error) {
479
+ return null;
480
+ }
481
+ }
482
+ }
483
+ exports.WACIProtocol = WACIProtocol;
484
+ var WACIRequest;
485
+ (function (WACIRequest) {
486
+ WACIRequest[WACIRequest["CredentialManifestRequested"] = 0] = "CredentialManifestRequested";
487
+ })(WACIRequest = exports.WACIRequest || (exports.WACIRequest = {}));
488
+ var InternalStorageEnum;
489
+ (function (InternalStorageEnum) {
490
+ InternalStorageEnum["CredentialManifests"] = "CredentialManifests";
491
+ })(InternalStorageEnum = exports.InternalStorageEnum || (exports.InternalStorageEnum = {}));
492
+ class SelectiveDisclosure {
493
+ constructor() {
494
+ }
495
+ static from(frame, outputDescriptors) {
496
+ const allFieldsToReveal = [];
497
+ const credentialSubjectFieldsToReveal = [];
498
+ // Para allFieldsToReveal
499
+ for (const key in frame) {
500
+ if (key !== '@context' && key !== 'credentialSubject') {
501
+ allFieldsToReveal.push(key);
502
+ }
503
+ }
504
+ // Para credentialSubjectFieldsToReveal
505
+ for (const key in frame.credentialSubject) {
506
+ if (key !== '@explicit' && key !== 'type') {
507
+ const descriptor = outputDescriptors.find(descriptor => { var _a, _b; return (_b = (_a = descriptor.display) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.some(prop => prop.path.includes(`$.credentialSubject.${key}`)); });
508
+ if (descriptor) {
509
+ const property = descriptor.display.properties.find(prop => prop.path.includes(`$.credentialSubject.${key}`));
510
+ credentialSubjectFieldsToReveal.push(property.label);
511
+ }
512
+ else {
513
+ credentialSubjectFieldsToReveal.push(key); // usar la key directamente si no encontramos la descripción
514
+ }
515
+ }
516
+ }
517
+ const sd = new SelectiveDisclosure();
518
+ sd.allFieldsToReveal = allFieldsToReveal;
519
+ sd.credentialSubjectFieldsToReveal = credentialSubjectFieldsToReveal;
520
+ return sd;
521
+ }
522
+ }
523
+ exports.SelectiveDisclosure = SelectiveDisclosure;
524
+ class WACIEventArg {
525
+ }
526
+ exports.WACIEventArg = WACIEventArg;
527
+ class CredentialRequestedEventArg extends WACIEventArg {
528
+ }
529
+ exports.CredentialRequestedEventArg = CredentialRequestedEventArg;
530
+ var WACICredentialOfferResult;
531
+ (function (WACICredentialOfferResult) {
532
+ WACICredentialOfferResult[WACICredentialOfferResult["Succeded"] = 0] = "Succeded";
533
+ WACICredentialOfferResult[WACICredentialOfferResult["Failed"] = 1] = "Failed";
534
+ WACICredentialOfferResult[WACICredentialOfferResult["AsyncProcess"] = 2] = "AsyncProcess";
535
+ })(WACICredentialOfferResult = exports.WACICredentialOfferResult || (exports.WACICredentialOfferResult = {}));
536
+ var CredentialApplicationResponse;
537
+ (function (CredentialApplicationResponse) {
538
+ CredentialApplicationResponse[CredentialApplicationResponse["AsyncProcess"] = 0] = "AsyncProcess";
539
+ })(CredentialApplicationResponse = exports.CredentialApplicationResponse || (exports.CredentialApplicationResponse = {}));
540
+ class WACICredentialOfferRejected {
541
+ constructor() {
542
+ this.result = WACICredentialOfferResult.Failed;
543
+ }
544
+ }
545
+ exports.WACICredentialOfferRejected = WACICredentialOfferRejected;
546
+ class WACICredentialOfferSucceded {
547
+ constructor(credentialManifest) {
548
+ this.result = WACICredentialOfferResult.Succeded;
549
+ this.credentialManifest = credentialManifest;
550
+ }
551
+ }
552
+ exports.WACICredentialOfferSucceded = WACICredentialOfferSucceded;
553
553
  //# sourceMappingURL=waci-protocol.js.map