Skip to content

Commit

Permalink
fix(credential-w3c): forward DID resolution options to the resolver (#…
Browse files Browse the repository at this point in the history
…1344)

fixes #1343
  • Loading branch information
mirceanis authored Feb 22, 2024
1 parent bfd8fe6 commit 7237efd
Show file tree
Hide file tree
Showing 23 changed files with 420 additions and 81 deletions.
11 changes: 9 additions & 2 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
LdDefaultContexts,
VeramoEcdsaSecp256k1RecoverySignature2020,
VeramoEd25519Signature2018,
VeramoEd25519Signature2020,
VeramoJsonWebSignature2020,
} from '../packages/credential-ld/src'
import { EthrDIDProvider } from '../packages/did-provider-ethr/src'
import { WebDIDProvider } from '../packages/did-provider-web/src'
Expand Down Expand Up @@ -245,12 +247,17 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
new SdrMessageHandler(),
],
}),
new DIDComm({ transports: [new DIDCommHttpTransport()]}),
new DIDComm({ transports: [new DIDCommHttpTransport()] }),
new CredentialPlugin(),
new CredentialIssuerEIP712(),
new CredentialIssuerLD({
contextMaps: [LdDefaultContexts, credential_contexts as any],
suites: [new VeramoEcdsaSecp256k1RecoverySignature2020(), new VeramoEd25519Signature2018()],
suites: [
new VeramoEcdsaSecp256k1RecoverySignature2020(),
new VeramoEd25519Signature2018(),
new VeramoJsonWebSignature2020(),
new VeramoEd25519Signature2020(),
],
}),
new SelectiveDisclosure(),
new DIDDiscovery({
Expand Down
13 changes: 10 additions & 3 deletions __tests__/localJsonStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
LdDefaultContexts,
VeramoEcdsaSecp256k1RecoverySignature2020,
VeramoEd25519Signature2018,
VeramoEd25519Signature2020,
VeramoJsonWebSignature2020,
} from '../packages/credential-ld/src'
import { EthrDIDProvider } from '../packages/did-provider-ethr/src'
import { WebDIDProvider } from '../packages/did-provider-web/src'
Expand All @@ -51,7 +53,7 @@ import {
PrivateKeyStoreJson,
} from '../packages/data-store-json/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { PeerDIDProvider, getResolver as getDidPeerResolver } from '../packages/did-provider-peer/src'
import { getResolver as getDidPeerResolver, PeerDIDProvider } from '../packages/did-provider-peer/src'

import { Resolver } from 'did-resolver'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
Expand All @@ -77,7 +79,7 @@ import utils from './shared/utils'
import { JsonFileStore } from './utils/json-file-store'
import credentialStatus from './shared/credentialStatus'
import credentialPluginTests from './shared/credentialPluginTests'
import dbInitOptions from "./shared/dbInitOptions";
import dbInitOptions from './shared/dbInitOptions'

jest.setTimeout(120000)

Expand Down Expand Up @@ -205,7 +207,12 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
new CredentialIssuerEIP712(),
new CredentialIssuerLD({
contextMaps: [LdDefaultContexts, credential_contexts as any],
suites: [new VeramoEcdsaSecp256k1RecoverySignature2020(), new VeramoEd25519Signature2018()],
suites: [
new VeramoEcdsaSecp256k1RecoverySignature2020(),
new VeramoEd25519Signature2018(),
new VeramoEd25519Signature2020(),
new VeramoJsonWebSignature2020(),
],
}),
new SelectiveDisclosure(),
...(options?.plugins || []),
Expand Down
22 changes: 13 additions & 9 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import {
IResolver,
TAgent,
} from '../packages/core-types/src'
import {
Agent,
createAgent
} from '../packages/core/src'
import { Agent, createAgent } from '../packages/core/src'
import { MessageHandler } from '../packages/message-handler/src'
import { KeyManager } from '../packages/key-manager/src'
import { AliasDiscoveryProvider, DIDManager } from '../packages/did-manager/src'
Expand All @@ -41,13 +38,15 @@ import {
LdDefaultContexts,
VeramoEcdsaSecp256k1RecoverySignature2020,
VeramoEd25519Signature2018,
VeramoEd25519Signature2020,
VeramoJsonWebSignature2020,
} from '../packages/credential-ld/src'
import { EthrDIDProvider } from '../packages/did-provider-ethr/src'
import { WebDIDProvider } from '../packages/did-provider-web/src'
import { getDidKeyResolver, KeyDIDProvider } from '../packages/did-provider-key/src'
import { getDidPkhResolver, PkhDIDProvider } from '../packages/did-provider-pkh/src'
import { getDidJwkResolver, JwkDIDProvider } from '../packages/did-provider-jwk/src'
import { getResolver as getDidPeerResolver, PeerDIDProvider } from "../packages/did-provider-peer/src";
import { getResolver as getDidPeerResolver, PeerDIDProvider } from '../packages/did-provider-peer/src'
import { DIDComm, DIDCommHttpTransport, DIDCommMessageHandler, IDIDComm } from '../packages/did-comm/src'
import {
ISelectiveDisclosure,
Expand Down Expand Up @@ -99,7 +98,7 @@ import messageHandler from './shared/messageHandler'
import didDiscovery from './shared/didDiscovery'
import utils from './shared/utils'
import credentialStatus from './shared/credentialStatus'
import credentialPluginTests from "./shared/credentialPluginTests";
import credentialPluginTests from './shared/credentialPluginTests'

jest.setTimeout(120000)

Expand Down Expand Up @@ -197,7 +196,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
defaultKms: 'local',
}),
'did:peer': new PeerDIDProvider({
defaultKms: 'local'
defaultKms: 'local',
}),
'did:pkh': new PkhDIDProvider({
defaultKms: 'local',
Expand Down Expand Up @@ -230,13 +229,18 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
new SdrMessageHandler(),
],
}),
new DIDComm({ transports: [new DIDCommHttpTransport()]}),
new DIDComm({ transports: [new DIDCommHttpTransport()] }),
// intentionally use the deprecated name to test compatibility
new CredentialIssuer(),
new CredentialIssuerEIP712(),
new CredentialIssuerLD({
contextMaps: [LdDefaultContexts, credential_contexts as any],
suites: [new VeramoEcdsaSecp256k1RecoverySignature2020(), new VeramoEd25519Signature2018()],
suites: [
new VeramoEcdsaSecp256k1RecoverySignature2020(),
new VeramoEd25519Signature2018(),
new VeramoJsonWebSignature2020(),
new VeramoEd25519Signature2020(),
],
}),
new SelectiveDisclosure(),
new DIDDiscovery({
Expand Down
35 changes: 35 additions & 0 deletions __tests__/shared/verifiableDataLD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,41 @@ export default (testContext: {
expect(result.verified).toBe(true)
})

it('should create and verify verifiable credential in LD with did:key Ed25519VerificationKey2020', async () => {
const iss = await agent.didManagerCreate({ provider: 'did:key', options: { keyType: 'Ed25519' } })
const credential = await agent.createVerifiableCredential({
credential: {
issuer: { id: iss.did },
'@context': ['https://www.w3.org/2018/credentials/v1', 'https://veramo.io/contexts/profile/v1'],
type: ['VerifiableCredential', 'Profile'],
issuanceDate: new Date().toISOString(),
credentialSubject: {
id: didKeyIdentifier.did,
name: 'of the game',
},
},
proofFormat: 'lds',
resolutionOptions: {
publicKeyFormat: 'Ed25519VerificationKey2020',
},
})

// Check credential:
expect(credential).toHaveProperty('proof')
const proofValue = credential.proof.jws ?? credential.proof.proofValue
expect(proofValue).toBeDefined()

expect(credential.proof.type).toEqual('Ed25519Signature2020')

const verification = await agent.verifyCredential({
credential: credential,
resolutionOptions: {
publicKeyFormat: 'Ed25519VerificationKey2020',
},
})
expect(verification.verified).toBe(true)
})

describe('credential verification policies', () => {
it('can verify credential at a particular time', async () => {
const issuanceDate = '2019-08-19T09:15:20.000Z' // 1566206120
Expand Down
60 changes: 60 additions & 0 deletions packages/core-types/src/plugin.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4218,6 +4218,18 @@ export const schema = {
"ICreateVerifiableCredentialArgs": {
"type": "object",
"properties": {
"resolutionOptions": {
"type": "object",
"properties": {
"publicKeyFormat": {
"type": "string"
},
"accept": {
"type": "string"
}
},
"description": "Options to be passed to the DID resolver."
},
"credential": {
"$ref": "#/components/schemas/CredentialPayload",
"description": "The JSON payload of the Credential according to the\n {@link https://www.w3.org/TR/vc-data-model/#credentials | canonical model } \n\nThe signer of the Credential is chosen based on the `issuer.id` property of the `credential`\n\n`@context`, `type` and `issuanceDate` will be added automatically if omitted"
Expand Down Expand Up @@ -4432,6 +4444,18 @@ export const schema = {
"ICreateVerifiablePresentationArgs": {
"type": "object",
"properties": {
"resolutionOptions": {
"type": "object",
"properties": {
"publicKeyFormat": {
"type": "string"
},
"accept": {
"type": "string"
}
},
"description": "Options to be passed to the DID resolver."
},
"presentation": {
"$ref": "#/components/schemas/PresentationPayload",
"description": "The JSON payload of the Presentation according to the\n {@link https://www.w3.org/TR/vc-data-model/#presentations | canonical model } .\n\nThe signer of the Presentation is chosen based on the `holder` property of the `presentation`\n\n`@context`, `type` and `issuanceDate` will be added automatically if omitted"
Expand Down Expand Up @@ -4788,6 +4812,18 @@ export const schema = {
"IVerifyCredentialArgs": {
"type": "object",
"properties": {
"resolutionOptions": {
"type": "object",
"properties": {
"publicKeyFormat": {
"type": "string"
},
"accept": {
"type": "string"
}
},
"description": "Options to be passed to the DID resolver."
},
"credential": {
"$ref": "#/components/schemas/W3CVerifiableCredential",
"description": "The Verifiable Credential object according to the\n {@link https://www.w3.org/TR/vc-data-model/#credentials | canonical model } or the JWT representation.\n\nThe signer of the Credential is verified based on the `issuer.id` property of the `credential` or the `iss` property of the JWT payload respectively"
Expand Down Expand Up @@ -5017,6 +5053,18 @@ export const schema = {
"IVerifyPresentationArgs": {
"type": "object",
"properties": {
"resolutionOptions": {
"type": "object",
"properties": {
"publicKeyFormat": {
"type": "string"
},
"accept": {
"type": "string"
}
},
"description": "Options to be passed to the DID resolver."
},
"presentation": {
"$ref": "#/components/schemas/W3CVerifiablePresentation",
"description": "The Verifiable Presentation object according to the\n {@link https://www.w3.org/TR/vc-data-model/#presentations | canonical model } or the JWT representation.\n\nThe signer of the Presentation is verified based on the `holder` property of the `presentation` or the `iss` property of the JWT payload respectively"
Expand Down Expand Up @@ -5152,6 +5200,18 @@ export const schema = {
"ICheckCredentialStatusArgs": {
"type": "object",
"properties": {
"resolutionOptions": {
"type": "object",
"properties": {
"publicKeyFormat": {
"type": "string"
},
"accept": {
"type": "string"
}
},
"description": "Options to be passed to the DID resolver."
},
"credential": {
"$ref": "#/components/schemas/VerifiableCredential",
"description": "The credential whose status needs to be checked"
Expand Down
5 changes: 3 additions & 2 deletions packages/core-types/src/types/ICredentialIssuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IDIDManager } from './IDIDManager.js'
import { IDataStore } from './IDataStore.js'
import { IKeyManager } from './IKeyManager.js'
import { IIdentifier, IKey } from "./IIdentifier.js";
import { UsingResolutionOptions } from './ICredentialVerifier.js'

/**
* The type of encoding to be used for the Verifiable Credential or Presentation to be generated.
Expand All @@ -26,7 +27,7 @@ export type ProofFormat = 'jwt' | 'lds' | 'EthereumEip712Signature2021'
*
* @public
*/
export interface ICreateVerifiablePresentationArgs {
export interface ICreateVerifiablePresentationArgs extends UsingResolutionOptions {
/**
* The JSON payload of the Presentation according to the
* {@link https://www.w3.org/TR/vc-data-model/#presentations | canonical model}.
Expand Down Expand Up @@ -97,7 +98,7 @@ export interface ICreateVerifiablePresentationArgs {
*
* @public
*/
export interface ICreateVerifiableCredentialArgs {
export interface ICreateVerifiableCredentialArgs extends UsingResolutionOptions {
/**
* The JSON payload of the Credential according to the
* {@link https://www.w3.org/TR/vc-data-model/#credentials | canonical model}
Expand Down
3 changes: 2 additions & 1 deletion packages/core-types/src/types/ICredentialStatusVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DIDDocument } from 'did-resolver'
import { IAgentContext, IPluginMethodMap } from './IAgent.js'
import { VerifiableCredential, CredentialStatus } from './vc-data-model.js'
import { IResolver } from './IResolver.js'
import { UsingResolutionOptions } from './ICredentialVerifier.js'

/**
* Arguments for calling {@link ICredentialStatusVerifier.checkCredentialStatus | checkCredentialStatus}.
Expand All @@ -12,7 +13,7 @@ import { IResolver } from './IResolver.js'
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
export interface ICheckCredentialStatusArgs {
export interface ICheckCredentialStatusArgs extends UsingResolutionOptions {
/**
* The credential whose status needs to be checked
*/
Expand Down
19 changes: 17 additions & 2 deletions packages/core-types/src/types/ICredentialVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@ import { IVerifyResult } from './IVerifyResult.js'
import { W3CVerifiableCredential, W3CVerifiablePresentation } from './vc-data-model.js'
import { IResolver } from './IResolver.js'
import { IDIDManager } from './IDIDManager.js'
import { DIDResolutionOptions } from 'did-resolver'

/**
* Options that are forwarded to the DID resolver.
* @public
*/
export interface UsingResolutionOptions {
/**
* Options to be passed to the DID resolver.
*/
resolutionOptions?: DIDResolutionOptions & {
// Used by did:key to determine the format of the public key. Specified here for discoverability.
publicKeyFormat?: string
}
}

/**
* Encapsulates the parameters required to verify a
* {@link https://www.w3.org/TR/vc-data-model/#credentials | W3C Verifiable Credential}
*
* @public
*/
export interface IVerifyCredentialArgs {
export interface IVerifyCredentialArgs extends UsingResolutionOptions {
/**
* The Verifiable Credential object according to the
* {@link https://www.w3.org/TR/vc-data-model/#credentials | canonical model} or the JWT representation.
Expand Down Expand Up @@ -48,7 +63,7 @@ export interface IVerifyCredentialArgs {
*
* @public
*/
export interface IVerifyPresentationArgs {
export interface IVerifyPresentationArgs extends UsingResolutionOptions {
/**
* The Verifiable Presentation object according to the
* {@link https://www.w3.org/TR/vc-data-model/#presentations | canonical model} or the JWT representation.
Expand Down
Loading

0 comments on commit 7237efd

Please sign in to comment.