diff --git a/VERSION b/VERSION index 411a3f46..40e8402a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.8.6-rc.1-SNAPSHOT \ No newline at end of file +1.8.6-rc.2-SNAPSHOT \ No newline at end of file diff --git a/src/main/java/com/webank/weid/protocol/base/WeIdPojo.java b/src/main/java/com/webank/weid/protocol/base/WeIdPojo.java deleted file mode 100644 index beb112c8..00000000 --- a/src/main/java/com/webank/weid/protocol/base/WeIdPojo.java +++ /dev/null @@ -1,33 +0,0 @@ - - -package com.webank.weid.protocol.base; - -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -/** - * WeIdentity DID information for created. - * @author v_wbgyang - * - */ -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class WeIdPojo extends WeIdDocumentMetadata { - - /** - * the blockNum for the WeIdentity DID. - */ - private Integer currentBlockNum; - - /** - * the index for the blockNum. - */ - private Integer index; - - /** - * the previous blockNum. - */ - private Integer previousBlockNum; -} diff --git a/src/main/java/com/webank/weid/rpc/WeIdService.java b/src/main/java/com/webank/weid/rpc/WeIdService.java index 8ffe40e0..12be8bb4 100644 --- a/src/main/java/com/webank/weid/rpc/WeIdService.java +++ b/src/main/java/com/webank/weid/rpc/WeIdService.java @@ -30,8 +30,18 @@ public interface WeIdService { /** * Create a WeIdentity DID from the provided public key. + * A private key is required to send transaction, but may be not matching the given public key * - * @param createWeIdArgs you need to input a public key + * @param weIdPublicKey you need to input a public key + * @param weIdPrivateKey you need to input a private key + * @return a data set including a WeIdentity DID and a keypair + */ + ResponseData createWeIdByPublicKey(WeIdPublicKey weIdPublicKey, WeIdPrivateKey weIdPrivateKey); + + /** + * Create a WeIdentity DID from the provided public key. + * + *@param createWeIdArgs the create WeIdentity DID args * @return WeIdentity DID */ ResponseData createWeId(CreateWeIdArgs createWeIdArgs); diff --git a/src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java b/src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java index 39e9e96b..a418acf5 100644 --- a/src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java +++ b/src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java @@ -73,6 +73,46 @@ public ResponseData createWeId() { innerResp.getTransactionInfo()); } + /** + * Create a WeIdentity DID with null input param. + * + * @return the response data + */ + @Override + public ResponseData createWeIdByPublicKey(WeIdPublicKey weIdPublicKey, WeIdPrivateKey weIdPrivateKey) { + if (!WeIdUtils.isPrivateKeyValid(weIdPrivateKey) || !WeIdUtils + .isPrivateKeyLengthValid(weIdPrivateKey.getPrivateKey())) { + return new ResponseData<>(StringUtils.EMPTY, ErrorCode.WEID_PRIVATEKEY_INVALID); + } + String privateKey = weIdPrivateKey.getPrivateKey(); + String publicKey = weIdPublicKey.getPublicKey(); + if (StringUtils.isNotBlank(publicKey)) { + String weId = WeIdUtils.convertPublicKeyToWeId(publicKey); + ResponseData isWeIdExistResp = this.isWeIdExist(weId); + if (isWeIdExistResp.getResult() == null || isWeIdExistResp.getResult()) { + logger + .error("[createWeId]: create weid failed, the weid :{} is already exist", weId); + return new ResponseData<>(StringUtils.EMPTY, ErrorCode.WEID_ALREADY_EXIST); + } + ResponseData innerResp = processCreateWeId(weId, publicKey, privateKey); + if (innerResp.getErrorCode() != ErrorCode.SUCCESS.getCode()) { + logger.error( + "[createWeId]: create weid failed. error message is :{}, public key is {}", + innerResp.getErrorMessage(), + publicKey + ); + return new ResponseData<>(StringUtils.EMPTY, + ErrorCode.getTypeByErrorCode(innerResp.getErrorCode()), + innerResp.getTransactionInfo()); + } + return new ResponseData<>(weId, + ErrorCode.getTypeByErrorCode(innerResp.getErrorCode()), + innerResp.getTransactionInfo()); + } else { + return new ResponseData<>(StringUtils.EMPTY, ErrorCode.WEID_PUBLICKEY_INVALID); + } + } + /** * Create a WeIdentity DID. * @@ -93,7 +133,6 @@ public ResponseData createWeId(CreateWeIdArgs createWeIdArgs) { String privateKey = createWeIdArgs.getWeIdPrivateKey().getPrivateKey(); String publicKey = createWeIdArgs.getPublicKey(); if (StringUtils.isNotBlank(publicKey)) { - //替换国密 if (!WeIdUtils.isKeypairMatch(new BigInteger(privateKey), publicKey)) { return new ResponseData<>( StringUtils.EMPTY, diff --git a/src/main/java/com/webank/weid/service/impl/engine/WeIdServiceEngine.java b/src/main/java/com/webank/weid/service/impl/engine/WeIdServiceEngine.java index 4c596242..d75981c3 100644 --- a/src/main/java/com/webank/weid/service/impl/engine/WeIdServiceEngine.java +++ b/src/main/java/com/webank/weid/service/impl/engine/WeIdServiceEngine.java @@ -1,13 +1,10 @@ package com.webank.weid.service.impl.engine; - -import java.security.NoSuchAlgorithmException; import java.util.List; import com.webank.weid.protocol.base.WeIdDocument; import com.webank.weid.protocol.base.WeIdDocumentMetadata; -import com.webank.weid.protocol.base.WeIdPojo; import com.webank.weid.protocol.response.ResponseData; /** diff --git a/src/test/java/com/webank/weid/full/weid/TestCreateWeId1.java b/src/test/java/com/webank/weid/full/weid/TestCreateWeId1.java index eb83b88b..8c960838 100644 --- a/src/test/java/com/webank/weid/full/weid/TestCreateWeId1.java +++ b/src/test/java/com/webank/weid/full/weid/TestCreateWeId1.java @@ -4,6 +4,9 @@ import java.security.NoSuchProviderException; +import com.webank.weid.full.TestBaseUtil; +import com.webank.weid.protocol.base.WeIdPublicKey; +import com.webank.weid.protocol.request.CreateWeIdArgs; import mockit.Mock; import mockit.MockUp; import org.junit.Assert; @@ -56,4 +59,34 @@ public void testCreateWeId_doubleCreateSucess() { Assert.assertNotEquals(response.getResult().getWeId(), response1.getResult().getWeId()); } + /** + * case: create WeId success. + */ + @Test + public void testCreateWeIdByPublicKey_createSucess() { + CreateWeIdArgs createWeIdArgs = TestBaseUtil.buildCreateWeIdArgs(); + WeIdPublicKey weIdPublicKey = new WeIdPublicKey(); + weIdPublicKey.setPublicKey(createWeIdArgs.getPublicKey()); + ResponseData response = weIdService.createWeIdByPublicKey(weIdPublicKey, createWeIdArgs.getWeIdPrivateKey()); + LogUtil.info(logger, "createWeId", response); + + Assert.assertEquals(ErrorCode.SUCCESS.getCode(), response.getErrorCode().intValue()); + Assert.assertNotNull(response.getResult()); + } + + /** + * case: create WeId success, public key not match with private key. + */ + @Test + public void testCreateWeIdByPublicKey_createSucess2() { + CreateWeIdArgs createWeIdArgs = TestBaseUtil.buildCreateWeIdArgs(); + WeIdPublicKey weIdPublicKey = new WeIdPublicKey(); + weIdPublicKey.setPublicKey(createWeIdArgs.getPublicKey()); + ResponseData response = weIdService.createWeIdByPublicKey(weIdPublicKey, createWeIdNew.getUserWeIdPrivateKey()); + LogUtil.info(logger, "createWeId", response); + + Assert.assertEquals(ErrorCode.SUCCESS.getCode(), response.getErrorCode().intValue()); + Assert.assertNotNull(response.getResult()); + } + }