@kaytrust/prooftypes is a library that allows the creation and verification of W3C Verifiable Credentials and Presentations in JWT format, using the JwtProof2020 proof type.
- Creation of Verifiable Credentials (VC) in JWT format.
- Creation of Verifiable Presentations (VP) in JWT format.
- Verification of VC and VP using DID-compatible resolvers.
- Support for multiple DID methods, such as
did:nearanddid:ethr.
You can install the library using npm or yarn:
npm install @kaytrust/prooftypes
# or
yarn add @kaytrust/prooftypesThe library provides the ProofTypeJWT class to handle the creation and verification of proofs in JWT format. Below are the steps required to use it.
constructor(options: ProofTypeJWTConfig={},is_presentation?: boolean)ProofTypeJWTConfig: Optional configuration for the issuer (Issuer) or verification options (resolverandverifyOptions).is_presentation: Iftrue, it will work with Verifiable Presentations; otherwise, it will work with Verifiable Credentials.
typeProofTypeJWTConfig={issuer?: Issuer,// Issuer to sign JWTsresolver?: ResolverOrOptions,// Resolver to verify JWTsverifyOptions?: VerifyOptions,// Additional verification options}Before creating a JWT, you need to configure an Issuer object to sign the JWTs. You can use libraries such as @kaytrust/did-near or @kaytrust/did-ethr.
import{NearDID}from'@kaytrust/did-near'import{EthrDID}from'@kaytrust/did-ethr'// NearDID Configurationconstissuer=newNearDID({privateKey: 'YOUR_PRIVATE_KEY',identifier: 'alice.near',// Optional})// EthrDID ConfigurationconstissuerEthr=newEthrDID({identifier: '0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198',// RequiredprivateKey: 'YOUR_PRIVATE_KEY'})The Issuer object includes:
did: Identifier of the issuer.alg: Algorithm used in the JWT header.signer: Function to generate the signature.
- Define the credential payload following the
JwtCredentialPayloadinterface. - Use the
generateProofmethod of theProofTypeJWTclass to generate the JWT.
import{JwtCredentialPayload,ProofTypeJWT}from'@kaytrust/prooftypes'constvcPayload: JwtCredentialPayload={sub: 'did:near:...',nbf: 1562950282,vc: {'@context': ['https://www.w3.org/2018/credentials/v1'],type: ['VerifiableCredential'],credentialSubject: {degree: {type: 'BachelorDegree',name: 'Baccalauréat en musiques numériques'}}}}constproofTypeJwtCredential=newProofTypeJWT({ issuer })constvcJwt=awaitproofTypeJwtCredential.generateProof(vcPayload)console.log(vcJwt)// eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQi...0CQmqB14NnN5XxD0d_glLRs1Myc_LBJjnuNwE- Define the presentation payload following the
JwtPresentationPayloadinterface. - Use the
generateProofmethod of theProofTypeJWTclass, setting theis_presentationparameter totrue.
import{JwtPresentationPayload,ProofTypeJWT}from'@kaytrust/prooftypes'constvpPayload: JwtPresentationPayload={vp: {'@context': ['https://www.w3.org/2018/credentials/v1'],type: ['VerifiablePresentation'],verifiableCredential: [vcJwt]}}constproofTypeJwtPresentation=newProofTypeJWT({ issuer },true)constvpJwt=awaitproofTypeJwtPresentation.generateProof(vpPayload)console.log(vpJwt)// eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1ODI1NDc...JNMUzZ6naacuWNGdZGuU0ZDwmgpUMUqIzMqFFRmge0R8QATo verify a JWT, you need a resolver that can resolve DID documents. You can use did-resolver with specific resolvers such as did:near or did:ethr.
import{ResolverOrOptions}from'@kaytrust/prooftypes'import{Resolver}from'did-resolver'import{getResolverasgetNearResolver}from'@kaytrust/did-near-resolver'importethrfrom'ethr-did-resolver'constnearResolver=getNearResolver()constethrResolver=ethr.getResolver()//If you are using multiple methods you need to flatten them into one objectconstresolver:ResolverOrOptions=newResolver({
...nearResolver,
...ethrResolver,})//If you are using one method you can simply pass the result of getResolver( into the constructorconstresolver:ResolverOrOptions=newResolver(nearResolver);Setting resolver options for ethr-did-resolver
import{ResolverOrOptions,ProofTypeJWT}from'@kaytrust/prooftypes'constRPC_AMOY="<RPC_AMOY>";constAMOY_CHAIN_ID=80002;constresolver:ResolverOrOptions={registry: '0xBC56d0883ef228b2B16420E9002Ece0A46c893F8',rpcUrl: RPC_AMOY,chainId: AMOY_CHAIN_ID}Use the verifyProof method of the ProofTypeJWT class to verify the JWT.
import{ProofTypeJWT}from'@kaytrust/prooftypes'constproofTypeJwtCredential=newProofTypeJWT({ resolver })constverifiedVC=awaitproofTypeJwtCredential.verifyProof(vcJwt)console.log(verifiedVC)Use the verifyProof method of the ProofTypeJWT class, setting the is_presentation parameter to true.
import{ProofTypeJWT}from'@kaytrust/prooftypes'constproofTypeJwtPresentation=newProofTypeJWT({ resolver },true)constverifiedVP=awaitproofTypeJwtPresentation.verifyProof(vpJwt)console.log(verifiedVP)- The
JwtProof2020proof type is a synthetic type for internal use and is not registered in the W3C data model. - Verified payloads are aligned with the W3C data model, making them easier to use in systems that handle multiple credential formats.
If you want to contribute to this project, please open an issue or submit a pull request.
This proof type allows you to register and verify verifiable credentials directly on the NEAR blockchain using a smart contract.
- Network: testnet
- Contract ID:
neardtiprooftype.testnet - CID: The proof is the base64-encoded SHA-256 hash of the VC payload
Make sure near-api-js is available in your project.
npm install near-api-jsimport{ProofTypeNear}from'./proof-type-near';import{connect,keyStores,WalletConnection}from'near-api-js';importCryptoJSfrom'crypto-js';constproofType=newProofTypeNear();constwallet: WalletConnection=/* your connected wallet */;constvcPayload={issuer: 'did:near:issuer.testnet',credentialSubject: {id: 'did:near:user.testnet',name: 'Alice'},issuanceDate: newDate().toISOString(),type: ['VerifiableCredential']};// Generate CID from VC payloadconstcid=CryptoJS.SHA256(JSON.stringify(vcPayload)).toString(CryptoJS.enc.Base64);// Generate on-chain proofconstproof=awaitproofType.generateProof(vcPayload,{
wallet,
cid
});// Verifyconstvalid=awaitproofType.verifyProof(proof);console.log(valid);// true or false- The contract only allows a
(did, cid)pair to be used once. - The proof is linked to the NEAR account that issues the credential.