Search in sources :

Example 6 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup 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)

Example 7 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testCreateResidentGroup.

@Test
public void testCreateResidentGroup() {
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
    when(privacyGroupDAO.retrieve("generatedId".getBytes())).thenReturn(Optional.empty());
    final PrivacyGroup privacyGroup = privacyGroupManager.saveResidentGroup("name", "desc", List.of(localKey));
    // Verify entity being saved has the correct values
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).update(argCaptor.capture());
    PrivacyGroupEntity savedEntity = argCaptor.getValue();
    assertThat(savedEntity).isNotNull();
    assertThat(savedEntity.getId()).isEqualTo("name".getBytes());
    assertThat(savedEntity.getLookupId()).isEqualTo("lookup".getBytes());
    assertThat(savedEntity.getData()).isEqualTo("encoded".getBytes());
    // Verify generated privacy group has the correct values
    assertThat(privacyGroup).isNotNull();
    assertThat(privacyGroup.getId().getBytes()).isEqualTo("name".getBytes());
    assertThat(privacyGroup.getName()).isEqualTo("name");
    assertThat(privacyGroup.getDescription()).isEqualTo("desc");
    assertThat(privacyGroup.getMembers()).containsExactly(localKey);
    assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.RESIDENT);
    assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 8 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testDeleteDeletedPrivacyGroup.

@Test
public void testDeleteDeletedPrivacyGroup() {
    PrivacyGroupEntity retrievedEt = mock(PrivacyGroupEntity.class);
    when(retrievedEt.getData()).thenReturn("data".getBytes());
    PrivacyGroup mockPG = mock(PrivacyGroup.class);
    when(mockPG.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(mockPG.getMembers()).thenReturn(Collections.emptyList());
    when(mockPG.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(mockPG.getType()).thenReturn(PrivacyGroup.Type.PANTHEON);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(retrievedEt));
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPG);
    when(privacyGroupUtil.encode(any())).thenReturn("deletedData".getBytes());
    assertThatThrownBy(() -> privacyGroupManager.deletePrivacyGroup(mock(PublicKey.class), PrivacyGroup.Id.fromBytes("id".getBytes()))).isInstanceOf(PrivacyGroupNotFoundException.class);
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 9 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testRetrievePrivacyGroupDeleted.

@Test
public void testRetrievePrivacyGroupDeleted() {
    final PrivacyGroup.Id id = PrivacyGroup.Id.fromBytes("id".getBytes());
    final PrivacyGroupEntity mockResult = mock(PrivacyGroupEntity.class);
    final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
    when(mockPrivacyGroup.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(mockResult.getData()).thenReturn("data".getBytes());
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPrivacyGroup);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(mockResult));
    try {
        privacyGroupManager.retrievePrivacyGroup(id);
        failBecauseExceptionWasNotThrown(any());
    } catch (Exception ex) {
        assertThat(ex).isInstanceOf(PrivacyGroupNotFoundException.class);
    }
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PrivacyGroupNotFoundException(com.quorum.tessera.privacygroup.exception.PrivacyGroupNotFoundException) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroupNotFoundException(com.quorum.tessera.privacygroup.exception.PrivacyGroupNotFoundException) Test(org.junit.Test)

Example 10 with PrivacyGroup

use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testFindPrivacyGroup.

@Test
public void testFindPrivacyGroup() {
    final PrivacyGroupEntity et1 = mock(PrivacyGroupEntity.class);
    when(et1.getData()).thenReturn("data1".getBytes());
    final PrivacyGroupEntity et2 = mock(PrivacyGroupEntity.class);
    when(et2.getData()).thenReturn("data2".getBytes());
    final PrivacyGroupEntity et3 = mock(PrivacyGroupEntity.class);
    when(et3.getData()).thenReturn("data3".getBytes());
    final List<PrivacyGroupEntity> dbResult = List.of(et1, et2, et3);
    final PrivacyGroup pg1 = mock(PrivacyGroup.class);
    final PrivacyGroup pg2 = mock(PrivacyGroup.class);
    final PrivacyGroup pg3 = mock(PrivacyGroup.class);
    when(pg1.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(pg2.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(pg3.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(privacyGroupDAO.findByLookupId("lookup".getBytes())).thenReturn(dbResult);
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.decode("data1".getBytes())).thenReturn(pg1);
    when(privacyGroupUtil.decode("data2".getBytes())).thenReturn(pg2);
    when(privacyGroupUtil.decode("data3".getBytes())).thenReturn(pg3);
    final List<PrivacyGroup> privacyGroups = privacyGroupManager.findPrivacyGroup(List.of());
    assertThat(privacyGroups).isNotEmpty();
    assertThat(privacyGroups).contains(pg2, pg3);
    verify(privacyGroupDAO).findByLookupId("lookup".getBytes());
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Aggregations

PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)36 Test (org.junit.Test)22 PublicKey (com.quorum.tessera.encryption.PublicKey)21 PrivacyGroupEntity (com.quorum.tessera.data.PrivacyGroupEntity)17 MessageHash (com.quorum.tessera.data.MessageHash)8 Response (jakarta.ws.rs.core.Response)8 PrivacyViolationException (com.quorum.tessera.transaction.exception.PrivacyViolationException)7 Operation (io.swagger.v3.oas.annotations.Operation)7 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)7 SendResponse (com.quorum.tessera.api.SendResponse)6 Config (com.quorum.tessera.config.Config)6 SendRequest (com.quorum.tessera.api.SendRequest)5 ResidentGroup (com.quorum.tessera.config.ResidentGroup)5 PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)4 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)4 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4