Search in sources :

Example 6 with EncodedPayloadCodec

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

the class EncryptedTransactionListener method onSave.

@PrePersist
public void onSave(EncryptedTransaction encryptedTransaction) {
    LOGGER.debug("onSave {}", encryptedTransaction);
    final EncodedPayload encodedPayload = encryptedTransaction.getPayload();
    final EncodedPayloadCodec encodedPayloadCodec = EncodedPayloadCodec.current();
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(encodedPayloadCodec);
    final byte[] encodedPayloadData = payloadEncoder.encode(encodedPayload);
    encryptedTransaction.setEncodedPayloadCodec(encodedPayloadCodec);
    encryptedTransaction.setEncodedPayload(encodedPayloadData);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) PrePersist(jakarta.persistence.PrePersist)

Example 7 with EncodedPayloadCodec

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

the class EncryptedTransactionListener method onLoad.

@PostLoad
public void onLoad(EncryptedTransaction encryptedTransaction) {
    LOGGER.debug("onLoad[{}]", encryptedTransaction);
    final EncodedPayloadCodec encodedPayloadCodec = Optional.ofNullable(encryptedTransaction.getEncodedPayloadCodec()).orElse(EncodedPayloadCodec.LEGACY);
    final byte[] encodedPayloadData = encryptedTransaction.getEncodedPayload();
    final PayloadEncoder payloadEncoder = PayloadEncoder.create(encodedPayloadCodec);
    final EncodedPayload encodedPayload = payloadEncoder.decode(encodedPayloadData);
    encryptedTransaction.setPayload(encodedPayload);
    encryptedTransaction.setEncodedPayloadCodec(encodedPayloadCodec);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) PostLoad(jakarta.persistence.PostLoad)

Example 8 with EncodedPayloadCodec

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

the class RecoveryResource method push.

// path /push with application/octet-stream is overloaded (RecoveryResource &
// TransactionResource); swagger annotations cannot handle situations like this so hide this
// operation and use TransactionResource::push to document both
@Hidden
@POST
@Path("push")
@Consumes(APPLICATION_OCTET_STREAM)
public Response push(final byte[] payload, @HeaderParam(Constants.API_VERSION_HEADER) final List<String> headers) {
    LOGGER.debug("Received push request during recovery mode");
    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 EncodedPayload encodedPayload = payloadEncoder.decode(payload);
    if (encodedPayload.getPrivacyMode() != PrivacyMode.STANDARD_PRIVATE) {
        return Response.status(Response.Status.FORBIDDEN).entity("Transactions with enhanced privacy are not accepted during recovery mode").build();
    }
    final MessageHash messageHash = transactionManager.storePayload(encodedPayload);
    LOGGER.debug("Push request generated hash {}", messageHash);
    return Response.status(Response.Status.CREATED).entity(Objects.toString(messageHash)).build();
}
Also used : java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) HeaderParam(jakarta.ws.rs.HeaderParam) LoggerFactory(org.slf4j.LoggerFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) PushBatchRequest(com.quorum.tessera.p2p.recovery.PushBatchRequest) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) Path(jakarta.ws.rs.Path) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) APPLICATION_OCTET_STREAM(jakarta.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) MessageHash(com.quorum.tessera.data.MessageHash) Constants(com.quorum.tessera.shared.Constants) Consumes(jakarta.ws.rs.Consumes) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) Hidden(io.swagger.v3.oas.annotations.Hidden) POST(jakarta.ws.rs.POST) Collections.emptyList(java.util.Collections.emptyList) TransactionManager(com.quorum.tessera.transaction.TransactionManager) Collectors(java.util.stream.Collectors) Tag(io.swagger.v3.oas.annotations.tags.Tag) APPLICATION_JSON(jakarta.ws.rs.core.MediaType.APPLICATION_JSON) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Hidden(io.swagger.v3.oas.annotations.Hidden)

Example 9 with EncodedPayloadCodec

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

the class ResendAllIT method transactionFromSenderDoesNotContainDataOfOtherParties.

@Test
public void transactionFromSenderDoesNotContainDataOfOtherParties() throws UnsupportedEncodingException, SQLException {
    // setup (sending in a tx)
    Response sendRawResponse = partyOne.getRestClient().target(partyOne.getQ2TUri()).path("/sendraw").request().header("c11n-from", partyOne.getPublicKey()).header("c11n-to", partyTwo.getPublicKey() + "," + partyThree.getPublicKey()).post(Entity.entity(transactionData, MediaType.APPLICATION_OCTET_STREAM));
    URI location = sendRawResponse.getLocation();
    String hash = sendRawResponse.readEntity(String.class);
    final String encodedHash = URLEncoder.encode(hash, UTF_8.toString());
    // delete it from a recipient node
    final Response deleteReq = partyTwo.getRestClient().target(partyTwo.getQ2TUri()).path("transaction").path(encodedHash).request().delete();
    assertThat(deleteReq).isNotNull();
    assertThat(deleteReq.getStatus()).isEqualTo(204);
    // check it is deleted
    final Response deleteCheck = partyTwo.getRestClient().target(partyTwo.getQ2TUri()).path("transaction").path(encodedHash).request().get();
    assertThat(deleteCheck).isNotNull();
    assertThat(deleteCheck.getStatus()).isEqualTo(404);
    // request resend from sender
    final ResendRequest req = new ResendRequest();
    req.setType(RESEND_ALL_VALUE);
    req.setPublicKey(partyTwo.getPublicKey());
    final Response resendRequest = vanillaHttpOnlyClient.target(partyOne.getP2PUri()).path(RESEND_PATH).request().buildPost(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE)).invoke();
    assertThat(resendRequest).isNotNull();
    assertThat(resendRequest.getStatus()).isEqualTo(200);
    final String fetch = "SELECT ENCODED_PAYLOAD, PAYLOAD_CODEC FROM ENCRYPTED_TRANSACTION WHERE HASH = ?";
    final Connection databaseConnection = PartyHelper.create().findByPublicKey(partyTwo.getPublicKey()).getDatabaseConnection();
    try (PreparedStatement statement = databaseConnection.prepareStatement(fetch)) {
        statement.setBytes(1, Base64.getDecoder().decode(hash));
        try (ResultSet rs = statement.executeQuery()) {
            assertThat(rs.next()).isTrue();
            final byte[] output = rs.getBytes(1);
            final EncodedPayloadCodec codec = EncodedPayloadCodec.valueOf(rs.getString(2));
            final PayloadEncoder encoderToUse = PayloadEncoder.create(codec);
            final EncodedPayload payload = encoderToUse.decode(output);
            assertThat(payload.getRecipientKeys()).hasSize(1);
            assertThat(payload.getRecipientKeys().get(0).encodeToBase64()).isEqualTo(partyTwo.getPublicKey());
            assertThat(payload.getSenderKey().encodeToBase64()).isEqualTo(partyOne.getPublicKey());
            assertThat(payload.getRecipientBoxes()).hasSize(1);
        }
    }
}
Also used : Response(jakarta.ws.rs.core.Response) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) URI(java.net.URI) ResendRequest(com.quorum.tessera.p2p.resend.ResendRequest) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Test(org.junit.Test)

Aggregations

EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)9 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)7 Response (jakarta.ws.rs.core.Response)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)3 MessageHash (com.quorum.tessera.data.MessageHash)2 BatchResendManager (com.quorum.tessera.recovery.workflow.BatchResendManager)2 Constants (com.quorum.tessera.shared.Constants)2 TransactionManager (com.quorum.tessera.transaction.TransactionManager)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 Tag (io.swagger.v3.oas.annotations.tags.Tag)2 PostLoad (jakarta.persistence.PostLoad)2 Valid (jakarta.validation.Valid)2 NotNull (jakarta.validation.constraints.NotNull)2 URI (java.net.URI)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 java.util (java.util)2