Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selective disclosure example to spec w/ conversion of QueryByExample to JSON pointers #39

Open
dlongley opened this issue Feb 6, 2025 · 1 comment

Comments

@dlongley
Copy link
Contributor

dlongley commented Feb 6, 2025

We will want to eventually add an algorithm that can convert a QueryByExample "example" to a list of JSON pointers that could look something like this:

import jsonpointer from 'json-pointer';

// first perform matching on `@context`, optionally performing
// JSON-LD transformations as desired, then...

// convert `vprQuery.credentialQuery.example` to JSON pointers, modulo
// `@context` field; any VCDM mandatory pointers can be automatically
// added based on VC version
// (e.g., `/issuer`, `/issuanceDate` for VC 1.x)
const object = {...example};
delete object['@context'];
const pointers = _adjustPointers(Object.keys(jsonpointer.dict(object)));

function _adjustPointers(pointers) {
  // ensure `credentialSubject` is included in any reveal because the VCDM
  // requires it; presume that if it isn't present that the entire credential
  // subject was requested and inform the user of this (as always)
  const hasCredentialSubject = pointers.some(
    pointer => pointers.includes('/credentialSubject/') ||
      pointer.endsWith('/credentialSubject'));
  if(!hasCredentialSubject) {
    pointers = pointers.slice();
    pointers.push('/credentialSubject');
  }

  pointers = _pruneShallowPointers(pointers);

  // make `type` pointers generic
  return pointers.map(pointer => {
    const index = pointer.indexOf('/type/');
    return index === -1 ? pointer : pointer.slice(0, index) + '/type';
  });
}

// gets only the deepest pointers from the given list of pointers, for example,
// `['/a/b', '/a/b/c', '/a/b/c/d']` will be pruned to: `['/a/b/c/d']`
function _pruneShallowPointers(pointers) {
  const deep = [];
  for(const pointer of pointers) {
    let isDeep = true;
    for(const p of pointers) {
      if(pointer.length < p.length && p.startsWith(pointer)) {
        isDeep = false;
        break;
      }
    }
    if(isDeep) {
      deep.push(pointer);
    }
  }
  return deep;
}
@dlongley
Copy link
Contributor Author

dlongley commented Feb 6, 2025

Also see related: #38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant