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

feat(core-types): allow inline @context for Credentials and Presentations #1119

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 182 additions & 140 deletions packages/core-types/src/plugin.schema.json

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions packages/core-types/src/types/vc-data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export interface ProofType {
[x: string]: any
}

/**
* The data type for `@context` properties of credentials, presentations, etc.
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
export type ContextType = string | Record<string, any> | (string | Record<string, any>)[]

/**
* Represents an unsigned W3C Credential payload.
* See {@link https://www.w3.org/TR/vc-data-model/#credentials | VC data model}
Expand All @@ -86,7 +93,7 @@ export interface UnsignedCredential {
issuer: IssuerType
credentialSubject: CredentialSubject
type?: string[] | string
'@context': string[] | string
'@context': ContextType
issuanceDate: string
expirationDate?: string
credentialStatus?: CredentialStatusReference
Expand Down Expand Up @@ -121,7 +128,7 @@ export interface UnsignedPresentation {
holder: string
verifiableCredential?: W3CVerifiableCredential[]
type?: string[] | string
'@context': string[] | string
'@context': ContextType
verifier?: string[]
issuanceDate?: string
expirationDate?: string
Expand Down Expand Up @@ -162,7 +169,7 @@ export interface CredentialPayload {
issuer: IssuerType
credentialSubject?: CredentialSubject
type?: string[]
'@context'?: string[]
'@context'?: ContextType
issuanceDate?: DateType
expirationDate?: DateType
credentialStatus?: CredentialStatusReference
Expand All @@ -180,7 +187,7 @@ export interface PresentationPayload {
holder: string
verifiableCredential?: W3CVerifiableCredential[]
type?: string[]
'@context'?: string[]
'@context'?: ContextType
verifier?: string[]
issuanceDate?: DateType
expirationDate?: DateType
Expand Down
58 changes: 28 additions & 30 deletions packages/credential-eip712/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
}
},
"@context": {
"type": "array",
"items": {
"type": "string"
}
"$ref": "#/components/schemas/ContextType"
},
"issuanceDate": {
"$ref": "#/components/schemas/DateType"
Expand Down Expand Up @@ -86,6 +83,30 @@
},
"description": "The value of the credentialSubject property is defined as a set of objects that contain one or more properties that are each related to a subject of the verifiable credential. Each object MAY contain an id.\n\nSee {@link https://www.w3.org/TR/vc-data-model/#credential-subject | Credential Subject }"
},
"ContextType": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
},
{
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
}
}
],
"description": "The data type for `@context` properties of credentials, presentations, etc."
},
"DateType": {
"type": "string",
"description": "Represents an issuance or expiration date for Credentials / Presentations. This is used as input when creating them."
Expand Down Expand Up @@ -132,17 +153,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"issuanceDate": {
"type": "string"
Expand Down Expand Up @@ -211,10 +222,7 @@
}
},
"@context": {
"type": "array",
"items": {
"type": "string"
}
"$ref": "#/components/schemas/ContextType"
},
"verifier": {
"type": "array",
Expand Down Expand Up @@ -281,17 +289,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"verifier": {
"type": "array",
Expand Down
28 changes: 28 additions & 0 deletions packages/credential-ld/src/__tests__/issue-verify-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ describe('credential-LD full flow', () => {
didEthrIdentifier = await agent.didManagerCreate({ provider: 'did:ethr:goerli' })
})

it('create credential with inline context', async () => {
const credential: CredentialPayload = {
issuer: didKeyIdentifier.did,
'@context': [
{
'@context': {
nothing: 'custom:example.context#blank',
},
},
],
credentialSubject: {
nothing: 'else matters',
},
}
const verifiableCredential = await agent.createVerifiableCredential({
credential,
proofFormat: 'lds',
})

expect(verifiableCredential).toBeDefined()

const result = await agent.verifyCredential({
credential: verifiableCredential,
})

expect(result.verified).toBe(true)
})

it('works with Ed25519Signature2018 credential', async () => {
const credential: CredentialPayload = {
issuer: didKeyIdentifier.did,
Expand Down
58 changes: 28 additions & 30 deletions packages/credential-ld/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
}
},
"@context": {
"type": "array",
"items": {
"type": "string"
}
"$ref": "#/components/schemas/ContextType"
},
"issuanceDate": {
"$ref": "#/components/schemas/DateType"
Expand Down Expand Up @@ -93,6 +90,30 @@
},
"description": "The value of the credentialSubject property is defined as a set of objects that contain one or more properties that are each related to a subject of the verifiable credential. Each object MAY contain an id.\n\nSee {@link https://www.w3.org/TR/vc-data-model/#credential-subject | Credential Subject }"
},
"ContextType": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
},
{
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
}
}
],
"description": "The data type for `@context` properties of credentials, presentations, etc."
},
"DateType": {
"type": "string",
"description": "Represents an issuance or expiration date for Credentials / Presentations. This is used as input when creating them."
Expand Down Expand Up @@ -139,17 +160,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"issuanceDate": {
"type": "string"
Expand Down Expand Up @@ -233,10 +244,7 @@
}
},
"@context": {
"type": "array",
"items": {
"type": "string"
}
"$ref": "#/components/schemas/ContextType"
},
"verifier": {
"type": "array",
Expand Down Expand Up @@ -303,17 +311,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"verifier": {
"type": "array",
Expand Down
15 changes: 10 additions & 5 deletions packages/credential-ld/src/suites/JsonWebSignature2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { CredentialPayload, DIDDocument, IAgentContext, IKey, TKeyType } from "@
import { RequiredAgentMethods, VeramoLdSignature } from "../ld-suites.js";
import * as u8a from "uint8arrays";
import { JsonWebKey, JsonWebSignature } from "@transmute/json-web-signature";
import { encodeJoseBlob } from "@veramo/utils";
import { asArray, encodeJoseBlob } from "@veramo/utils";


/**
* Veramo wrapper for the JsonWebSignature2020 suite by Transmute Industries
*
*
* @alpha This API is experimental and is very likely to change or disappear in future releases without notice.
*/
export class VeramoJsonWebSignature2020 extends VeramoLdSignature {
getSupportedVerificationType(): 'JsonWebKey2020' {
return 'JsonWebKey2020'
}

getSupportedVeramoKeyType(): TKeyType {
return 'Ed25519'
}
Expand Down Expand Up @@ -68,6 +68,10 @@ export class VeramoJsonWebSignature2020 extends VeramoLdSignature {
key: verificationKey,
})

suite.ensureSuiteContext = ({ document }: { document: any, addSuiteContext: boolean }) => {
document['@context'] = [...asArray(document['@context'] || []), 'https://w3id.org/security/suites/jws-2020/v1']
}

return suite
}

Expand All @@ -76,10 +80,11 @@ export class VeramoJsonWebSignature2020 extends VeramoLdSignature {
}

preSigningCredModification(credential: CredentialPayload): void {
credential['@context'] = [...credential['@context'] || [], 'https://w3id.org/security/suites/jws-2020/v1']
// nop
}

preDidResolutionModification(didUrl: string, didDoc: DIDDocument): void {
// do nothing
}
}

}
48 changes: 26 additions & 22 deletions packages/did-comm/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"issuanceDate": {
"type": "string"
Expand Down Expand Up @@ -476,6 +466,30 @@
},
"description": "The value of the credentialSubject property is defined as a set of objects that contain one or more properties that are each related to a subject of the verifiable credential. Each object MAY contain an id.\n\nSee {@link https://www.w3.org/TR/vc-data-model/#credential-subject | Credential Subject }"
},
"ContextType": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
},
{
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
}
}
],
"description": "The data type for `@context` properties of credentials, presentations, etc."
},
"CredentialStatusReference": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -521,17 +535,7 @@
]
},
"@context": {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "string"
}
]
"$ref": "#/components/schemas/ContextType"
},
"verifier": {
"type": "array",
Expand Down
Loading