Search in sources :

Example 16 with ReceiveResponse

use of com.quorum.tessera.api.ReceiveResponse in project tessera by ConsenSys.

the class CustomPayloadEncryptionIT method createAndDecryptPayload.

@Test
public void createAndDecryptPayload() {
    final Party sender = partyHelper.findByAlias(NodeAlias.A);
    final Party recipient = partyHelper.findByAlias(NodeAlias.B);
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(Base64.getEncoder().encode("Test Payload".getBytes()));
    sendRequest.setTo(recipient.getPublicKey());
    final Response encryptResult = sender.getRestClientWebTarget().path("/encodedpayload/create").request().post(Entity.entity(sendRequest, mediaType));
    assertThat(encryptResult.getStatus()).isEqualTo(200);
    final PayloadEncryptResponse payloadEncryptResponse = encryptResult.readEntity(PayloadEncryptResponse.class);
    // decrypt it again with the sender
    final Response decryptResultForSender = sender.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
    final ReceiveResponse decryptedPayload = decryptResultForSender.readEntity(ReceiveResponse.class);
    assertThat(Base64.getDecoder().decode(decryptedPayload.getPayload())).isEqualTo("Test Payload".getBytes());
    // decrypt it using the recipient
    final String firstRecipientInList = Base64.getEncoder().encodeToString(payloadEncryptResponse.getRecipientKeys().get(0));
    if (Objects.equals(firstRecipientInList, sender.getPublicKey())) {
        payloadEncryptResponse.getRecipientBoxes().remove(0);
    } else {
        payloadEncryptResponse.getRecipientBoxes().remove(1);
    }
    payloadEncryptResponse.setRecipientKeys(Collections.emptyList());
    final Response decryptResultForRecipient = recipient.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
    final ReceiveResponse decryptedPayloadForRecipient = decryptResultForRecipient.readEntity(ReceiveResponse.class);
    assertThat(Base64.getDecoder().decode(decryptedPayloadForRecipient.getPayload())).isEqualTo("Test Payload".getBytes());
}
Also used : PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Test(org.junit.Test)

Example 17 with ReceiveResponse

use of com.quorum.tessera.api.ReceiveResponse in project tessera by ConsenSys.

the class EncodedPayloadResource method decryptEncodedPayload.

// hide this operation from swagger generation; the /encodedpayload/decrypt operation is
// overloaded and must be documented in a single place
@Hidden
@POST
@Path("decrypt")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response decryptEncodedPayload(@Valid @NotNull final PayloadDecryptRequest request) {
    LOGGER.info("Received request to decrypt custom transaction");
    final Base64.Decoder decoder = Base64.getDecoder();
    final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
    final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
    final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
    final ReceiveResponse receiveResponse = new ReceiveResponse();
    receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
    receiveResponse.setPayload(response.getUnencryptedTransactionData());
    receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
    Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
    return Response.ok(receiveResponse).type(APPLICATION_JSON).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) RecipientBox(com.quorum.tessera.enclave.RecipientBox) Path(jakarta.ws.rs.Path) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) MIME_TYPE_JSON_2_1(com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1) Produces(jakarta.ws.rs.Produces) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Consumes(jakarta.ws.rs.Consumes) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Hidden(io.swagger.v3.oas.annotations.Hidden) POST(jakarta.ws.rs.POST) TransactionManager(com.quorum.tessera.transaction.TransactionManager) Collectors(java.util.stream.Collectors) PayloadDecryptRequest(com.quorum.tessera.api.PayloadDecryptRequest) SendRequest(com.quorum.tessera.api.SendRequest) EncodedPayloadManager(com.quorum.tessera.transaction.EncodedPayloadManager) Stream(java.util.stream.Stream) Tag(io.swagger.v3.oas.annotations.tags.Tag) APPLICATION_JSON(jakarta.ws.rs.core.MediaType.APPLICATION_JSON) TxHash(com.quorum.tessera.enclave.TxHash) TxHash(com.quorum.tessera.enclave.TxHash) PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Hidden(io.swagger.v3.oas.annotations.Hidden)

Example 18 with ReceiveResponse

use of com.quorum.tessera.api.ReceiveResponse in project tessera by ConsenSys.

the class EncodedPayloadResource method receive21.

// path /encodedpayload/decrypt is overloaded (application/json and
// application/vnd.tessera-2.1+json); swagger annotations cannot handle situations like this so
// this operation documents both
@POST
@Path("decrypt")
@Operation(summary = "/encodedpayload/decrypt", operationId = "decrypt", description = "decrypt an encrypted payload and return the result; does not store to the database or push to peers", requestBody = @RequestBody(content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PayloadDecryptRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = PayloadDecryptRequest.class)) }))
@ApiResponse(responseCode = "200", description = "decrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ReceiveResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = ReceiveResponse.class)) })
@Consumes(MIME_TYPE_JSON_2_1)
@Produces(MIME_TYPE_JSON_2_1)
public Response receive21(@Valid @NotNull final PayloadDecryptRequest request) {
    LOGGER.info("Received request to decrypt custom transaction");
    final Base64.Decoder decoder = Base64.getDecoder();
    final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
    final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
    final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
    final ReceiveResponse receiveResponse = new ReceiveResponse();
    receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
    receiveResponse.setPayload(response.getUnencryptedTransactionData());
    receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
    Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
    return Response.ok(receiveResponse).type(MIME_TYPE_JSON_2_1).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) RecipientBox(com.quorum.tessera.enclave.RecipientBox) Path(jakarta.ws.rs.Path) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) MIME_TYPE_JSON_2_1(com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1) Produces(jakarta.ws.rs.Produces) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Consumes(jakarta.ws.rs.Consumes) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Hidden(io.swagger.v3.oas.annotations.Hidden) POST(jakarta.ws.rs.POST) TransactionManager(com.quorum.tessera.transaction.TransactionManager) Collectors(java.util.stream.Collectors) PayloadDecryptRequest(com.quorum.tessera.api.PayloadDecryptRequest) SendRequest(com.quorum.tessera.api.SendRequest) EncodedPayloadManager(com.quorum.tessera.transaction.EncodedPayloadManager) Stream(java.util.stream.Stream) Tag(io.swagger.v3.oas.annotations.tags.Tag) APPLICATION_JSON(jakarta.ws.rs.core.MediaType.APPLICATION_JSON) TxHash(com.quorum.tessera.enclave.TxHash) TxHash(com.quorum.tessera.enclave.TxHash) PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 19 with ReceiveResponse

use of com.quorum.tessera.api.ReceiveResponse in project tessera by ConsenSys.

the class ReceiveIT method fetchExistingTransactionUsingRecipientKey.

@Test
public void fetchExistingTransactionUsingRecipientKey() {
    final Response response = partyTwo.getRestClient().target(partyTwo.getQ2TUri()).path(RECEIVE_PATH).queryParam("to", this.encodedRecipient).path(encodedHash).request().accept(MIME_TYPE_JSON_2_1).get();
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    final ReceiveResponse result = response.readEntity(ReceiveResponse.class);
    assertThat(result.getPayload()).isEqualTo(transactionData);
    assertThat(result.getManagedParties()).containsExactly(partyTwo.getPublicKey());
    assertThat(result.getSenderKey()).isEqualTo(partyOne.getPublicKey());
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Test(org.junit.Test)

Example 20 with ReceiveResponse

use of com.quorum.tessera.api.ReceiveResponse in project tessera by ConsenSys.

the class ReceiveIT method fetchExistingTransactionUsingOwnKey.

// @Test
public void fetchExistingTransactionUsingOwnKey() {
    final Response response = partyOne.getRestClient().target(partyOne.getQ2TUri()).path(RECEIVE_PATH).path(this.encodedHash).queryParam("to", this.encodedSender).request().accept(MIME_TYPE_JSON_2_1).buildGet().invoke();
    // validate result
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    final ReceiveResponse result = response.readEntity(ReceiveResponse.class);
    assertThat(result.getPayload()).isEqualTo(transactionData);
    assertThat(result.getManagedParties()).containsExactly(partyOne.getPublicKey());
    assertThat(result.getSenderKey()).isEqualTo(partyOne.getPublicKey());
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse)

Aggregations

ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)30 Response (jakarta.ws.rs.core.Response)29 Test (org.junit.Test)26 SendResponse (com.quorum.tessera.api.SendResponse)21 Party (com.quorum.tessera.test.Party)19 SendRequest (com.quorum.tessera.api.SendRequest)16 URI (java.net.URI)15 MIME_TYPE_JSON_2_1 (com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1)4 Stream (java.util.stream.Stream)4 PayloadEncryptResponse (com.quorum.tessera.api.PayloadEncryptResponse)3 MessageHash (com.quorum.tessera.data.MessageHash)3 PublicKey (com.quorum.tessera.encryption.PublicKey)3 PartyHelper (com.quorum.tessera.test.PartyHelper)3 Operation (io.swagger.v3.oas.annotations.Operation)3 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)3 Json (jakarta.json.Json)3 Entity (jakarta.ws.rs.client.Entity)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 PayloadDecryptRequest (com.quorum.tessera.api.PayloadDecryptRequest)2 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)2