diff --git a/Changelog.md b/Changelog.md index ae02e0557..f75343dca 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,19 @@ +### v3.1.1(2024-01-19) + +**Add** +- 系统配置中增加`auth_check_status`配置,用于启用权限控制功能 + +**Fix** +- 修复预编译合约中报错权限不足的bug,如修改共识类型 + +**兼容性** +- 支持FISCO-BCOS v3.0.0 及以上版本 +- WeBASE-Web v3.1.1及以上版本 +- WeBASE-Node-Manager v3.1.0 + +详细了解,请阅读[**技术文档**](https://webasedoc.readthedocs.io/zh_CN/lab/)。 + + ### v3.1.0(2023-07-20) **Add** @@ -10,7 +26,7 @@ **兼容性** - 支持FISCO-BCOS v3.0.0 及以上版本 -- WeBASE-Node-Manager v3.0.2及以上版本 +- WeBASE-Node-Manager v3.1.0及以上版本 - WeBASE-Sign v3.0.2 详细了解,请阅读[**技术文档**](https://webasedoc.readthedocs.io/zh_CN/lab/)。 diff --git a/release_note.txt b/release_note.txt index 6c8dc7eb2..70cdc0898 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1 +1 @@ -v3.1.0 +v3.1.1 \ No newline at end of file diff --git a/src/main/java/com/webank/webase/front/base/config/Web3Config.java b/src/main/java/com/webank/webase/front/base/config/Web3Config.java index 651a98124..001f0f28e 100644 --- a/src/main/java/com/webank/webase/front/base/config/Web3Config.java +++ b/src/main/java/com/webank/webase/front/base/config/Web3Config.java @@ -42,6 +42,7 @@ public class Web3Config { private String threadPoolSize; private String certPath; private String useSmSsl; + private String disableSsl; private List peers; @@ -50,6 +51,7 @@ public ConfigOption getConfigOptionFromFile() throws ConfigException { log.info("start init ConfigProperty"); // cert config, encrypt type Map cryptoMaterial = new HashMap<>(); + cryptoMaterial.put("disableSsl", disableSsl); cryptoMaterial.put("certPath", certPath); cryptoMaterial.put("useSMCrypto", useSmSsl); log.info("init cert cryptoMaterial:{}, (using conf as cert path)", JsonUtils.objToString(cryptoMaterial)); diff --git a/src/main/java/com/webank/webase/front/configapi/ConfigController.java b/src/main/java/com/webank/webase/front/configapi/ConfigController.java index 222bf2311..97c4e7c86 100644 --- a/src/main/java/com/webank/webase/front/configapi/ConfigController.java +++ b/src/main/java/com/webank/webase/front/configapi/ConfigController.java @@ -16,28 +16,18 @@ package com.webank.webase.front.configapi; -import com.webank.webase.front.base.code.ConstantCode; import com.webank.webase.front.base.config.Web3Config; import com.webank.webase.front.base.properties.VersionProperties; -import com.webank.webase.front.base.response.BaseResponse; -import com.webank.webase.front.configapi.entity.ConfigInfo; -import com.webank.webase.front.configapi.entity.ReqSdkConfig; import com.webank.webase.front.version.VersionService; import com.webank.webase.front.web3api.Web3ApiService; import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import java.time.Duration; -import java.time.Instant; -import java.util.Map; -import javax.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; /** * get or update config local @@ -57,7 +47,8 @@ public class ConfigController { private VersionService versionService; @Autowired private ConfigService configService; - + @Autowired + private RestTemplate restTemplate; @GetMapping("encrypt/{groupId}") public Integer getEncryptType(@PathVariable("groupId") String groupId) { @@ -116,4 +107,6 @@ public String getSignVersion() { return versionService.getSignServerVersion(); } + + } diff --git a/src/main/java/com/webank/webase/front/tool/ToolController.java b/src/main/java/com/webank/webase/front/tool/ToolController.java index 9dfb3c3d5..c4e6213d9 100644 --- a/src/main/java/com/webank/webase/front/tool/ToolController.java +++ b/src/main/java/com/webank/webase/front/tool/ToolController.java @@ -1,225 +1,239 @@ -///** -// * Copyright 2014-2020 the original author or authors. -// *

-// * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -// * in compliance with the License. You may obtain a copy of the License at -// *

-// * http://www.apache.org/licenses/LICENSE-2.0 -// *

-// * Unless required by applicable law or agreed to in writing, software distributed under the License -// * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -// * or implied. See the License for the specific language governing permissions and limitations under -// * the License. -// */ -// -//package com.webank.webase.front.tool; -// -//import com.webank.webase.front.base.code.ConstantCode; -//import com.webank.webase.front.base.exception.FrontException; -//import com.webank.webase.front.base.response.BaseResponse; -//import com.webank.webase.front.tool.entity.ReqDecodeParam; -//import com.webank.webase.front.tool.entity.ReqPrivateKey; -//import com.webank.webase.front.tool.entity.ReqSign; -//import com.webank.webase.front.tool.entity.RspHash; -//import com.webank.webase.front.tool.entity.RspKeyPair; -//import com.webank.webase.front.tool.entity.RspSignData; -//import com.webank.webase.front.util.CommonUtils; -//import com.webank.webase.front.util.JsonUtils; -//import com.webank.webase.front.web3api.Web3ApiService; -//import io.swagger.annotations.Api; -//import io.swagger.annotations.ApiImplicitParam; -//import io.swagger.annotations.ApiImplicitParams; -//import io.swagger.annotations.ApiOperation; -//import java.io.ByteArrayInputStream; -//import java.io.IOException; -//import javax.validation.Valid; -//import lombok.extern.slf4j.Slf4j; -//import org.apache.commons.lang3.StringUtils; -//import org.fisco.bcos.sdk.v3.codec.ContractCodec; -//import org.fisco.bcos.sdk.v3.codec.ContractCodecException; -//import org.fisco.bcos.sdk.v3.codec.datatypes.generated.Bytes32; -//import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; -//import org.fisco.bcos.sdk.v3.crypto.keypair.ECDSAKeyPair; -//import org.fisco.bcos.sdk.v3.crypto.keypair.SM2KeyPair; -//import org.fisco.bcos.sdk.v3.crypto.keystore.KeyTool; -//import org.fisco.bcos.sdk.v3.crypto.keystore.P12KeyStore; -//import org.fisco.bcos.sdk.v3.crypto.keystore.PEMKeyStore; -//import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; -//import org.fisco.bcos.sdk.v3.model.CryptoType; -//import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderService; -//import org.fisco.bcos.sdk.v3.utils.Numeric; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.beans.factory.annotation.Qualifier; -//import org.springframework.web.bind.annotation.GetMapping; -//import org.springframework.web.bind.annotation.PostMapping; -//import org.springframework.web.bind.annotation.RequestBody; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.RequestParam; -//import org.springframework.web.bind.annotation.RestController; -//import org.springframework.web.multipart.MultipartFile; -// -// -//@Api(value = "/tool", tags = "tools controller") -//@Slf4j -//@RestController -//@RequestMapping("tool") -//public class ToolController { -// -// @Autowired -// private Web3ApiService web3ApiService; -// -// @ApiOperation(value = "decode input/output", notes = "decode tx receipt's input/output") -// @ApiImplicitParam(name = "param", value = "param to be transfer", required = true, dataType = "ReqDecodeParam") -// @PostMapping("/decode") -// public Object decode(@Valid @RequestBody ReqDecodeParam param) { -// log.info("decode output start. param:{}", JsonUtils.toJSONString(param)); -// // todo 自测返回值 -// TransactionDecoderService txDecoder = new TransactionDecoderService(web3ApiService.getCryptoSuite(groupId), false); -// // decode input -// if (param.getDecodeType() == 1) { -// return txDecoder.decodeReceiptMessage(param.getInput()); -// } else if (param.getDecodeType() == 2) { -// String abi = JsonUtils.objToString(param.getAbiList()); -// ContractCodec abiCodec = new ContractCodec(web3ApiService.getCryptoSuite(groupId), false); -// // decode output -// try { -// return abiCodec.decodeMethodAndGetOutputObject(abi, param.getMethodName(), -// param.getOutput()); -// } catch (ContractCodecException e) { -// log.error("abi decode fail:{}", e.getMessage()); -// throw new FrontException(ConstantCode.CONTRACT_ABI_PARSE_JSON_ERROR); -// } -// } -// return new BaseResponse(ConstantCode.PARAM_ERROR); -// } -// -// @ApiOperation(value = "get key pair", notes = "generate key pair of front's encrypt type") -// @ApiImplicitParam(name = "param", value = "private key(hex string), representation of BigInteger", dataType = "ReqPrivateKey") -// @PostMapping("/keypair") -// public RspKeyPair getKeyPair(@Valid @RequestBody ReqPrivateKey param) { -// String privateKey = param.getPrivateKey(); -// CryptoKeyPair keyPair; -// if (StringUtils.isNotBlank(privateKey)) { -// keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); -// } else { -// keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().generateKeyPair(); -// } -// return new RspKeyPair(keyPair, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); -// } -// -// @ApiOperation(value = "get public key's address", notes = "get address from pub") -// @ApiImplicitParam(name = "publicKey", value = "pub key(hex string format), representation of BigInteger", dataType = "String") -// @GetMapping("/address") -// public RspKeyPair getKey(@RequestParam String publicKey) { -// RspKeyPair response = new RspKeyPair(); -// if (web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig == CryptoType.SM_TYPE) { -// response.setAddress(SM2KeyPair.getAddressByPublicKey(publicKey)); -// } else { -// response.setAddress(ECDSAKeyPair.getAddressByPublicKey(publicKey)); -// } -// response.setEncryptType(web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); -// response.setPublicKey(publicKey); -// return response; -// } -// -// @ApiOperation(value = "get hash value", notes = "get hash value") -// @ApiImplicitParam(name = "input", value = "input to hash(hexString)", dataType = "String") -// @GetMapping("/hash") -// public RspHash getHashValue(@RequestParam String input) { -// String hashValue = web3ApiService.getCryptoSuite(groupId).hash(input); -// return new RspHash(hashValue, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); -// } -// -// -// @ApiOperation(value = "get private key of pem", notes = "get private key of pem") -// @ApiImplicitParam(name = "pemFile", value = ".pem file of private key", dataType = "MultipartFile") -// @PostMapping("/decodePem") -// public RspKeyPair decodePem(@RequestParam MultipartFile pemFile) { -// if (pemFile.getSize() == 0) { -// throw new FrontException(ConstantCode.PEM_FORMAT_ERROR); -// } -// String privateKey; -// try { -// PEMKeyStore pemManager = new PEMKeyStore(new ByteArrayInputStream(pemFile.getBytes())); -// privateKey = KeyTool.getHexedPrivateKey(pemManager.getKeyPair().getPrivate()); -// } catch (IOException e) { -// log.error("decodePem error:[]", e); -// throw new FrontException(ConstantCode.PEM_CONTENT_ERROR); -// } -// -// return getRspKeyPair(privateKey); -// } -// -// @ApiOperation(value = "get PrivateKey of p12", notes = "get PrivateKey by p12") -// @ApiImplicitParams({ -// @ApiImplicitParam(name = "p12File", value = ".p12 file of private key", dataType = "MultipartFile"), -// @ApiImplicitParam(name = "p12Password", value = ".p12 file password", dataType = "String") -// }) -// @PostMapping("/decodeP12") -// public RspKeyPair decodeP12(@RequestParam MultipartFile p12File, -// @RequestParam(required = false, defaultValue = "") String p12Password) { -// if (!CommonUtils.notContainsChinese(p12Password)) { -// throw new FrontException(ConstantCode.P12_PASSWORD_NOT_CHINESE); -// } -// if (p12File.getSize() == 0) { -// throw new FrontException(ConstantCode.P12_FILE_ERROR); -// } -// String privateKey; -// try { -// P12KeyStore p12Manager = new P12KeyStore(new ByteArrayInputStream(p12File.getBytes()), p12Password); -// privateKey = KeyTool.getHexedPrivateKey(p12Manager.getKeyPair().getPrivate()); -// } catch (IOException e) { -// log.error("decodeP12 error:[]", e); -// if (e.getMessage().contains("password")) { -// throw new FrontException(ConstantCode.P12_PASSWORD_ERROR); -// } -// throw new FrontException(ConstantCode.P12_FILE_ERROR); -// } -// return getRspKeyPair(privateKey); -// } -// -// private RspKeyPair getRspKeyPair(String privateKey) { -// CryptoKeyPair keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); -// return new RspKeyPair(keyPair, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); -// } -// -// @ApiOperation(value = "get hash value", notes = "get hash value") -// @ApiImplicitParams({ -// @ApiImplicitParam(name = "input", value = "input to hash(hexString or utf-8)", dataType = "String"), -// @ApiImplicitParam(name = "type", value = " input type input type 1-hexString,2-utf8(default hex)", dataType = "Integer") -// }) -// @GetMapping("/convert2Bytes32") -// public String getBytes32FromStr(@RequestParam String input, @RequestParam(defaultValue = "1") Integer type) { -// Bytes32 bytes32; -// if (type == 1) { -// // hex input todo use java sdk -// bytes32 = CommonUtils.hexStrToBytes32(input); -// } else if (type == 2) { -// // utf8 input -// bytes32 = CommonUtils.utf8StringToBytes32(input); -// } else { -// throw new FrontException(ConstantCode.PARAM_ERROR); -// } -// return Hex.toHexString(bytes32.getValue()); -// } -// -// @ApiOperation(value = "get utf8's hex string", notes = "get hex string") -// @ApiImplicitParam(name = "input", value = "input to convert(utf8 String)", dataType = "String") -// @GetMapping("/utf8ToHexString") -// public String getHexStringFromUtf8(@RequestParam String input) { -// return CommonUtils.utf8StringToHex(input); -// } -// -// @ApiOperation(value = "sign raw data", notes = "sign raw data by private key") -// @ApiImplicitParam(name = "reqSign", value = "raw data & private key", dataType = "ReqSign") -// @PostMapping("/signMsg") -// public RspSignData getSignedData(@Valid @RequestBody ReqSign reqSign) { -// String privateKey = reqSign.getPrivateKey(); -// String rawData = reqSign.getRawData(); -// CryptoKeyPair cryptoKeyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); -// SignatureResult signatureData = web3ApiService.getCryptoSuite(groupId).sign(Hex.toHexString(rawData.getBytes()), cryptoKeyPair); -// return new RspSignData(signatureData, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); -// } -// -// -//} +/** + * Copyright 2014-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.webank.webase.front.tool; + +import com.webank.webase.front.base.code.ConstantCode; +import com.webank.webase.front.base.exception.FrontException; +import com.webank.webase.front.base.response.BaseResponse; +import com.webank.webase.front.tool.entity.ReqDecodeParam; +import com.webank.webase.front.tool.entity.ReqPrivateKey; +import com.webank.webase.front.tool.entity.ReqSign; +import com.webank.webase.front.tool.entity.RspHash; +import com.webank.webase.front.tool.entity.RspKeyPair; +import com.webank.webase.front.tool.entity.RspSignData; +import com.webank.webase.front.transaction.TransService; +import com.webank.webase.front.util.CommonUtils; +import com.webank.webase.front.util.JsonUtils; +import com.webank.webase.front.web3api.Web3ApiService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import javax.validation.Valid; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.fisco.bcos.sdk.v3.codec.ContractCodec; +import org.fisco.bcos.sdk.v3.codec.ContractCodecException; +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.Bytes32; +import org.fisco.bcos.sdk.v3.codec.wrapper.ABIDefinition; +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; +import org.fisco.bcos.sdk.v3.crypto.keypair.ECDSAKeyPair; +import org.fisco.bcos.sdk.v3.crypto.keypair.SM2KeyPair; +import org.fisco.bcos.sdk.v3.crypto.keystore.KeyTool; +import org.fisco.bcos.sdk.v3.crypto.keystore.P12KeyStore; +import org.fisco.bcos.sdk.v3.crypto.keystore.PEMKeyStore; +import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; +import org.fisco.bcos.sdk.v3.model.CryptoType; +import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderService; +import org.fisco.bcos.sdk.v3.utils.Hex; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + + +@Api(value = "/tool", tags = "tools controller") +@Slf4j +@RestController +@RequestMapping("tool") +public class ToolController { + + @Autowired + private Web3ApiService web3ApiService; + + @ApiOperation(value = "decode input/output", notes = "decode tx receipt's input/output") + @ApiImplicitParam(name = "param", value = "param to be transfer", required = true, dataType = "ReqDecodeParam") + @PostMapping("/decode") + public Object decode(@Valid @RequestBody ReqDecodeParam param) { + log.info("decode output start. param:{}", JsonUtils.toJSONString(param)); + // todo 自测返回值 + String groupId = param.getGroupId(); + TransactionDecoderService txDecoder = new TransactionDecoderService(web3ApiService.getCryptoSuite(groupId), false); + ContractCodec abiCodec = new ContractCodec(web3ApiService.getCryptoSuite(groupId), false); + String abi = JsonUtils.objToString(param.getAbiList()); + ABIDefinition abiDefinition = TransService.getFunctionAbiDefinition(param.getMethodName(), abi); + // decode input + if (param.getDecodeType() == 1) { + try { + return abiCodec.decodeMethodInput(abiDefinition, param.getInput()); + } catch (ContractCodecException e) { + log.error("decodeMethodInput ", e); + } + } else if (param.getDecodeType() == 2) { + // decode output + try { + return abiCodec.decodeMethodAndGetOutputObject(abi, param.getMethodName(), + param.getOutput()); + } catch (ContractCodecException e) { + log.error("abi decode fail:{}", e.getMessage()); + throw new FrontException(ConstantCode.PARAM_FAIL_ABI_INVALID); + } + } + return new BaseResponse(ConstantCode.PARAM_ERROR); + } + + @ApiOperation(value = "get key pair", notes = "generate key pair of front's encrypt type") + @ApiImplicitParam(name = "param", value = "private key(hex string), representation of BigInteger", dataType = "ReqPrivateKey") + @PostMapping("/keypair") + public RspKeyPair getKeyPair(@Valid @RequestBody ReqPrivateKey param) { + String groupId = param.getGroupId(); + String privateKey = param.getPrivateKey(); + CryptoKeyPair keyPair; + if (StringUtils.isNotBlank(privateKey)) { + keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); + } else { + keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().generateKeyPair(); + } + return new RspKeyPair(keyPair, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); + } + + @ApiOperation(value = "get public key's address", notes = "get address from pub") + @ApiImplicitParam(name = "publicKey", value = "pub key(hex string format), representation of BigInteger", dataType = "String") + @GetMapping("/address") + public RspKeyPair getKey(@RequestParam String publicKey, + @RequestParam(defaultValue = "group0") String groupId) { + RspKeyPair response = new RspKeyPair(); + if (web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig == CryptoType.SM_TYPE) { + response.setAddress(SM2KeyPair.getAddressByPublicKey(publicKey)); + } else { + response.setAddress(ECDSAKeyPair.getAddressByPublicKey(publicKey)); + } + response.setEncryptType(web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); + response.setPublicKey(publicKey); + return response; + } + + @ApiOperation(value = "get hash value", notes = "get hash value") + @ApiImplicitParam(name = "input", value = "input to hash(hexString)", dataType = "String") + @GetMapping("/hash") + public RspHash getHashValue(@RequestParam String input, + @RequestParam(defaultValue = "group0") String groupId) { + String hashValue = web3ApiService.getCryptoSuite(groupId).hash(input); + return new RspHash(hashValue, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); + } + + + @ApiOperation(value = "get private key of pem", notes = "get private key of pem") + @ApiImplicitParam(name = "pemFile", value = ".pem file of private key", dataType = "MultipartFile") + @PostMapping("/decodePem") + public RspKeyPair decodePem(@RequestParam MultipartFile pemFile, + @RequestParam(defaultValue = "group0") String groupId) { + if (pemFile.getSize() == 0) { + throw new FrontException(ConstantCode.PEM_FORMAT_ERROR); + } + String privateKey; + try { + PEMKeyStore pemManager = new PEMKeyStore(new ByteArrayInputStream(pemFile.getBytes())); + privateKey = KeyTool.getHexedPrivateKey(pemManager.getKeyPair().getPrivate()); + } catch (IOException e) { + log.error("decodePem error:[]", e); + throw new FrontException(ConstantCode.PEM_CONTENT_ERROR); + } + + return getRspKeyPair(groupId, privateKey); + } + + @ApiOperation(value = "get PrivateKey of p12", notes = "get PrivateKey by p12") + @ApiImplicitParams({ + @ApiImplicitParam(name = "p12File", value = ".p12 file of private key", dataType = "MultipartFile"), + @ApiImplicitParam(name = "p12Password", value = ".p12 file password", dataType = "String") + }) + @PostMapping("/decodeP12") + public RspKeyPair decodeP12(@RequestParam MultipartFile p12File, + @RequestParam(required = false, defaultValue = "") String p12Password, + @RequestParam(defaultValue = "group0") String groupId) { + if (!CommonUtils.notContainsChinese(p12Password)) { + throw new FrontException(ConstantCode.P12_PASSWORD_NOT_CHINESE); + } + if (p12File.getSize() == 0) { + throw new FrontException(ConstantCode.P12_FILE_ERROR); + } + String privateKey; + try { + P12KeyStore p12Manager = new P12KeyStore(new ByteArrayInputStream(p12File.getBytes()), p12Password); + privateKey = KeyTool.getHexedPrivateKey(p12Manager.getKeyPair().getPrivate()); + } catch (IOException e) { + log.error("decodeP12 error:[]", e); + if (e.getMessage().contains("password")) { + throw new FrontException(ConstantCode.P12_PASSWORD_ERROR); + } + throw new FrontException(ConstantCode.P12_FILE_ERROR); + } + return getRspKeyPair(groupId, privateKey); + } + + private RspKeyPair getRspKeyPair(String groupId, String privateKey) { + CryptoKeyPair keyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); + return new RspKeyPair(keyPair, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); + } + + @ApiOperation(value = "get hash value", notes = "get hash value") + @ApiImplicitParams({ + @ApiImplicitParam(name = "input", value = "input to hash(hexString or utf-8)", dataType = "String"), + @ApiImplicitParam(name = "type", value = " input type input type 1-hexString,2-utf8(default hex)", dataType = "Integer") + }) + @GetMapping("/convert2Bytes32") + public String getBytes32FromStr(@RequestParam String input, @RequestParam(defaultValue = "1") Integer type, + @RequestParam(defaultValue = "group0") String groupId) { + Bytes32 bytes32; + if (type == 1) { + // hex input todo use java sdk + bytes32 = CommonUtils.hexStrToBytes32(input); + } else if (type == 2) { + // utf8 input + bytes32 = CommonUtils.utf8StringToBytes32(input); + } else { + throw new FrontException(ConstantCode.PARAM_ERROR); + } + return Hex.toHexString(bytes32.getValue()); + } + + @ApiOperation(value = "get utf8's hex string", notes = "get hex string") + @ApiImplicitParam(name = "input", value = "input to convert(utf8 String)", dataType = "String") + @GetMapping("/utf8ToHexString") + public String getHexStringFromUtf8(@RequestParam String input) { + return CommonUtils.utf8StringToHex(input); + } + + @ApiOperation(value = "sign raw data", notes = "sign raw data by private key") + @ApiImplicitParam(name = "reqSign", value = "raw data & private key", dataType = "ReqSign") + @PostMapping("/signMsg") + public RspSignData getSignedData(@Valid @RequestBody ReqSign reqSign) { + String privateKey = reqSign.getPrivateKey(); + String rawData = reqSign.getRawData(); + String groupId = reqSign.getGroupId(); + CryptoKeyPair cryptoKeyPair = web3ApiService.getCryptoSuite(groupId).getKeyPairFactory().createKeyPair(privateKey); + SignatureResult signatureData = web3ApiService.getCryptoSuite(groupId).sign(Hex.toHexString(rawData.getBytes()), cryptoKeyPair); + return new RspSignData(signatureData, web3ApiService.getCryptoSuite(groupId).cryptoTypeConfig); + } + + +} diff --git a/src/main/java/com/webank/webase/front/tool/entity/ReqDecodeParam.java b/src/main/java/com/webank/webase/front/tool/entity/ReqDecodeParam.java index e78c53313..37c096c93 100644 --- a/src/main/java/com/webank/webase/front/tool/entity/ReqDecodeParam.java +++ b/src/main/java/com/webank/webase/front/tool/entity/ReqDecodeParam.java @@ -37,4 +37,6 @@ public class ReqDecodeParam { * add in 1.5.0, not null if decode output */ private String methodName; + @NotNull + private String groupId; } diff --git a/src/main/java/com/webank/webase/front/tool/entity/ReqPrivateKey.java b/src/main/java/com/webank/webase/front/tool/entity/ReqPrivateKey.java index 6437960ff..b39891b0c 100644 --- a/src/main/java/com/webank/webase/front/tool/entity/ReqPrivateKey.java +++ b/src/main/java/com/webank/webase/front/tool/entity/ReqPrivateKey.java @@ -14,6 +14,7 @@ package com.webank.webase.front.tool.entity; +import javax.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @@ -25,4 +26,6 @@ public class ReqPrivateKey { * string of BigInteger */ private String privateKey; + @NotNull + private String groupId; } diff --git a/src/main/java/com/webank/webase/front/tool/entity/ReqSign.java b/src/main/java/com/webank/webase/front/tool/entity/ReqSign.java index cee666970..2cce6e057 100644 --- a/src/main/java/com/webank/webase/front/tool/entity/ReqSign.java +++ b/src/main/java/com/webank/webase/front/tool/entity/ReqSign.java @@ -14,12 +14,15 @@ package com.webank.webase.front.tool.entity; +import javax.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor public class ReqSign { + @NotNull + private String groupId; private String rawData; private String privateKey; } diff --git a/src/main/resources/application-docker.yml b/src/main/resources/application-docker.yml index a9d59f6b9..b61b36080 100644 --- a/src/main/resources/application-docker.yml +++ b/src/main/resources/application-docker.yml @@ -1,5 +1,5 @@ # server version -version: v3.0.2 +version: v3.1.1 server: port: ${SERVER_PORT:5002} @@ -15,6 +15,7 @@ sdk: certPath: conf # sdk cert path of relative or absolute peers: ${PEERS:"['127.0.0.1:20200','127.0.0.1:20201']"} threadPoolSize: 50 # thread pool of sdk + disableSsl: false # disable node ssl or not spring: datasource: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2ddfbdfe6..a8704cc3a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,5 +1,5 @@ # server version -version: v3.0.2 +version: v3.1.1 server: port: 5002 @@ -15,6 +15,7 @@ sdk: peers: ['127.0.0.1:20200'] certPath: conf # sdk cert path of relative or absolute, default 'conf' threadPoolSize: 50 # thread pool of sdk + disableSsl: false # disable node ssl or not spring: datasource: