Search in sources :

Example 11 with ReceiveResponse

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

the class SendRawIT method sendTransactionWithoutASender.

@Test
public void sendTransactionWithoutASender() {
    URI uriToSendToWithoutPublicKey = partyHelper.findByAlias("A").getQ2TUri();
    Party recipient = partyHelper.findByAlias("D");
    byte[] transactionData = restUtils.createTransactionData();
    final Response response = client.target(uriToSendToWithoutPublicKey).path(SEND_PATH).request().header(RECIPIENTS, recipient.getPublicKey()).post(Entity.entity(transactionData, MediaType.APPLICATION_OCTET_STREAM));
    // validate result
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    String persistedKey = response.readEntity(String.class);
    assertThat(persistedKey).isNotNull();
    URI location = response.getLocation();
    final Response checkPersistedTxnResponse = client.target(location).request().get();
    assertThat(checkPersistedTxnResponse.getStatus()).isEqualTo(200);
    ReceiveResponse receiveResponse = checkPersistedTxnResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResponse.getPayload()).isEqualTo(transactionData);
    restUtils.findTransaction(persistedKey, partyHelper.findByAlias("A"), recipient).forEach(r -> {
        assertThat(r.getStatus()).isEqualTo(200);
    });
    restUtils.findTransaction(persistedKey, partyHelper.findByAlias("C"), partyHelper.findByAlias("B")).forEach(r -> {
        assertThat(r.getStatus()).isEqualTo(404);
    });
}
Also used : Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) URI(java.net.URI) Test(org.junit.Test)

Example 12 with ReceiveResponse

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

the class SendRawIT method sendTransactionWithMissingRecipients.

/**
 * In the case where no recipients are defined its is assumed that
 */
@Test
public void sendTransactionWithMissingRecipients() {
    Party sender = partyHelper.findByAlias("A");
    byte[] txnData = restUtils.createTransactionData();
    final Response response = restUtils.sendRaw(sender, txnData);
    // validate result
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    String persistedKey = response.readEntity(String.class);
    assertThat(persistedKey).isNotNull();
    URI location = response.getLocation();
    final Response checkPersistedTxnResponse = client.target(location).request().get();
    assertThat(checkPersistedTxnResponse.getStatus()).isEqualTo(200);
    ReceiveResponse receiveResponse = checkPersistedTxnResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResponse.getPayload()).isEqualTo(txnData);
}
Also used : Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) URI(java.net.URI) Test(org.junit.Test)

Example 13 with ReceiveResponse

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

the class SendReceiveBesuIT method sendAndReceivePrivacyGroup.

@Test
public void sendAndReceivePrivacyGroup() {
    final Party a = partyHelper.findByAlias("A");
    final Party b = partyHelper.findByAlias("B");
    final String output = privacyGroupTestUtil.create("A", "B");
    final JsonObject jsonObj = Json.createReader(new StringReader(output)).readObject();
    final String groupId = jsonObj.getString("privacyGroupId");
    byte[] transactionData = utils.createTransactionData();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPrivacyGroupId(groupId);
    sendRequest.setPayload(transactionData);
    final Response response = client.target(partyHelper.findByAlias("A").getQ2TUri()).path("/send").request().post(Entity.entity(sendRequest, MediaType.APPLICATION_JSON));
    final SendResponse result = response.readEntity(SendResponse.class);
    final String hash = result.getKey();
    assertThat(hash).isNotNull().isNotBlank();
    // Hash length = 32 bytes
    assertThat(Base64.getDecoder().decode(hash)).hasSize(32);
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(hash);
    final Response receiveResponse = client.target(a.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResult = receiveResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResult.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResult.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResult.getPrivacyGroupId()).isEqualTo(groupId);
    final Response receiveResponseOnB = client.target(b.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponseOnB).isNotNull();
    assertThat(receiveResponseOnB.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResultOnB = receiveResponseOnB.readEntity(ReceiveResponse.class);
    assertThat(receiveResultOnB.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResultOnB.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResultOnB.getPrivacyGroupId()).isEqualTo(groupId);
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Example 14 with ReceiveResponse

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

the class SendReceivePrivacyGroupIT method sendTransactionToPrivacyGroup.

@Test
public void sendTransactionToPrivacyGroup() throws UnsupportedEncodingException {
    final Party a = partyHelper.findByAlias(NodeAlias.A);
    final Party b = partyHelper.findByAlias(NodeAlias.B);
    final String output = privacyGroupTestUtil.create("A", "B");
    final JsonObject jsonObj = Json.createReader(new StringReader(output)).readObject();
    final String groupId = jsonObj.getString("privacyGroupId");
    byte[] transactionData = utils.createTransactionData();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPrivacyGroupId(groupId);
    sendRequest.setPayload(transactionData);
    final Response response = a.getRestClient().target(a.getQ2TUri()).path("/send").request().post(Entity.entity(sendRequest, MediaType.APPLICATION_JSON));
    final SendResponse result = response.readEntity(SendResponse.class);
    final String hash = result.getKey();
    // Hash length should be 64 bytes
    assertThat(Base64.getDecoder().decode(hash)).hasSize(64);
    final String encodedHash = URLEncoder.encode(hash, UTF_8.toString());
    assertThat(hash).isNotNull().isNotBlank();
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(201);
    final Response receiveResponse = a.getRestClient().target(a.getQ2TUri()).path("/transaction").path(encodedHash).request().accept(MIME_TYPE_JSON_2_1).buildGet().invoke();
    // validate result
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResult = receiveResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResult.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResult.getManagedParties()).containsExactly(a.getPublicKey());
    assertThat(receiveResult.getSenderKey()).isEqualTo(a.getPublicKey());
    // assertThat(receiveResult.getPrivacyGroupId()).isEqualTo(groupId);
    final Response receiveResponseOnB = b.getRestClient().target(b.getQ2TUri()).path("/transaction").path(encodedHash).request().accept(MIME_TYPE_JSON_2_1).buildGet().invoke();
    // validate result
    assertThat(receiveResponseOnB).isNotNull();
    assertThat(receiveResponseOnB.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResultOnB = receiveResponseOnB.readEntity(ReceiveResponse.class);
    assertThat(receiveResultOnB.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResultOnB.getManagedParties()).containsExactly(b.getPublicKey());
    assertThat(receiveResultOnB.getSenderKey()).isEqualTo(a.getPublicKey());
// assertThat(receiveResultOnB.getPrivacyGroupId()).isEqualTo(groupId);
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject) Test(org.junit.Test)

Example 15 with ReceiveResponse

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

the class TransactionResource3 method receive.

// path /transaction/{hash} is overloaded (application/json and application/vnd.tessera-2.1+json);
// swagger
// annotations cannot handle situations like this so this operation documents both
@Operation(summary = "/transaction/{hash}", operationId = "getDecryptedPayloadJsonUrl", description = "get payload from database, decrypt, and return")
@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)) })
@GET
@Path("/transaction/{hash}")
@Produces({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
public Response receive(@Parameter(description = "hash indicating encrypted payload to retrieve from database", schema = @Schema(format = "base64")) @Valid @ValidBase64 @PathParam("hash") final String hash, @Parameter(description = "(optional) public key of recipient of the encrypted payload; used in decryption; if not provided, decryption is attempted with all known recipient keys in turn", schema = @Schema(format = "base64")) @QueryParam("to") final String toStr, @Parameter(description = "(optional) indicates whether the payload is raw; determines which database the payload is retrieved from; possible values\n* true - for pre-stored payloads in the \"raw\" database\n* false (default) - for already sent payloads in \"standard\" database") @Valid @Pattern(flags = Pattern.Flag.CASE_INSENSITIVE, regexp = "^(true|false)$") @QueryParam("isRaw") final String isRaw) {
    final PublicKey recipient = Optional.ofNullable(toStr).filter(Predicate.not(String::isEmpty)).map(base64Decoder::decode).map(PublicKey::from).orElse(null);
    final MessageHash transactionHash = Optional.of(hash).map(base64Decoder::decode).map(MessageHash::new).get();
    final com.quorum.tessera.transaction.ReceiveRequest request = com.quorum.tessera.transaction.ReceiveRequest.Builder.create().withRecipient(recipient).withTransactionHash(transactionHash).withRaw(Boolean.parseBoolean(isRaw)).build();
    com.quorum.tessera.transaction.ReceiveResponse response = transactionManager.receive(request);
    final ReceiveResponse receiveResponse = new ReceiveResponse();
    receiveResponse.setPayload(response.getUnencryptedTransactionData());
    receiveResponse.setSenderKey(response.sender().encodeToBase64());
    receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).toArray(String[]::new));
    Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
    receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
    receiveResponse.setManagedParties(Optional.ofNullable(response.getManagedParties()).orElse(Collections.emptySet()).stream().map(PublicKey::encodeToBase64).toArray(String[]::new));
    response.getPrivacyGroupId().map(PrivacyGroup.Id::getBase64).ifPresent(receiveResponse::setPrivacyGroupId);
    return Response.ok(receiveResponse).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

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