Search in sources :

Example 1 with PrivKey

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

the class LedgerTestUtils method createKeyPair.

public static BlockchainKeypair createKeyPair(String pub, String priv) {
    PubKey pubKey = Crypto.resolveAsPubKey(Base58Utils.decode(pub));
    PrivKey privKey = Crypto.resolveAsPrivKey(Base58Utils.decode(priv));
    return new BlockchainKeypair(pubKey, privKey);
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) PrivKey(com.jd.blockchain.crypto.PrivKey)

Example 2 with PrivKey

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

the class BftsmartConsensusClientFactory method buildCredential.

@Override
public BftsmartClientAuthCredit buildCredential(SessionCredential sessionCredential, AsymmetricKeypair clientKeyPair, X509Certificate gatewayCertificate) {
    if (sessionCredential == null) {
        sessionCredential = BftsmartSessionCredentialConfig.createEmptyCredential();
    } else if (!(sessionCredential instanceof BftsmartSessionCredential)) {
        throw new IllegalArgumentException("Illegal credential info type! Requrie [" + BftsmartSessionCredential.class.getName() + "] but it is [" + sessionCredential.getClass().getName() + "]!");
    }
    PubKey pubKey = clientKeyPair.getPubKey();
    PrivKey privKey = clientKeyPair.getPrivKey();
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(pubKey.getAlgorithm());
    byte[] credentialBytes = BinaryProtocol.encode(sessionCredential, BftsmartSessionCredential.class);
    SignatureDigest signatureDigest = signatureFunction.sign(privKey, credentialBytes);
    BftsmartClientAuthCredit bftsmartClientAuthCredential = new BftsmartClientAuthCredit();
    bftsmartClientAuthCredential.setSessionCredential((BftsmartSessionCredential) sessionCredential);
    bftsmartClientAuthCredential.setPubKey(pubKey);
    bftsmartClientAuthCredential.setSignatureDigest(signatureDigest);
    bftsmartClientAuthCredential.setCertificate(null != gatewayCertificate ? CertificateUtils.toPEMString(gatewayCertificate) : null);
    return bftsmartClientAuthCredential;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) BftsmartClientAuthCredit(com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) BftsmartSessionCredential(com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential) PrivKey(com.jd.blockchain.crypto.PrivKey)

Example 3 with PrivKey

use of com.jd.blockchain.crypto.PrivKey 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)

Example 4 with PrivKey

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

the class KeyGenCommand method readKey.

/**
 * 读取密钥; <br>
 * 如果是私钥,则需要输入密码;
 *
 * @param keyFile
 */
public static void readKey(String keyFile, boolean decrypting) {
    String base58KeyString = FileUtils.readText(keyFile);
    byte[] keyBytes = Base58Utils.decode(base58KeyString);
    if (KeyGenUtils.isPubKeyBytes(keyBytes)) {
        if (decrypting) {
            // Try reading pubKey;
            PubKey pubKey = decodePubKey(keyBytes);
            ConsoleUtils.info("======================== pub key ========================\r\n" + "[%s]\r\n" + "Raw:[%s][%s]\r\n", base58KeyString, pubKey.getAlgorithm(), Base58Utils.encode(pubKey.toBytes()));
        } else {
            ConsoleUtils.info("======================== pub key ========================\r\n" + "[%s]\r\n", base58KeyString);
        }
        return;
    } else if (KeyGenUtils.isPrivKeyBytes(keyBytes)) {
        // Try reading privKye;
        try {
            if (decrypting) {
                byte[] pwdBytes = readPassword();
                PrivKey privKey = decryptedPrivKeyBytes(keyBytes, pwdBytes);
                ConsoleUtils.info("======================== priv key ========================\r\n" + "[%s]\r\n" + "Raw:[%s][%s]\r\n", base58KeyString, privKey.getAlgorithm(), Base58Utils.encode(privKey.toBytes()));
            } else {
                ConsoleUtils.info("======================== priv key ========================\r\n[%s]\r\n", base58KeyString);
            }
        } catch (DecryptionException e) {
            ConsoleUtils.error("Invalid password!");
        }
        return;
    } else {
        ConsoleUtils.error("Unknow key!!");
        return;
    }
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) KeyGenUtils.decodePubKey(com.jd.blockchain.crypto.KeyGenUtils.decodePubKey) KeyGenUtils.encodePubKey(com.jd.blockchain.crypto.KeyGenUtils.encodePubKey) KeyGenUtils.encodePrivKey(com.jd.blockchain.crypto.KeyGenUtils.encodePrivKey) PrivKey(com.jd.blockchain.crypto.PrivKey) DecryptionException(utils.security.DecryptionException)

Aggregations

PrivKey (com.jd.blockchain.crypto.PrivKey)4 PubKey (com.jd.blockchain.crypto.PubKey)4 BftsmartClientAuthCredit (com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit)1 BftsmartSessionCredential (com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 KeyGenUtils.decodePubKey (com.jd.blockchain.crypto.KeyGenUtils.decodePubKey)1 KeyGenUtils.encodePrivKey (com.jd.blockchain.crypto.KeyGenUtils.encodePrivKey)1 KeyGenUtils.encodePubKey (com.jd.blockchain.crypto.KeyGenUtils.encodePubKey)1 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)1 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 DecryptionException (utils.security.DecryptionException)1