Search in sources :

Example 1 with EnhancedPrivacyNotSupportedException

use of com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException in project tessera by ConsenSys.

the class RestPayloadPublisher method publishPayload.

@Override
public void publishPayload(EncodedPayload payload, PublicKey recipientKey) {
    final NodeInfo remoteNodeInfo = discovery.getRemoteNodeInfo(recipientKey);
    final Set<String> supportedApiVersions = remoteNodeInfo.supportedApiVersions();
    final EncodedPayloadCodec preferredCodec = EncodedPayloadCodec.getPreferredCodec(supportedApiVersions);
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(preferredCodec);
    if (PrivacyMode.STANDARD_PRIVATE != payload.getPrivacyMode() && !supportedApiVersions.contains(EnhancedPrivacyVersion.API_VERSION_2)) {
        throw new EnhancedPrivacyNotSupportedException("Transactions with enhanced privacy is not currently supported on recipient " + recipientKey.encodeToBase64());
    }
    if (PrivacyMode.MANDATORY_RECIPIENTS == payload.getPrivacyMode() && !supportedApiVersions.contains(MandatoryRecipientsVersion.API_VERSION_4)) {
        throw new MandatoryRecipientsNotSupportedException("Transactions with mandatory recipients are not currently supported on recipient " + recipientKey.encodeToBase64());
    }
    final String targetUrl = remoteNodeInfo.getUrl();
    LOGGER.info("Publishing message to {}", targetUrl);
    final byte[] encoded = payloadEncoder.encode(payload);
    try (Response response = client.target(targetUrl).path("/push").request().post(Entity.entity(encoded, MediaType.APPLICATION_OCTET_STREAM_TYPE))) {
        if (Response.Status.OK.getStatusCode() != response.getStatus() && Response.Status.CREATED.getStatusCode() != response.getStatus()) {
            throw new PublishPayloadException("Unable to push payload to recipient url " + targetUrl);
        }
        LOGGER.info("Published to {}", targetUrl);
    } catch (ProcessingException ex) {
        LOGGER.debug("", ex);
        throw new NodeOfflineException(URI.create(targetUrl));
    }
}
Also used : Response(jakarta.ws.rs.core.Response) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) PublishPayloadException(com.quorum.tessera.transaction.publish.PublishPayloadException) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) MandatoryRecipientsNotSupportedException(com.quorum.tessera.transaction.exception.MandatoryRecipientsNotSupportedException) NodeOfflineException(com.quorum.tessera.transaction.publish.NodeOfflineException) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 2 with EnhancedPrivacyNotSupportedException

use of com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException in project tessera by ConsenSys.

the class LegacyResendManagerImpl method resendIndividual.

protected ResendResponse resendIndividual(final PublicKey targetResendKey, final MessageHash messageHash) {
    final EncryptedTransaction encryptedTransaction = encryptedTransactionDAO.retrieveByHash(messageHash).orElseThrow(() -> new TransactionNotFoundException("Message with hash " + messageHash + " was not found"));
    final EncodedPayload payload = encryptedTransaction.getPayload();
    if (payload.getPrivacyMode() != PrivacyMode.STANDARD_PRIVATE) {
        throw new EnhancedPrivacyNotSupportedException("Cannot resend enhanced privacy transaction in legacy resend");
    }
    if (!Objects.equals(payload.getSenderKey(), targetResendKey)) {
        final EncodedPayload formattedPayload = EncodedPayload.Builder.forRecipient(payload, targetResendKey).build();
        return ResendResponse.Builder.create().withPayload(formattedPayload).build();
    }
    // split all the boxes out into their own payload
    final Set<EncodedPayload> allTxns = payload.getRecipientBoxes().stream().map(box -> EncodedPayload.Builder.from(payload).withNewRecipientKeys(Collections.emptyList()).withRecipientBoxes(List.of(box.getData())).build()).collect(Collectors.toSet());
    final BatchWorkflowContext context = new BatchWorkflowContext();
    context.setPayloadsToPublish(allTxns);
    context.setEncryptedTransaction(encryptedTransaction);
    new SearchRecipientKeyForPayload(enclave).execute(context);
    final EncodedPayload.Builder builder = EncodedPayload.Builder.from(payload).withNewRecipientKeys(new ArrayList<>()).withRecipientBoxes(new ArrayList<>());
    context.getPayloadsToPublish().forEach(formattedPayload -> {
        builder.withRecipientKey(formattedPayload.getRecipientKeys().get(0));
        builder.withRecipientBox(formattedPayload.getRecipientBoxes().get(0).getData());
    });
    return ResendResponse.Builder.create().withPayload(builder.build()).build();
}
Also used : IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) java.util(java.util) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Collectors(java.util.stream.Collectors) com.quorum.tessera.recovery.workflow(com.quorum.tessera.recovery.workflow) Discovery(com.quorum.tessera.discovery.Discovery) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) PayloadPublisher(com.quorum.tessera.transaction.publish.PayloadPublisher) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Enclave(com.quorum.tessera.enclave.Enclave) MessageHash(com.quorum.tessera.data.MessageHash) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)

Example 3 with EnhancedPrivacyNotSupportedException

use of com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException in project tessera by ConsenSys.

the class RestPayloadPublisherTest method publishEnhancedTransactionsToNodesThatDoNotSupport.

@Test
public void publishEnhancedTransactionsToNodesThatDoNotSupport() {
    Map<PrivacyMode, Set<String>> privacyModeAndVersions = new HashMap<>();
    privacyModeAndVersions.put(PrivacyMode.PARTY_PROTECTION, Set.of("v1"));
    privacyModeAndVersions.put(PrivacyMode.PRIVATE_STATE_VALIDATION, Set.of("v1"));
    for (Map.Entry<PrivacyMode, Set<String>> pair : privacyModeAndVersions.entrySet()) {
        String targetUrl = "http://someplace.com";
        EncodedPayload encodedPayload = mock(EncodedPayload.class);
        when(encodedPayload.getPrivacyMode()).thenReturn(pair.getKey());
        byte[] payloadData = "Some Data".getBytes();
        when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
        PublicKey recipientKey = mock(PublicKey.class);
        NodeInfo nodeInfo = mock(NodeInfo.class);
        when(nodeInfo.supportedApiVersions()).thenReturn(pair.getValue());
        Recipient recipient = mock(Recipient.class);
        when(recipient.getKey()).thenReturn(recipientKey);
        when(recipient.getUrl()).thenReturn(targetUrl);
        when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
        when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
        EnhancedPrivacyNotSupportedException exception = catchThrowableOfType(() -> payloadPublisher.publishPayload(encodedPayload, recipientKey), EnhancedPrivacyNotSupportedException.class);
        assertThat(exception).hasMessageContaining("Transactions with enhanced privacy is not currently supported");
        verify(discovery).getRemoteNodeInfo(eq(recipientKey));
    }
    payloadEncoderFactoryFunction.verify(times(2), () -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Recipient(com.quorum.tessera.partyinfo.node.Recipient) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) HashMap(java.util.HashMap) Map(java.util.Map) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Test(org.junit.Test)

Example 4 with EnhancedPrivacyNotSupportedException

use of com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException in project tessera by ConsenSys.

the class EnhancedPrivacyNotSupportedExceptionMapperTest method handleException.

@Test
public void handleException() {
    final String message = ".. all outta gum";
    final EnhancedPrivacyNotSupportedException exception = new EnhancedPrivacyNotSupportedException(message);
    final Response result = mapper.toResponse(exception);
    assertThat(result.getStatus()).isEqualTo(403);
    assertThat(result.getEntity()).isEqualTo(message);
}
Also used : Response(jakarta.ws.rs.core.Response) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Test(org.junit.Test)

Aggregations

EnhancedPrivacyNotSupportedException (com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)4 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)2 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)2 PublicKey (com.quorum.tessera.encryption.PublicKey)2 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)2 Response (jakarta.ws.rs.core.Response)2 Test (org.junit.Test)2 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)1 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)1 MessageHash (com.quorum.tessera.data.MessageHash)1 Discovery (com.quorum.tessera.discovery.Discovery)1 Enclave (com.quorum.tessera.enclave.Enclave)1 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)1 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)1 Recipient (com.quorum.tessera.partyinfo.node.Recipient)1 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)1 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)1 com.quorum.tessera.recovery.workflow (com.quorum.tessera.recovery.workflow)1 MandatoryRecipientsNotSupportedException (com.quorum.tessera.transaction.exception.MandatoryRecipientsNotSupportedException)1 TransactionNotFoundException (com.quorum.tessera.transaction.exception.TransactionNotFoundException)1