Java Code Examples for org.fisco.bcos.web3j.crypto.EncryptType#encryptType()

The following examples show how to use org.fisco.bcos.web3j.crypto.EncryptType#encryptType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeMgrTools.java    From WeBASE-Node-Manager with Apache License 2.0 6 votes vote down vote up
/**
 * get hash value
 * type: sha256 or sm3
 */
public static byte[] getHashValue(byte[] byteArray) {
    byte[] hashResult;
    if(EncryptType.encryptType == 1) {
       hashResult = Hash.sha3(byteArray);
       return hashResult;
    } else {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA-256");
            hashResult = sha.digest(byteArray);
            return hashResult;
        } catch (Exception e) {
            log.error("shaEncode getHashValue fail:", e);
            return null;
        }
    }
}
 
Example 2
Source File: FrontCertService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * get cert file's path through concatting nodePath with certType
 * @param nodePath
 * @param certType
 * @return
 * 2019/12 support guomi
 */
public Path getCertPath(String nodePath, int certType) {
    if (certType == CertTypes.CHAIN.getValue()) {
        if (EncryptType.encryptType == 1){
            return Paths.get(nodePath.concat(gmCaCrtPath));
        }
        return Paths.get(nodePath.concat(caCrtPath));
    } else if (certType == CertTypes.NODE.getValue()) {
        if (EncryptType.encryptType == 1){
            return Paths.get(nodePath.concat(gmNodeCrtPath));
        }
        return Paths.get(nodePath.concat(nodeCrtPath));
    } else if(certType == CertTypes.OTHERS.getValue()){
        return getEncrytCertPath(nodePath);
    }
    return null;
}
 
Example 3
Source File: CommonUtils.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * signatureDataToString. 19/12/24 support guomi: add byte[] pub in signatureData
 * 
 * @param signatureData signatureData
 */
public static String signatureDataToString(SignatureData signatureData) {
    byte[] byteArr;
    if (EncryptType.encryptType == 1) {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length
                + PUBLIC_KEY_LENGTH_64];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
        System.arraycopy(signatureData.getPub(), 0, byteArr,
                signatureData.getS().length + signatureData.getR().length + 1,
                signatureData.getPub().length);
    } else {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example 4
Source File: CommonUtils.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
/**
 * signatureDataToString.
 * 19/12/24 support guomi: add byte[] pub in signatureData
 * @param signatureData signatureData
 */
public static String signatureDataToString(SignatureData signatureData) {
    byte[] byteArr;
    if(EncryptType.encryptType == 1) {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length + publicKeyLength_64];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
        System.arraycopy(signatureData.getPub(), 0, byteArr,
                signatureData.getS().length + signatureData.getR().length + 1,
                signatureData.getPub().length);
    } else {
        byteArr = new byte[1 + signatureData.getR().length + signatureData.getS().length];
        byteArr[0] = signatureData.getV();
        System.arraycopy(signatureData.getR(), 0, byteArr, 1, signatureData.getR().length);
        System.arraycopy(signatureData.getS(), 0, byteArr, signatureData.getR().length + 1,
                signatureData.getS().length);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example 5
Source File: FrontCertService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
public Path getEncrytCertPath(String nodePath) {
    if (EncryptType.encryptType == 1){
        return Paths.get(nodePath.concat(gmEncryptCrtPath));
    } else {
        return null;
    }
}
 
Example 6
Source File: CommonUtils.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * stringToSignatureData. 19/12/24 support guomi: add byte[] pub in signatureData
 * 
 * @param signatureData signatureData
 * @return
 */
public static SignatureData stringToSignatureData(String signatureData) {
    byte[] byteArr = Numeric.hexStringToByteArray(signatureData);
    byte[] signR = new byte[32];
    System.arraycopy(byteArr, 1, signR, 0, signR.length);
    byte[] signS = new byte[32];
    System.arraycopy(byteArr, 1 + signR.length, signS, 0, signS.length);
    if (EncryptType.encryptType == 1) {
        byte[] pub = new byte[64];
        System.arraycopy(byteArr, 1 + signR.length + signS.length, pub, 0, pub.length);
        return new SignatureData(byteArr[0], signR, signS, pub);
    } else {
        return new SignatureData(byteArr[0], signR, signS);
    }
}
 
Example 7
Source File: EncryptTypeController.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
@GetMapping("")
public ResponseEntity getEncryptType() {
    ResponseEntity response = new ResponseEntity(ConstantCode.RET_SUCCEED);
    int encrypt = EncryptType.encryptType;
    log.info("getEncryptType:{}", encrypt);
    response.setData(encrypt);
    return response;
}
 
Example 8
Source File: CommonUtils.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * stringToSignatureData.
 * 19/12/24 support guomi: add byte[] pub in signatureData
 * @param signatureData signatureData
 * @return
 */
public static SignatureData stringToSignatureData(String signatureData) {
    byte[] byteArr = Numeric.hexStringToByteArray(signatureData);
    byte[] signR = new byte[32];
    System.arraycopy(byteArr, 1, signR, 0, signR.length);
    byte[] signS = new byte[32];
    System.arraycopy(byteArr, 1 + signR.length, signS, 0, signS.length);
    if (EncryptType.encryptType == 1) {
        byte[] pub = new byte[64];
        System.arraycopy(byteArr, 1 + signR.length + signS.length, pub, 0, pub.length);
        return new SignatureData(byteArr[0], signR, signS, pub);
    } else {
        return new SignatureData(byteArr[0], signR, signS);
    }
}
 
Example 9
Source File: GenCredential.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static ECKeyPair createKeyPair(String privKey) {
    if (EncryptType.encryptType == 1) return createGuomiKeyPair(privKey);
    return createECDSAKeyPair(privKey);
}
 
Example 10
Source File: GenCredential.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static ECKeyPair createKeyPair() {
    // use guomi
    if (EncryptType.encryptType == 1) return createGuomiKeyPair();
    return createECDSAKeyPair(); // default use ECDSA
}
 
Example 11
Source File: DagTransfer.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static final String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : BINARY_GM);
}
 
Example 12
Source File: Ok.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : SM_BINARY);
}
 
Example 13
Source File: Topic.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public static String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : SM_BINARY);
}
 
Example 14
Source File: TopicController.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
public static String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : SM_BINARY);
}
 
Example 15
Source File: ContractService.java    From WeBASE-Transaction with Apache License 2.0 4 votes vote down vote up
/**
 * contract compile.
 * 
 * @param zipFile file
 * @return
 */
public ResponseEntity compile(MultipartFile zipFile) throws BaseException, IOException {
    ResponseEntity response = new ResponseEntity(ConstantCode.RET_SUCCEED);
    String path = new File("temp").getAbsolutePath();
    // clear temp folder
    CommonUtils.deleteFiles(path);
    // unzip
    CommonUtils.unZipFiles(zipFile, path);
    // get sol files
    File solFileList = new File(path);
    File[] solFiles = solFileList.listFiles();
    if (solFiles == null || solFiles.length == 0) {
        return response;
    }

    // whether use guomi to compile, 1-guomi, 0-ecdsa
    boolean useSM2 = EncryptType.encryptType == 1;
    List<CompileInfo> compileInfos = new ArrayList<>();
    for (File solFile : solFiles) {
        if (!solFile.getName().endsWith(".sol")) {
            continue;
        }
        String contractName =
                solFile.getName().substring(0, solFile.getName().lastIndexOf("."));
        // compile
        SolidityCompiler.Result res =
                SolidityCompiler.compile(solFile, useSM2, true, ABI, BIN);
        // check result
        if (res.isFailed()) {
            log.warn("compile fail. contract:{} compile error. {}", contractName, res.getErrors());
            throw new BaseException(ConstantCode.CONTRACT_COMPILE_ERROR.getCode(), res.getErrors());
        }
        // parse result
        CompilationResult result = CompilationResult.parse(res.getOutput());
        List<ContractMetadata> contracts = result.getContracts();
        if (contracts.size() > 0) {
            CompileInfo compileInfo = new CompileInfo();
            compileInfo.setContractName(contractName);
            compileInfo.setContractBin(result.getContract(contractName).bin);
            compileInfo
                    .setContractAbi(JsonUtils.toJavaObjectList(result.getContract(contractName).abi, Object.class));
            compileInfos.add(compileInfo);
        }
    }
    response.setData(compileInfos);
    return response;
}
 
Example 16
Source File: EvidenceVerify.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : SM_BINARY);
}
 
Example 17
Source File: EncryptTypeController.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
@GetMapping("")
public Integer getEncryptType() {
    int encrypt = EncryptType.encryptType;
    log.info("getEncryptType:{}", encrypt);
    return encrypt;
}
 
Example 18
Source File: ParallelOk.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static final String getBinary() {
    return (EncryptType.encryptType == EncryptType.ECDSA_TYPE ? BINARY : BINARY_GM);
}
 
Example 19
Source File: EncryptTypeController.java    From WeBASE-Node-Manager with Apache License 2.0 4 votes vote down vote up
@GetMapping("")
public Object getEncryptType() {
    int encrypt = EncryptType.encryptType;
    log.info("getEncryptType:{}", encrypt);
    return new BaseResponse(ConstantCode.SUCCESS, encrypt);
}
 
Example 20
Source File: FrontService.java    From WeBASE-Node-Manager with Apache License 2.0 4 votes vote down vote up
/**
 * add new front
 */
@Transactional
public TbFront newFront(FrontInfo frontInfo) {
    log.debug("start newFront frontInfo:{}", frontInfo);
    TbFront tbFront = new TbFront();
    String frontIp = frontInfo.getFrontIp();
    Integer frontPort = frontInfo.getFrontPort();
    //check valid ip
    checkNotSupportIp(frontIp);
    //check front ip and port
    NodeMgrTools.checkServerConnect(frontIp, frontPort);
    //query group list
    List<String> groupIdList = null;
    try {
        groupIdList = frontInterface.getGroupListFromSpecificFront(frontIp, frontPort);
    } catch (Exception e) {
        log.error("fail newFront, frontIp:{},frontPort:{}",frontIp,frontPort);
        throw new NodeMgrException(ConstantCode.REQUEST_FRONT_FAIL);
    }
    // check front's encrypt type same as nodemgr(guomi or standard)
    int encryptType = frontInterface.getEncryptTypeFromSpecificFront(frontIp, frontPort);
    if (encryptType != EncryptType.encryptType) {
        log.error("fail newFront, frontIp:{},frontPort:{},front's encryptType:{}," +
                        "local encryptType not match:{}",
                frontIp, frontPort, encryptType, EncryptType.encryptType);
        throw new NodeMgrException(ConstantCode.ENCRYPT_TYPE_NOT_MATCH);
    }
    //check front not exist
    SyncStatus syncStatus = frontInterface.getSyncStatusFromSpecificFront(frontIp, 
            frontPort, Integer.valueOf(groupIdList.get(0)));
    FrontParam param = new FrontParam();
    param.setNodeId(syncStatus.getNodeId());
    int count = getFrontCount(param);
    if (count > 0) {
        throw new NodeMgrException(ConstantCode.FRONT_EXISTS);
    }
    String clientVersion = frontInterface.getClientVersion(frontIp,
            frontPort, Integer.valueOf(groupIdList.get(0)));
    //copy attribute
    BeanUtils.copyProperties(frontInfo, tbFront);
    tbFront.setNodeId(syncStatus.getNodeId());
    tbFront.setClientVersion(clientVersion);
    //save front info
    frontMapper.add(tbFront);
    if (tbFront.getFrontId() == null || tbFront.getFrontId() == 0) {
        log.warn("fail newFront, after save, tbFront:{}", JsonTools.toJSONString(tbFront));
        throw new NodeMgrException(ConstantCode.SAVE_FRONT_FAIL);
    }
    for (String groupId : groupIdList) {
        Integer group = Integer.valueOf(groupId);
        //peer in group
        List<String> groupPeerList = frontInterface
            .getGroupPeersFromSpecificFront(frontIp, frontPort, group);
        //get peers on chain
        PeerInfo[] peerArr = frontInterface
            .getPeersFromSpecificFront(frontIp, frontPort, group);
        List<PeerInfo> peerList = Arrays.asList(peerArr);
        //add group
        // check group not existed or node count differs
        TbGroup checkGroup = groupService.getGroupById(group);
        if (Objects.isNull(checkGroup) || groupPeerList.size() != checkGroup.getNodeCount()) {
            groupService.saveGroup(group, groupPeerList.size(), "synchronous",
                    GroupType.SYNC.getValue(), DataStatus.NORMAL.getValue());
        }
        //save front group map
        frontGroupMapService.newFrontGroup(tbFront, group);
        //save nodes
        for (String nodeId : groupPeerList) {
            PeerInfo newPeer = peerList.stream().map(p -> NodeMgrTools
                .object2JavaBean(p, PeerInfo.class))
                .filter(peer -> nodeId.equals(peer.getNodeId()))
                .findFirst().orElseGet(() -> new PeerInfo(nodeId));
            nodeService.addNodeInfo(group, newPeer);
        }
        //add sealer(consensus node) and observer in nodeList
         refreshSealerAndObserverInNodeList(frontIp, frontPort, group);
    }
    // pull cert from new front and its node
    CertTools.isPullFrontCertsDone = false;
    //clear cache
    frontGroupMapCache.clearMapList();
    return tbFront;
}