Skip to content

Commit

Permalink
Merge pull request #243 from WeBankFinTech/feature/issuer-recognize
Browse files Browse the repository at this point in the history
* Recognize based register authority issuer
  • Loading branch information
junqizhang-dev committed Aug 4, 2020
2 parents 1e2fd60 + 169990d commit ce48fb9
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 93 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
- name: Verify MySQL connection and create CI DB
run: |
sudo systemctl start mysql.service
sudo apt-get install -y mysql-client
mysql --host=127.0.0.1 --port=3306 --user=root --password=root -e 'CREATE DATABASE IF NOT EXISTS cidb;'
mysql --host=127.0.0.1 --port=3306 --user=root --password=root -e 'show databases;'
- name: Prepare blockchain nodes and certificates
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ dependencies {
if (gradleVer.startsWith("4")) {
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.22-rc.2-SNAPSHOT") {
compile("com.webank:weid-contract-java:1.2.22-rc.4-SNAPSHOT") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
// exclude group: "org.fisco-bcos", module: "web3sdk"
// exclude group: "org.fisco-bcos", module: "web3sdk-weevent"
Expand All @@ -170,7 +170,7 @@ dependencies {
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.22-rc.2-SNAPSHOT") {
compile("com.webank:weid-contract-java:1.2.22-rc.4-SNAPSHOT") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
// exclude group: "org.fisco-bcos", module: "web3sdk"
// exclude group: "org.fisco-bcos", module: "web3sdk-weevent"
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,14 @@ public enum ErrorCode {
"the authority issuer name already exists."
),

/**
* The issuer is not recognized.
*/
AUTHORITY_ISSUER_CONTRACT_ERROR_UNRECOGNIZED(
500204,
"this issuer has not been recognized yet."
),

/**
* The Specific Issuer Contract level error: already exists.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ public final class WeIdConstant {

public static final Integer ADD_PUBKEY_FAILURE_CODE = -1;

public static final Long RECOGNIZED_AUTHORITY_ISSUER_FLAG = 1L;

public static enum PublicKeyType {
RSA("RSA"),
SECP256K1("Secp256k1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class AuthorityIssuer {
*/
private List<Integer> extraInt;

/**
* Optional: whether this authority issuer is recognized by admin/committee, or not.
*/
private boolean recognized = false;

/**
* Constructor with initialized values.
*
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/webank/weid/rpc/AuthorityIssuerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.webank.weid.protocol.base.AuthorityIssuer;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs;
import com.webank.weid.protocol.request.RemoveAuthorityIssuerArgs;
import com.webank.weid.protocol.response.ResponseData;
Expand Down Expand Up @@ -63,6 +64,24 @@ public interface AuthorityIssuerService {
*/
ResponseData<Boolean> isAuthorityIssuer(String weId);

/**
* Recognize this WeID to be an authority issuer.
*
* @param weId the WeID
* @param weIdPrivateKey the private key set
* @return true if succeeds, false otherwise
*/
ResponseData<Boolean> recognizeAuthorityIssuer(String weId, WeIdPrivateKey weIdPrivateKey);

/**
* De-recognize this WeID to no longer be and authority issuer.
*
* @param weId the WeID
* @param weIdPrivateKey the private key set
* @return true if succeeds, false otherwise
*/
ResponseData<Boolean> deRecognizeAuthorityIssuer(String weId, WeIdPrivateKey weIdPrivateKey);

/**
* Query the authority issuer information from a given WeIdentity DID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.protocol.base.AuthorityIssuer;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs;
import com.webank.weid.protocol.request.RemoveAuthorityIssuerArgs;
import com.webank.weid.protocol.response.ResponseData;
Expand Down Expand Up @@ -112,7 +113,60 @@ public ResponseData<Boolean> isAuthorityIssuer(String weId) {
return authEngine.isAuthorityIssuer(addr);
} catch (Exception e) {
logger.error("check authority issuer id failed.", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR.getCode(),
e.getMessage());
}
}

/**
* Recognize this WeID to be an authority issuer.
*
* @param weId the WeID
* @param weIdPrivateKey the private key set
* @return true if succeeds, false otherwise
*/
@Override
public ResponseData<Boolean> recognizeAuthorityIssuer(String weId,
WeIdPrivateKey weIdPrivateKey) {
if (!weIdService.isWeIdExist(weId).getResult()) {
return new ResponseData<>(false, ErrorCode.WEID_DOES_NOT_EXIST);
}
if (!WeIdUtils.isPrivateKeyValid(weIdPrivateKey)) {
return new ResponseData<>(false, ErrorCode.WEID_PRIVATEKEY_INVALID);
}
String addr = WeIdUtils.convertWeIdToAddress(weId);
try {
return authEngine.recognizeWeId(true, addr, weIdPrivateKey.getPrivateKey());
} catch (Exception e) {
logger.error("Failed to recognize authority issuer.", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR.getCode(),
e.getMessage());
}
}

/**
* De-recognize this WeID to no longer be and authority issuer.
*
* @param weId the WeID
* @param weIdPrivateKey the private key set
* @return true if succeeds, false otherwise
*/
@Override
public ResponseData<Boolean> deRecognizeAuthorityIssuer(String weId,
WeIdPrivateKey weIdPrivateKey) {
if (!weIdService.isWeIdExist(weId).getResult()) {
return new ResponseData<>(false, ErrorCode.WEID_DOES_NOT_EXIST);
}
if (!WeIdUtils.isPrivateKeyValid(weIdPrivateKey)) {
return new ResponseData<>(false, ErrorCode.WEID_PRIVATEKEY_INVALID);
}
String addr = WeIdUtils.convertWeIdToAddress(weId);
try {
return authEngine.recognizeWeId(false, addr, weIdPrivateKey.getPrivateKey());
} catch (Exception e) {
logger.error("Failed to recognize authority issuer.", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR.getCode(),
e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ public ResponseData<Boolean> addIssuer(
);

public ResponseData<String> getWeIdFromOrgId(String orgId);

public ResponseData<Boolean> recognizeWeId(Boolean isRecognize, String addr, String privateKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public ResponseData<String> getWeIdFromOrgId(String orgId) {
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.FISCO_BCOS_VERSION_NOT_SUPPORTED);
}

@Override
public ResponseData<Boolean> recognizeWeId(Boolean type, String weId, String privateKey) {
return new ResponseData<>(false, ErrorCode.FISCO_BCOS_VERSION_NOT_SUPPORTED);
}

/**
* Use the given private key to send the transaction to call the contract.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,53 @@ public ResponseData<Boolean> removeAuthorityIssuer(RemoveAuthorityIssuerArgs arg
}
}

@Override
public ResponseData<Boolean> recognizeWeId(Boolean isRecognize, String addr,
String privateKey) {
try {
AuthorityIssuerController authorityIssuerController = reloadContract(
fiscoConfig.getIssuerAddress(),
privateKey,
AuthorityIssuerController.class);
TransactionReceipt receipt;
if (isRecognize) {
receipt = authorityIssuerController.recognizeAuthorityIssuer(addr).send();
} else {
receipt = authorityIssuerController.deRecognizeAuthorityIssuer(addr).send();
}
List<AuthorityIssuerRetLogEventResponse> eventList =
authorityIssuerController.getAuthorityIssuerRetLogEvents(receipt);
TransactionInfo info = new TransactionInfo(receipt);
AuthorityIssuerRetLogEventResponse event = eventList.get(0);
if (event != null) {
ErrorCode errorCode;
if (isRecognize) {
errorCode = verifyAuthorityIssuerRelatedEvent(
event,
WeIdConstant.ADD_AUTHORITY_ISSUER_OPCODE
);
} else {
errorCode = verifyAuthorityIssuerRelatedEvent(
event,
WeIdConstant.REMOVE_AUTHORITY_ISSUER_OPCODE
);
}
if (ErrorCode.SUCCESS.getCode() != errorCode.getCode()) {
return new ResponseData<>(false, errorCode, info);
} else {
return new ResponseData<>(true, errorCode, info);
}
} else {
logger.error("(de-)recognize authority issuer failed, event decoding failure.");
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR, info);
}

} catch (Exception e) {
logger.error("(de-)recognize authority issuer failed.", e);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR.getCode(),
e.getMessage());
}
}

/* (non-Javadoc)
* @see com.webank.weid.service.impl.engine.AuthorityIssuerController
Expand Down Expand Up @@ -315,6 +362,11 @@ public ResponseData<AuthorityIssuer> getAuthorityIssuerInfoNonAccValue(String we

// Accumulator Value is unable to load due to Solidity 0.4.4 restrictions - left blank.
result.setAccValue("");

// Set recognition status
boolean recognized = Long.valueOf(int256Attributes.get(15).longValue())
.equals(WeIdConstant.RECOGNIZED_AUTHORITY_ISSUER_FLAG) ? true : false;
result.setRecognized(recognized);
resultData.setResult(result);
return resultData;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class TestAddIssuerIntoIssuerType extends TestBaseService {

private static CreateWeIdDataResult createWeId;

private String issuerType = null;
private String issuerType = null;

@Override
public synchronized void testInit() {
public synchronized void testInit() {

super.testInit();
if (createWeId == null) {
Expand All @@ -35,29 +35,14 @@ public synchronized void testInit() {
}
}

/**
* case : test add issuer into issuer type success.
*/
@Test
public void testAddIssuerIntoIssuerType_success() {
WeIdAuthentication weIdAuthentication =
TestBaseUtil.buildWeIdAuthentication(createWeId);

ResponseData<Boolean> response = authorityIssuerService
.addIssuerIntoIssuerType(weIdAuthentication, issuerType, super.createWeId().getWeId());
LogUtil.info(logger, "addIssuerIntoIssuerType", response);

Assert.assertEquals(ErrorCode.SUCCESS.getCode(), response.getErrorCode().intValue());
Assert.assertTrue(response.getResult());
}

/**
* case : test repeat add issuer into issuer type success.
*/
@Test
public void testAddIssuerIntoIssuerType_repeatFail() {
WeIdAuthentication weIdAuthentication =
TestBaseUtil.buildWeIdAuthentication(createWeId);
weIdAuthentication.setWeIdPrivateKey(new WeIdPrivateKey(privateKey));
CreateWeIdDataResult weId = super.createWeId();

ResponseData<Boolean> response = authorityIssuerService
Expand Down Expand Up @@ -207,7 +192,7 @@ public void testAddIssuerIntoIssuerType_privateKeyBlank() {
public void testAddIssuerIntoIssuerType_otherPriKey() {
WeIdAuthentication weIdAuthentication = TestBaseUtil.buildWeIdAuthentication(createWeId);
weIdAuthentication.setWeIdPrivateKey(createWeIdResult.getUserWeIdPrivateKey());

ResponseData<Boolean> response = authorityIssuerService
.addIssuerIntoIssuerType(weIdAuthentication, issuerType, createWeIdResult.getWeId());
LogUtil.info(logger, "addIssuerIntoIssuerType", response);
Expand All @@ -224,6 +209,8 @@ public void testAddIssuerIntoIssuerType_otherPriKey() {
public void testAddIssuerIntoIssuerType_otherAuthorPriKey() {
WeIdAuthentication weIdAuthentication = TestBaseUtil.buildWeIdAuthentication(createWeId);
CreateWeIdDataResult weIdDataResult = super.registerAuthorityIssuer();
authorityIssuerService
.recognizeAuthorityIssuer(weIdDataResult.getWeId(), new WeIdPrivateKey(privateKey));
weIdAuthentication.setWeIdPrivateKey(weIdDataResult.getUserWeIdPrivateKey());

ResponseData<Boolean> response = authorityIssuerService
Expand All @@ -242,6 +229,7 @@ public void testAddIssuerIntoIssuerType_otherAuthorPriKey() {
public void testAddIssuerIntoIssuerType_PubKeyNull() {

WeIdAuthentication weIdAuthentication = TestBaseUtil.buildWeIdAuthentication(createWeId);
weIdAuthentication.setWeIdPrivateKey(new WeIdPrivateKey(privateKey));
weIdAuthentication.setWeIdPublicKeyId(null);

ResponseData<Boolean> response = authorityIssuerService
Expand All @@ -261,9 +249,10 @@ public void testAddIssuerIntoIssuerType_PubKeyContainChar() {
WeIdAuthentication weIdAuthentication = TestBaseUtil.buildWeIdAuthentication(createWeId);
char[] chars = new char[100];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char)(i % 127);
chars[i] = (char) (i % 127);
}
weIdAuthentication.setWeIdPublicKeyId(String.valueOf(chars));
weIdAuthentication.setWeIdPrivateKey(new WeIdPrivateKey(privateKey));

ResponseData<Boolean> response = authorityIssuerService
.addIssuerIntoIssuerType(weIdAuthentication, issuerType, super.createWeId().getWeId());
Expand Down

0 comments on commit ce48fb9

Please sign in to comment.