Skip to content

Commit

Permalink
verification flow
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Aug 21, 2023
1 parent a9d92f7 commit bc24e02
Show file tree
Hide file tree
Showing 18 changed files with 592 additions and 73 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
"@babel/preset-flow": "^7.16.7",
"@babel/preset-typescript": "^7.22.5",
"@digitalbazaar/x25519-key-agreement-key-2020": "2.1.0",
"@inquirer/checkbox": "^1.3.8",
"@inquirer/input": "^1.2.8",
"@inquirer/prompts": "^3.0.3",
"@inquirer/select": "^1.2.8",
"@noble/hashes": "1.3.1",
"@react-native-async-storage/async-storage": "^1.15.17",
"@rollup/plugin-alias": "^4.0.2",
Expand All @@ -118,6 +122,7 @@
"babel-preset-es2015": "^6.24.1",
"bignumber.js": "^9.0.1",
"buffer": "^6.0.3",
"chalk": "^5.3.0",
"core-js": "^3.20.3",
"crypto-browserify": "^3.12.0",
"documentation": "^13.2.5",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ module.exports = {
'/node_modules/(?!@polkadot|@babel|@docknetwork|@digitalbazaar)',
],
};

116 changes: 101 additions & 15 deletions packages/cli/src/commands/verification.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,117 @@
import {Command} from 'commander';
import inquirer from 'inquirer';
import axios from 'axios';
import select, {Separator} from '@inquirer/select';
import checkbox from '@inquirer/checkbox';
import input from '@inquirer/input';
import {IWallet} from '@docknetwork/wallet-sdk-core/lib/types';
import {getWallet} from '../helpers';

import {createVerificationController} from '@docknetwork/wallet-sdk-core/src/verification-controller';
// import template from '@docknetwork/wallet-sdk-core/src/fixtures/iiw-template.json';
import {WalletEvents} from '@docknetwork/wallet-sdk-wasm/src/modules/wallet';
import clipboardy from 'clipboardy';
const verificationCommands = new Command('verification');

verificationCommands
.command('start')
.option('-t, --template <template>', 'Verification template')
.description('Start wallet verification flow')
.action(async (options) => {
.action(async options => {
const wallet: IWallet = await getWallet();

console.log(options.template);
// prompt for the verification template
// The following process must be implement in the core package, using the verificationProvider
// So that we can reuse it on the react native wallet as well
await wallet.waitForEvent(WalletEvents.networkConnected);

const controller = createVerificationController({
wallet,
});

let template = options.template;

if (!template) {
template = await input({
message:
'Enter the proof request template URL (leave it empty to copy a JSON from clipboard)',
});

if (!template) {
template = await clipboardy.read();
}
}
console.log('Starting verification flow...');
await controller.start({
template,
});

console.log('Proof Request Template JSON:');
console.log(JSON.stringify(controller.getTemplateJSON(), null, 2));

const filteredCredentials = await controller.getFilteredCredentials();
console.log('Credentials filtered');

if (!filteredCredentials.length) {
console.log('No credentials found for this proof request template');
return;
}

const credentialId = await select({
message: 'Select a credential',
choices: await Promise.all(
filteredCredentials.map(async item => {
const isBBS = await controller.isBBSPlusCredential(item);
return {
name: `Name: ${item.name}\n Is BBS: ${
isBBS ? 'Yes' : 'No'
}\n Type: ${JSON.stringify(item.type)}`,
value: item.id,
description: `Subject: ${JSON.stringify(item.credentialSubject)}`,
};
}),
),
});

const credential = filteredCredentials.find(
item => item.id === credentialId,
);
console.log(`Credential selected: ${credentialId}`);

let attributesToReveal = [];

// check if bbs
if (await controller.isBBSPlusCredential(credential)) {
const availableAttributes = Object.keys(credential.credentialSubject);

attributesToReveal = await checkbox({
message: 'Select the attributes to reveal',
choices: availableAttributes.map(key => {
return {
name: `${key}: ${credential.credentialSubject[key]}`,
value: `credentialSubject.${key}`,
};
}),
});

console.log(`Attributes selected: ${attributesToReveal.join(', ')}`);
}

controller.selectedCredentials.set(credentialId, {
credential: filteredCredentials.find(item => item.id === credentialId),
attributesToReveal,
});

console.log('Generating presentation...');

const presentation = await controller.createPresentation();

console.log('Presentation generated:');
console.log(JSON.stringify(presentation, null, 2));
console.log('Verifying presentation...');

// Logic:
// Execute pex filters
// show credentials matching
// user select credential
// if bbs, prompt for which attribute to share
// handle range proof here
const result = await controller.evaluatePresentation(presentation);

console.log('verify credentials');
if (result.isValid) {
console.log('Verification successful!');
} else {
console.log('Verification result:');
console.log(JSON.stringify(result, null, 2));
}
});

verificationCommands
Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/fixtures/any-credential-proof-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"qr": "https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767",
"id": "6de279ba-caf3-4979-a067-553284b40767",
"name": "Any credential",
"nonce": "b75fab9a5006216173500bfd3df5d0c5",
"created": "2023-08-09T20:19:46.278Z",
"updated": "2023-08-09T20:19:46.278Z",
"verified": false,
"response_url": "https://api-testnet.dock.io/proof-requests/6de279ba-caf3-4979-a067-553284b40767/send-presentation",
"request": {
"id": "6de279ba-caf3-4979-a067-553284b40767",
"input_descriptors": [
{
"id": "Credential 1",
"name": "Any credential",
"purpose": "Any credential",
"constraints": {
"fields": [
{
"path": [
"$.id"
]
}
]
}
}
]
},
"type": "proof-request"
}
59 changes: 59 additions & 0 deletions packages/core/src/fixtures/iiw-credential.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions packages/core/src/fixtures/iiw-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"qr": "https://creds-staging.dock.io/proof/f6b0c6a7-ae27-4044-80a3-07f5af6e61c8",
"id": "f6b0c6a7-ae27-4044-80a3-07f5af6e61c8",
"name": "boolean",
"nonce": "e2cb13292035d73e97543c8f463c3f06",
"created": "2023-08-17T17:38:38.276Z",
"updated": "2023-08-17T17:38:38.276Z",
"verified": false,
"response_url": "https://api-staging.dock.io/proof-requests/f6b0c6a7-ae27-4044-80a3-07f5af6e61c8/send-presentation",
"request": {
"id": "f6b0c6a7-ae27-4044-80a3-07f5af6e61c8",
"input_descriptors": [
{
"id": "Credential 1",
"name": "boolean",
"constraints": {
"fields": [
{
"path": [
"$.credentialSubject.booleanYesNO",
"$.credentialSubject.name",
"$.credentialSubject.id",
"$.credentialSubject.email",
"$.issuer.name"
]
}
]
}
}
]
},
"type": "proof-request"
}
57 changes: 57 additions & 0 deletions packages/core/src/fixtures/university-degree-bbs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
{
"dk": "https://ld.dock.io/credentials#",
"UniversityDegree": "dk:UniversityDegree",
"degreeName": "dk:degreeName",
"degreeType": "dk:degreeType",
"dateEarned": "dk:dateEarned",
"name": "dk:name",
"email": "dk:email",
"dateOfBirth": "dk:dateOfBirth",
"description": "dk:description",
"logo": "dk:logo"
}
],
"id": "https://creds-testnet.dock.io/cc2e0be92a9533ca68466ad011ea916216fedac6473d17b1bd0fc3a01b623b09",
"type": [
"VerifiableCredential",
"UniversityDegree"
],
"credentialSubject": {
"id": "recipient id",
"degreeName": "degree name",
"degreeType": "degree type",
"dateEarned": "2023-02-07",
"name": "full name",
"email": "[email protected]",
"dateOfBirth": "2023-02-08"
},
"issuanceDate": "2023-02-06T11:15:19.995Z",
"name": "University Degree",
"proof": {
"@context": [
{
"sec": "https://w3id.org/security#",
"proof": {
"@id": "sec:proof",
"@type": "@id",
"@container": "@graph"
}
},
"https://ld.dock.io/security/bbs/v1"
],
"type": "Bls12381BBS+SignatureDock2022",
"created": "2023-02-06T11:42:24Z",
"verificationMethod": "did:dock:5GAFgFX6dUMYzZKiRKHvV4nK92X64D5ekfy3WVpNEeMUnu38#keys-2",
"proofPurpose": "assertionMethod",
"proofValue": "znokfKbFLiBqTjuZVkg3FGftrogKGm47DKDSNRjwqd8SBQiw7iqeYpTdapL9cRGhax4R9pVwrDicWrm4P9fAmAAVjSHz79jZGnX9AVWesbn7HFssbguZgx2ZzXS6ySyfPfDa8qhUuMsepV6oDcydhEA4Hd"
},
"issuer": {
"name": "My New DID",
"description": "",
"logo": "",
"id": "did:dock:5GAFgFX6dUMYzZKiRKHvV4nK92X64D5ekfy3WVpNEeMUnu38"
}
}
32 changes: 32 additions & 0 deletions packages/core/src/fixtures/university-degree-proof-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"qr": "https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767",
"id": "6de279ba-caf3-4979-a067-553284b40767",
"name": "University Degree Credential",
"nonce": "b75fab9a5006216173500bfd3df5d0c5",
"created": "2023-08-09T20:19:46.278Z",
"updated": "2023-08-09T20:19:46.278Z",
"verified": false,
"response_url": "https://api-testnet.dock.io/proof-requests/6de279ba-caf3-4979-a067-553284b40767/send-presentation",
"request": {
"id": "6de279ba-caf3-4979-a067-553284b40767",
"input_descriptors": [
{
"id": "Credential 1",
"name": "University Degree Credential",
"purpose": "Any University Degree Credential",
"constraints": {
"fields": [
{
"path": ["$.name"],
"filter": {
"type": "string",
"pattern": "University Degree"
}
}
]
}
}
]
},
"type": "proof-request"
}
8 changes: 6 additions & 2 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ export function getJSON(jsonOrURL: string | any) {
return jsonOrURL;
}

if (typeof jsonOrURL === 'string' && isURL(jsonOrURL)) {
return getJSONFromURL(jsonOrURL);
if (typeof jsonOrURL === 'string') {
if (isURL(jsonOrURL)) {
return getJSONFromURL(jsonOrURL);
} else {
return JSON.parse(jsonOrURL);
}
}

throw new Error(`Invalid data ${jsonOrURL}`);
Expand Down
Loading

0 comments on commit bc24e02

Please sign in to comment.