Overview
The W3cCredentialsApi provides methods for working with W3C Verifiable Credentials 1.1, including signing, verification, storage, and presentation creation.
import { ClaimFormat } from '@credo-ts/core'
const signedCredential = await agent.w3cCredentials.signCredential({
format: ClaimFormat.JwtVc,
credential: { /* ... */ },
})
Methods
signCredential()
Sign a W3C Verifiable Credential.
const { signedCredential } = await agent.w3cCredentials.signCredential({
format: ClaimFormat.JwtVc,
credential: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
issuer: issuerDid,
issuanceDate: new Date().toISOString(),
credentialSubject: {
id: holderDid,
name: 'Alice Smith',
},
},
verificationMethod: `${issuerDid}#key-1`,
})
options.format
ClaimFormat.JwtVc | ClaimFormat.LdpVc
required
Credential format: JWT or Linked Data Proof
options.verificationMethod
Verification method to use for signing
Returns: Promise<{ signedCredential: W3cVerifiableCredential }>
verifyCredential()
Verify a W3C Verifiable Credential.
const result = await agent.w3cCredentials.verifyCredential({
credential: signedCredential,
})
if (result.verified) {
console.log('Credential is valid')
} else {
console.log('Verification failed:', result.error)
}
options.credential
W3cVerifiableCredential
required
The credential to verify
Returns: Promise<W3cVerifyCredentialResult>
The result includes:
verified: boolean
error: verification error if failed
- Additional metadata
createPresentation()
Create a W3C Verifiable Presentation from credentials.
const presentation = await agent.w3cCredentials.createPresentation({
credentials: [credential1, credential2],
id: 'urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5',
holder: holderDid,
})
options.credentials
W3cVerifiableCredential[]
required
Array of credentials to include
Returns: Promise<W3cPresentation>
signPresentation()
Sign a W3C Verifiable Presentation.
const { signedPresentation } = await agent.w3cCredentials.signPresentation({
format: ClaimFormat.JwtVp,
presentation: presentation,
challenge: 'nonce-from-verifier',
domain: 'https://verifier.example.com',
verificationMethod: `${holderDid}#key-1`,
})
options.format
ClaimFormat.JwtVp | ClaimFormat.LdpVp
required
Presentation format: JWT or Linked Data Proof
Domain for domain binding
options.verificationMethod
Verification method to use for signing
Returns: Promise<{ signedPresentation: W3cVerifiablePresentation }>
verifyPresentation()
Verify a W3C Verifiable Presentation.
const result = await agent.w3cCredentials.verifyPresentation({
presentation: signedPresentation,
challenge: 'nonce-from-verifier',
domain: 'https://verifier.example.com',
})
if (result.verified) {
console.log('Presentation is valid')
console.log('Credential results:', result.credentialResults)
}
options.presentation
W3cVerifiablePresentation
required
The presentation to verify
Returns: Promise<W3cVerifyPresentationResult>
store()
Store a credential in the wallet.
const record = await agent.w3cCredentials.store({
credential: signedCredential,
})
console.log('Stored credential:', record.id)
options.credential
W3cVerifiableCredential
required
The credential to store
Returns: Promise<W3cCredentialRecord>
getAll()
Retrieve all stored credentials.
const credentials = await agent.w3cCredentials.getAll()
console.log(`Found ${credentials.length} credentials`)
Returns: Promise<W3cCredentialRecord[]>
getById()
Retrieve a credential by ID.
const record = await agent.w3cCredentials.getById(credentialId)
console.log('Credential:', record.credential)
Returns: Promise<W3cCredentialRecord>
Throws: RecordNotFoundError if not found
findAllByQuery()
Find credentials by query.
const credentials = await agent.w3cCredentials.findAllByQuery(
{
type: 'UniversityDegreeCredential',
},
{
limit: 10,
}
)
query
Query<W3cCredentialRecord>
required
Query to filter credentials
Query options (limit, offset)
Returns: Promise<W3cVerifiableCredential[]>
deleteById()
Delete a credential by ID.
await agent.w3cCredentials.deleteById(credentialId)
Returns: Promise<void>
Example Usage
import { Agent, ClaimFormat } from '@credo-ts/core'
// Issue a credential
const { signedCredential } = await agent.w3cCredentials.signCredential({
format: ClaimFormat.JwtVc,
credential: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential', 'UniversityDegreeCredential'],
issuer: issuerDid,
issuanceDate: new Date().toISOString(),
credentialSubject: {
id: holderDid,
degree: {
type: 'BachelorDegree',
name: 'Bachelor of Science',
},
},
},
verificationMethod: `${issuerDid}#key-1`,
})
// Store credential
const record = await agent.w3cCredentials.store({
credential: signedCredential,
})
// Create presentation
const presentation = await agent.w3cCredentials.createPresentation({
credentials: [signedCredential],
holder: holderDid,
})
// Sign presentation
const { signedPresentation } = await agent.w3cCredentials.signPresentation({
format: ClaimFormat.JwtVp,
presentation,
challenge: verifierChallenge,
verificationMethod: `${holderDid}#key-1`,
})
// Verify presentation
const result = await agent.w3cCredentials.verifyPresentation({
presentation: signedPresentation,
challenge: verifierChallenge,
})
if (result.verified) {
console.log('Presentation verified successfully!')
}