Skip to content

Commit

Permalink
Merge pull request #174 from WeBankFinTech/feature/support-lite-crede…
Browse files Browse the repository at this point in the history
…ntial

Feature/support lite credential
  • Loading branch information
chaoxinhu committed Apr 24, 2020
2 parents e35eb93 + 326a1a0 commit dd6a234
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ public final class CredentialConstant {
public static final String CPT_TYPE_KEY = "cptType";

/**
* The Constant zkp Credential type.
* The Constant selective Credential type.
*/
public static final String ZKP_CREDENTIAL_TYPE = "zkp";

/**
* The Constant original Credential type.
*/
public static final String ORIGINAL_CREDENTIAL_TYPE = "hashTree";
public static final String SELECTIVE_CREDENTIAL_TYPE = "hashTree";

/**
* The Constant is an field in PresentationPolicyE.
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/webank/weid/constant/CredentialType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright© (2018-2020) WeBank Co., Ltd.
*
* This file is part of weid-java-sdk.
*
* weid-java-sdk is free software: you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* weid-java-sdk is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/


package com.webank.weid.constant;

/**
* Credential type.
* @author tonychen 2020年4月22日
*/
public enum CredentialType {

/**
* original type, used to create original type credential.
*/
ORIGINAL(0, "original"),

/**
* zkp type, used to create zkp type credential.
*/
ZKP(1, "zkp"),

/**
* lite1 type, used to create lite1 type credential.
*/
LITE1(2, "lite1");

/**
* type code.
*/
private Integer code;
/**
* type name.
*/
private String name;

/**
* constructor.
*
* @param code credential type code
* @param name credential type name
*/
CredentialType(Integer code, String name) {
this.code = code;
this.name = name;
}

/**
* get type code.
*
* @return type code
*/
public Integer getCode() {
return this.code;
}

/**
* get type name.
*
* @return type name
*/
public String getName() {
return this.name;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ public enum ErrorCode {
"presentation from pdf transportation, please use verifyPresentationFromPDF function"),


/**
* lite credential does not support selective disclosure.
*/
CREDENTIAL_NOT_SUPPORT_SELECTIVE_DISCLOSURE(100440,
"lite credential does not support selective disclosure."),


/**
* Authorization WeIDs: from and to must be different.
*/
Expand Down
125 changes: 89 additions & 36 deletions src/main/java/com/webank/weid/protocol/base/CredentialPojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.CredentialConstant;
import com.webank.weid.constant.CredentialType;
import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.ParamKeyConstant;
import com.webank.weid.exception.DataTypeCastException;
Expand Down Expand Up @@ -99,6 +101,72 @@ public class CredentialPojo implements IProof, JsonSerializer, Hashable {
*/
private List<String> type;

/**
* create CredentialPojo with JSON String.
*
* @param credentialJson the CredentialPojo JSON String
* @return CredentialPojo
*/
public static CredentialPojo fromJson(String credentialJson) {
if (StringUtils.isBlank(credentialJson)) {
logger.error("create credential with JSON String failed, "
+ "the credential JSON String is null");
throw new DataTypeCastException("the credential JSON String is null");
}

String credentialString = credentialJson;
if (DataToolUtils.isValidFromToJson(credentialJson)) {
credentialString = DataToolUtils.removeTagFromToJson(credentialJson);
}
Map<String, Object> credentialMap = DataToolUtils
.deserialize(credentialString, HashMap.class);

Object type = credentialMap.get(ParamKeyConstant.PROOF_TYPE);

//lite1类型的credential要做特殊处理,将廋身过的json还原成可验证的lite credential对象
if (type instanceof String && StringUtils
.equals(String.valueOf(type), CredentialType.LITE1.getName())) {
//result.addType(CredentialConstant.DEFAULT_CREDENTIAL_TYPE);
String signature = String.valueOf(credentialMap.get(ParamKeyConstant.PROOF));
credentialMap.remove(ParamKeyConstant.PROOF_TYPE);
credentialMap.remove(ParamKeyConstant.PROOF);
CredentialPojo credentialPojo;
try {
credentialPojo = DataToolUtils.mapToObj(
credentialMap,
CredentialPojo.class
);
credentialPojo.putProofValue(ParamKeyConstant.PROOF_SIGNATURE, signature);
credentialPojo.setIssuanceDate(0L);
credentialPojo.addType(CredentialConstant.DEFAULT_CREDENTIAL_TYPE);
credentialPojo.addType(CredentialType.LITE1.getName());
return credentialPojo;
} catch (Exception e) {
logger.error("[fromJson] deserialize failed. error message:{}", e);
return null;
}

}
CredentialPojo credentialPojo = DataToolUtils.deserialize(
DataToolUtils.convertUtcToTimestamp(credentialString),
CredentialPojo.class
);
ErrorCode checkResp = CredentialPojoUtils.isCredentialPojoValid(credentialPojo);
if (ErrorCode.SUCCESS.getCode() != checkResp.getCode()) {
logger.error("create CredentialPojo with JSON String failed, {}",
checkResp.getCodeDesc());
throw new DataTypeCastException(checkResp.getCodeDesc());
}
if (!CredentialPojoUtils.validClaimAndSaltForMap(
credentialPojo.getClaim(),
credentialPojo.getSalt())) {
logger.error("create PresentationE with JSON String failed, claim and salt of "
+ "credentialPojo not match.");
throw new DataTypeCastException("claim and salt of credentialPojo not match.");
}
return credentialPojo;
}

/**
* 添加type.
*
Expand Down Expand Up @@ -167,44 +235,26 @@ public void putProofValue(String key, Object value) {
*/
@Override
public String toJson() {
String json = DataToolUtils.convertTimestampToUtc(DataToolUtils.serialize(this));
return DataToolUtils.addTagFromToJson(json);
}

/**
* create CredentialPojo with JSON String.
*
* @param credentialJson the CredentialPojo JSON String
* @return CredentialPojo
*/
public static CredentialPojo fromJson(String credentialJson) {
if (StringUtils.isBlank(credentialJson)) {
logger.error("create credential with JSON String failed, "
+ "the credential JSON String is null");
throw new DataTypeCastException("the credential JSON String is null");
}
String credentialString = credentialJson;
if (DataToolUtils.isValidFromToJson(credentialJson)) {
credentialString = DataToolUtils.removeTagFromToJson(credentialJson);
}
CredentialPojo credentialPojo = DataToolUtils.deserialize(
DataToolUtils.convertUtcToTimestamp(credentialString),
CredentialPojo.class
);
ErrorCode checkResp = CredentialPojoUtils.isCredentialPojoValid(credentialPojo);
if (ErrorCode.SUCCESS.getCode() != checkResp.getCode()) {
logger.error("create CredentialPojo with JSON String failed, {}",
checkResp.getCodeDesc());
throw new DataTypeCastException(checkResp.getCodeDesc());
}
if (!CredentialPojoUtils.validClaimAndSaltForMap(
credentialPojo.getClaim(),
credentialPojo.getSalt())) {
logger.error("create PresentationE with JSON String failed, claim and salt of "
+ "credentialPojo not match.");
throw new DataTypeCastException("claim and salt of credentialPojo not match.");
//如果是LITE1类型的credential,则需要廋身
if (type.contains(CredentialType.LITE1.getName())) {
try {
String signature = this.getSignature();
Map<String, Object> credMap = DataToolUtils.objToMap(this);
credMap.remove(ParamKeyConstant.ISSUANCE_DATE);
credMap.remove(ParamKeyConstant.CONTEXT);
credMap.remove(ParamKeyConstant.PROOF);
credMap.put(ParamKeyConstant.PROOF_TYPE, CredentialType.LITE1.getName());

credMap.put(ParamKeyConstant.PROOF, signature);
return DataToolUtils.serialize(credMap);
} catch (Exception e) {
logger.error("[CredentialPojo:toJson] failed, error message:{}", e);
return StringUtils.EMPTY;
}
}
return credentialPojo;
String json = DataToolUtils.convertTimestampToUtc(DataToolUtils.serialize(this));
return DataToolUtils.addTagFromToJson(json);
}

/**
Expand All @@ -213,6 +263,9 @@ public static CredentialPojo fromJson(String credentialJson) {
* @return hash value
*/
public String getHash() {
if (type.contains(CredentialType.LITE1.getName())) {
return CredentialPojoUtils.getLiteCredentialPojoHash(this);
}
if (CredentialPojoUtils.isCredentialPojoValid(this) != ErrorCode.SUCCESS) {
return StringUtils.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import lombok.Data;

import com.webank.weid.constant.CredentialType;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.util.CredentialUtils;

Expand Down Expand Up @@ -71,4 +72,9 @@ public class CreateCredentialPojoArgs<T> {
* Optional:credential context.
*/
private String context = CredentialUtils.getDefaultCredentialContext();

/**
* credential type.
*/
private CredentialType type = CredentialType.ORIGINAL;
}
14 changes: 14 additions & 0 deletions src/main/java/com/webank/weid/rpc/WeIdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package com.webank.weid.rpc;

import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.base.WeIdPublicKey;
import com.webank.weid.protocol.request.CreateWeIdArgs;
import com.webank.weid.protocol.request.SetAuthenticationArgs;
import com.webank.weid.protocol.request.SetPublicKeyArgs;
Expand Down Expand Up @@ -50,6 +52,18 @@ public interface WeIdService {
*/
ResponseData<String> createWeId(CreateWeIdArgs createWeIdArgs);

/**
* Create a WeIdentity DID from the provided public key.
*
* @param publicKey the public key to create a weid
* @param weIdAuthentication your private key
* @return WeIdentity DID
*/
ResponseData<String> delegateCreateWeId(
WeIdPublicKey publicKey,
WeIdAuthentication weIdAuthentication
);

/**
* Query WeIdentity DID document.
*
Expand Down

0 comments on commit dd6a234

Please sign in to comment.