forked from digitalcredentials/vc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nam Hoang <hoangxuannam160493@gmail.com>
- Loading branch information
1 parent
51d1f3f
commit 345624c
Showing
2 changed files
with
332 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*! | ||
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
const credentialsContext = require('credentials-context'); | ||
|
||
const { | ||
constants: {CREDENTIALS_CONTEXT_V1_URL}, | ||
} = credentialsContext; | ||
const CREDENTIALS_CONTEXT_V2_URL = 'https://www.w3.org/ns/credentials/v2'; | ||
|
||
const credentialContextUrls = new Set([ | ||
CREDENTIALS_CONTEXT_V1_URL, | ||
CREDENTIALS_CONTEXT_V2_URL, | ||
]); | ||
|
||
/** | ||
* Asserts that a context array's first item is a credentials context. | ||
* | ||
* @param {object} options - Options. | ||
* @param {Array} options.context - An array of contexts. | ||
* | ||
* @throws {Error} - Throws if the first context | ||
* is not a credentials context. | ||
* | ||
* @returns {undefined} | ||
*/ | ||
function assertCredentialContext({context}) { | ||
// ensure first context is credentials context url | ||
if(credentialContextUrls.has(context[0]) === false) { | ||
// throw if the first context is not a credentials context | ||
throw new Error( | ||
`"${CREDENTIALS_CONTEXT_V1_URL}" or "${CREDENTIALS_CONTEXT_V2_URL}"` + | ||
' needs to be first in the list of contexts.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks to see if a VC has a V1 context. | ||
* | ||
* @param {object} options - Options. | ||
* @param {object} options.credential - A VC. | ||
* | ||
* @returns {boolean} Indicates whether the VC contains a V1 context. | ||
*/ | ||
function hasV1CredentialContext({credential}) { | ||
return credential?.['@context']?.[0] === CREDENTIALS_CONTEXT_V1_URL; | ||
} | ||
|
||
module.exports = { | ||
assertCredentialContext, | ||
hasV1CredentialContext, | ||
CREDENTIALS_CONTEXT_V1_URL, | ||
CREDENTIALS_CONTEXT_V2_URL, | ||
credentialContextUrls, | ||
}; |
Oops, something went wrong.