-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: remove unwanted functions from public api
- Loading branch information
1 parent
50670b1
commit 87ace40
Showing
4 changed files
with
55 additions
and
113 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
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
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,53 @@ | ||
/** | ||
* Copyright (c) 2018-2023, BOTLabs GmbH. | ||
* | ||
* This source code is licensed under the BSD 4-Clause "Original" license | ||
* found in the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { CType } from '@kiltprotocol/core' | ||
import { PartialClaim } from '@kiltprotocol/types' | ||
import { SDKErrors } from '@kiltprotocol/utils' | ||
|
||
/** | ||
* Produces JSON-LD readable representations of [[IClaim]]['contents']. This is done by implicitly or explicitly transforming property keys to globally unique predicates. | ||
* Where possible these predicates are taken directly from the Verifiable Credentials vocabulary. Properties that are unique to a [[CType]] are transformed into predicates by prepending the [[CType]][schema][$id]. | ||
* | ||
* @param claim A (partial) [[IClaim]] from to build a JSON-LD representation from. The `cTypeHash` property is required. | ||
* @param expanded Return an expanded instead of a compacted representation. While property transformation is done explicitly in the expanded format, it is otherwise done implicitly via adding JSON-LD's reserved `@context` properties while leaving [[IClaim]][contents] property keys untouched. | ||
* @returns An object which can be serialized into valid JSON-LD representing an [[IClaim]]'s ['contents']. | ||
*/ | ||
export function jsonLDcontents( | ||
claim: PartialClaim, | ||
expanded = true | ||
): Record<string, unknown> { | ||
const { cTypeHash, contents, owner } = claim | ||
if (!cTypeHash) throw new SDKErrors.CTypeHashMissingError() | ||
const vocabulary = `${CType.hashToId(cTypeHash)}#` | ||
const result: Record<string, unknown> = {} | ||
if (owner) result['@id'] = owner | ||
if (!expanded) { | ||
return { | ||
...result, | ||
'@context': { '@vocab': vocabulary }, | ||
...contents, | ||
} | ||
} | ||
Object.entries(contents || {}).forEach(([key, value]) => { | ||
result[vocabulary + key] = value | ||
}) | ||
return result | ||
} | ||
|
||
/** | ||
* Produces canonical statements for selective disclosure based on a JSON-LD expanded representation of the claims. | ||
* | ||
* @param claim A (partial) [[IClaim]] from to build a JSON-LD representation from. The `cTypeHash` property is required. | ||
* @returns An array of stringified statements. | ||
*/ | ||
export function makeStatementsJsonLD(claim: PartialClaim): string[] { | ||
const normalized = jsonLDcontents(claim, true) | ||
return Object.entries(normalized).map(([key, value]) => | ||
JSON.stringify({ [key]: value }) | ||
) | ||
} |
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