Skip to content

Commit

Permalink
Start adding derive() API.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Nov 12, 2023
1 parent d491d91 commit 3242a14
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @digitalbazaar/vc ChangeLog

## 6.1.0 - 2023-11-dd

### Added
- Add `derive()` API for deriving new verifiable credentials from
existing ones, for the purpose of selective disclosure or
unlinkable presentation.

## 6.0.2 - 2023-08-04

### Fixed
Expand Down
43 changes: 41 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const defaultDocumentLoader =
jsigs.extendContextLoader(_documentLoader);
import * as credentialsContext from 'credentials-context';

const {AuthenticationProofPurpose} = jsigs.purposes;
const {AssertionProofPurpose, AuthenticationProofPurpose} = jsigs.purposes;
const {constants: {CREDENTIALS_CONTEXT_V1_URL}} = credentialsContext;

export {CredentialIssuancePurpose};
Expand Down Expand Up @@ -99,7 +99,7 @@ export const dateRegex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
*
* @param {object} options.credential - Base credential document.
* @param {LinkedDataSignature} options.suite - Signature suite (with private
* key material), passed in to sign().
* key material or an API to use it), passed in to sign().
*
* @param {ProofPurpose} [options.purpose] - A ProofPurpose. If not specified,
* a default purpose will be created.
Expand Down Expand Up @@ -144,6 +144,45 @@ export async function issue({
return jsigs.sign(credential, {purpose, documentLoader, suite});
}

/**
* Derives a proof from the given verifiable credential, resulting in a new
* verifiable credential. This method is usually used to generate selective
* disclosure and / or unlinkable proofs.
*
* @param {object} [options={}] - The options to use.
*
* @param {object} options.verifiableCredential - The verifiable credential
* containing a base proof to derive another proof from.
* @param {LinkedDataSignature} options.suite - Derived proof signature suite.
*
* Other optional params passed to `derive()`:
* @param {object} [options.documentLoader] - A document loader.
*
* @throws {Error} If missing required properties.
*
* @returns {Promise<VerifiableCredential>} Resolves on completion.
*/
export async function derive({
verifiableCredential, suite,
documentLoader = defaultDocumentLoader
} = {}) {
if(!verifiableCredential) {
throw new TypeError('"credential" parameter is required for deriving.');
}
if(!suite) {
throw new TypeError('"suite" parameter is required for deriving.');
}

// run common credential checks
_checkCredential({credential: verifiableCredential, mode: 'issue'});

return jsigs.derive(verifiableCredential, {
purpose: AssertionProofPurpose,
documentLoader,
suite
});
}

/**
* Verifies a verifiable presentation:
* - Checks that the presentation is well-formed
Expand Down

0 comments on commit 3242a14

Please sign in to comment.