use of com.quorum.tessera.api.SendSignedRequest 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.SendSignedRequest in project tessera by ConsenSys.
the class TransactionResource3Test method sendSignedTransactionEmptyRecipients.
@Test
public void sendSignedTransactionEmptyRecipients() {
final PublicKey sender = PublicKey.from("sender".getBytes());
com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
byte[] transactionHashData = "I Love Sparrows".getBytes();
final String base64EncodedTransactionHAshData = Base64.getEncoder().encodeToString(transactionHashData);
MessageHash transactionHash = mock(MessageHash.class);
when(transactionHash.getHashBytes()).thenReturn(transactionHashData);
when(sendResponse.getTransactionHash()).thenReturn(transactionHash);
when(sendResponse.getSender()).thenReturn(sender);
when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
// jersey.target("sendsignedtx")
// .request()
// .header("Content-Type", "application/vnd.tessera-2.1+json")
// .header("Accept", "application/vnd.tessera-2.1+json")
// .post(Entity.entity(sendSignedRequest,
// "application/vnd.tessera-2.1+json"));
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(resultResponse.getSenderKey()).isEqualTo(sender.encodeToBase64());
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(transactionManager).sendSignedTransaction(argumentCaptor.capture());
com.quorum.tessera.transaction.SendSignedRequest obj = argumentCaptor.getValue();
assertThat(obj).isNotNull();
assertThat(obj.getSignedData()).isEqualTo("SOMEDATA".getBytes());
assertThat(obj.getRecipients()).hasSize(0);
assertThat(obj.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
assertThat(obj.getAffectedContractTransactions()).isEmpty();
assertThat(obj.getExecHash()).isEmpty();
}
use of com.quorum.tessera.api.SendSignedRequest in project tessera by ConsenSys.
the class TransactionResource4Test method sendSignedTransactionWithMandatoryRecipients.
@Test
public void sendSignedTransactionWithMandatoryRecipients() {
final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="));
com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
byte[] transactionHashData = "I Love Sparrows".getBytes();
final String base64EncodedTransactionHAshData = Base64.getEncoder().encodeToString(transactionHashData);
MessageHash transactionHash = mock(MessageHash.class);
when(transactionHash.getHashBytes()).thenReturn(transactionHashData);
when(sendResponse.getTransactionHash()).thenReturn(transactionHash);
when(sendResponse.getSender()).thenReturn(sender);
when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
final String base64AffectedHash1 = Base64.getEncoder().encodeToString("aHash1".getBytes());
final String base64AffectedHash2 = Base64.getEncoder().encodeToString("aHash2".getBytes());
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
sendSignedRequest.setTo("recipient1", "recipient2");
sendSignedRequest.setPrivacyFlag(2);
sendSignedRequest.setAffectedContractTransactions(base64AffectedHash1, base64AffectedHash2);
sendSignedRequest.setMandatoryRecipients("recipient2");
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(resultResponse.getSenderKey()).isEqualTo(sender.encodeToBase64());
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(transactionManager).sendSignedTransaction(argumentCaptor.capture());
com.quorum.tessera.transaction.SendSignedRequest obj = argumentCaptor.getValue();
assertThat(obj).isNotNull();
assertThat(obj.getSignedData()).isEqualTo("SOMEDATA".getBytes());
assertThat(obj.getRecipients()).hasSize(2);
assertThat(obj.getPrivacyMode()).isEqualTo(PrivacyMode.MANDATORY_RECIPIENTS);
assertThat(obj.getAffectedContractTransactions().stream().map(MessageHash::toString)).hasSize(2).containsExactlyInAnyOrder(base64AffectedHash1, base64AffectedHash2);
assertThat(obj.getMandatoryRecipients()).hasSize(1);
}
use of com.quorum.tessera.api.SendSignedRequest in project tessera by ConsenSys.
the class TransactionResource4 method sendSignedTransaction.
@POST
@Path("sendsignedtx")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
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 Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendSignedRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
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).withMandatoryRecipients(mandatoryRecipients);
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.SendSignedRequest in project tessera by ConsenSys.
the class RequestPrivacyValidatorTest method testPrivacyValidationOnSendSignedRequest.
@Test
public void testPrivacyValidationOnSendSignedRequest() {
SendSignedRequest request = new SendSignedRequest();
request.setPrivacyFlag(-1000);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(0);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(1);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(2);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(3);
assertThat(validator.isValid(request, context)).isFalse();
verify(context).buildConstraintViolationWithTemplate("Exec hash missing");
verify(builder).addConstraintViolation();
request.setExecHash("execHash");
assertThat(validator.isValid(request, context)).isTrue();
}
Aggregations