Search in sources :

Example 1 with ParticipantProperties

use of com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method prepareLocalPermission.

public LedgerInitProposal prepareLocalPermission(int currentId, PrivKey privKey, LedgerInitConfiguration ledgerInitConfig) {
    // 创建初始化配置;
    ParticipantProperties[] participants = ledgerInitConfig.getParticipants();
    if (currentId < 0 || currentId >= participants.length) {
        throw new LedgerInitException("Your id is out of bound of participant list!");
    }
    this.currentId = currentId;
    // 校验当前的公钥、私钥是否匹配;
    byte[] testBytes = BytesUtils.toBytes(currentId);
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(privKey.getAlgorithm());
    SignatureDigest testSign = signatureFunction.sign(privKey, testBytes);
    PubKey myPubKey = participants[currentId].getPubKey();
    if (!signatureFunction.verify(testSign, myPubKey, testBytes)) {
        throw new LedgerInitException("Your pub-key specified in the init-settings isn't match your priv-key!");
    }
    this.initializerAddresses = new NetworkAddress[participants.length];
    // 记录每个参与方的账本初始化服务地址;
    for (int i = 0; i < participants.length; i++) {
        initializerAddresses[i] = participants[i].getInitializerAddress();
    }
    // 初始化账本;
    this.initializer = LedgerInitializer.create(ledgerInitConfig.getLedgerSettings(), ledgerInitConfig.getSecuritySettings());
    // 对初始交易签名,生成当前参与者的账本初始化许可;
    SignatureDigest permissionSign = initializer.signTransaction(privKey);
    LedgerInitProposalData permission = new LedgerInitProposalData(currentId, permissionSign);
    this.currentId = currentId;
    this.permissions = new LedgerInitProposal[participants.length];
    this.permissions[currentId] = permission;
    this.localPermission = permission;
    return permission;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) LedgerInitProposalData(com.jd.blockchain.ledger.core.LedgerInitProposalData) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Example 2 with ParticipantProperties

use of com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties 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 ParticipantProperties

use of com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties in project jdchain-core by blockchain-jd-com.

the class LedgerInitConfiguration method create.

public static LedgerInitConfiguration create(LedgerInitProperties ledgerInitProps) {
    LedgerInitConfiguration ledgerConfig = new LedgerInitConfiguration();
    CryptoConfig cryptoConfig = createCryptoConfig(ledgerInitProps.getCryptoProperties());
    ledgerConfig.cryptoConfig = cryptoConfig;
    ConsensusConfig consensusConfig = createConsensusConfig(ledgerInitProps);
    ledgerConfig.consensusConfig = consensusConfig;
    ParticipantProperties[] participants = resolveParticipants(ledgerInitProps);
    ledgerConfig.participants = participants;
    LedgerInitData ledgerSettings = createLedgerInitSettings(ledgerInitProps, cryptoConfig, consensusConfig, participants);
    ledgerSettings.setCreatedTime(ledgerInitProps.getCreatedTime());
    ledgerConfig.ledgerSettings = ledgerSettings;
    SecurityInitData securitySettings = createSecurityInitSettings(ledgerInitProps);
    ledgerConfig.securitySettings = securitySettings;
    return ledgerConfig;
}
Also used : SecurityInitData(com.jd.blockchain.ledger.SecurityInitData) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Aggregations

ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)3 PubKey (com.jd.blockchain.crypto.PubKey)1 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)1 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)1 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)1 LedgerInitProperties (com.jd.blockchain.ledger.LedgerInitProperties)1 SecurityInitData (com.jd.blockchain.ledger.SecurityInitData)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 LedgerInitProposalData (com.jd.blockchain.ledger.core.LedgerInitProposalData)1 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)1 File (java.io.File)1 X509Certificate (java.security.cert.X509Certificate)1 ArgumentSet (utils.ArgumentSet)1 ArgEntry (utils.ArgumentSet.ArgEntry)1 Setting (utils.ArgumentSet.Setting)1