Skip to content

Commit

Permalink
Merge pull request #53 from chaoxinhu/feature/restapi-adaption
Browse files Browse the repository at this point in the history
Adaptions for the RESTful API
  • Loading branch information
chenhaozx committed Mar 29, 2019
2 parents c5ea015 + 4cd7035 commit 5d636f6
Show file tree
Hide file tree
Showing 27 changed files with 1,045 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ public final class CredentialConstant {
*/
public static final String DEFAULT_CREDENTIAL_CONTEXT =
"https://www.w3.org/2018/credentials/v1";
/**
* The Constant default Credential Context field name.
*/
public static final String CREDENTIAL_CONTEXT_FIELD = "\"context\"";

/**
* The Constant default Credential Context field name in Credential Json String.
*/
public static final String CREDENTIAL_CONTEXT_PORTABLE_JSON_FIELD = "\"@context\"";
public static final String CREDENTIAL_CONTEXT_PORTABLE_JSON_FIELD = "@context";

}
62 changes: 62 additions & 0 deletions src/main/java/com/webank/weid/constant/ParamKeyConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
* weidentity-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.
*
* weidentity-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 weidentity-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.constant;

/**
* Define param key names to be allowed to enable calls to Java SDK.
*
* @author chaoxinhu
*/
public final class ParamKeyConstant {

/**
* Universal param key names.
*/
public static final String WEID = "weId";

/**
* WeIdService related param names.
*/
public static final String PUBLIC_KEY = "publicKey";

/**
* AuthorityIssuer related param names.
*/
public static final String AUTHORITY_ISSUER_NAME = "name";

/**
* CptService related param names.
*/
public static final String CPT_JSON_SCHEMA = "cptJsonSchema";
public static final String CPT_SIGNATURE = "cptSignature";

/**
* CredentialService related param names.
*/
public static final String CPT_ID = "cptId";
public static final String ISSUER = "issuer";
public static final String CLAIM = "claim";
public static final String EXPIRATION_DATE = "expirationDate";
public static final String CREDENTIAL_SIGNATURE = "signature";
public static final String CONTEXT = "context";
public static final String CREDENTIAL_ID = "id";
public static final String ISSURANCE_DATE = "issuranceDate";

}
44 changes: 32 additions & 12 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,22 @@ public final class WeIdConstant {
/**
* The Constant WeIdentity DID Long Array Length.
*/
public static final Integer LONG_ARRAY_LENGTH = 8;
public static final Integer CPT_LONG_ARRAY_LENGTH = 8;

/**
* The Constant WeIdentity DID String Array Length.
*/
public static final Integer STRING_ARRAY_LENGTH = 8;
public static final Integer CPT_STRING_ARRAY_LENGTH = 8;

/**
* The Constant Authority Issuer contract array length.
*/
public static final Integer AUTHORITY_ISSUER_ARRAY_LEGNTH = 16;

/**
* The default accumulator value.
*/
public static final String DEFAULT_ACCUMULATOR_VALUE = "1";

/**
* The Constant WeIdentity DID Json Schema Array Length.
Expand Down Expand Up @@ -134,11 +144,6 @@ public final class WeIdConstant {
*/
public static final Long LONG_VALUE_ZERO = 0L;

/**
* -1.
*/
public static final Integer VALUE_NEG_FIRST = -1;

/**
* Hex Prefix.
*/
Expand All @@ -149,14 +154,29 @@ public final class WeIdConstant {
*/
public static final String UUID_SEPARATOR = "-";

/**
* Quote.
*/
public static final String DOUBLE_QUOTE = "\"";

/**
* UUID Pattern.
*/
public static final String UUID_PATTERN =
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";

/**
* The transaction poll interval.
*/
public static final Integer POLL_TRANSACTION_SLEEP_DURATION = 1500;

/**
* The transaction poll attempts (max).
*/
public static final Integer POLL_TRANSACTION_ATTEMPTS = 5;

/**
* The additive block height.
*/
public static final Integer ADDITIVE_BLOCK_HEIGHT = 500;

/**
* The big-enough block limit number.
*/
public static final String BIG_BLOCK_LIMIT = "9999999999";
}
10 changes: 9 additions & 1 deletion src/main/java/com/webank/weid/rpc/AuthorityIssuerService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -44,6 +44,14 @@ public interface AuthorityIssuerService {
*/
ResponseData<Boolean> registerAuthorityIssuer(RegisterAuthorityIssuerArgs args);

/**
* Register a new Authority Issuer on Chain by sending preset transaction hex value.
*
* @param transactionHex the transaction hex value
* @return true if succeeds, false otherwise
*/
ResponseData<String> registerAuthorityIssuer(String transactionHex);

/**
* Remove a new Authority Issuer on Chain.
*
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/webank/weid/rpc/CptService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -40,6 +40,13 @@ public interface CptService {
*/
ResponseData<CptBaseInfo> registerCpt(CptMapArgs args);

/**
* Register a new CPT to the blockchain by sending preset transaction hex value.
*
* @param transactionHex the transaction hex value
* @return The registered CPT info
*/
ResponseData<String> registerCpt(String transactionHex);

/**
* Register a new CPT to the blockchain.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/webank/weid/rpc/CredentialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface CredentialService {
* Generate a credential for full claim content.
*
* @param args the args
* @return credential
* @return credential wrapper
*/
ResponseData<CredentialWrapper> createCredential(CreateCredentialArgs args);

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/webank/weid/rpc/WeIdService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -49,6 +49,14 @@ public interface WeIdService {
*/
ResponseData<String> createWeId(CreateWeIdArgs createWeIdArgs);

/**
* Create a WeIdentity DID by sending preset transaction hex value to chain.
*
* @param transactionHex the transaction hex value
* @return Error message if any
*/
ResponseData<String> createWeId(String transactionHex);

/**
* Query WeIdentity DID document.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -100,7 +100,7 @@ private static boolean initCredentials() {
*
* @return the web3j
*/
protected static Web3j getWeb3j() {
public static Web3j getWeb3j() {
if (web3j == null && !initWeb3j()) {
throw new InitWeb3jException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -53,6 +53,7 @@
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.service.BaseService;
import com.webank.weid.util.DataTypetUtils;
import com.webank.weid.util.TransactionUtils;
import com.webank.weid.util.WeIdUtils;

/**
Expand Down Expand Up @@ -135,25 +136,9 @@ public ResponseData<Boolean> registerAuthorityIssuer(RegisterAuthorityIssuerArgs
WeIdConstant.TRANSACTION_RECEIPT_TIMEOUT,
TimeUnit.SECONDS
);
List<AuthorityIssuerRetLogEventResponse> eventList =
AuthorityIssuerController.getAuthorityIssuerRetLogEvents(receipt);

AuthorityIssuerRetLogEventResponse event = eventList.get(0);
if (event != null) {
ErrorCode errorCode = verifyAuthorityIssuerRelatedEvent(
event,
addr,
WeIdConstant.ADD_AUTHORITY_ISSUER_OPCODE
);
if (ErrorCode.SUCCESS.getCode() != errorCode.getCode()) {
return new ResponseData<>(false, errorCode);
} else {
return new ResponseData<>(true, errorCode);
}
} else {
logger.error(
"register authority issuer failed due to transcation event decoding failure.");
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
Boolean result = resolveRegisterAuthorityIssuerEvents(receipt);
if (result) {
return new ResponseData<>(result, ErrorCode.SUCCESS);
}
} catch (TimeoutException e) {
logger.error("register authority issuer failed due to system timeout. ", e);
Expand All @@ -163,7 +148,53 @@ public ResponseData<Boolean> registerAuthorityIssuer(RegisterAuthorityIssuerArgs
return new ResponseData<>(false, ErrorCode.TRANSACTION_EXECUTE_ERROR);
} catch (Exception e) {
logger.error("register authority issuer failed.", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
}
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
}

/**
* Register a new Authority Issuer on Chain with preset transaction hex value. The inputParam is
* a Json String, with two keys: WeIdentity DID and Name. Parameters will be ordered as
* mentioned after validity check; then transactionHex will be sent to blockchain.
*
* @param transactionHex the transaction hex value
* @return true if succeeds, false otherwise
*/
@Override
public ResponseData<String> registerAuthorityIssuer(String transactionHex) {
try {
if (StringUtils.isEmpty(transactionHex)) {
logger.error("AuthorityIssuer transaction error");
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.ILLEGAL_INPUT);
}
TransactionReceipt transactionReceipt = TransactionUtils
.sendTransaction(getWeb3j(), transactionHex);
Boolean result = resolveRegisterAuthorityIssuerEvents(transactionReceipt);
if (result) {
return new ResponseData<>(Boolean.TRUE.toString(), ErrorCode.SUCCESS);
}
} catch (Exception e) {
logger.error("[registerAuthorityIssuer] register failed due to transaction error.", e);
}
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.TRANSACTION_EXECUTE_ERROR);
}

private Boolean resolveRegisterAuthorityIssuerEvents(
TransactionReceipt transactionReceipt) {
List<AuthorityIssuerRetLogEventResponse> eventList =
AuthorityIssuerController.getAuthorityIssuerRetLogEvents(transactionReceipt);

AuthorityIssuerRetLogEventResponse event = eventList.get(0);
if (event != null) {
ErrorCode errorCode = verifyAuthorityIssuerRelatedEvent(
event,
WeIdConstant.ADD_AUTHORITY_ISSUER_OPCODE
);
return (ErrorCode.SUCCESS == errorCode);
} else {
logger.error(
"register authority issuer failed due to transcation event decoding failure.");
return false;
}
}

Expand Down Expand Up @@ -196,7 +227,6 @@ public ResponseData<Boolean> removeAuthorityIssuer(RemoveAuthorityIssuerArgs arg
if (event != null) {
ErrorCode errorCode = verifyAuthorityIssuerRelatedEvent(
event,
addr,
WeIdConstant.REMOVE_AUTHORITY_ISSUER_OPCODE
);
if (ErrorCode.SUCCESS.getCode() != errorCode.getCode()) {
Expand Down Expand Up @@ -377,23 +407,19 @@ private ErrorCode checkAuthorityIssuerArgsValidity(AuthorityIssuer args) {

private ErrorCode verifyAuthorityIssuerRelatedEvent(
AuthorityIssuerRetLogEventResponse event,
Address addr,
Integer opcode) {

if (event.addr == null || event.operation == null || event.retCode == null) {
return ErrorCode.ILLEGAL_INPUT;
}
if (event.addr.getValue().equals(addr.getValue())) {
Integer eventOpcode = event.operation.getValue().intValue();
if (eventOpcode.equals(opcode)) {
Integer eventRetCode = event.retCode.getValue().intValue();
return ErrorCode.getTypeByErrorCode(eventRetCode);
} else {
return ErrorCode.AUTHORITY_ISSUER_OPCODE_MISMATCH;
}
Integer eventOpcode = event.operation.getValue().intValue();
if (eventOpcode.equals(opcode)) {
Integer eventRetCode = event.retCode.getValue().intValue();
return ErrorCode.getTypeByErrorCode(eventRetCode);
} else {
return ErrorCode.AUTHORITY_ISSUER_ADDRESS_MISMATCH;
return ErrorCode.AUTHORITY_ISSUER_OPCODE_MISMATCH;
}

}

private boolean isValidAuthorityIssuerName(String name) {
Expand All @@ -404,7 +430,7 @@ private boolean isValidAuthorityIssuerName(String name) {
}

private String[] loadNameToStringAttributes(String name) {
String[] nameArray = new String[16];
String[] nameArray = new String[WeIdConstant.AUTHORITY_ISSUER_ARRAY_LEGNTH];
nameArray[0] = name;
return nameArray;
}
Expand Down

0 comments on commit 5d636f6

Please sign in to comment.