Skip to content

Commit

Permalink
bbs attribute selection
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Aug 16, 2023
1 parent b52395f commit a9d92f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
32 changes: 30 additions & 2 deletions packages/core/src/verification-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Verification provider', () => {
expect(controller.getStatus()).toEqual(
VerificationStatus.SelectingCredentials,
);
expect(controller.getSelectedAttributes()).toEqual([]);
expect(controller.selectedCredentials.size).toEqual(0);
expect(controller.getTemplateJSON()).toEqual(verificationTemplateJSON);
expect(controller.getFilteredCredentials()).toEqual([
customerCredentialJSON,
Expand All @@ -96,7 +96,9 @@ describe('Verification provider', () => {
const credentials = controller.getFilteredCredentials();

// select the first credential in the filtered list
controller.setSelectedCredentialIds([credentials[0].id]);
controller.selectedCredentials.set(credentials[0].id, {
credential: credentials[0],
});

const presentation = await controller.createPresentation();

Expand All @@ -108,4 +110,30 @@ describe('Verification provider', () => {

expect(result.isValid).toBe(true);
});

it('expect to generate presentation for a bbs credential, selecting specific attributes to share', async () => {
const controller = createVerificationController({
wallet,
didProvider,
});

await controller.start({
template:
'https://creds-testnet.dock.io/proof/6de279ba-caf3-4979-a067-553284b40767',
});

const credentials = controller.getFilteredCredentials();

// select the first credential in the filtered list
controller.selectedCredentials.set(credentials[0].id, {
credential: credentials[0],
});

// set selected attributes

const presentation = await controller.createPresentation();

const result = await controller.evaluatePresentation(presentation);
expect(result.isValid).toBe(true);
});
});
50 changes: 16 additions & 34 deletions packages/core/src/verification-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ function isRangeProofTemplate(templateJSON) {
return templateJSON.proving_key;
}

type CredentialId = string;
type CredentialSelection = {
credential: any;
attributesToReval?: string[];
};
type CredentialSelectionMap = Map<CredentialId, CredentialSelection>;

export function createVerificationController({
wallet,
credentialProvider,
Expand All @@ -43,8 +50,7 @@ export function createVerificationController({
*/
let statusData = null;
let filteredCredentials = [];
let selectedCredentialIds: string[] = [];
let selectedAttributes = [];
let selectedCredentials: CredentialSelectionMap = new Map();
let selectedDID = null;
let provingKey = null;

Expand Down Expand Up @@ -130,28 +136,22 @@ export function createVerificationController({
return templateJSON.request;
}

function setSelectedCredentialIds(_credentialIds: string[]) {
selectedCredentialIds = _credentialIds;
}

function selectCredentialAttribute(
credentialId: string,
attributePath: string,
) {}

async function createPresentation() {
assert(!!selectedDID, 'No DID selected');
assert(!!selectedCredentialIds.length, 'No credentials selected');
assert(!!selectedCredentials.size, 'No credentials selected');
assert(!!filteredCredentials.length, 'No filtered credentials found');

if (isRangeProofTemplate(templateJSON)) {
// TODO: Implement proving key usage for range-proofs
assert(!!provingKey, 'No proving key found');
}

const credentials = filteredCredentials.filter(credential => {
return selectedCredentialIds.includes(credential.id);
});
const credentials = [];

for (const credentialSelection of selectedCredentials.values()) {
// check if is bbs
credentials.push(credentialSelection.credential);
}

const didKeyPairList = await didProvider.getDIDKeyPairs();
const keyDoc = didKeyPairList.find(doc => doc.controller === selectedDID);
Expand All @@ -178,20 +178,6 @@ export function createVerificationController({
return filteredCredentials;
}

function getSelectedCredentialIds() {
return selectedCredentialIds;
}

function getSelectedCredentials() {
return filteredCredentials.filter(credential => {
return selectedCredentialIds.includes(credential.id);
});
}

function getSelectedAttributes() {
return selectedAttributes;
}

function getStatus() {
return status;
}
Expand Down Expand Up @@ -224,6 +210,7 @@ export function createVerificationController({

return {
emitter,
selectedCredentials,
getStatus,
getStatusData,
getSelectedDID() {
Expand All @@ -232,12 +219,7 @@ export function createVerificationController({
setSelectedDID,
start,
loadCredentials,
getSelectedAttributes,
getFilteredCredentials,
getSelectedCredentials,
setSelectedCredentialIds,
getSelectedCredentialIds,
selectCredentialAttribute,
createPresentation,
evaluatePresentation,
getTemplateJSON() {
Expand Down

0 comments on commit a9d92f7

Please sign in to comment.