2. createSelectiveCredential

基本信息

  1. 接口名称:com.webank.weid.rpc.CredentialPojoService.createSelectiveCredential
  2. 接口定义: ResponseData<CredentialPojo> createSelectiveCredential(CredentialPojo credentialPojo, ClaimPolicy claimPolicy)
  3. 接口描述: 通过原始凭证和披漏策略,创建选择性披露的Credential

接口入参:

com.webank.weid.protocol.base.CredentialPojo

名称类型非空说明备注
contextStringY
typeList<String>Y
idStringY证书ID
cptIdIntegerYcptId
issuerStringYissuer 的 WeIdentity DID
issuanceDateLongY创建日期
expirationDateLongY到期日期
claimMap<String, Object>YClaim数据
proofMap<String, Object>Y签名数据结构体

com.webank.weid.protocol.base.ClaimPolicy

名称类型非空说明备注
fieldsToBeDisclosedStringY披露配置根据claim匹配的结构,详见调用示例

接口返回: com.webank.weid.protocol.response.ResponseData<CredentialPojo>;

名称类型说明备注
errorCodeInteger返回结果码
errorMessageString返回结果描述
resultCredentialPojo凭证对象业务数据
transactionInfoTransactionInfo交易信息

com.webank.weid.protocol.response.TransactionInfo

名称类型说明备注
blockNumberBigInteger交易块高
transactionHashString交易hash
transactionIndexBigInteger交易索引

此方法返回code

enumcodedesc
SUCCESS0成功
CPT_ID_ILLEGAL100303cptId无效
CREDENTIAL_ERROR100400Credential标准错误
CREDENTIAL_SIGNATURE_BROKEN100405签名破坏
CREDENTIAL_CREATE_DATE_ILLEGAL100408创建日期格式非法
CREDENTIAL_CLAIM_NOT_EXISTS100410Claim数据不能为空
CREDENTIAL_CLAIM_DATA_ILLEGAL100411Claim数据无效
CREDENTIAL_ID_NOT_EXISTS100412ID为空
CREDENTIAL_CONTEXT_NOT_EXISTS100413context为空
CREDENTIAL_ISSUER_INVALID100418WeIdentity DID无效
CREDENTIAL_CLAIM_POLICY_NOT_EXIST100420披露策略为null
CREDENTIAL_POLICY_DISCLOSUREVALUE_ILLEGAL100423policy披露信息非法
CREDENTIAL_POLICY_FORMAT_DOSE_NOT_MATCH_CLAIM100427披露策略与Claim不匹配
CREDENTIAL_DISCLOSURE_DATA_TYPE_ILLEGAL100428披露数据格式错误
CREDENTIAL_SIGNATURE_TYPE_ILLEGAL100429验证签名类型异常
ILLEGAL_INPUT160004参数非法

调用示例

  1. CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl();
  2. CreateCredentialPojoArgs<Map<String, Object>> createCredentialPojoArgs =
  3. new CreateCredentialPojoArgs<Map<String, Object>>();
  4. createCredentialPojoArgs.setCptId(1017);
  5. createCredentialPojoArgs
  6. .setIssuer("did:weid:101:0x39e5e6f663ef77409144014ceb063713b65600e7");
  7. createCredentialPojoArgs
  8. .setExpirationDate(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 100);
  9.  
  10. WeIdAuthentication weIdAuthentication = new WeIdAuthentication();
  11. weIdAuthentication.setWeId("did:weid:101:0x39e5e6f663ef77409144014ceb063713b65600e7");
  12.  
  13. WeIdPrivateKey weIdPrivateKey = new WeIdPrivateKey();
  14. weIdPrivateKey.setPrivateKey(
  15. "60866441986950167911324536025850958917764441489874006048340539971987791929772");
  16. weIdAuthentication.setWeIdPrivateKey(weIdPrivateKey);
  17.  
  18. weIdAuthentication
  19. .setWeIdPublicKeyId("did:weid:101:0x39e5e6f663ef77409144014ceb063713b65600e7#key0");
  20. createCredentialPojoArgs.setWeIdAuthentication(weIdAuthentication);
  21.  
  22. Map<String, Object> claim = new HashMap<String, Object>();
  23. claim.put("name", "zhangsan");
  24. claim.put("gender", "F");
  25. claim.put("age", 22);
  26. createCredentialPojoArgs.setClaim(claim);
  27.  
  28. ResponseData<CredentialPojo> response =
  29. credentialPojoService.createCredential(createCredentialPojoArgs);
  30.  
  31. // 选择性披露
  32. ClaimPolicy claimPolicy = new ClaimPolicy();
  33. claimPolicy.setFieldsToBeDisclosed("{\"name\":1,\"gender\":0,\"age\":1}");
  34. ResponseData<CredentialPojo> selectiveResponse =
  35. credentialPojoService.createSelectiveCredential(response.getResult(), claimPolicy);
  1. 返回结果如:
  2. result:(com.webank.weid.protocol.base.CredentialPojo)
  3. context: https://github.com/WeBankFinTech/WeIdentity/blob/master/context/v1
  4. id: c4f8ca00-7c1b-4ba0-993f-008106075d9c
  5. cptId: 1017
  6. issuer: did:weid:101:0x39e5e6f663ef77409144014ceb063713b65600e7
  7. issuanceDate: 1560420975268
  8. expirationDate: 1560471040676
  9. claim:(java.util.HashMap)
  10. gender: 0x0756ccf78a0ebd5bd186b054376f1e9d86139bf04f660e9171a74673e5a21c75
  11. name: zhangsan
  12. age: 22
  13. proof:(java.util.HashMap)
  14. creator: did:weid:101:0x39e5e6f663ef77409144014ceb063713b65600e7#key0
  15. salt:(java.util.HashMap)
  16. gender: 0
  17. name: rr3g0
  18. age: 9ysgr
  19. created: 1560420975268
  20. type: Secp256k1
  21. signatureValue: GxVcZJFEnC7w+ZKOZAjmKy5JfFxoEFqffmCMvbUnVYmzEVKIUtDCiDmokZ2X3jIV/uFvUHQ4DWXksrD6Opr1vLo=
  22. type:(java.util.ArrayList)
  23. [0]:VerifiableCredential
  24. errorCode: 0
  25. errorMessage: success
  26. transactionInfo:null

时序图

sequenceDiagramparticipant 调用者participant CredentialPojoService调用者->>CredentialPojoService: 调用createSelectiveCredential(),传入原始凭证CredentialPojoService->>CredentialPojoService: 入参非空、格式及合法性检查opt 入参校验失败CredentialPojoService—>>调用者: 报错,提示参数不合法并退出endCredentialPojoService->>CredentialPojoService: 根据claimPolicy来隐藏不披露的字段CredentialPojoService->>CredentialPojoService: 生成签发日期、生成数字签名CredentialPojoService—>>调用者: 返回凭证