Search in sources :

Example 21 with SendRequest

use of com.quorum.tessera.api.SendRequest 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 22 with SendRequest

use of com.quorum.tessera.api.SendRequest 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 23 with SendRequest

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

the class ReceiveRawIT method fetchExistingTransactionNotUsingKeyOnRecipient.

@Test
public void fetchExistingTransactionNotUsingKeyOnRecipient() {
    Party sender = partyHelper.findByAlias("A");
    byte[] transactionPayload = new RestUtils().createTransactionData();
    SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(transactionPayload);
    sendRequest.setFrom(sender.getPublicKey());
    sendRequest.setTo(partyHelper.findByAlias("B").getPublicKey());
    final Response r = sender.getRestClient().target(sender.getQ2TUri()).path("/send").request().post(Entity.entity(sendRequest, MediaType.APPLICATION_JSON));
    final SendResponse sendResponse = r.readEntity(SendResponse.class);
    final Party pty = partyHelper.findByAlias(NodeAlias.B);
    final Response response = pty.getRestClient().target(pty.getQ2TUri()).path(RECEIVE_PATH).request().header(C11N_KEY, sendResponse.getKey()).buildGet().invoke();
    // validate result
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    final byte[] result = response.readEntity(byte[].class);
    assertThat(result).isEqualTo(transactionPayload);
}
Also used : Response(jakarta.ws.rs.core.Response) SendResponse(com.quorum.tessera.api.SendResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) Test(org.junit.Test)

Example 24 with SendRequest

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

the class EncodedPayloadResource method createEncodedPayload.

// hide this operation from swagger generation; the /encodedpayload/create operation is overloaded
// and must be documented in a single place
@Hidden
@POST
@Path("create")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response createEncodedPayload(@NotNull @Valid final SendRequest sendRequest) {
    LOGGER.info("Received request for custom payload encryption");
    final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
    final List<PublicKey> recipientList = Stream.of(sendRequest).filter(sr -> Objects.nonNull(sr.getTo())).flatMap(s -> Stream.of(s.getTo())).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(Base64.getDecoder()::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final com.quorum.tessera.transaction.SendRequest request = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(PrivacyMode.fromFlag(sendRequest.getPrivacyFlag())).withAffectedContractTransactions(affectedTransactions).build();
    final EncodedPayload encodedPayload = encodedPayloadManager.create(request);
    final Map<String, String> affectedContractTransactionMap = encodedPayload.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().encodeToBase64(), e -> Base64.getEncoder().encodeToString(e.getValue().getData())));
    final PayloadEncryptResponse response = new PayloadEncryptResponse();
    response.setSenderKey(encodedPayload.getSenderKey().getKeyBytes());
    response.setCipherText(encodedPayload.getCipherText());
    response.setCipherTextNonce(encodedPayload.getCipherTextNonce().getNonceBytes());
    response.setRecipientBoxes(encodedPayload.getRecipientBoxes().stream().map(RecipientBox::getData).collect(Collectors.toList()));
    response.setRecipientNonce(encodedPayload.getRecipientNonce().getNonceBytes());
    response.setRecipientKeys(encodedPayload.getRecipientKeys().stream().map(PublicKey::getKeyBytes).collect(Collectors.toList()));
    response.setPrivacyMode(encodedPayload.getPrivacyMode().getPrivacyFlag());
    response.setAffectedContractTransactions(affectedContractTransactionMap);
    response.setExecHash(encodedPayload.getExecHash());
    return Response.ok(response).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) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) RecipientBox(com.quorum.tessera.enclave.RecipientBox) 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 25 with SendRequest

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

the class EncodedPayloadResource method createEncodedPayload21.

// path /encodedpayload/create 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("create")
@Operation(summary = "/encodedpayload/create", operationId = "encrypt", description = "encrypt a 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 = SendRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendRequest.class)) }))
@ApiResponse(responseCode = "200", description = "encrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PayloadEncryptResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = PayloadEncryptResponse.class)) })
@Consumes(MIME_TYPE_JSON_2_1)
@Produces(MIME_TYPE_JSON_2_1)
public Response createEncodedPayload21(@NotNull @Valid final SendRequest sendRequest) {
    LOGGER.info("Received request for custom payload encryption");
    final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
    final List<PublicKey> recipientList = Stream.of(sendRequest).filter(sr -> Objects.nonNull(sr.getTo())).flatMap(s -> Stream.of(s.getTo())).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(Base64.getDecoder()::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final com.quorum.tessera.transaction.SendRequest request = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(PrivacyMode.fromFlag(sendRequest.getPrivacyFlag())).withAffectedContractTransactions(affectedTransactions).build();
    final EncodedPayload encodedPayload = encodedPayloadManager.create(request);
    final Map<String, String> affectedContractTransactionMap = encodedPayload.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().encodeToBase64(), e -> Base64.getEncoder().encodeToString(e.getValue().getData())));
    final PayloadEncryptResponse response = new PayloadEncryptResponse();
    response.setSenderKey(encodedPayload.getSenderKey().getKeyBytes());
    response.setCipherText(encodedPayload.getCipherText());
    response.setCipherTextNonce(encodedPayload.getCipherTextNonce().getNonceBytes());
    response.setRecipientBoxes(encodedPayload.getRecipientBoxes().stream().map(RecipientBox::getData).collect(Collectors.toList()));
    response.setRecipientNonce(encodedPayload.getRecipientNonce().getNonceBytes());
    response.setRecipientKeys(encodedPayload.getRecipientKeys().stream().map(PublicKey::getKeyBytes).collect(Collectors.toList()));
    response.setPrivacyMode(encodedPayload.getPrivacyMode().getPrivacyFlag());
    response.setAffectedContractTransactions(affectedContractTransactionMap);
    response.setExecHash(encodedPayload.getExecHash());
    return Response.ok(response).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) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) RecipientBox(com.quorum.tessera.enclave.RecipientBox) 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)

Aggregations

SendRequest (com.quorum.tessera.api.SendRequest)64 Response (jakarta.ws.rs.core.Response)60 SendResponse (com.quorum.tessera.api.SendResponse)50 Party (com.quorum.tessera.test.Party)43 Test (org.junit.Test)43 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)27 MessageHash (com.quorum.tessera.data.MessageHash)13 PublicKey (com.quorum.tessera.encryption.PublicKey)13 URI (java.net.URI)13 RestUtils (com.quorum.tessera.test.rest.RestUtils)11 PayloadEncryptResponse (com.quorum.tessera.api.PayloadEncryptResponse)8 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)8 Stream (java.util.stream.Stream)6 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)5 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)5 MIME_TYPE_JSON_2_1 (com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1)5 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4 Operation (io.swagger.v3.oas.annotations.Operation)4 PartyHelper (com.quorum.tessera.test.PartyHelper)3 Content (io.swagger.v3.oas.annotations.media.Content)3