-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentations.service.ts
198 lines (178 loc) · 5.62 KB
/
presentations.service.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
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
import { Injectable, Logger } from '@nestjs/common';
import { validateOrReject } from 'class-validator';
import { nanoid } from 'nanoid';
import { DBService } from 'src/db/db.service';
import { InvitationMessage } from '@aviarytech/didcomm-protocols.out-of-band';
import { mapValidationErrorsToMessages } from 'src/utils/errors';
import { sha256 } from 'src/utils/sha256';
import {
IDIFPresentationExchangeSubmission,
SUBMISSION_FORMATS,
} from '@aviarytech/dif-presentation-exchange';
import {
InputDescriptor,
PresentationDefinition,
PresentationRequest,
PRESENTATION_REQUEST_ROLES,
PRESENTATION_REQUEST_STATUSES,
} from './entities/presentation.entity';
import { DIDWebService } from 'src/didweb/didweb.service';
import { UpdatePresentationRequestDto } from './dto/update-presentation-request.dto';
import { ConfigService } from '@nestjs/config';
import { VerifiableCredential } from 'src/credentials/interfaces';
import { verifiable } from '@transmute/vc.js';
import { DocumentLoaderService } from 'src/documentLoader/documentLoader.service';
import { JsonWebKey, JsonWebKey2020, JWS } from '@aviarytech/crypto-core';
@Injectable()
export class PresentationsService {
constructor(
private db: DBService,
private log: Logger,
private didWeb: DIDWebService,
private config: ConfigService,
private documentLoader: DocumentLoaderService,
) {}
async createDefinition(
createPresentationDefinitionDto: {
input_descriptors: InputDescriptor[];
frame: object;
},
id?: string,
) {
const { input_descriptors, frame } = createPresentationDefinitionDto;
id = id ?? sha256(nanoid());
const definition = new PresentationDefinition(id, frame, input_descriptors);
await validateOrReject(definition, {
validationError: { target: false },
}).catch((e) => {
this.log.error('validation failed. errors: ', e);
throw new Error(mapValidationErrorsToMessages(e));
});
return await this.db.create({
'@type': 'PresentationDefinition',
'@id': id,
...JSON.parse(JSON.stringify(definition)),
});
}
async findOneDefinition(id: string) {
return await this.db.getById(id);
}
async findAllDefinitions() {
return await this.db.getAllByType('PresentationDefinition');
}
async createRequest(createRequest: {
id?: string;
definition: PresentationDefinition;
role: PRESENTATION_REQUEST_ROLES;
requester: string;
invitationId?: string;
status?: PRESENTATION_REQUEST_STATUSES;
}): Promise<PresentationRequest> {
let { definition, role, requester, invitationId, id, status } =
createRequest;
let url;
if (!invitationId) {
const invitation = new InvitationMessage(
this.didWeb.did,
this.didWeb.basePath + '/invitation',
'streamlined-vp',
);
url = invitation.url;
invitationId = invitation.payload.id;
}
id = id ?? invitationId;
const domain = this.config.get('HOST');
const presReq = new PresentationRequest(
id,
url,
sha256(nanoid()),
domain,
definition,
invitationId,
role,
requester,
status,
);
await validateOrReject(presReq, {
validationError: { target: false },
}).catch((e) => {
this.log.error('validation failed. errors: ', e);
throw new Error(mapValidationErrorsToMessages(e));
});
return await this.db.create({
'@type': 'PresentationRequest',
'@id': id,
...JSON.parse(JSON.stringify(presReq)),
});
}
async findOneRequest(id: string): Promise<PresentationRequest> {
return await this.db.getById(id);
}
async findAllRequests() {
return await this.db.getAllByType('PresentationRequest');
}
async findOneRequestByInvitationId(id: string): Promise<PresentationRequest> {
return await this.db.getByProps({
'@type': 'PresentationRequest',
invitationId: id,
});
}
async updatePresentationRequest(
id: string,
updatePresentationRequest: UpdatePresentationRequestDto,
): Promise<PresentationRequest> {
const request = await this.db.getById(id);
return await this.db.upsert({
id,
...request,
...updatePresentationRequest,
});
}
async createPresentation(
presentationRequestId: string,
derivedCredential: VerifiableCredential,
) {
const presentationRequest = await this.findOneRequest(
presentationRequestId,
);
const submission: IDIFPresentationExchangeSubmission = {
presentation_submission: {
id: sha256(nanoid()),
definition_id: presentationRequest.definition.id,
descriptor_map: [
{
id: 'input',
format: SUBMISSION_FORMATS.LinkedDataProof_VerifiablePresentation,
path: '$.verifiableCredential[0]',
},
],
},
};
const presentation = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://identity.foundation/presentation-exchange/submission/v1',
'https://w3id.org/security/suites/jws-2020/v1',
],
type: ['VerifiablePresentation', 'PresentationSubmission'],
holder: {
id: this.didWeb.did,
},
verifiableCredential: [derivedCredential],
...submission,
};
const result = await verifiable.presentation.create({
presentation,
format: ['vp'],
documentLoader: this.documentLoader.loader,
challenge: presentationRequest.challenge,
domain: presentationRequest.domain,
suite: new JWS.Suite({
key: await JsonWebKey.from(
(await this.didWeb.getAuthenticationKey()) as JsonWebKey2020,
),
}),
});
return result;
}
}