Search in sources :

Example 46 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method individualMissingTxFails.

@Test
public void individualMissingTxFails() {
    when(dao.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    final MessageHash txHash = new MessageHash("sample-hash".getBytes());
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.INDIVIDUAL).withHash(txHash).withRecipient(targetResendKey).build();
    final Throwable throwable = catchThrowable(() -> resendManager.resend(request));
    assertThat(throwable).isInstanceOf(TransactionNotFoundException.class).hasMessage("Message with hash c2FtcGxlLWhhc2g= was not found");
    verify(dao).retrieveByHash(txHash);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) MessageHash(com.quorum.tessera.data.MessageHash) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) Test(org.junit.Test)

Example 47 with MessageHash

use of com.quorum.tessera.data.MessageHash 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 48 with MessageHash

use of com.quorum.tessera.data.MessageHash 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 49 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class PrivacyHelperTest method findAffectedContractTransactionsFromPayload.

@Test
public void findAffectedContractTransactionsFromPayload() {
    final EncodedPayload payload = mock(EncodedPayload.class);
    Map<TxHash, SecurityHash> affected = new HashMap<>();
    affected.put(TxHash.from("Hash1".getBytes()), SecurityHash.from("secHash1".getBytes()));
    affected.put(TxHash.from("Hash2".getBytes()), SecurityHash.from("secHash2".getBytes()));
    EncryptedTransaction et1 = mock(EncryptedTransaction.class);
    when(et1.getEncodedPayload()).thenReturn("payload1".getBytes());
    when(et1.getHash()).thenReturn(new MessageHash("Hash1".getBytes()));
    when(et1.getPayload()).thenReturn(mock(EncodedPayload.class));
    when(payload.getAffectedContractTransactions()).thenReturn(affected);
    when(encryptedTransactionDAO.findByHashes(any())).thenReturn(singletonList(et1));
    List<AffectedTransaction> result = privacyHelper.findAffectedContractTransactionsFromPayload(payload);
    assertThat(result).hasSize(1);
    verify(encryptedTransactionDAO).findByHashes(any());
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 50 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class PrivacyHelperTest method findAffectedContractTransactionsFromSendRequestNotFound.

@Test
public void findAffectedContractTransactionsFromSendRequestNotFound() {
    final MessageHash hash1 = mock(MessageHash.class);
    final MessageHash hash2 = mock(MessageHash.class);
    EncryptedTransaction et1 = mock(EncryptedTransaction.class);
    when(et1.getEncodedPayload()).thenReturn("payload1".getBytes());
    when(et1.getHash()).thenReturn(new MessageHash("hash1".getBytes()));
    when(encryptedTransactionDAO.findByHashes(anyCollection())).thenReturn(List.of(et1));
    assertThatExceptionOfType(PrivacyViolationException.class).isThrownBy(() -> {
        privacyHelper.findAffectedContractTransactionsFromSendRequest(Set.of(hash1, hash2));
        failBecauseExceptionWasNotThrown(Exception.class);
    }).withMessageContaining("Unable to find affectedContractTransaction");
    verify(encryptedTransactionDAO).findByHashes(any());
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Test(org.junit.Test)

Aggregations

MessageHash (com.quorum.tessera.data.MessageHash)81 PublicKey (com.quorum.tessera.encryption.PublicKey)56 Test (org.junit.Test)51 Response (jakarta.ws.rs.core.Response)44 Operation (io.swagger.v3.oas.annotations.Operation)21 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)21 SendResponse (com.quorum.tessera.api.SendResponse)17 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)16 SendRequest (com.quorum.tessera.api.SendRequest)15 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)15 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)15 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 java.util (java.util)13 Collectors (java.util.stream.Collectors)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)12 TransactionManager (com.quorum.tessera.transaction.TransactionManager)12 Tag (io.swagger.v3.oas.annotations.tags.Tag)11 Valid (jakarta.validation.Valid)11