Skip to content

Commit

Permalink
Merge pull request #175 from WeBankFinTech/feature/adapt-contract-1218
Browse files Browse the repository at this point in the history
* Adapt to latest WeID-contract-java 1.2.18
  • Loading branch information
chaoxinhu committed Apr 24, 2020
2 parents dd6a234 + 4fbb196 commit 83da944
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 10 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,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.17") {
compile("com.webank:weid-contract-java:1.2.18") {
exclude group: "org.fisco-bcos", module: "web3sdk"
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
Expand All @@ -155,7 +155,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.17") {
compile("com.webank:weid-contract-java:1.2.18") {
exclude group: "org.fisco-bcos", module: "web3sdk"
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/webank/weid/rpc/AuthorityIssuerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,12 @@ ResponseData<List<String>> getAllSpecificTypeIssuerList(
Integer index,
Integer num
);

/**
* Get an issuer's WeID from its name (org ID).
*
* @param orgId the org id
* @return WeID
*/
ResponseData<String> getWeIdByOrgId(String orgId);
}
14 changes: 11 additions & 3 deletions src/main/java/com/webank/weid/rpc/EvidenceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

package com.webank.weid.rpc;

import java.util.List;
import java.util.Map;

import com.webank.weid.protocol.base.EvidenceInfo;
import com.webank.weid.protocol.base.HashString;
import com.webank.weid.protocol.base.WeIdPrivateKey;
Expand Down Expand Up @@ -139,4 +136,15 @@ ResponseData<Boolean> addLogByCustomKey(
*/
ResponseData<Boolean> verifySigner(EvidenceInfo evidenceInfo, String weId, String publicKey);

/**
* A direct pass-thru method to create raw evidence where all inputs can be customized.
*/
ResponseData<Boolean> createRawEvidenceWithCustomKey(
String hashValue,
String signature,
String log,
Long timestamp,
String extraKey,
String privateKey
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,17 @@ private boolean isValidAuthorityIssuerName(String name) {
< WeIdConstant.MAX_AUTHORITY_ISSUER_NAME_LENGTH
&& !StringUtils.isWhitespace(name);
}

@Override
public ResponseData<String> getWeIdByOrgId(String orgId) {
if (!isValidAuthorityIssuerName(orgId)) {
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.AUTHORITY_ISSUER_NAME_ILLEGAL);
}
try {
return authEngine.getWeIdFromOrgId(orgId);
} catch (Exception e) {
logger.error("Failed to get WeID, Error Message:{}", e);
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.AUTHORITY_ISSUER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ public class EvidenceServiceImpl extends AbstractService implements EvidenceServ

private WeIdService weIdService = new WeIdServiceImpl();

@Override
public ResponseData<Boolean> createRawEvidenceWithCustomKey(
String hashValue,
String signature,
String log,
Long timestamp,
String extraKey,
String privateKey
) {
ResponseData<String> hashResp = evidenceServiceEngine.createEvidenceWithCustomKey(
hashValue,
signature,
log,
timestamp,
extraKey,
privateKey
);
if (hashResp.getResult().equalsIgnoreCase(hashValue)) {
return new ResponseData<>(true, ErrorCode.SUCCESS);
} else {
return new ResponseData<>(false, hashResp.getErrorCode(), hashResp.getErrorMessage());
}
}

/**
* Create a new evidence to the blockchain and get the evidence address.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author tonychen 2019年6月25日
*/
public interface AuthorityIssuerServiceEngine extends ReloadStaticContract {

/**
* call authority issuer contract method to add an authority issuer.
*
Expand Down Expand Up @@ -134,4 +134,6 @@ public ResponseData<Boolean> addIssuer(
String issuerAddress,
String privateKey
);

public ResponseData<String> getWeIdFromOrgId(String orgId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ public void reload() {
AuthorityIssuerController.class);
specificIssuerController = getContractService(
fiscoConfig.getSpecificIssuerAddress(),
SpecificIssuerController.class);
SpecificIssuerController.class);
}

@Override
public ResponseData<String> getWeIdFromOrgId(String orgId) {
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.FISCO_BCOS_VERSION_NOT_SUPPORTED);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AuthorityIssuerEngineV2() {
reload();
}
}

/**
* 重新加载静态合约对象.
*/
Expand All @@ -78,6 +78,24 @@ public void reload() {
SpecificIssuerController.class);
}

@Override
public ResponseData<String> getWeIdFromOrgId(String orgId) {
try {
byte[] name = new byte[32];
System.arraycopy(orgId.getBytes(), 0, name, 0, orgId.getBytes().length);
String address = authorityIssuerController
.getAddressFromName(name).send();
if (WeIdConstant.EMPTY_ADDRESS.equalsIgnoreCase(address)) {
return new ResponseData<>(StringUtils.EMPTY,
ErrorCode.AUTHORITY_ISSUER_CONTRACT_ERROR_NOT_EXISTS);
}
return new ResponseData<>(WeIdUtils.convertAddressToWeId(address), ErrorCode.SUCCESS);
} catch (Exception e) {
logger.error("get authority issuer WeID failed.", e);
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.AUTHORITY_ISSUER_ERROR);
}
}

/* (non-Javadoc)
* @see com.webank.weid.service.impl.engine.AuthorityIssuerController
* #addAuthorityIssuer(com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ public ResponseData<String> createEvidenceWithCustomKey(
ErrorCode.CREDENTIAL_EVIDENCE_BASE_ERROR, info);
} else {
for (EvidenceAttributeChangedEventResponse event : eventList) {
if (event.sigs.get(0).equalsIgnoreCase(signature)
&& event.signer.get(0).equalsIgnoreCase(address)) {
if (event.sigs.toArray()[0].toString().equalsIgnoreCase(signature)
&& event.signer.toArray()[0].toString().equalsIgnoreCase(address)) {
return new ResponseData<>(hashValue, ErrorCode.SUCCESS, info);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ public void testRegisterAuthorityIssuerChineseName() {
ResponseData<AuthorityIssuer> queryResponse = authorityIssuerService
.queryAuthorityIssuerInfo(weId.getWeId());
Assert.assertTrue(queryResponse.getResult().getName().equalsIgnoreCase(chiName));
ResponseData<String> weIdresp = authorityIssuerService.getWeIdByOrgId(chiName);
Assert.assertTrue(weIdresp.getResult().equalsIgnoreCase(weId.getWeId()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.webank.weid.protocol.inf.Hashable;
import com.webank.weid.protocol.response.CreateWeIdDataResult;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.rpc.EvidenceService;
import com.webank.weid.service.impl.EvidenceServiceImpl;
import com.webank.weid.service.impl.engine.EngineFactory;
import com.webank.weid.service.impl.engine.EvidenceServiceEngine;
Expand Down Expand Up @@ -361,6 +362,23 @@ public void testBatchCreate() throws Exception {
Assert.assertEquals(evidenceInfo1.getCredentialHash(), evidenceInfo1k.getCredentialHash());
}

@Test
public void testRawCreation() {
CredentialPojo credential = createCredentialPojo(createCredentialPojoArgs);
credential.setId(UUID.randomUUID().toString());
String hash = credential.getHash();
String sig = "testSig";
String log = "";
String customKey = credential.getId();
ResponseData<Boolean> resp = evidenceService
.createRawEvidenceWithCustomKey(hash, sig, log, System.currentTimeMillis(), customKey,
privateKey);
Assert.assertTrue(resp.getResult());
ResponseData<EvidenceInfo> eviResp = evidenceService.getEvidenceByCustomKey(customKey);
System.out.println();
Assert.assertTrue(eviResp.getResult().getSignatures().get(0).equalsIgnoreCase(sig));
}

/**
* case3: weIdPrivateKey is null.
*/
Expand Down

0 comments on commit 83da944

Please sign in to comment.