use of com.quorum.tessera.api.SendResponse 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.SendResponse 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.SendResponse 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.SendResponse in project tessera by ConsenSys.
the class TransactionResource3 method sendSignedTransaction.
// path /sendsignedtx is overloaded (application/octet-stream, application/json and
// application/vnd.tessera-2.1+json); swagger annotations cannot handle situations like this so
// this operation
// documents both
@Operation(operationId = "sendStored", summary = "/sendsignedtx", description = "re-wraps a pre-stored & pre-encrypted payload, stores result in database, and publishes result to recipients", requestBody = @RequestBody(content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendSignedRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendSignedRequest.class)), @Content(mediaType = APPLICATION_OCTET_STREAM, array = @ArraySchema(schema = @Schema(description = "hash of pre-stored payload", type = "string", format = "base64"))) }))
@ApiResponse(responseCode = "200", description = "hash of rewrapped payload (for application/octet-stream requests)", content = @Content(mediaType = APPLICATION_OCTET_STREAM, schema = @Schema(description = "hash of rewrapped payload", type = "string", format = "base64")))
@ApiResponse(responseCode = "201", description = "hash of rewrapped payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendResponse.class, description = "hash of rewrapped payload")), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendResponse.class, description = "hash of rewrapped payload")) })
@POST
@Path("sendsignedtx")
@Consumes({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
@Produces({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
public Response sendSignedTransaction(@NotNull @Valid @PrivacyValid final SendSignedRequest sendSignedRequest) {
final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendSignedRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
final List<PublicKey> recipients = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Optional.ofNullable(sendSignedRequest.getTo()).stream().flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendSignedRequest.getPrivacyFlag());
final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendSignedRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
final byte[] execHash = Optional.ofNullable(sendSignedRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
final com.quorum.tessera.transaction.SendSignedRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withSignedData(sendSignedRequest.getHash()).withRecipients(recipients).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withExecHash(execHash);
privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(requestBuilder.build());
final String encodedTransactionHash = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
LOGGER.debug("Encoded key: {}", encodedTransactionHash);
final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedTransactionHash, StandardCharsets.UTF_8)).build();
final String[] managedParties = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getManagedParties).orElse(Collections.emptySet()).stream().map(PublicKey::encodeToBase64).toArray(String[]::new);
final SendResponse responseEntity = new SendResponse();
responseEntity.setKey(encodedTransactionHash);
responseEntity.setManagedParties(managedParties);
responseEntity.setSenderKey(response.getSender().encodeToBase64());
LOGGER.debug("Encoded key: {}", encodedTransactionHash);
return Response.created(location).entity(responseEntity).build();
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class BesuTransactionResourceTest method sendToPrivacyGroup.
@Test
public void sendToPrivacyGroup() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setPrivacyGroupId(base64Key);
final PublicKey sender = mock(PublicKey.class);
when(transactionManager.defaultPublicKey()).thenReturn(sender);
final com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
final MessageHash messageHash = mock(MessageHash.class);
final byte[] txnData = "TxnData".getBytes();
when(messageHash.getHashBytes()).thenReturn(txnData);
when(sendResponse.getTransactionHash()).thenReturn(messageHash);
when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
PrivacyGroup retrieved = mock(PrivacyGroup.class);
PrivacyGroup.Id groupId = PrivacyGroup.Id.fromBase64String(base64Key);
PublicKey member = PublicKey.from("member".getBytes());
when(retrieved.getId()).thenReturn(groupId);
when(retrieved.getMembers()).thenReturn(List.of(member));
when(privacyGroupManager.retrievePrivacyGroup(groupId)).thenReturn(retrieved);
final Response result = besuTransactionResource.send(sendRequest);
// jersey.target("send").request().post(Entity.entity(sendRequest,
// MediaType.APPLICATION_JSON));
assertThat(result.getStatus()).isEqualTo(200);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = (SendResponse) result.getEntity();
assertThat(resultSendResponse.getKey()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
verify(transactionManager).send(argumentCaptor.capture());
verify(transactionManager).defaultPublicKey();
verify(privacyGroupManager).retrievePrivacyGroup(groupId);
com.quorum.tessera.transaction.SendRequest businessObject = argumentCaptor.getValue();
assertThat(businessObject).isNotNull();
assertThat(businessObject.getPayload()).isEqualTo(sendRequest.getPayload());
assertThat(businessObject.getSender()).isEqualTo(sender);
assertThat(businessObject.getRecipients()).hasSize(1);
assertThat(businessObject.getRecipients().get(0)).isEqualTo(member);
assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
assertThat(businessObject.getExecHash()).isEmpty();
assertThat(businessObject.getPrivacyGroupId()).isPresent().get().isEqualTo(groupId);
}
Aggregations