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);
}
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);
}
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);
}
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();
}
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();
}
Aggregations