Search in sources :

Example 6 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class BftsmartConsensusSettingsBuilder method createSettings.

@Override
public BftsmartConsensusViewSettings createSettings(Properties props, Replica[] participantNodes) {
    Properties resolvingProps = PropertiesUtils.cloneFrom(props);
    int serversNum = PropertiesUtils.getInt(resolvingProps, SERVER_NUM_KEY);
    if (serversNum < 0) {
        throw new IllegalArgumentException(String.format("Property[%s] is negative!", SERVER_NUM_KEY));
    }
    if (serversNum < 4) {
        throw new IllegalArgumentException(String.format("Property[%s] is less than 4!", SERVER_NUM_KEY));
    }
    if (participantNodes == null) {
        throw new IllegalArgumentException("ParticipantNodes is Empty !!!");
    }
    // if (serversNum != participantNodes.length) {
    // throw new IllegalArgumentException(
    // String.format("Property[%s] which is [%s] unequal " + "ParticipantNodes's length which is [%s] !",
    // SERVER_NUM_KEY, serversNum, participantNodes.length));
    // }
    serversNum = participantNodes.length;
    // BftsmartCommitBlockConfig blockConfig = createBlockConfig(resolvingProps);
    BftsmartNodeSettings[] nodesSettings = new BftsmartNodeSettings[serversNum];
    for (int i = 0; i < serversNum; i++) {
        int id = i;
        // String keyOfPubkey = keyOfNode(PUBKEY_PATTERN, id);
        // String base58PubKey = PropertiesUtils.getRequiredProperty(resolvingProps, keyOfPubkey);
        // PubKey pubKey = new PubKey(Base58Utils.decode(base58PubKey));
        // PubKey pubKey = KeyGenCommand.decodePubKey(base58PubKey);
        PubKey pubKey = participantNodes[i].getPubKey();
        // resolvingProps.remove(keyOfPubkey);
        String keyOfHost = keyOfNode(CONSENSUS_HOST_PATTERN, participantNodes[id].getId());
        String networkAddressHost = PropertiesUtils.getOptionalProperty(resolvingProps, keyOfHost, null);
        resolvingProps.remove(keyOfHost);
        String keyOfPort = keyOfNode(CONSENSUS_PORT_PATTERN, participantNodes[id].getId());
        int networkAddressPort = PropertiesUtils.getIntOptional(resolvingProps, keyOfPort, -1);
        resolvingProps.remove(keyOfPort);
        String keyOfSecure = keyOfNode(CONSENSUS_SECURE_PATTERN, participantNodes[id].getId());
        boolean networkAddressSecure = PropertiesUtils.getBooleanOptional(resolvingProps, keyOfSecure, false);
        resolvingProps.remove(keyOfSecure);
        if (participantNodes[i] instanceof NetworkReplica) {
            NetworkReplica replica = (NetworkReplica) participantNodes[i];
            networkAddressHost = replica.getNetworkAddress().getHost();
            networkAddressPort = replica.getNetworkAddress().getPort();
            networkAddressSecure = replica.getNetworkAddress().isSecure();
        }
        BftsmartNodeConfig nodeConfig = new BftsmartNodeConfig(pubKey, participantNodes[id].getId(), new NetworkAddress(networkAddressHost, networkAddressPort, networkAddressSecure));
        nodesSettings[i] = nodeConfig;
    }
    BftsmartConsensusConfig config = new BftsmartConsensusConfig(nodesSettings, // blockConfig,
    PropertiesUtils.getOrderedValues(resolvingProps), 0);
    return config;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) NetworkAddress(utils.net.NetworkAddress) Properties(java.util.Properties) NetworkReplica(com.jd.blockchain.consensus.NetworkReplica)

Example 7 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class MsgQueueClientFactory method buildCredential.

@Override
public MsgQueueClientIdentification buildCredential(SessionCredential credentialInfo, AsymmetricKeypair clientKeyPair, X509Certificate gatewayCertificate) {
    MQCredentialInfo mqCredentialInfo = (MQCredentialInfo) credentialInfo;
    if (null == mqCredentialInfo) {
        mqCredentialInfo = MQCredentialConfig.createEmptyCredential();
    }
    byte[] credentialBytes = BinaryProtocol.encode(mqCredentialInfo, MQCredentialInfo.class);
    PubKey pubKey = clientKeyPair.getPubKey();
    // byte[] address = pubKey.toBytes(); // 使用公钥地址作为认证信息
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(pubKey.getAlgorithm());
    SignatureDigest signatureDigest = signatureFunction.sign(clientKeyPair.getPrivKey(), credentialBytes);
    MsgQueueClientIdentification mqci = new MsgQueueClientIdentification().setPubKey(clientKeyPair.getPubKey()).setIdentityInfo(mqCredentialInfo).setSignature(signatureDigest).setCertificate(gatewayCertificate != null ? CertificateUtils.toPEMString(gatewayCertificate) : null);
    ;
    return mqci;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest)

Example 8 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class MerkleAccountSetEditor method register.

/**
 * 注册一个新账户; <br>
 *
 * 如果账户已经存在,则会引发 {@link LedgerException} 异常; <br>
 *
 * 如果指定的地址和公钥不匹配,则会引发 {@link LedgerException} 异常;
 *
 * @param address 区块链地址;
 * @param pubKey  公钥;
 * @return 注册成功的账户对象;
 */
public CompositeAccount register(BlockchainIdentity accountId) {
    if (isReadonly()) {
        throw new IllegalArgumentException("This AccountSet is readonly!");
    }
    Bytes address = accountId.getAddress();
    PubKey pubKey = accountId.getPubKey();
    verifyAddressEncoding(address, pubKey);
    InnerMerkleAccount cachedAcc = latestAccountsCache.get(address);
    if (cachedAcc != null) {
        if (cachedAcc.getVersion() < 0) {
            // 同一个新账户已经注册,但尚未提交,所以重复注册不会引起任何变化;
            return cachedAcc;
        }
        // 相同的账户已经存在;
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    long version = merkleDataset.getVersion(address);
    if (version >= 0) {
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    if (!accessPolicy.checkRegistering(address, pubKey)) {
        throw new LedgerException("Account Registering was rejected for the access policy!");
    }
    Bytes prefix = keyPrefix.concat(address);
    InnerMerkleAccount acc = createInstance(accountId, cryptoSetting, prefix);
    latestAccountsCache.put(address, acc);
    updated = true;
    return acc;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 9 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class KvAccountSetEditor method loadId.

private BlockchainIdentity loadId(Bytes idkeyPrefix) {
    byte[] pubBytes = kvDataset.getValue(idkeyPrefix, -1);
    BytesValue pubBytesValue = BinaryProtocol.decode(pubBytes, BytesValue.class);
    PubKey pubKey = (PubKey) TypedValue.wrap(pubBytesValue).getValue();
    return new BlockchainIdentityData(AddressEncoding.generateAddress(pubKey), pubKey);
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) BytesValue(com.jd.blockchain.ledger.BytesValue) BlockchainIdentityData(com.jd.blockchain.ledger.BlockchainIdentityData)

Example 10 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class ContractDeployMojo method execute.

@Override
public void execute() throws MojoFailureException {
    Properties prop = new Properties();
    InputStream input = null;
    try {
        input = new FileInputStream(config);
        prop.load(input);
    } catch (IOException ex) {
        logger.error(ex.getMessage());
        throw new MojoFailureException("io error");
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
    }
    int port;
    try {
        port = Integer.parseInt(prop.getProperty("port"));
    } catch (NumberFormatException e) {
        logger.error(e.getMessage());
        throw new MojoFailureException("invalid port");
    }
    String host = prop.getProperty("host");
    String ledger = prop.getProperty("ledger");
    String pubKey = prop.getProperty("pubKey");
    String prvKey = prop.getProperty("prvKey");
    String password = prop.getProperty("password");
    String contractPath = prop.getProperty("contractPath");
    if (StringUtils.isEmpty(host)) {
        logger.info("host不能为空");
        return;
    }
    if (StringUtils.isEmpty(ledger)) {
        logger.info("ledger不能为空.");
        return;
    }
    if (StringUtils.isEmpty(pubKey)) {
        logger.info("pubKey不能为空.");
        return;
    }
    if (StringUtils.isEmpty(prvKey)) {
        logger.info("prvKey不能为空.");
        return;
    }
    if (StringUtils.isEmpty(contractPath)) {
        logger.info("contractPath不能为空.");
        return;
    }
    File contract = new File(contractPath);
    if (!contract.isFile()) {
        logger.info("文件" + contractPath + "不存在");
        return;
    }
    byte[] contractBytes = FileUtils.readBytes(contractPath);
    PrivKey prv = KeyGenUtils.decodePrivKeyWithRawPassword(prvKey, password);
    PubKey pub = KeyGenUtils.decodePubKey(pubKey);
    BlockchainKeypair blockchainKeyPair = new BlockchainKeypair(pub, prv);
    HashDigest ledgerHash = new HashDigest(Base58Utils.decode(ledger));
    StringBuffer sb = new StringBuffer();
    sb.append("host:" + host).append(",port:" + port).append(",ledgerHash:" + ledgerHash.toBase58()).append(",pubKey:" + pubKey).append(",prvKey:" + prv).append(",contractPath:" + contractPath);
    logger.info(sb.toString());
    ContractDeployExeUtil.instance.deploy(host, port, ledgerHash, blockchainKeyPair, contractBytes);
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) PrivKey(com.jd.blockchain.crypto.PrivKey) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) HashDigest(com.jd.blockchain.crypto.HashDigest) File(java.io.File)

Aggregations

PubKey (com.jd.blockchain.crypto.PubKey)21 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)6 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)5 Bytes (utils.Bytes)5 PrivKey (com.jd.blockchain.crypto.PrivKey)4 ParticipantNode (com.jd.blockchain.ledger.ParticipantNode)4 NetworkAddress (utils.net.NetworkAddress)4 NodeSettings (com.jd.blockchain.consensus.NodeSettings)3 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)2 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerException (com.jd.blockchain.ledger.LedgerException)2 NetworkReplica (com.jd.blockchain.consensus.NetworkReplica)1 BftsmartClientAuthCredit (com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit)1 BftsmartConsensusConfig (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig)1 BftsmartNodeConfig (com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig)1 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)1