Search in sources :

Example 26 with SendRequest

use of com.quorum.tessera.api.SendRequest 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);
}
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) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) SendResponse(com.quorum.tessera.api.SendResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 27 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class EncodedPayloadResourceTest method createPayloadNullPayloadVersion21.

@Test
public void createPayloadNullPayloadVersion21() {
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final SendRequest sampleBadRequest = new SendRequest();
    sampleBadRequest.setFrom(base64Key);
    try {
        encodedPayloadResource.createEncodedPayload21(sampleBadRequest);
        failBecauseExceptionWasNotThrown(NullPointerException.class);
    } catch (NullPointerException nullPointerException) {
        assertThat(nullPointerException).hasMessage("Payload is required");
    }
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) Test(org.junit.Test)

Example 28 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class TransactionResource3Test method sendForRecipient.

@Test
public void sendForRecipient() {
    final Base64.Encoder base64Encoder = Base64.getEncoder();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
    sendRequest.setTo(Base64.getEncoder().encodeToString("Mr Benn".getBytes()));
    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));
    verify(transactionManager).send(any(com.quorum.tessera.transaction.SendRequest.class));
    verify(transactionManager).defaultPublicKey();
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) SendRequest(com.quorum.tessera.api.SendRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 29 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class TransactionResource3Test method send.

@Test
public void send() {
    final Base64.Encoder base64Encoder = Base64.getEncoder();
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
    sendRequest.setTo(base64Key);
    final PublicKey sender = PublicKey.from(Base64.getDecoder().decode("QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc="));
    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(sendResponse.getManagedParties()).thenReturn(Set.of(sender));
    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()).endsWith("transaction/" + base64Encoder.encodeToString(txnData));
    SendResponse resultSendResponse = (SendResponse) result.getEntity();
    assertThat(resultSendResponse.getKey()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
    assertThat(resultSendResponse.getManagedParties()).containsExactlyInAnyOrder(sender.encodeToBase64());
    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.STANDARD_PRIVATE);
    assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
    assertThat(businessObject.getExecHash()).isEmpty();
    assertThat(businessObject.getPrivacyGroupId()).isEmpty();
}
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) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 30 with SendRequest

use of com.quorum.tessera.api.SendRequest 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();
}
Also used : SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) 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) TEXT_PLAIN(jakarta.ws.rs.core.MediaType.TEXT_PLAIN) 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) MIME_TYPE_JSON_4(com.quorum.tessera.version.MandatoryRecipientsVersion.MIME_TYPE_JSON_4) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) URI(java.net.URI) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) 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) Parameter(io.swagger.v3.oas.annotations.Parameter) URLEncoder(java.net.URLEncoder) SendRequest(com.quorum.tessera.api.SendRequest) Stream(java.util.stream.Stream) 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)

Aggregations

SendRequest (com.quorum.tessera.api.SendRequest)64 Response (jakarta.ws.rs.core.Response)60 SendResponse (com.quorum.tessera.api.SendResponse)50 Party (com.quorum.tessera.test.Party)43 Test (org.junit.Test)43 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)27 MessageHash (com.quorum.tessera.data.MessageHash)13 PublicKey (com.quorum.tessera.encryption.PublicKey)13 URI (java.net.URI)13 RestUtils (com.quorum.tessera.test.rest.RestUtils)11 PayloadEncryptResponse (com.quorum.tessera.api.PayloadEncryptResponse)8 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)8 Stream (java.util.stream.Stream)6 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)5 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)5 MIME_TYPE_JSON_2_1 (com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1)5 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4 Operation (io.swagger.v3.oas.annotations.Operation)4 PartyHelper (com.quorum.tessera.test.PartyHelper)3 Content (io.swagger.v3.oas.annotations.media.Content)3