Search in sources :

Example 11 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method serializeChannel.

/**
 * Serialize channel to a byte array using Java serialization.
 * Deserialized channel will NOT be in an initialized state.
 *
 * @throws InvalidArgumentException
 * @throws IOException
 */
public byte[] serializeChannel() throws IOException, InvalidArgumentException {
    if (isShutdown()) {
        throw new InvalidArgumentException(format("Channel %s has been shutdown.", getName()));
    }
    ObjectOutputStream out = null;
    try {
        ByteArrayOutputStream bai = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bai);
        out.writeObject(this);
        out.flush();
        return bai.toByteArray();
    } finally {
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                // best effort.
                logger.error(e);
            }
        }
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 12 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class Channel method queryBlockchainInfo.

/**
 * query for chain information
 *
 * @param peers       The peers to try send the request.
 * @param userContext the user context.
 * @return a {@link BlockchainInfo} object containing the chain info requested
 * @throws InvalidArgumentException
 * @throws ProposalException
 */
public BlockchainInfo queryBlockchainInfo(Collection<Peer> peers, User userContext) throws ProposalException, InvalidArgumentException {
    checkChannelState();
    checkPeers(peers);
    User.userContextCheck(userContext);
    try {
        logger.debug("queryBlockchainInfo to peer " + " on channel " + name);
        QuerySCCRequest querySCCRequest = new QuerySCCRequest(userContext);
        querySCCRequest.setFcn(QuerySCCRequest.GETCHAININFO);
        querySCCRequest.setArgs(name);
        ProposalResponse proposalResponse = sendProposalSerially(querySCCRequest, peers);
        return new BlockchainInfo(Ledger.BlockchainInfo.parseFrom(proposalResponse.getProposalResponse().getResponse().getPayload()));
    } catch (Exception e) {
        logger.error(e);
        throw new ProposalException(e);
    }
}
Also used : ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) FabricProposalResponse(org.hyperledger.fabric.protos.peer.FabricProposalResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 13 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class CryptoPrimitives method setProperties.

// /* (non-Javadoc)
// * @see org.hyperledger.fabric.sdk.security.CryptoSuite#setProperties(java.util.Properties)
// */
// @Override
void setProperties(Properties properties) throws CryptoException, InvalidArgumentException {
    if (properties == null) {
        throw new InvalidArgumentException("properties must not be null");
    }
    // if (properties != null) {
    hashAlgorithm = Optional.ofNullable(properties.getProperty(Config.HASH_ALGORITHM)).orElse(hashAlgorithm);
    String secLevel = Optional.ofNullable(properties.getProperty(Config.SECURITY_LEVEL)).orElse(Integer.toString(securityLevel));
    securityLevel = Integer.parseInt(secLevel);
    if (properties.containsKey(Config.SECURITY_CURVE_MAPPING)) {
        securityCurveMapping = Config.parseSecurityCurveMappings(properties.getProperty(Config.SECURITY_CURVE_MAPPING));
    } else {
        securityCurveMapping = config.getSecurityCurveMapping();
    }
    final String providerName = properties.containsKey(Config.SECURITY_PROVIDER_CLASS_NAME) ? properties.getProperty(Config.SECURITY_PROVIDER_CLASS_NAME) : config.getSecurityProviderClassName();
    try {
        SECURITY_PROVIDER = setUpExplictProvider(providerName);
    } catch (Exception e) {
        throw new InvalidArgumentException(format("Getting provider for class name: %s", providerName), e);
    }
    CERTIFICATE_FORMAT = Optional.ofNullable(properties.getProperty(Config.CERTIFICATE_FORMAT)).orElse(CERTIFICATE_FORMAT);
    DEFAULT_SIGNATURE_ALGORITHM = Optional.ofNullable(properties.getProperty(Config.SIGNATURE_ALGORITHM)).orElse(DEFAULT_SIGNATURE_ALGORITHM);
    resetConfiguration();
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KeyStoreException(java.security.KeyStoreException) CertPathValidatorException(java.security.cert.CertPathValidatorException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException)

Example 14 with InvalidArgumentException

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 caCertPem an X.509 certificate in PEM format
 * @param alias     an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
 * @throws CryptoException
 * @throws InvalidArgumentException
 */
public void addCACertificateToTrustStore(File caCertPem, String alias) throws CryptoException, InvalidArgumentException {
    if (caCertPem == 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(FileUtils.readFileToByteArray(caCertPem)));
        Certificate caCert = cf.generateCertificate(bis);
        addCACertificateToTrustStore(caCert, alias);
    } catch (CertificateException | IOException e) {
        throw new CryptoException("Unable to add CA certificate to trust store. Error: " + e.getMessage(), e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 15 with InvalidArgumentException

use of org.hyperledger.fabric.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.

the class CryptoPrimitives method createTrustStore.

private void createTrustStore() throws CryptoException {
    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        setTrustStore(keyStore);
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | InvalidArgumentException e) {
        throw new CryptoException("Cannot create trust store. Error: " + e.getMessage(), e);
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) KeyStore(java.security.KeyStore)

Aggregations

InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)38 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)26 IOException (java.io.IOException)22 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)22 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)21 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)17 StatusRuntimeException (io.grpc.StatusRuntimeException)16 ExecutionException (java.util.concurrent.ExecutionException)16 TimeoutException (java.util.concurrent.TimeoutException)16 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)16 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)16 ByteString (com.google.protobuf.ByteString)11 TransactionContext (org.hyperledger.fabric.sdk.transaction.TransactionContext)11 FabricProposalResponse (org.hyperledger.fabric.protos.peer.FabricProposalResponse)9 SignedProposal (org.hyperledger.fabric.protos.peer.FabricProposal.SignedProposal)8 ProtoUtils.getSignatureHeaderAsByteString (org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString)7 FabricProposal (org.hyperledger.fabric.protos.peer.FabricProposal)6 CertificateException (java.security.cert.CertificateException)5 BroadcastResponse (org.hyperledger.fabric.protos.orderer.Ab.BroadcastResponse)5 ArrayList (java.util.ArrayList)4