owner: credential owner, who hold some credentials and create presentation;
issuer: credential issuer, who issue credential;
verifier: credential verifier, who verify credential and presentation.
There are two formats of verifiable credential, JSON-LD and JWT.
Refer: https://www.w3.org/TR/vc-data-model/#basic-concepts Refer: https://www.w3.org/TR/vc-data-model/#json-web-token
Refer: https://www.w3.org/TR/vc-data-model/#status
There are two type credential status now, AttestContract and RevocationList.
If CredentialStatus.type equals AttestContract, CredentialStatus.id should be CredentialRecord contract address.
Refer: https://www.w3.org/TR/vc-data-model/#basic-concepts
VerifiableCredential.issuer: issuer ontId.
Refer: https://www.w3.org/TR/vc-data-model/#proofs-signatures
Proof.type: an instance of PubKeyType;
ProofPurpose: only use assertionMethod at currently;
Proof.verificationMethod: pubkey uri, like:did:ont:AVe4zVZzteo6HoLpdBwpKNtDXLjJBzB9fv#keys-2;
Proof.hex: hex-encoded ontology signature;
Proof.jws: jws-encoded ontology signature;
Refer: https://www.w3.org/TR/vc-data-model/#presentations-0
VerifiablePresentation.holder: maybe an ontId of String type or an object that has "id" attribute and "id" must be ontId.
The verifiable credential with JWT format.
The JWT header.
The JWT payload.
Refer: https://www.w3.org/TR/vc-data-model/#json-web-token-extensions
The all attributes of VerifiableCredential that not contained in JWT payload should be defined in JWTVC.
Refer: https://www.w3.org/TR/vc-data-model/#json-web-token-extensions
The all attributes of VerifiablePresentation that not contained in JWT payload should be defined in JWTVP.
There are some utility class.
Credential owner send SignRequest to issuer to create credential.
It's a utility class, represent one public key of ontId.
It's a utility class, represent ontId signer.
OntId controller pubkey type.
Defined jwt.header.alg. Each PubKeyType has a corresponding ALG, each ALG has {algorithm type, curve type, hash method}.
The corresponding relationship is as follows:
| PubKeyType | ALG | Algorithm | Curve | Hash Method |
|---|---|---|---|---|
| EcdsaSecp224r1VerificationKey2019 | ES224 | ECDSA | P-224 | SHA-224 |
| EcdsaSecp256r1VerificationKey2019 | ES256 | ECDSA | P-256 | SHA-256 |
| EcdsaSecp384r1VerificationKey2019 | ES384 | ECDSA | P-384 | SHA-384 |
| EcdsaSecp521r1VerificationKey2019 | ES512 | ECDSA | P-521 | SHA-512 |
| EcdsaSecp256k1VerificationKey2019 | ES256K | ECDSA | secp256k1 | SHA-256 |
| Ed25519VerificationKey2018 | EdDSA | EDDSA | Curve25519 | SHA-256 |
| SM2VerificationKey2019 | SM | SM2 | SM2P256V1 | SM3 |
Providing some static method.
The OntId2 class is Ontology Verifiable Credential 2.0 protocol SDK class.
It's ONTID2 SDK class, all Ontology Verifiable Credential 2.0 function should entry from here.
OntId2.CredentialRecord: credential record contract;
OntId2.OntId: ontId contract.
There are many interfaces to use Ontology Verifiable Credential 2.0 protocol.
-
public OntId2(String ontId, Account signer, CredentialRecord credRecord, OntId ontIdContract)
Generate OntId2 object.
If
ontIdnot empty andsignernot null, there will queryOntIdPubKeyfrom ontology chain and use it as self signer.So owner and issuer should set
ontIdandsignerwhile create OntId2 object, and verifier may not set those. -
public void updateOntIdAndSigner(String ontId, Account signer)
Update
ontIdandsigneraccount.
-
public SignRequest genSignReq(Object credentialSubject, ProofPurpose proofPurpose, boolean hasSignature)
- generate
SignRequestobject, calculate its' hash; - if hasSignature, use self
signerto sign hash; - return SignRequest object.
- generate
-
public boolean verifySignReq(SignRequest req)
- use
req.ontIdto verifyreq.signatureofreq.
- use
Issuer create credential.
-
public VerifiableCredential createCred(String[] context, String[] type, Object issuer, Object credentialSubject, Date expiration, CredentialStatusType credentialStatusType, ProofPurpose proofPurpose)
comment:
- create VerifiableCredential object;
- check
expiration; - check
issuerequals self signer ontId; - generate
Proof.
param:
- context: refer w3c definition;
- type: refer w3c definition;
- issuer: maybe an ontId of String type or an object that has "id" attribute and "id" must be ontId;
- credentialSubject: refer w3c definition;
- expiration: refer w3c definition;
- credentialStatusType: only use
AttestContractandRevocationListat current; - proofPurpose: only use
assertionMethodat current;
return:
VerifiableCredential -
public String createJWTCred(String[] context, String[] type, Object issuer, Object credentialSubject, Date expiration, CredentialStatusType statusType, ProofPurpose purpose)
comment:
- parse params to JWT header and payload;
- generate jws.
param: same with above.
return: JWT credential.
Issuer commit credential hash to ontology chain.
-
public String commitCred(VerifiableCredential cred, String ownerOntId, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: issuer invoke this method to commit a verifiable credential(JSON-LD) to ontology chain, issuer should use self ontId to sign this transaction.
param:
- cred: a instance of verifiable credential;
- ownerOntId: ontId of credential owner;
- payer: transaction payer;
- gasLimit & gasPrice: transaction gas limit and price;
- sdk: a instance of OntSdk.
return: transaction hash.
-
public String commitCred(String cred, String ownerOntId, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: issuer invoke this method to commit a verifiable credential(JWT) to ontology chain, issuer should use self ontId to sign this transaction.
param:
credis JWT verifiable credential, the others params are same with the former. -
public String commitCredById(String credId, String ownerOntId, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: commit a credential to ontology chain.
param:
credIdis identification of credential, others param are same with above.
Verifier could verify credential.
The process consists of 4 aspects: verifyCredOntIdCredible, verifyCredNotExpired, verifyCredSignature, verifyCredNotRevoked.
-
public boolean verifyCred(String[] credibleOntIds, VerifiableCredential cred)
comment: Verify credential from four aspects.
param:
- credibleOntIds: credible ONTID list, similar to trust certificate;
- cred: instance of
VerifiableCredential.
return: boolean value of whether credential valid or invalid.
-
public boolean verifyJWTCred(String[] credibleOntIds, String cred)
comment: verify JWT credential;
param:
- credibleOntIds: credible ONTID list, similar to trust certificate;
- cred:
credis JWT format of verifiable credential.
return: boolean value of whether credential valid or invalid.
-
public boolean verifyCredOntIdCredible(String[] credibleOntIds, VerifiableCredential cred)
comment:
- check
cred.proof.verificationMethodiscred.issuer; - check
cred.proof.verificationMethodis existed incredibleOntIds.
param:
- credibleOntIds: credible ONTID list, similar to trust certificate;
- cred: a instance of
VerifiableCredential.
return: boolean.
- check
-
public boolean verifyJWTCredOntIdCredible(String[] credibleOntIds, String cred)
comment: verify
cred.payload.issexisted in credibleOntIds.param:
credis JWT format of verifiable credential.return: boolean.
-
public boolean verifyCredDate(VerifiableCredential cred)
comment: check credential not expired, check credential issuance date before current.
param:
credis an instance ofVerifiableCredential.return: boolean.
-
public boolean verifyJWTCredDate(String cred)
comment: check
cred.payload.expnot expired, checkcred.payload.iatandcred.payload.nbfbefore current.param:
credis JWT format of verifiable credential.return: boolean.
-
public boolean verifyCredSignature(VerifiableCredential cred)
comment:
- check
cred.proof.verificationMethodiscred.issuer; - use
cred.proof.verificationMethodas public key to verifycred.proof.
param:
credis an instance ofVerifiableCredential.return: boolean.
- check
-
public boolean verifyJWTCredSignature(String cred)
comment: verify JWT credential signature valid.
param:
credis JWT format of verifiable credential.return: value.
-
public boolean verifyCredNotRevoked(VerifiableCredential cred)
comment:
- query status of credential from
CredentialRecordcontract that identified bycred.credentialStatus.idifcred.credentialStatus.type == AttestContract; - check status equals
01.
param:
- cred: a instance of
VerifiableCredential.
return: boolean.
- query status of credential from
-
public boolean verifyJWTCredNotRevoked(String cred)
comment:
- deserialize
credtoJWTCredentialinstance; - query status of credential from
CredentialRecordcontract that identified bycred.credentialStatus.idifcred.credentialStatus.type == AttestContract; - check status equals
01.
param:
credis JWT format of verifiable credential.return: boolean value.
- deserialize
Refer: https://www.w3.org/TR/vc-data-model/#presentations-0.
Owner could create presentation by using one or multi VerifiableCredential.
-
public VerifiablePresentation createPresentation(VerifiableCredential[] creds, String[] context, String[] type, List
<String>challenge, List<Object>domain, Object holder, OntIdSigner[] otherSigners, ProofPurpose proofPurpose)comment:
- create
VerifiablePresentationobject; - use self signer to generate first proof;
- use
otherSignersto generate other proofs.
param:
- creds: array instance of verifiable credential;
- context: refer w3c definition;
- type: refer w3c definition;
- challenge: each proof should contain a challenge to prevent replay attack;
- domain: like challenge;
- holder: maybe an ontId of String type or an object that has "id" attribute and "id" must be ontId;
- otherSigners: presentation could own multi proof, so there would be other signer to sign presentation;
- proofPurpose: only use
assertionMethodat current;
return: instance of
VerifiablePresentation. - create
-
public String createJWTPresentation(String[] creds, String[] context, String[] type, Object holder, String challenge, Object domain, String nonce, ProofPurpose purpose)
comment: use multi
VerifiableCredentialinstance to create a JWT credential.param:
- creds:
credsis array of JWTVerifiableCredential. Others params are same with above; - context: same with above;
- type: same with above;
- challenge: JWT presentation only has one jws, so there only need one challenge;
- domain: like challenge;
- holder: same with above;
- nonce: refer w3c specification;
- proofPurpose: only use
assertionMethodat current;
return: a JWT format of
VerifiablePresentation. - creds:
Verifier could verify presentation.
-
public boolean verifyPresentationProof(VerifiablePresentation presentation, int proofIndex)
comment: check proof at
presentation.proof[proofIndex]is valid.param:
- presentation: an instance of
VerifiablePresentation; - proofIndex: the index of destination proof at
presentation.proof.
return: boolean value.
- presentation: an instance of
-
public boolean verifyJWTPresentation(String[] credibleOntIds, String presentation)
comment:
- parse
presentationtoJWTCredential; - verify each
JWTCredential.vc; - verify
JWTCredential.jws.
param:
- credibleOntIds: array of credible ontId;
- presentation: a JWT string of
VerifiablePresentation.
return: boolean value.
- parse
Because the SDK used by different roles, so we encapsulate a credential revoking interface. User would use these interface at different circumstance.
-
public String revokeCred(VerifiableCredential cred, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: revoke a credential by an instance of
VerifiableCredential.param:
- cred: an instance of
VerifiableCredential; - payer: transaction payer;
- gasLimit & gasPrice: transaction param;
- sdk: an instance of
OntSdk;
return: transaction hash of revoking credential.
- cred: an instance of
-
public String revokeCredById(String credId, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: revoke credential by credential id.
param:
credIdis identification of credential, others param are same with above;return: transaction hash of revoking credential.
-
public String revokeJWTCred(String cred, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: revoke credential by JWT format credential.
- parse credential to
JWTCredentialinstance; - use
JWTCredential.payload.jtito revoke this credential;
param:
credis a JWT format ofVerifiableCredentialorVerifiablePresentation. Others param are same with above.return: transaction hash of revoking credential.
- parse credential to
-
public String removeCredById(String credId, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: remove credential by credential id.
param:
credIdis identification of credential, other params are ontology transaction param;return: transaction hash of remove credential.
-
public String removeJWTCred(String cred, Account payer, long gasLimit, long gasPrice, OntSdk sdk)
comment: remove a JWT credential.
param:
credis JWT format of credential, other params are ontology transaction param;return: transaction hash of remove credential.
We also provide some interface to parse verifiable credential between the format of JSON-LD and JWT.
-
public JWTCredential(VerifiableCredential credential)
comment:
- check
credential.proof.jwsis empty; - parse
jwt.header; - parse
jwt.payload; - retrieve
credential.proof.jws.
param:
credentialis an instance ofVerifiableCredential.return: an instance of
JWTCredential. - check
-
public JWTCredential(VerifiablePresentation presentation, Proof proof, String nonce)
comment: Because there maybe many proof in presentation, you must specify the proof instance when you want to parse
VerifiablePresentationinstance to JWT.param:
- presentation: an instance of
VerifiablePresntation; - proof: an instance of
Proof; - nonce: refer w3c specification;
deserializeToJWTCred
return: an instance of
JWTCredentialthat representVerifiablePresntation.
- presentation: an instance of
-
public static JWTCredential deserializeToJWTCred(String jwt)
comment: deserialize a jwt string to
JWTCredentialobject.param: a JWT format string of
VerifiableCredentialorVerifiablePresentation.return: an instance of
JWTCredential.
-
public static VerifiableCredential deserializeFromJWT(JWTCredential cred)
comment: parse
JWTCredentialtoVerifiableCredential;param:
credis an instance ofJWTCredential;return: an instance of
VerifiableCredential. -
public static VerifiablePresentation deserializeFromJWT(JWTCredential cred)
comment: parse
JWTCredentialtoVerifiablePresentation;param:
credis an instance ofJWTCredential;return: an instance of
VerifiableCredential.
Ontology Verifiable Credential 2.0 use a new version CredentialRecord contract. There are 4 new interface.
public String sendCommit2(String issuerOntid, String password, byte[] salt, String subjectOntid, String credId, int pubkeyIndex, Account payerAcct, long gaslimit, long gasprice)
Used to commit a credential id to ontology chain.
public String sendRevoke2(String ownerId, String password, byte[] salt, String credId, int pubkeyIndex, Account payerAcct, long gaslimit, long gasprice)
Used to revoke a recorded credential id.
public String sendRemove2(String ownerId, String password, byte[] salt, String credId, int pubkeyIndex, Account payerAcct, long gaslimit, long gasprice)
Used to remove a recorded credential id.
public String sendGetStatus2(String credId)
Used to query credential status by cred id, there are 3 return value:
- "00": revoked.
- "01": committed;
- "02": removed;
Considering that there are a lot of old version of credential, the protocol upgrade must be compatible with old data. The main difference between the new and old protocol is in the form of credential.
The old ONTID protocol use OntId to generate and verify credential. However, the new use OntId2 to generate and verify credential.
In the other hands, we also provide the methods that verify old credential by new CredentialRecord contract:
- public boolean verifyCredOntIdCredible(String cred, String[] credibleIds)
- public boolean verifyCredNotExpired(String cred)
- public boolean verifyCredSignature(String cred)
- public boolean verifyCredNotRevoked(String cred)
See these code at here.
The document is a simple introduction about how to use Ontology Verifiable Credential 2.0.
There are some code to illustrate how to use Ontology Verifiable Credential 2.0 sdk.