use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionResource4 method send.
@POST
@Path("send")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
public Response send(@NotNull @Valid @PrivacyValid final SendRequest sendRequest) {
final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
final List<PublicKey> recipientList = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(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(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendRequest.getPrivacyFlag());
final Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
final com.quorum.tessera.transaction.SendRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withMandatoryRecipients(mandatoryRecipients);
privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
final com.quorum.tessera.transaction.SendResponse response = transactionManager.send(requestBuilder.build());
final String encodedKey = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
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 sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, managedParties, sender.encodeToBase64())).get();
final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
return Response.created(location).entity(sendResponse).build();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionResourceTest method sendWithPrivacy.
@Test
public void sendWithPrivacy() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final String base64Hash = "yKNxAAPdBMiEZFkyQifH1PShwHTHTdE92T3hAfSQ3RtGce9IB8jrsrXxGuCe+Vu3Wyv2zgSbUnt+QBN2Rf48qQ==";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setTo(base64Key);
sendRequest.setPrivacyFlag(3);
sendRequest.setAffectedContractTransactions(base64Hash);
sendRequest.setExecHash("executionHash");
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);
final Response result = transactionResource.send(sendRequest);
// jersey.target("send").request().post(Entity.entity(sendRequest,
// MediaType.APPLICATION_JSON));
assertThat(result.getStatus()).isEqualTo(201);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = SendResponse.class.cast(result.getEntity());
assertThat(resultSendResponse.getKey());
ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
verify(transactionManager).send(argumentCaptor.capture());
verify(transactionManager).defaultPublicKey();
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).encodeToBase64()).isEqualTo(base64Key);
assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
assertThat(businessObject.getAffectedContractTransactions()).hasSize(1);
final MessageHash hash = businessObject.getAffectedContractTransactions().iterator().next();
assertThat(Base64.getEncoder().encodeToString(hash.getHashBytes())).isEqualTo(base64Hash);
assertThat(businessObject.getExecHash()).isEqualTo("executionHash".getBytes());
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class AsyncBatchPayloadPublisherTest method publishPayloadUsesThreadForEachRecipient.
@Test
public void publishPayloadUsesThreadForEachRecipient() throws InterruptedException {
final EncodedPayload payload = mock(EncodedPayload.class);
final PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
final PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
final List<PublicKey> recipients = List.of(recipient, otherRecipient);
asyncPublisher.publishPayload(payload, recipients);
verify(countDownLatchFactory).create(2);
verify(executorFactory).createCachedThreadPool();
verify(executor, times(2)).execute(any(Runnable.class));
verify(countDownLatch).await();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class AsyncBatchPayloadPublisherTest method publishPayloadStripsAndPublishes.
@Test
public void publishPayloadStripsAndPublishes() throws InterruptedException {
final Executor realExecutor = Executors.newSingleThreadExecutor();
when(executorFactory.createCachedThreadPool()).thenReturn(realExecutor);
asyncPublisher = new AsyncBatchPayloadPublisher(executorFactory, countDownLatchFactory, publisher);
final PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
final PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
final List<PublicKey> recipients = List.of(recipient, otherRecipient);
final EncodedPayload payload = EncodedPayload.Builder.create().withSenderKey(mock(PublicKey.class)).withRecipientKeys(recipients).withRecipientBoxes(List.of("box1".getBytes(), "box2".getBytes())).build();
doAnswer(invocation -> {
// sleep main thread so publish threads can work
Thread.sleep(200);
return null;
}).when(countDownLatch).await();
asyncPublisher.publishPayload(payload, recipients);
verify(executorFactory, times(2)).createCachedThreadPool();
verify(countDownLatchFactory).create(2);
verify(publisher).publishPayload(any(), eq(recipient));
verify(publisher).publishPayload(any(), eq(otherRecipient));
verify(countDownLatch, times(2)).countDown();
verify(countDownLatch).await();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class AsyncBatchPayloadPublisherTest method publishPayloadCancelsCountDownLatchIfOneTaskFails.
@Test
public void publishPayloadCancelsCountDownLatchIfOneTaskFails() throws InterruptedException {
final Executor realExecutor = Executors.newCachedThreadPool();
when(executorFactory.createCachedThreadPool()).thenReturn(realExecutor);
asyncPublisher = new AsyncBatchPayloadPublisher(executorFactory, countDownLatchFactory, publisher);
final PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
final PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
final List<PublicKey> recipients = List.of(recipient, otherRecipient);
final EncodedPayload payload = EncodedPayload.Builder.create().withSenderKey(mock(PublicKey.class)).withRecipientKeys(recipients).withRecipientBoxes(List.of("box1".getBytes(), "box2".getBytes())).build();
final PublishPayloadException cause = new PublishPayloadException("some exception");
doThrow(cause).doNothing().when(publisher).publishPayload(any(EncodedPayload.class), any(PublicKey.class));
doAnswer(invocation -> {
// sleep main thread so publish threads can work
Thread.sleep(200);
return null;
}).when(countDownLatch).await();
asyncPublisher.publishPayload(payload, recipients);
verify(executorFactory, times(2)).createCachedThreadPool();
verify(countDownLatchFactory).create(2);
verify(publisher).publishPayload(any(), eq(recipient));
verify(publisher).publishPayload(any(), eq(otherRecipient));
verify(countDownLatch).countDown();
verify(countDownLatch).cancelWithException(cause);
verify(countDownLatch).await();
}
Aggregations