Search in sources :

Example 11 with PayloadEncoder

use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.

the class TransactionResource method push.

// path push is overloaded (RecoveryResource & TransactionResource); swagger cannot handle
// situations like this so this operation documents both
@Operation(summary = "/push", operationId = "pushPayload", description = "store encoded payload to the server's database")
@ApiResponse(responseCode = "201", description = "hash of encoded payload", content = @Content(mediaType = TEXT_PLAIN, schema = @Schema(description = "hash of encrypted payload", type = "string", format = "base64")))
@ApiResponse(responseCode = "403", description = "server is in recovery mode and encoded payload is not a Standard Private transaction")
@POST
@Path("push")
@Consumes(APPLICATION_OCTET_STREAM)
public Response push(@Schema(description = "encoded payload") final byte[] payload, @HeaderParam(Constants.API_VERSION_HEADER) @Parameter(description = "client's supported API versions", array = @ArraySchema(schema = @Schema(type = "string"))) final List<String> headers) {
    LOGGER.debug("Received push request");
    final Set<String> versions = Optional.ofNullable(headers).orElse(emptyList()).stream().filter(Objects::nonNull).flatMap(v -> Arrays.stream(v.split(","))).collect(Collectors.toSet());
    final EncodedPayloadCodec codec = EncodedPayloadCodec.getPreferredCodec(versions);
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(codec);
    final MessageHash messageHash = transactionManager.storePayload(payloadEncoder.decode(payload));
    LOGGER.debug("Push request generated hash {}", messageHash);
    return Response.status(Response.Status.CREATED).entity(Objects.toString(messageHash)).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) MessageHash(com.quorum.tessera.data.MessageHash) ResendBatchRequest(com.quorum.tessera.p2p.recovery.ResendBatchRequest) LegacyResendManager(com.quorum.tessera.recovery.workflow.LegacyResendManager) Constants(com.quorum.tessera.shared.Constants) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) TransactionManager(com.quorum.tessera.transaction.TransactionManager) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) Parameter(io.swagger.v3.oas.annotations.Parameter) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) MediaType(jakarta.ws.rs.core.MediaType) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Tag(io.swagger.v3.oas.annotations.tags.Tag) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) ResendRequest(com.quorum.tessera.p2p.resend.ResendRequest) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) MessageHash(com.quorum.tessera.data.MessageHash) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 12 with PayloadEncoder

use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.

the class TransactionResource method resend.

@Operation(summary = "/resend", operationId = "requestPayloadResend", description = "initiate resend of either an INDIVIDUAL transaction or ALL transactions involving a given public key")
@ApiResponse(responseCode = "200", description = "resent payload", content = @Content(array = @ArraySchema(schema = @Schema(description = "empty if request was for ALL; else the encoded INDIVIDUAL transaction", type = "string", format = "byte"))))
@POST
@Path("resend")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
public Response resend(@Valid @NotNull final ResendRequest resendRequest) {
    LOGGER.debug("Received resend request");
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(EncodedPayloadCodec.LEGACY);
    final PublicKey recipient = Optional.of(resendRequest).map(ResendRequest::getPublicKey).map(Base64Codec.create()::decode).map(PublicKey::from).get();
    final MessageHash transactionHash = Optional.of(resendRequest).map(ResendRequest::getKey).map(Base64.getDecoder()::decode).map(MessageHash::new).orElse(null);
    final com.quorum.tessera.recovery.resend.ResendRequest request = com.quorum.tessera.recovery.resend.ResendRequest.Builder.create().withType(com.quorum.tessera.recovery.resend.ResendRequest.ResendRequestType.valueOf(resendRequest.getType())).withRecipient(recipient).withHash(transactionHash).build();
    final com.quorum.tessera.recovery.resend.ResendResponse response = legacyResendManager.resend(request);
    final Response.ResponseBuilder builder = Response.ok();
    Optional.ofNullable(response.getPayload()).map(payloadEncoder::encode).ifPresent(builder::entity);
    return builder.build();
}
Also used : Response(jakarta.ws.rs.core.Response) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 13 with PayloadEncoder

use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.

the class ResendBatchPublisherProvider method provider.

public static ResendBatchPublisher provider() {
    RecoveryClient client = RecoveryClient.create();
    PayloadEncoder payloadEncoder = PayloadEncoder.create(EncodedPayloadCodec.LEGACY);
    return new RestResendBatchPublisher(payloadEncoder, client);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder)

Example 14 with PayloadEncoder

use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method saveTransactionWithCallbackException.

@Test
public void saveTransactionWithCallbackException() throws Exception {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    byte[] payloadData = "payloadData".getBytes();
    when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
    when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        MessageHash transactionHash = new MessageHash(UUID.randomUUID().toString().getBytes());
        EncryptedTransaction transaction = new EncryptedTransaction();
        transaction.setPayload(encodedPayload);
        transaction.setHash(transactionHash);
        Callable<Void> callback = mock(Callable.class);
        when(callback.call()).thenThrow(new Exception("OUCH"));
        try {
            encryptedTransactionDAO.save(transaction, callback);
            failBecauseExceptionWasNotThrown(PersistenceException.class);
        } catch (PersistenceException ex) {
            assertThat(ex).isNotNull().hasMessageContaining("OUCH");
        }
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        final EncryptedTransaction result = entityManager.find(EncryptedTransaction.class, transactionHash);
        assertThat(result).isNull();
        verify(callback).call();
    }
    verify(payloadEncoder).encode(encodedPayload);
    verifyNoMoreInteractions(payloadEncoder);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 15 with PayloadEncoder

use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.

the class EncryptedTransactionDAOTest method saveDoesntAllowNullEncodedPayload.

@Test
public void saveDoesntAllowNullEncodedPayload() {
    PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(payloadEncoder.encode(encodedPayload)).thenReturn(null);
    try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
        createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
        EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setHash(new MessageHash(new byte[] { 5 }));
        encryptedTransaction.setPayload(encodedPayload);
        try {
            encryptedTransactionDAO.save(encryptedTransaction);
            failBecauseExceptionWasNotThrown(PersistenceException.class);
        } catch (PersistenceException ex) {
            String expectedMessage = String.format(testConfig.getRequiredFieldColumnTemplate(), "ENCODED_PAYLOAD");
            assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(expectedMessage).hasMessageContaining("ENCODED_PAYLOAD");
        }
    }
    verify(payloadEncoder).encode(encodedPayload);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Aggregations

PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)27 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)23 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)10 Response (jakarta.ws.rs.core.Response)6 Collectors (java.util.stream.Collectors)5 MessageHash (com.quorum.tessera.data.MessageHash)3 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)3 PublishPayloadException (com.quorum.tessera.transaction.publish.PublishPayloadException)3 Operation (io.swagger.v3.oas.annotations.Operation)3 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)3 java.util (java.util)3 IntStream (java.util.stream.IntStream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.Test)3 RunWith (org.junit.runner.RunWith)3 Parameterized (org.junit.runners.Parameterized)3 Mockito (org.mockito.Mockito)3 PublicKey (com.quorum.tessera.encryption.PublicKey)2 ResendBatchResponse (com.quorum.tessera.recovery.resend.ResendBatchResponse)2 BatchResendManager (com.quorum.tessera.recovery.workflow.BatchResendManager)2