From 32d4a2c95bde9ec9f4f99679f00ef71f00aa1896 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Fri, 17 Apr 2020 18:11:49 +0300 Subject: [PATCH] feat: Handling credentials in SDR message --- packages/daf-cli/src/sdr.ts | 59 +++++++++++++++++++ .../src/message-handler.ts | 20 +++++++ 2 files changed, 79 insertions(+) diff --git a/packages/daf-cli/src/sdr.ts b/packages/daf-cli/src/sdr.ts index 2c4a74344..3b0ec736d 100644 --- a/packages/daf-cli/src/sdr.ts +++ b/packages/daf-cli/src/sdr.ts @@ -127,6 +127,64 @@ program 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 Daf.Credential.find({ + where: { subject: answers.iss }, + relations: ['claims'], + }) + const list: any = [] + if (vcs.length > 0) { + for (const credential of vcs) { + const issuer = credential.issuer.shortDid() + const claims = [] + for (const claim of credential.claims) { + claims.push(claim.type + ' = ' + claim.value) + } + list.push({ + name: claims.join(', ') + ' | Issuer: ' + issuer, + value: credential.raw, + }) + } + + 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 signAction: SD.ActionSignSdr = { type: SD.ActionTypes.signSdr, data: { @@ -134,6 +192,7 @@ program subject: answers.sub === '' ? undefined : answers.sub, tag: answers.tag === '' ? undefined : answers.tag, claims, + credentials, }, } diff --git a/packages/daf-selective-disclosure/src/message-handler.ts b/packages/daf-selective-disclosure/src/message-handler.ts index fafdea556..d4c31fd71 100644 --- a/packages/daf-selective-disclosure/src/message-handler.ts +++ b/packages/daf-selective-disclosure/src/message-handler.ts @@ -33,6 +33,26 @@ export class SdrMessageHandler extends AbstractMessageHandler { message.threadId = message.data.tag message.createdAt = this.timestampToDate(message.data.nbf || message.data.iat) + + if ( + message.data.credentials && + Array.isArray(message.data.credentials) && + message.data.credentials.length > 0 + ) { + debug('Verifying included credentials') + message.credentials = [] + for (const raw of message.data.credentials) { + try { + const tmpMessage = await agent.handleMessage({ raw, save: false }) + if (tmpMessage.credentials) { + message.credentials = [...message.credentials, ...tmpMessage.credentials] + } + } catch (e) { + // Unsupported message type, or some other error + debug(e) + } + } + } return message }