Search in sources :

Example 1 with ArgEntry

use of utils.ArgumentSet.ArgEntry in project jdchain-core by blockchain-jd-com.

the class KeyGenCommand method main.

/**
 * 入口;
 *
 * @param args
 */
public static void main(String[] args) {
    Configurator.setRootLevel(Level.ERROR);
    Setting setting = ArgumentSet.setting().prefix(READ_ARG, NAME_ARG, OUT_DIR_ARG, LOCAL_CONF_ARG, CRYPTO_ALGORITHM).option(OPT_DECRYPTING, OPT_DEBUG);
    ArgumentSet argSet = ArgumentSet.resolve(args, setting);
    try {
        ArgEntry[] argEntries = argSet.getArgs();
        if (argEntries.length == 0) {
            ConsoleUtils.info("Miss argument!\r\n" + "-r : Run in reading mode if set this option, or in generating mode if not set.\r\n" + "-d : Decrypt priv key in reading mode, optional.\r\n" + "-n : name of key.\r\n" + "-o : output dir of key under generating mode.\r\n");
            return;
        }
        if (argSet.getArg(READ_ARG) != null) {
            readKey(argSet.getArg(READ_ARG).getValue(), argSet.hasOption(OPT_DECRYPTING));
        } else {
            ArgEntry name = argSet.getArg(NAME_ARG);
            if (name == null) {
                ConsoleUtils.info("Miss name of key!");
                return;
            }
            String outputDir = null;
            ArgEntry dirArg = argSet.getArg(OUT_DIR_ARG);
            if (dirArg == null || dirArg.getValue() == null) {
                // 在当前目录生成;
                outputDir = "." + File.separatorChar;
            // ConsoleUtils.info("Miss storage dir of keys!");
            // return;
            } else {
                outputDir = dirArg.getValue();
            }
            if (!FileUtils.existDirectory(outputDir)) {
                // 创建目录;
                ConsoleUtils.info("The storage dir[" + outputDir + "] doesn't exist,  it will be created automatically!");
                FileUtils.makeDirectory(outputDir);
            // return;
            }
            ArgEntry cryptoAlgorithm = argSet.getArg(CRYPTO_ALGORITHM);
            String algorithm = null != cryptoAlgorithm ? cryptoAlgorithm.getValue() : DEFAULT_CRYPTO_ALGORITHM;
            ArgEntry localConfArg = argSet.getArg(LOCAL_CONF_ARG);
            String localConfPath = localConfArg == null ? null : localConfArg.getValue();
            generateKeyPair(algorithm, name.getValue(), outputDir, localConfPath);
        }
    } catch (Exception e) {
        ConsoleUtils.info("Error!!! %s", e.getMessage());
        if (argSet.hasOption(OPT_DEBUG)) {
            e.printStackTrace();
        }
    }
}
Also used : Setting(utils.ArgumentSet.Setting) ArgumentSet(utils.ArgumentSet) ArgEntry(utils.ArgumentSet.ArgEntry) DecryptionException(utils.security.DecryptionException)

Example 2 with ArgEntry

use of utils.ArgumentSet.ArgEntry in project jdchain-core by blockchain-jd-com.

the class LedgerInitCommand method main.

/**
 * 入口;
 *
 * @param args
 */
public static void main(String[] args) {
    Prompter prompter = DEFAULT_PROMPTER;
    Setting argSetting = ArgumentSet.setting().prefix(LOCAL_ARG, INI_ARG).option(MONITOR_OPT);
    ArgumentSet argSet = ArgumentSet.resolve(args, argSetting);
    try {
        if (argSet.hasOption(MONITOR_OPT)) {
            prompter = LOG_PROMPTER;
        }
        ArgEntry localArg = argSet.getArg(LOCAL_ARG);
        if (localArg == null) {
            prompter.info("Miss local config file which can be specified with arg [%s]!!!", LOCAL_ARG);
        }
        LocalConfig localConf = LocalConfig.resolve(localArg.getValue());
        ArgEntry iniArg = argSet.getArg(INI_ARG);
        if (iniArg == null) {
            prompter.info("Miss ledger initializing config file which can be specified with arg [%s]!!!", INI_ARG);
            return;
        }
        // load ledger init setting;
        LedgerInitProperties ledgerInitProperties = LedgerInitProperties.resolve(iniArg.getValue());
        // 加载当前节点的私钥;
        // 根据 identity-mode 验证 local.conf 参数的正确性
        String base58Pwd = localConf.getLocal().getPassword();
        PubKey localNodePubKey;
        PrivKey privKey;
        if (ledgerInitProperties.getIdentityMode() == IdentityMode.CA) {
            X509Certificate certificate = CertificateUtils.parseCertificate(FileUtils.readText(localConf.getLocal().getCaPath()));
            localNodePubKey = CertificateUtils.resolvePubKey(certificate);
            if (StringUtils.isEmpty(base58Pwd)) {
                privKey = CertificateUtils.parsePrivKey(localNodePubKey.getAlgorithm(), FileUtils.readText(localConf.getLocal().getPrivKeyPath()));
            } else {
                privKey = CertificateUtils.parsePrivKey(localNodePubKey.getAlgorithm(), FileUtils.readText(localConf.getLocal().getPrivKeyPath()), base58Pwd);
            }
            if (!StringUtils.isEmpty(base58Pwd)) {
                base58Pwd = Base58Utils.encode(base58Pwd.getBytes());
            }
        } else {
            if (StringUtils.isEmpty(base58Pwd)) {
                base58Pwd = KeyGenUtils.readPasswordString();
            }
            localNodePubKey = KeyGenUtils.decodePubKey(localConf.getLocal().getPubKeyString());
            privKey = KeyGenUtils.decodePrivKey(localConf.getLocal().getPrivKeyString(), base58Pwd);
        }
        // 地址根据公钥生成
        String localNodeAddress = AddressEncoding.generateAddress(localNodePubKey).toBase58();
        // 加载全部公钥;
        int currId = -1;
        for (int i = 0; i < ledgerInitProperties.getConsensusParticipantCount(); i++) {
            ParticipantProperties partiConf = ledgerInitProperties.getConsensusParticipant(i);
            if (localNodeAddress.equals(partiConf.getAddress().toBase58())) {
                currId = i;
            }
        }
        if (currId == -1) {
            throw new IllegalStateException("The current node specified in local.conf is not found in ledger.init!");
        }
        // Output ledger binding config of peer;
        if (!FileUtils.existDirectory(localConf.getBindingOutDir())) {
            FileUtils.makeDirectory(localConf.getBindingOutDir());
        }
        File ledgerBindingFile = new File(localConf.getBindingOutDir(), LEDGER_BINDING_FILE_NAME);
        LedgerBindingConfig conf;
        if (ledgerBindingFile.exists()) {
            conf = LedgerBindingConfig.resolve(ledgerBindingFile);
        } else {
            conf = new LedgerBindingConfig();
        }
        // 启动初始化;
        LedgerInitCommand initCommand = new LedgerInitCommand();
        HashDigest newLedgerHash = initCommand.startInit(currId, privKey, base58Pwd, ledgerInitProperties, localConf, prompter, conf);
        if (newLedgerHash != null) {
            // success;
            // so save ledger binding config to file system;
            conf.store(ledgerBindingFile);
            prompter.info("\r\n------ Update Ledger binding configuration success! ------[%s]", ledgerBindingFile.getAbsolutePath());
        }
    } catch (Exception e) {
        e.printStackTrace();
        prompter.error("\r\n Ledger init process has been broken by error!");
    }
    prompter.confirm(InitializingStep.LEDGER_INIT_COMPLETED.toString(), "\r\n\r\n Press any key to quit. :>");
    if (argSet.hasOption(MONITOR_OPT)) {
        // 管理工具启动的方式下,需自动退出
        System.exit(0);
    }
}
Also used : Setting(utils.ArgumentSet.Setting) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) ArgEntry(utils.ArgumentSet.ArgEntry) X509Certificate(java.security.cert.X509Certificate) LedgerInitProperties(com.jd.blockchain.ledger.LedgerInitProperties) ArgumentSet(utils.ArgumentSet) File(java.io.File)

Example 3 with ArgEntry

use of utils.ArgumentSet.ArgEntry in project jdchain-core by blockchain-jd-com.

the class GatewayServerBooter method main.

public static void main(String[] args) {
    try {
        ArgumentSet arguments = ArgumentSet.resolve(args, ArgumentSet.setting().prefix(HOST_ARG, SPRING_CF_LOCATION));
        ArgEntry argHost = arguments.getArg(HOST_ARG);
        String configFile = argHost == null ? null : argHost.getValue();
        GatewayConfigProperties configProps;
        if (configFile == null) {
            LOGGER.info("Load build-in default configuration ...");
            ClassPathResource configResource = new ClassPathResource("gateway.conf");
            try (InputStream in = configResource.getInputStream()) {
                configProps = GatewayConfigProperties.resolve(in);
            }
        } else {
            LOGGER.info("Load configuration ...");
            configProps = GatewayConfigProperties.resolve(argHost.getValue());
        }
        // spring config location;
        String springConfigLocation = null;
        ArgumentSet.ArgEntry spConfigLocation = arguments.getArg(SPRING_CF_LOCATION);
        if (spConfigLocation != null) {
            springConfigLocation = spConfigLocation.getValue();
        } else {
            // if no the config file, then should tip as follows. but it's not a good
            // feeling, so we create it by inputStream;
            LOGGER.info("no param:-sp, format: -sp /x/xx.properties, use the default application-gw.properties 	");
            ClassPathResource configResource = new ClassPathResource(DEFAULT_GATEWAY_PROPS);
            InputStream in = configResource.getInputStream();
            // 将文件写入至config目录下
            String configPath = HOME_DIR + CONFIG_FOLDER_NAME + File.separator + DEFAULT_GATEWAY_PROPS;
            File targetFile = new File(configPath);
            // 先将原来文件删除再Copy
            if (targetFile.exists()) {
                FileUtils.forceDelete(targetFile);
            }
            FileUtils.copyInputStreamToFile(in, targetFile);
            springConfigLocation = "file:" + targetFile.getAbsolutePath();
        }
        // 启动服务器;
        LOGGER.info("Starting web server......");
        GatewayServerBooter booter = new GatewayServerBooter(configProps, springConfigLocation);
        Signal.handle(new Signal("TERM"), booter);
        booter.start();
    } catch (Exception e) {
        LOGGER.error("Gateway start error", e);
    }
}
Also used : Signal(sun.misc.Signal) InputStream(java.io.InputStream) ArgumentSet(utils.ArgumentSet) ArgEntry(utils.ArgumentSet.ArgEntry) ArgEntry(utils.ArgumentSet.ArgEntry) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

ArgumentSet (utils.ArgumentSet)3 ArgEntry (utils.ArgumentSet.ArgEntry)3 File (java.io.File)2 Setting (utils.ArgumentSet.Setting)2 LedgerInitProperties (com.jd.blockchain.ledger.LedgerInitProperties)1 ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)1 InputStream (java.io.InputStream)1 X509Certificate (java.security.cert.X509Certificate)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Signal (sun.misc.Signal)1 DecryptionException (utils.security.DecryptionException)1