use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class Channel method queryBlockByHash.
/**
* Query a peer in this channel for a Block by the block hash.
*
* @param peers the Peers to query.
* @param blockHash the hash of the Block in the chain.
* @param userContext the user context
* @return the {@link BlockInfo} with the given block Hash
* @throws InvalidArgumentException if the channel is shutdown or any of the arguments are not valid.
* @throws ProposalException if an error occurred processing the query.
*/
public BlockInfo queryBlockByHash(Collection<Peer> peers, byte[] blockHash, User userContext) throws InvalidArgumentException, ProposalException {
checkChannelState();
checkPeers(peers);
userContextCheck(userContext);
if (blockHash == null) {
throw new InvalidArgumentException("blockHash parameter is null.");
}
try {
logger.trace("queryBlockByHash with hash : " + Hex.encodeHexString(blockHash) + " on channel " + name);
QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
querySCCRequest.setFcn(QuerySCCRequest.GETBLOCKBYHASH);
querySCCRequest.setArgs(name);
querySCCRequest.setArgBytes(new byte[][] { blockHash });
ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
return new BlockInfo(Block.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
} catch (InvalidProtocolBufferException e) {
ProposalException proposalException = new ProposalException(e);
logger.error(proposalException);
throw proposalException;
}
}
use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class Channel method sendInstallProposal.
/**
* Send install chaincode request proposal to the channel.
*
* @param installProposalRequest
* @param peers
* @return
* @throws ProposalException
* @throws InvalidArgumentException
*/
Collection<ProposalResponse> sendInstallProposal(InstallProposalRequest installProposalRequest, Collection<Peer> peers) throws ProposalException, InvalidArgumentException {
checkChannelState();
checkPeers(peers);
if (null == installProposalRequest) {
throw new InvalidArgumentException("InstallProposalRequest is null");
}
try {
TransactionContext transactionContext = getTransactionContext(installProposalRequest.getUserContext());
// Install will have no signing cause it's not really targeted to a channel.
transactionContext.verify(false);
transactionContext.setProposalWaitTime(installProposalRequest.getProposalWaitTime());
InstallProposalBuilder installProposalbuilder = InstallProposalBuilder.newBuilder();
installProposalbuilder.context(transactionContext);
installProposalbuilder.setChaincodeLanguage(installProposalRequest.getChaincodeLanguage());
installProposalbuilder.chaincodeName(installProposalRequest.getChaincodeName());
installProposalbuilder.chaincodePath(installProposalRequest.getChaincodePath());
installProposalbuilder.chaincodeVersion(installProposalRequest.getChaincodeVersion());
installProposalbuilder.setChaincodeSource(installProposalRequest.getChaincodeSourceLocation());
installProposalbuilder.setChaincodeInputStream(installProposalRequest.getChaincodeInputStream());
installProposalbuilder.setChaincodeMetaInfLocation(installProposalRequest.getChaincodeMetaInfLocation());
FabricProposal.Proposal deploymentProposal = installProposalbuilder.build();
SignedProposal signedProposal = getSignedProposal(transactionContext, deploymentProposal);
return sendProposalToPeers(peers, signedProposal, transactionContext);
} catch (Exception e) {
throw new ProposalException(e);
}
}
use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method addCACertificateToTrustStore.
/**
* addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
*
* @param bytes an X.509 certificate in PEM format in bytes
* @param alias an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
* @throws CryptoException
* @throws InvalidArgumentException
*/
public void addCACertificateToTrustStore(byte[] bytes, String alias) throws CryptoException, InvalidArgumentException {
if (bytes == null) {
throw new InvalidArgumentException("The certificate cannot be null");
}
if (alias == null || alias.isEmpty()) {
throw new InvalidArgumentException("You must assign an alias to a certificate when adding to the trust store");
}
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
Certificate caCert = cf.generateCertificate(bis);
addCACertificateToTrustStore(caCert, alias);
} catch (CertificateException e) {
throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
}
}
use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method generateCertificationRequest.
/**
* generateCertificationRequest
*
* @param subject The subject to be added to the certificate
* @param pair Public private key pair
* @return PKCS10CertificationRequest Certificate Signing Request.
* @throws OperatorCreationException
*/
public String generateCertificationRequest(String subject, KeyPair pair) throws InvalidArgumentException {
try {
PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(new X500Principal("CN=" + subject), pair.getPublic());
JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withECDSA");
if (null != SECURITY_PROVIDER) {
csBuilder.setProvider(SECURITY_PROVIDER);
}
ContentSigner signer = csBuilder.build(pair.getPrivate());
return certificationRequestToPEM(p10Builder.build(signer));
} catch (Exception e) {
logger.error(e);
throw new InvalidArgumentException(e);
}
}
use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HLSDKJCryptoSuiteFactory method getCryptoSuite.
@Override
public CryptoSuite getCryptoSuite(Properties properties) throws CryptoException, InvalidArgumentException {
CryptoSuite ret = cache.get(properties);
if (ret == null) {
try {
CryptoPrimitives cp = new CryptoPrimitives();
cp.setProperties(properties);
cp.init();
ret = cp;
} catch (Exception e) {
throw new CryptoException(e.getMessage(), e);
}
cache.put(properties, ret);
}
return ret;
}
Aggregations