-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathsdr.ts
359 lines (326 loc) · 9.46 KB
/
sdr.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { ICredentialRequestInput } from '@veramo/selective-disclosure'
import { getAgent } from './setup.js'
import { Command } from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'
import { shortDate, shortDid } from './explore/utils.js'
import { VerifiableCredential } from '@veramo/core-types'
import { asArray, extractIssuer } from '@veramo/utils'
import fuzzy from 'fuzzy'
const sdr = new Command('sdr').description('Selective Disclosure Request')
sdr
.command('create', { isDefault: true })
.description('create Selective Disclosure Request')
.option('-q, --qrcode', 'Show qrcode')
.action(async (opts: { qrcode: boolean }, cmd: Command) => {
const agent = await getAgent(cmd.optsWithGlobals().config)
const identifiers = await agent.didManagerFind()
const knownDids = await agent.dataStoreORMGetIdentifiers()
const subjects = [...knownDids.map((id) => id.did)]
if (identifiers.length === 0) {
console.error('No dids')
process.exit()
}
const answers = await inquirer.prompt([
{
type: 'list',
name: 'iss',
choices: identifiers.map((item) => item.did),
message: 'Issuer DID',
},
{
name: 'sub',
message: 'Subject DID',
type: 'autocomplete',
pageSize: 15,
source: async (answers: any, input: string) => {
const res = fuzzy
.filter(input, subjects)
.map((el: any) => (typeof el === 'string' ? el : el.original))
return res
},
validate: (val) => {
if (val && typeof val !== 'string') {
val = val.value
}
if (!val || !val.startsWith('did:')) {
return "Subject DID does not start with 'did:'..."
}
return true
},
},
{
type: 'input',
name: 'tag',
message: 'Tag (threadId)',
},
])
let addMoreRequests = true
const claims = []
while (addMoreRequests) {
const answers2 = await inquirer.prompt([
{
type: 'input',
name: 'claimType',
message: 'Claim type',
default: 'name',
},
{
type: 'input',
name: 'reason',
message: 'Reason',
default: 'We need this to comply with local law',
},
{
type: 'list',
name: 'essential',
message: 'Is essential',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
{
type: 'list',
name: 'addIssuer',
message: 'Add accepted issuer?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
let addIssuer = answers2.addIssuer
const issuers = []
while (addIssuer) {
const issuerAnswers = await inquirer.prompt([
{
type: 'input',
name: 'did',
message: 'Issuer DID',
default: 'did:web:uport.me',
},
{
type: 'input',
name: 'url',
message: 'URL',
default: 'https://uport.me',
},
{
type: 'list',
name: 'addIssuer',
message: 'Add another accepted issuer?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
issuers.push({
did: issuerAnswers.did,
url: issuerAnswers.url,
})
addIssuer = issuerAnswers.addIssuer
}
const answers4 = await inquirer.prompt([
{
type: 'list',
name: 'addMore',
message: 'Add another claim?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
claims.push({
issuers: issuers,
essential: answers2.essential,
claimType: answers2.claimType,
reason: answers2.reason,
} as ICredentialRequestInput)
addMoreRequests = answers4.addMore
}
const answers5 = await inquirer.prompt([
{
type: 'list',
name: 'addCredentials',
message: 'Add profile credentials?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
const credentials = []
if (answers5.addCredentials) {
const vcs = await agent.dataStoreORMGetVerifiableCredentials({
where: [{ column: 'subject', value: [answers.iss] }],
})
const list: any = []
if (vcs.length > 0) {
for (const credential of vcs) {
list.push({
name:
JSON.stringify(credential.verifiableCredential.credentialSubject) +
' | Issuer: ' +
extractIssuer(credential.verifiableCredential),
value: credential.verifiableCredential.proof.jwt,
})
}
let addMoreCredentials = true
while (addMoreCredentials) {
const answers6 = await inquirer.prompt([
{
type: 'list',
name: 'credential',
choices: list,
message: 'Select credential',
},
{
type: 'list',
name: 'addMore',
message: 'Add another credential?',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
},
])
credentials.push(answers6.credential)
addMoreCredentials = answers6.addMore
}
}
}
const data: any = {
issuer: answers.iss,
claims,
credentials,
}
if (answers.tag !== '') {
data.tag = answers.tag
}
if (answers.sub !== '') {
data.subject = answers.sub
}
const jwt = await agent.createSelectiveDisclosureRequest({
data,
})
const { send } = await inquirer.prompt([
{
type: 'confirm',
name: 'send',
message: 'Send',
},
])
const msg_data = {
from: answers.iss,
to: answers.sub,
type: 'jwt',
body: jwt,
}
if (!send) {
await agent.handleMessage({ raw: JSON.stringify(msg_data), metaData: [{ type: 'cli' }], save: true })
} else if (answers.sub !== '') {
try {
const result = await agent.sendMessageDIDCommAlpha1({
data: msg_data,
})
console.log('Sent:', result)
} catch (e) {
console.error(e)
}
} else {
console.log('Subject not specified')
}
if (opts.qrcode) {
qrcode.generate(jwt)
} else {
console.dir(data, { depth: 10 })
console.log(`jwt: ${jwt}`)
}
})
sdr
.command('respond')
.description('respond to Selective Disclosure Request')
.action(async (opts: {}, cmd: Command) => {
const agent = await getAgent(cmd.optsWithGlobals().config)
const sdrMessages = await agent.dataStoreORMGetMessages({
where: [{ column: 'type', value: ['sdr'] }],
order: [{ column: 'createdAt', direction: 'DESC' }],
})
const list = sdrMessages.map((message) => ({
//FIXME
name:
shortDate(message.createdAt) +
' ' +
shortDid(message.from) +
' asking to share: ' +
// @ts-ignore
message.data?.claims?.map((claim) => claim.claimType).join(','),
value: message,
}))
const { message } = await inquirer.prompt([
{
type: 'list',
name: 'message',
choices: list,
message: 'Selective disclosure request',
},
])
const args: any = {
sdr: message.data,
}
if (message.to) {
args.did = message.to
}
const credentialsForSdr = await agent.getVerifiableCredentialsForSdr(args)
const questions = []
for (const item of credentialsForSdr) {
questions.push({
type: 'checkbox',
name: item.claimType + ' ' + (item.essential ? '(essential)' : '') + item.reason,
choices: item.credentials.map((c) => ({
name:
c.verifiableCredential.credentialSubject[item.claimType] +
' (' +
asArray(c.verifiableCredential.type || []).join(',') +
') issued by: ' +
extractIssuer(c.verifiableCredential) +
' ' +
shortDate(c.verifiableCredential.issuanceDate) +
' ago',
value: c,
})),
})
}
const answers = await inquirer.prompt(questions)
let selectedCredentials: Array<VerifiableCredential> = []
for (const questionName of Object.keys(answers)) {
selectedCredentials = selectedCredentials.concat(answers[questionName])
}
const verifiablePresentation = await agent.createVerifiablePresentation({
save: false,
presentation: {
holder: message.to,
verifier: [message.from],
tag: message.tag,
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
issuanceDate: new Date().toISOString(),
verifiableCredential: selectedCredentials,
},
proofFormat: 'jwt',
})
await agent.sendMessageDIDCommAlpha1({
save: true,
data: {
from: message.to,
to: message.from,
type: 'jwt',
body: verifiablePresentation.proof.jwt,
},
})
console.dir(verifiablePresentation, { depth: 10 })
})
export { sdr }