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