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);
}
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;
}
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);
}
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;
}
}
Aggregations