Skip to content

Commit

Permalink
Merge pull request #138 from yg3630536/feature/add-netId
Browse files Browse the repository at this point in the history
add chainId in weId
  • Loading branch information
chenhaozx committed Jun 6, 2019
2 parents 2d844f5 + ce93bba commit 2af8190
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 98 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/webank/weid/config/FiscoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public class FiscoConfig {
@Value("${bcos.version:}")
private String version;

@Value("${orgid:}")
private String orgId;

@Value("${nodes:}")
private String nodes;

Expand All @@ -49,6 +46,9 @@ public class FiscoConfig {

@Value("${specificissuer.contractaddress:}")
private String specificIssuerAddress;

@Value("${chain.id:}")
private String chainId;

@Value("${web3sdk.timeout:10000}")
private String web3sdkTimeout;
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,20 @@ public enum ErrorCode {
PRESENTATION_WEID_PUBLICKEY_ID_INVALID(100604, "the publicKeyId is invalid."),

/**
* The nonce of challenge does not match the nonce of presentation.
* the nonce of challenge does not match the nonce of presentation.
*/
PRESENTATION_CHALLENGE_NONCE_MISMATCH(
100605,
"the nonce of challenge does not match the nonce of presentation."
),

/**
* the signature of presentation does not match the presenter.
*/
PRESENTATION_SIGNATURE_MISMATCH(
100606,
"the signature of presentation does not match the presenter."
),

/**
* the encrypt key is not exists.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public final class WeIdConstant {
* UUID Separator.
*/
public static final String UUID_SEPARATOR = "-";

/**
* WeId Separator.
*/
public static final String WEID_SEPARATOR = ":";

/**
* UUID Pattern.
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/webank/weid/protocol/base/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ public static Challenge create(String userWeId, String seed) {
challenge.setWeId(userWeId);
return challenge;
}


/**
* create Challenge with JSON String.
* @param challengeJson the challenge JSON String
* @return Challenge
*/
public static Challenge fromJson(String challengeJson) {
return DataToolUtils.deserialize(challengeJson, Challenge.class);
}

@Override
public String toRawData() {
return this.nonce;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public void putProofValue(String key, Object value) {
proof.put(key, value);
}

public static CredentialPojo create(String json) {
return DataToolUtils.deserialize(json, CredentialPojo.class);
/**
* create CredentialPojo with JSON String.
* @param credentialJson the CredentialPojo JSON String
* @return CredentialPojo
*/
public static CredentialPojo fromJson(String credentialJson) {
return DataToolUtils.deserialize(credentialJson, CredentialPojo.class);
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/webank/weid/protocol/base/PresentationE.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.webank.weid.constant.ParamKeyConstant;
import com.webank.weid.protocol.inf.IProof;
import com.webank.weid.protocol.inf.RawSerializer;
import com.webank.weid.util.DataToolUtils;

/**
* Created by Junqi Zhang on 2019/4/4.
Expand Down Expand Up @@ -89,4 +90,47 @@ public void putProofValue(String key, Object value) {
}
proof.put(key, value);
}

/**
* create PresentationE with JSON String.
* @param presentationJson the presentation JSON String
* @return PresentationE
*/
public static PresentationE fromJson(String presentationJson) {
return DataToolUtils.deserialize(presentationJson, PresentationE.class);
}

/**
* push the CredentialPojo into PresentationE.
* @param credentialPojo the credential
* @return true is success, others fail
*/
public boolean push(CredentialPojo credentialPojo) {
if (verifiableCredential == null) {
return false;
}
verifiableCredential.add(credentialPojo);
return true;
}

/**
* commit the credential to sign.
* @param weIdAuthentication the authentication
* @return true is success, others fail
*/
public boolean commit(WeIdAuthentication weIdAuthentication) {
if (weIdAuthentication == null
|| !weIdAuthentication.getWeIdPublicKeyId().equals(this.getVerificationMethod())) {
return false;
}
// 更新proof里面的签名
this.proof.remove(ParamKeyConstant.PROOF_SIGNATURE);
String signature =
DataToolUtils.sign(
this.toRawData(),
weIdAuthentication.getWeIdPrivateKey().getPrivateKey()
);
this.putProofValue(ParamKeyConstant.PROOF_SIGNATURE, signature);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static PresentationPolicyE create(String policyFileName) {
logger.error("can not find the {} file in your classpath.", policyFileName);
return policy;
}
policy = createByJson(jsonNode.toString());
policy = fromJson(jsonNode.toString());
} catch (IOException e) {
logger.error("create PresentationPolicyE has error, please check the log.", e);
}
Expand All @@ -111,7 +111,7 @@ public static PresentationPolicyE create(String policyFileName) {
* @param json the JSON String
* @return the PresentationPolicyE
*/
public static PresentationPolicyE createByJson(String json) {
public static PresentationPolicyE fromJson(String json) {
PresentationPolicyE policy = null;
try {
//将Json转换成Map
Expand Down Expand Up @@ -145,4 +145,22 @@ public static PresentationPolicyE createByJson(String json) {
}
return policy;
}

@Override
public String toJson() {
String jsonString = DataToolUtils.serialize(this);
HashMap<String, Object> policyEMap = DataToolUtils.deserialize(jsonString, HashMap.class);
Map<String, Object> policy1 =
(HashMap<String, Object>)policyEMap.get(CredentialConstant.CLAIM_POLICY_FIELD);
for (Map.Entry<String, Object> entry : policy1.entrySet()) {
HashMap<String, Object> claimPolicyMap = (HashMap<String, Object>)entry.getValue();
HashMap<String, Object> disclosureMap =
DataToolUtils.deserialize(
(String)claimPolicyMap.get(CredentialConstant.CLAIM_POLICY_DISCLOSED_FIELD),
HashMap.class
);
claimPolicyMap.put(CredentialConstant.CLAIM_POLICY_DISCLOSED_FIELD, disclosureMap);
}
return DataToolUtils.serialize(policyEMap);
}
}
19 changes: 18 additions & 1 deletion src/main/java/com/webank/weid/protocol/base/WeIdDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@

import lombok.Data;

import com.webank.weid.protocol.inf.JsonSerializer;
import com.webank.weid.util.DataToolUtils;

/**
* The base data structure to handle WeIdentity DID Document info.
*
* @author tonychen 2018.9.29
*/
@Data
public class WeIdDocument {
public class WeIdDocument implements JsonSerializer {

/**
* the serialVersionUID.
*/
private static final long serialVersionUID = 411522771907189878L;

/**
* Required: The id.
Expand Down Expand Up @@ -61,4 +69,13 @@ public class WeIdDocument {
* Required: The service list.
*/
private List<ServiceProperty> service = new ArrayList<>();

/**
* create WeIdDocument with JSON String.
* @param weIdDocumentJson the weIdDocument JSON String
* @return WeIdDocument
*/
public static WeIdDocument fromJson(String weIdDocumentJson) {
return DataToolUtils.deserialize(weIdDocumentJson, WeIdDocument.class);
}
}
11 changes: 7 additions & 4 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bcos.channel.client.Service;
import org.bcos.channel.dto.ChannelRequest;
import org.bcos.channel.dto.ChannelResponse;
Expand Down Expand Up @@ -69,7 +70,7 @@ public abstract class BaseService {
public static final int MAX_AMOP_REQUEST_TIMEOUT = 50000;
public static final int AMOP_REQUEST_TIMEOUT = Integer
.valueOf(PropertyUtils.getProperty("amop.request.timeout", "5000"));
protected static String fromOrgId = PropertyUtils.getProperty("blockchain.orgId");
protected static String currentOrgId = PropertyUtils.getProperty("blockchain.orgid");

private static final Logger logger = LoggerFactory.getLogger(BaseService.class);

Expand All @@ -83,6 +84,9 @@ public abstract class BaseService {
if (!fiscoConfig.load()) {
logger.error("[BaseService] Failed to load Fisco-BCOS blockchain node information.");
}
if (StringUtils.isEmpty(currentOrgId)) {
logger.error("[BaseService] the blockchain orgId is blank.");
}
}

/**
Expand All @@ -99,7 +103,6 @@ private static boolean initWeb3j() {
logger.info("[BaseService] begin to init web3j instance..");
service = TransactionUtils.buildFiscoBcosService(fiscoConfig);

String orgId = PropertyUtils.getProperty("blockchain.orgId");
OnNotifyCallback pushCallBack = new OnNotifyCallback();
service.setPushCallback(pushCallBack);
pushCallBack.registAmopCallback(
Expand All @@ -109,7 +112,7 @@ private static boolean initWeb3j() {

// Set topics for AMOP
List<String> topics = new ArrayList<String>();
topics.add(orgId);
topics.add(currentOrgId);
service.setTopics(topics);

try {
Expand Down Expand Up @@ -299,7 +302,7 @@ public ResponseData<AmopNotifyMsgResult> checkDirectRouteMsgHealth(
CheckAmopMsgHealthArgs arg) {

return this.getImpl(
fromOrgId,
currentOrgId,
toOrgId,
arg,
CheckAmopMsgHealthArgs.class,
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/com/webank/weid/service/impl/AmopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.webank.weid.constant.AmopMsgType;
import com.webank.weid.constant.ErrorCode;
Expand All @@ -43,30 +42,29 @@
/**
* Created by Junqi Zhang on 2019/4/10.
*/
@Component
public class AmopServiceImpl extends BaseService implements AmopService {

private static final Logger logger = LoggerFactory.getLogger(AmopServiceImpl.class);

@Override
public ResponseData<PolicyAndChallenge> getPolicyAndChallenge(
String orgId,
String targetOrgId,
Integer policyId,
String targetUserWeId
) {
try {
if (StringUtils.isBlank(orgId)) {
if (StringUtils.isBlank(currentOrgId)) {
logger.error("the orgId is null, policyId = {}", policyId);
return new ResponseData<PolicyAndChallenge>(null, ErrorCode.ILLEGAL_INPUT);
}
GetPolicyAndChallengeArgs args = new GetPolicyAndChallengeArgs();
args.setFromOrgId(fromOrgId);
args.setToOrgId(orgId);
args.setFromOrgId(currentOrgId);
args.setToOrgId(targetOrgId);
args.setPolicyId(String.valueOf(policyId));
args.setMessageId(getService().newSeq());
args.setTargetUserWeId(targetUserWeId);
ResponseData<GetPolicyAndChallengeResponse> retResponse =
this.getPolicyAndChallenge(orgId, args);
this.getPolicyAndChallenge(targetOrgId, args);
if (retResponse.getErrorCode().intValue() != ErrorCode.SUCCESS.getCode()) {
logger.error("AMOP response fail, policyId={}, errorCode={}, errorMessage={}",
policyId,
Expand All @@ -92,7 +90,7 @@ private ResponseData<GetPolicyAndChallengeResponse> getPolicyAndChallenge(
String toOrgId,
GetPolicyAndChallengeArgs args) {
return this.getImpl(
fromOrgId,
currentOrgId,
toOrgId,
args,
GetPolicyAndChallengeArgs.class,
Expand All @@ -107,7 +105,7 @@ private ResponseData<GetPolicyAndChallengeResponse> getPolicyAndChallenge(
*/
public ResponseData<AmopResponse> request(String toOrgId, AmopCommonArgs args) {
return this.getImpl(
fromOrgId,
currentOrgId,
toOrgId,
args,
AmopCommonArgs.class,
Expand All @@ -123,7 +121,7 @@ public ResponseData<AmopResponse> request(String toOrgId, AmopCommonArgs args) {
public ResponseData<GetEncryptKeyResponse> getEncryptKey(String toOrgId,
GetEncryptKeyArgs args) {
return this.getImpl(
fromOrgId,
currentOrgId,
toOrgId,
args,
GetEncryptKeyArgs.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import com.webank.weid.protocol.request.CptStringArgs;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.protocol.response.RsvSignature;
import com.webank.weid.protocol.response.TransactionInfo;
import com.webank.weid.rpc.CptService;
import com.webank.weid.service.BaseService;
import com.webank.weid.util.DataToolUtils;
Expand Down

0 comments on commit 2af8190

Please sign in to comment.