Search in sources :

Example 26 with SendResponse

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);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) Test(org.junit.Test)

Example 27 with SendResponse

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);
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 28 with SendResponse

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);
}
Also used : Base64(java.util.Base64) SendRequest(com.quorum.tessera.api.SendRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 29 with SendResponse

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();
}
Also used : SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) MIME_TYPE_JSON_3(com.quorum.tessera.version.PrivacyGroupVersion.MIME_TYPE_JSON_3) PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) SendResponse(com.quorum.tessera.api.SendResponse) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) PrivacyValid(com.quorum.tessera.api.constraint.PrivacyValid) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) MIME_TYPE_JSON_2_1(com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1) URI(java.net.URI) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Pattern(jakarta.validation.constraints.Pattern) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Predicate(java.util.function.Predicate) TransactionManager(com.quorum.tessera.transaction.TransactionManager) PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ValidBase64(com.quorum.tessera.config.constraints.ValidBase64) Parameter(io.swagger.v3.oas.annotations.Parameter) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) URLEncoder(java.net.URLEncoder) SendRequest(com.quorum.tessera.api.SendRequest) Stream(java.util.stream.Stream) MediaType(jakarta.ws.rs.core.MediaType) UriBuilder(jakarta.ws.rs.core.UriBuilder) Tag(io.swagger.v3.oas.annotations.tags.Tag) SendRequest(com.quorum.tessera.api.SendRequest) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 30 with SendResponse

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();
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) RestUtils(com.quorum.tessera.test.rest.RestUtils)

Aggregations

SendResponse (com.quorum.tessera.api.SendResponse)41 Response (jakarta.ws.rs.core.Response)38 SendRequest (com.quorum.tessera.api.SendRequest)32 Test (org.junit.Test)29 Party (com.quorum.tessera.test.Party)20 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)19 MessageHash (com.quorum.tessera.data.MessageHash)16 PublicKey (com.quorum.tessera.encryption.PublicKey)16 URI (java.net.URI)15 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)10 SendSignedRequest (com.quorum.tessera.api.SendSignedRequest)9 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)6 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)4 PartyHelper (com.quorum.tessera.test.PartyHelper)4 URLEncoder (java.net.URLEncoder)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Stream (java.util.stream.Stream)4 RestUtils (com.quorum.tessera.test.rest.RestUtils)3 MIME_TYPE_JSON_2_1 (com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1)3 Operation (io.swagger.v3.oas.annotations.Operation)3