Skip to content

Commit

Permalink
refactor!: remove unwanted functions from public api
Browse files Browse the repository at this point in the history
  • Loading branch information
rflechtner committed Aug 23, 2023
1 parent 50670b1 commit 87ace40
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 113 deletions.
44 changes: 0 additions & 44 deletions packages/legacy-credentials/src/Claim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,6 @@ describe('jsonld', () => {
owner: 'did:kilt:4r1WkS3t8rbCb11H8t3tJvGVCynwDXSUBiuGB6sLRHzCLCjs',
}

it('exports claim as json-ld', () => {
// this is what a kilt claim looks like when expressed in expanded JSON-LD
const jsonld = Claim.toJsonLD(claim, true)
expect(jsonld).toMatchInlineSnapshot(`
{
"https://www.w3.org/2018/credentials#credentialSchema": {
"@id": "kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2",
},
"https://www.w3.org/2018/credentials#credentialSubject": {
"@id": "did:kilt:4r1WkS3t8rbCb11H8t3tJvGVCynwDXSUBiuGB6sLRHzCLCjs",
"kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2#address": "homestreet, home",
"kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2#name": "John",
"kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2#number": 26,
"kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2#optIn": true,
},
}
`)
})

it('exports claim as json-ld', () => {
// this is what a kilt claim looks like when expressed in compact JSON-LD
const jsonld = Claim.toJsonLD(claim, false)
expect(jsonld).toMatchInlineSnapshot(`
{
"@context": {
"@vocab": "https://www.w3.org/2018/credentials#",
},
"credentialSchema": {
"@id": "kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2",
},
"credentialSubject": {
"@context": {
"@vocab": "kilt:ctype:0x90364302f3b6ccfa50f3d384ec0ab6369711e13298ba4a5316d7e2addd5647b2#",
},
"@id": "did:kilt:4r1WkS3t8rbCb11H8t3tJvGVCynwDXSUBiuGB6sLRHzCLCjs",
"address": "homestreet, home",
"name": "John",
"number": 26,
"optIn": true,
},
}
`)
})

it('validates hashes from snapshot', () => {
// given some nonces...
const nonces = [
Expand Down
69 changes: 1 addition & 68 deletions packages/legacy-credentials/src/Claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,7 @@ import type {
import { Crypto, DataUtils, SDKErrors } from '@kiltprotocol/utils'
import { hexToBn } from '@polkadot/util'

const VC_VOCAB = 'https://www.w3.org/2018/credentials#'

/**
* 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'].
*/
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 JSON-LD readable representations of KILT claims. 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]].
*/
export function toJsonLD(
claim: PartialClaim,
expanded = true
): Record<string, unknown> {
const credentialSubject = jsonLDcontents(claim, expanded)
const prefix = expanded ? VC_VOCAB : ''
const result = {
[`${prefix}credentialSubject`]: credentialSubject,
}
result[`${prefix}credentialSchema`] = {
'@id': CType.hashToId(claim.cTypeHash),
}
if (!expanded) result['@context'] = { '@vocab': VC_VOCAB }
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 })
)
}
import { makeStatementsJsonLD } from './utils.js'

/**
* Produces salted hashes of individual statements comprising a (partial) [[IClaim]] to enable selective disclosure of contents. Can also be used to reproduce hashes for the purpose of validation.
Expand Down
53 changes: 53 additions & 0 deletions packages/legacy-credentials/src/utils.ts
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 })
)
}
2 changes: 1 addition & 1 deletion packages/legacy-credentials/src/vcInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { KiltCredentialV1, Types, constants } from '@kiltprotocol/vc-export'

import type { ICType, IClaim, ICredential } from '@kiltprotocol/types'

import { makeStatementsJsonLD } from './Claim.js'
import { makeStatementsJsonLD } from './utils.js'

/**
* Produces an instance of [[KiltAttestationProofV1]] from an [[ICredential]].
Expand Down

0 comments on commit 87ace40

Please sign in to comment.