Skip to content

Commit

Permalink
Merge pull request #287 from anyspa/feature/support-gm
Browse files Browse the repository at this point in the history
support fisco gm
  • Loading branch information
junqizhang-dev committed Jan 11, 2021
2 parents 0db4ea9 + d4c3791 commit 5fef93f
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 149 deletions.
2 changes: 1 addition & 1 deletion docs/zh_CN/docs/weidentity-java-sdk-doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13752,7 +13752,7 @@ com.webank.weid.protocol.base.CredentialPojo
// Enforce a Register/Update system CPT first
WeIdAuthentication sdkAuthen = new WeIdAuthentication();
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(privateKey);
String keyWeId = WeIdUtils
.convertAddressToWeId(new Address(Keys.getAddress(keyPair)).toString());
sdkAuthen.setWeId(keyWeId);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/webank/weid/config/FiscoConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ public class FiscoConfig {
@NotNull(message = "the v2.node-key-path is undefined")
@NotEmpty(message = "the value of v2.node-key-path is null")
private String v2NodeKeyPath;

@NotNull(message = "the gm.ca-crt-path is undefined")
@NotEmpty(message = "the value of gm.ca-crt-path is null")
private String gmCaCrtPath;

@NotNull(message = "the gm.sdk-crt-path is undefined")
@NotEmpty(message = "the value of gm.sdk-crt-path is null")
private String gmSdkCrtPath;

@NotNull(message = "the gm.sdk-key-path is undefined")
@NotEmpty(message = "the value of gm.sdk-key-path is null")
private String gmSdkKeyPath;

@NotNull(message = "the gmen.sdk-crt-path is undefined")
@NotEmpty(message = "the value of gmen.sdk-crt-path is null")
private String gmenSdkCrtPath;

@NotNull(message = "the gmen.sdk-key-path is undefined")
@NotEmpty(message = "the value of gmen.sdk-key-path is null")
private String gmenSdkKeyPath;

@NotNull(message = "the blockchain.orgid is undefined")
@NotEmpty(message = "the value of blockchain.orgid is null")
Expand Down Expand Up @@ -174,6 +194,11 @@ public boolean load() {
v2CaCrtPath = PropertyUtils.getProperty("v2.ca-crt-path");
v2NodeCrtPath = PropertyUtils.getProperty("v2.node-crt-path");
v2NodeKeyPath = PropertyUtils.getProperty("v2.node-key-path");
gmCaCrtPath = PropertyUtils.getProperty("gm.ca-crt-path");
gmSdkCrtPath = PropertyUtils.getProperty("gm.sdk-crt-path");
gmSdkKeyPath = PropertyUtils.getProperty("gm.sdk-key-path");
gmenSdkCrtPath = PropertyUtils.getProperty("gmen.sdk-crt-path");
gmenSdkKeyPath = PropertyUtils.getProperty("gmen.sdk-key-path");
currentOrgId = PropertyUtils.getProperty("blockchain.orgid");
amopId = PropertyUtils.getProperty("amop.id");
cnsContractFollow = PropertyUtils.getProperty("cns.contract.follow");
Expand Down
35 changes: 29 additions & 6 deletions src/main/java/com/webank/weid/service/fisco/WeServerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.handler.ChannelConnections;
import org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig;
import org.fisco.bcos.web3j.crypto.EncryptType;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -146,12 +147,34 @@ public static GroupChannelConnectionsConfig buildGroupChannelConnectionsConfig(
ChannelConnections channelConnections = new ChannelConnections();
channelConnections.setGroupId(groupId);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
channelConnections
.setCaCert(resolver.getResource("classpath:" + fiscoConfig.getV2CaCrtPath()));
channelConnections
.setSslCert(resolver.getResource("classpath:" + fiscoConfig.getV2NodeCrtPath()));
channelConnections
.setSslKey(resolver.getResource("classpath:" + fiscoConfig.getV2NodeKeyPath()));

if (fiscoConfig.getEncryptType().equals(String.valueOf(EncryptType.ECDSA_TYPE))) {
EncryptType encryptType = new EncryptType(EncryptType.ECDSA_TYPE);
channelConnections.setCaCert(
resolver.getResource("classpath:" + fiscoConfig.getV2CaCrtPath()));
channelConnections.setSslCert(
resolver.getResource("classpath:" + fiscoConfig.getV2NodeCrtPath()));
channelConnections.setSslKey(
resolver.getResource("classpath:" + fiscoConfig.getV2NodeKeyPath()));
} else {
EncryptType encryptType = new EncryptType(EncryptType.SM2_TYPE);

// gmca.crt
channelConnections.setGmCaCert(
resolver.getResource("classpath:" + fiscoConfig.getGmCaCrtPath()));
// gmsdk.crt
channelConnections.setGmSslCert(
resolver.getResource("classpath:" + fiscoConfig.getGmSdkCrtPath()));
// gmsdk.key
channelConnections.setGmSslKey(
resolver.getResource("classpath:" + fiscoConfig.getGmSdkKeyPath()));
// gmensdk.crt
channelConnections.setGmEnSslCert(
resolver.getResource("classpath:" + fiscoConfig.getGmenSdkCrtPath()));
// gmensdk.key
channelConnections.setGmEnSslKey(
resolver.getResource("classpath:" + fiscoConfig.getGmenSdkKeyPath()));
}

channelConnections.setConnectionsStr(nodeList);
GroupChannelConnectionsConfig connectionsConfig = new GroupChannelConnectionsConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import org.fisco.bcos.channel.client.ChannelPushCallback;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.dto.ChannelRequest;
Expand Down Expand Up @@ -51,6 +52,7 @@
import com.webank.weid.service.fisco.WeServer;
import com.webank.weid.service.fisco.WeServerUtils;
import com.webank.weid.service.impl.base.AmopCommonArgs;
import com.webank.weid.util.DataToolUtils;

public final class WeServerV2 extends WeServer<Web3j, Credentials, Service> {

Expand Down Expand Up @@ -89,8 +91,8 @@ public Credentials getCredentials() {
public Credentials createCredentials(String privateKey) {
Credentials credentials;
try {
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
credentials = Credentials.create(keyPair);
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(new BigInteger(privateKey));
credentials = GenCredential.create(keyPair);
return credentials;
} catch (Exception e) {
throw new PrivateKeyIllegalException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ public ResponseData<CredentialPojo> addSignature(
return new ResponseData<>(null, ErrorCode.WEID_DOES_NOT_EXIST);
}
String privateKey = callerAuth.getWeIdPrivateKey().getPrivateKey();
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(new BigInteger(privateKey));
String keyWeId = WeIdUtils
.convertAddressToWeId(new Address(Keys.getAddress(keyPair)).toString());
result.setIssuer(keyWeId);
Expand Down Expand Up @@ -1985,7 +1985,7 @@ public ResponseData<CredentialPojo> createTrustedTimestamp(
CredentialPojo credential = new CredentialPojo();
credential.setCptId(CredentialConstant.EMBEDDED_TIMESTAMP_CPT);
String privateKey = weIdAuthentication.getWeIdPrivateKey().getPrivateKey();
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(new BigInteger(privateKey));
String keyWeId = WeIdUtils
.convertAddressToWeId(new Address(Keys.getAddress(keyPair)).toString());
credential.setIssuer(keyWeId);
Expand Down Expand Up @@ -2249,7 +2249,7 @@ public ResponseData<CredentialPojo> createDataAuthToken(
args.setContext(CredentialUtils.getDefaultCredentialContext());
args.setCptId(CredentialConstant.AUTHORIZATION_CPT);
String privateKey = weIdAuthentication.getWeIdPrivateKey().getPrivateKey();
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(new BigInteger(privateKey));
String keyWeId = WeIdUtils
.convertAddressToWeId(new Address(Keys.getAddress(keyPair)).toString());
args.setIssuer(keyWeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public ResponseData<Credential> addSignature(
result.setExpirationDate(newExpirationDate);
}
String privateKey = weIdPrivateKey.getPrivateKey();
ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey));
ECKeyPair keyPair = DataToolUtils.createKeyPairFromPrivate(new BigInteger(privateKey));
String keyWeId = WeIdUtils
.convertAddressToWeId(new Address(Keys.getAddress(keyPair)).toString());
if (!weIdService.isWeIdExist(keyWeId).getResult()) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.fisco.bcos.web3j.crypto.ECKeyPair;
import org.fisco.bcos.web3j.crypto.Keys;
import org.fisco.bcos.web3j.crypto.gm.GenCredential;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -71,12 +72,10 @@ public class WeIdServiceImpl extends AbstractService implements WeIdService {
public ResponseData<CreateWeIdDataResult> createWeId() {

CreateWeIdDataResult result = new CreateWeIdDataResult();
ECKeyPair keyPair = null;
ECKeyPair keyPair = GenCredential.createKeyPair();

try {
keyPair = Keys.createEcKeyPair();
} catch (Exception e) {
logger.error("Create weId failed.", e);
if (Objects.isNull(keyPair)) {
logger.error("Create weId failed.");
return new ResponseData<>(null, ErrorCode.WEID_KEYPAIR_CREATE_FAILED);
}

Expand Down

0 comments on commit 5fef93f

Please sign in to comment.