Skip to content

Commit

Permalink
verification flow in wallet-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Aug 16, 2023
1 parent abe797f commit 785b000
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 28 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/credential-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {IWallet} from './types';

export interface ICredentialProvider {
getCredentials(type?: string): any;
}

export function createCredentialProvider({wallet}: {wallet: IWallet}) {
function getCredentials(type: string = 'VerifiableCredential') {
return wallet.getDocumentsByType(type);
}

return {
getCredentials,
// TODO: move credential validity check to this provider
// TODO: move import credential from json or URL to this provider
};
}
39 changes: 39 additions & 0 deletions packages/core/src/fixtures/customer-credential.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
{
"dk": "https://ld.dock.io/credentials#",
"CustomerCredential": "dk:CustomerCredential",
"name": "dk:name",
"email": "dk:email",
"dateOfBirth": "dk:dateOfBirth",
"phone": "dk:phone",
"description": "dk:description",
"logo": "dk:logo"
}
],
"id": "https://creds-testnet.dock.io/986dccbe32eb61a0abb9678fd51aca796caa3d9f72088f66d60c9495b3d71174",
"type": ["VerifiableCredential", "CustomerCredential"],
"credentialSubject": {
"id": "did:key:z6MkoQzWru66w91EH7U9Xsv5eYXQabw9U3ZJd5GkatMWkmZT",
"name": "Alice Doe",
"email": "[email protected]",
"dateOfBirth": "1990-01-01",
"phone": "555-555-5555"
},
"issuanceDate": "2023-08-09T13:16:08.991Z",
"issuer": {
"name": "Test123",
"description": "",
"logo": "",
"id": "did:dock:5CgR1iELH33Qm8CN7LLXiEdTnrPTFKHgdxZgTuDwxgiyokjB"
},
"name": "Customer Credential",
"proof": {
"type": "Ed25519Signature2018",
"created": "2023-08-09T13:16:09Z",
"verificationMethod": "did:dock:5CgR1iELH33Qm8CN7LLXiEdTnrPTFKHgdxZgTuDwxgiyokjB#keys-1",
"proofPurpose": "assertionMethod",
"jws": "eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..2NJhT4Kx8W-q0pws4y86zsWMDkFrXz6Sfb5XRrJO_aRhvzhDZLsbXqvtRzRgGIrJKCBvTEDNiKBCicKT9f8hDA"
}
}
21 changes: 20 additions & 1 deletion packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import axios from 'axios';
import assert from 'assert';

function isURL(str) {
try {
// eslint-disable-next-line no-new
Expand All @@ -8,4 +11,20 @@ function isURL(str) {
}
}

export function getJSONOrUrl(data) {}
export function getJSONFromURL(url: string) {
return axios.get(url).then(res => res.data);
}

export function getJSON(jsonOrURL: string | any) {
assert(!!jsonOrURL, 'jsonOrURL is required');

if (typeof jsonOrURL === 'object') {
return jsonOrURL;
}

if (typeof jsonOrURL === 'string' && isURL(jsonOrURL)) {
return getJSONFromURL(jsonOrURL);
}

throw new Error(`Invalid data ${jsonOrURL}`);
}
72 changes: 68 additions & 4 deletions packages/core/src/verification-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,89 @@
import {IWallet} from './types';
import {createVerificationProvider} from './verification-provider';
import {createWallet} from './wallet';
import customerCredentialJSON from './fixtures/customer-credential.json';

describe('Verification provider', () => {
let verificationProvider;
let wallet: IWallet;
const verificationTemplateJSON = {
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',
};

beforeAll(async () => {
wallet = await createWallet({
databasePath: ':memory:',
});

await wallet.addDocument(customerCredentialJSON);

verificationProvider = createVerificationProvider({
wallet,
});
});

it('expect to create verification controller', () => {
const controller = verificationProvider.start({
template: 'https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767',
it('expect to create verification controller and fetch template JSON', async () => {
const controller = await verificationProvider.start({
template:
'https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767',
});

expect(controller.getTemplateJSON()).toEqual(verificationTemplateJSON);
});

it('expect to load credentials for a given template', async () => {
const controller = await verificationProvider.start({
template:
'https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767',
});

await controller.loadCredentials();

expect(controller.getFilteredCredentials()).toEqual([
customerCredentialJSON,
]);
});

it('expect to load credentials and generate presentation for a selected credential', async () => {
const controller = await verificationProvider.start({
template:
'https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767',
});

console.log(controller);
await controller.loadCredentials();
const credentials = controller.getFilteredCredentials();

// select the first credential in the filtered list
controller.setSelectedCredentialIds([credentials[0].id]);

const presentation = await controller.createPresentation();

expect(presentation).toEqual([]);
});
});
Loading

0 comments on commit 785b000

Please sign in to comment.