use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3Test method sendSignedTransactionToPrivacyGroup.
@Test
public void sendSignedTransactionToPrivacyGroup() {
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.getManagedParties()).thenReturn(Set.of(sender));
when(sendResponse.getSender()).thenReturn(sender);
when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
PrivacyGroup.Id groupId = PrivacyGroup.Id.fromBytes("groupId".getBytes());
SendSignedRequest sendSignedRequest = new SendSignedRequest();
sendSignedRequest.setHash("SOMEDATA".getBytes());
sendSignedRequest.setPrivacyGroupId(groupId.getBase64());
final PrivacyGroup pg = mock(PrivacyGroup.class);
when(pg.getMembers()).thenReturn(List.of(PublicKey.from("r1".getBytes()), PublicKey.from("r2".getBytes())));
when(pg.getId()).thenReturn(PrivacyGroup.Id.fromBytes("groupId".getBytes()));
when(privacyGroupManager.retrievePrivacyGroup(groupId)).thenReturn(pg);
Response result = transactionResource.sendSignedTransaction(sendSignedRequest);
// jersey.target("sendsignedtx")
// .request()
// .post(Entity.entity(sendSignedRequest,
// "application/vnd.tessera-3.0+json"));
assertThat(result.getStatus()).isEqualTo(201);
SendResponse resultResponse = (SendResponse) result.getEntity();
assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
verify(privacyGroupManager).retrievePrivacyGroup(groupId);
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.STANDARD_PRIVATE);
assertThat(obj.getAffectedContractTransactions()).isEmpty();
assertThat(obj.getExecHash()).isEmpty();
assertThat(obj.getPrivacyGroupId()).isPresent().get().isEqualTo(groupId);
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3Test 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 = transactionResource.send(sendRequest);
assertThat(result.getStatus()).isEqualTo(201);
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);
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource4Test method sendWithMandatoryRecipients.
@Test
public void sendWithMandatoryRecipients() {
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(2);
sendRequest.setMandatoryRecipients(new String[] { base64Key });
sendRequest.setAffectedContractTransactions(base64Hash);
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);
assertThat(result.getStatus()).isEqualTo(201);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = (SendResponse) 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.MANDATORY_RECIPIENTS);
assertThat(businessObject.getAffectedContractTransactions()).hasSize(1);
final MessageHash hash = businessObject.getAffectedContractTransactions().iterator().next();
assertThat(Base64.getEncoder().encodeToString(hash.getHashBytes())).isEqualTo(base64Hash);
assertThat(businessObject.getMandatoryRecipients().iterator().next().encodeToBase64()).isEqualTo(base64Key);
}
use of com.quorum.tessera.api.SendResponse in project tessera by ConsenSys.
the class TransactionResource3 method send.
// path /send is overloaded (application/json and application/vnd.tessera-2.1+json); swagger
// annotations cannot
// handle situations like this so this operation documents both
@Operation(summary = "/send", operationId = "encryptStoreAndSendJson", description = "encrypts a payload, stores result in database, and publishes result to recipients", 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 = "201", description = "encrypted payload hash", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendResponse.class)) })
@ApiResponse(responseCode = "200", description = "hash returned when running in orion mode", content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendResponse.class)))
@POST
@Path("send")
@Consumes({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
@Produces({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
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 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);
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.api.SendResponse in project tessera by ConsenSys.
the class PrivacyIT method sendContractCreationTransaction.
private String sendContractCreationTransaction(PrivacyMode privacyMode) {
Party sender = partyHelper.findByAlias(NodeAlias.A);
SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(new RestUtils().createTransactionData());
sendRequest.setFrom(sender.getPublicKey());
List<String> recipientList = List.of(partyHelper.findByAlias(NodeAlias.B).getPublicKey());
sendRequest.setTo(recipientList.toArray(new String[recipientList.size()]));
sendRequest.setPrivacyFlag(privacyMode.getPrivacyFlag());
sendRequest.setAffectedContractTransactions(new String[0]);
if (privacyMode == PrivacyMode.PRIVATE_STATE_VALIDATION) {
sendRequest.setExecHash("execHash");
}
Response response = sender.getRestClientWebTarget().path("send").request().post(Entity.entity(sendRequest, MIME_TYPE_JSON_2_1));
assertThat(response.getStatus()).isEqualTo(201);
final SendResponse result = response.readEntity(SendResponse.class);
return result.getKey();
}
Aggregations