Skip to content

Commit

Permalink
add createWeIdByPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
youwenbusi committed Sep 26, 2022
1 parent a97b5aa commit 13bffa6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.6-rc.1-SNAPSHOT
1.8.6-rc.2-SNAPSHOT
33 changes: 0 additions & 33 deletions src/main/java/com/webank/weid/protocol/base/WeIdPojo.java

This file was deleted.

12 changes: 11 additions & 1 deletion src/main/java/com/webank/weid/rpc/WeIdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> createWeId(CreateWeIdArgs createWeIdArgs);
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@ public ResponseData<CreateWeIdDataResult> createWeId() {
innerResp.getTransactionInfo());
}

/**
* Create a WeIdentity DID with null input param.
*
* @return the response data
*/
@Override
public ResponseData<String> 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<Boolean> 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<Boolean> 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.
*
Expand All @@ -93,7 +133,6 @@ public ResponseData<String> 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/webank/weid/full/weid/TestCreateWeId1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> response = weIdService.createWeIdByPublicKey(weIdPublicKey, createWeIdNew.getUserWeIdPrivateKey());
LogUtil.info(logger, "createWeId", response);

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

}

0 comments on commit 13bffa6

Please sign in to comment.