Search in sources :

Example 1 with PrivacyGroupManager

use of com.quorum.tessera.privacygroup.PrivacyGroupManager in project tessera by ConsenSys.

the class TransactionResource method send.

// hide this operation from swagger generation; the /send operation is overloaded and must be
// documented in a single place
@Hidden
@POST
@Path("send")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
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> optionalPrivacyGroup = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipientList = optionalPrivacyGroup.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);
    optionalPrivacyGroup.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 SendResponse sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, null, null)).get();
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
    return Response.status(Response.Status.CREATED).type(APPLICATION_JSON).location(location).entity(sendResponse).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Size(jakarta.validation.constraints.Size) 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) 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) Pattern(jakarta.validation.constraints.Pattern) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Hidden(io.swagger.v3.oas.annotations.Hidden) com.quorum.tessera.api(com.quorum.tessera.api) 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) 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) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) Hidden(io.swagger.v3.oas.annotations.Hidden)

Example 2 with PrivacyGroupManager

use of com.quorum.tessera.privacygroup.PrivacyGroupManager 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 3 with PrivacyGroupManager

use of com.quorum.tessera.privacygroup.PrivacyGroupManager in project tessera by ConsenSys.

the class PrivacyGroupManagerProviderTest method provider.

@Test
public void provider() {
    try (var enclaveMockedStatic = mockStatic(Enclave.class);
        var privacyGroupDAOMockStatic = mockStatic(PrivacyGroupDAO.class);
        var batchPrivacyGroupPublisherMockedStatic = mockStatic(BatchPrivacyGroupPublisher.class)) {
        enclaveMockedStatic.when(Enclave::create).thenReturn(mock(Enclave.class));
        privacyGroupDAOMockStatic.when(PrivacyGroupDAO::create).thenReturn(mock(PrivacyGroupDAO.class));
        batchPrivacyGroupPublisherMockedStatic.when(BatchPrivacyGroupPublisher::create).thenReturn(mock(BatchPrivacyGroupPublisher.class));
        PrivacyGroupManager result = PrivacyGroupManagerProvider.provider();
        assertThat(result).isNotNull();
        enclaveMockedStatic.verify(Enclave::create);
        enclaveMockedStatic.verifyNoMoreInteractions();
        privacyGroupDAOMockStatic.verify(PrivacyGroupDAO::create);
        privacyGroupDAOMockStatic.verifyNoMoreInteractions();
        batchPrivacyGroupPublisherMockedStatic.verify(BatchPrivacyGroupPublisher::create);
        batchPrivacyGroupPublisherMockedStatic.verifyNoMoreInteractions();
    }
}
Also used : PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) Enclave(com.quorum.tessera.enclave.Enclave) BatchPrivacyGroupPublisher(com.quorum.tessera.privacygroup.publish.BatchPrivacyGroupPublisher) PrivacyGroupDAO(com.quorum.tessera.data.PrivacyGroupDAO) Test(org.junit.Test)

Example 4 with PrivacyGroupManager

use of com.quorum.tessera.privacygroup.PrivacyGroupManager in project tessera by ConsenSys.

the class ResidentGroupHandlerProviderTest method provider.

@Test
public void provider() {
    PrivacyGroupManager privacyGroupManager = mock(PrivacyGroupManager.class);
    try (var privacyGroupManagerMockedStatic = mockStatic(PrivacyGroupManager.class)) {
        privacyGroupManagerMockedStatic.when(PrivacyGroupManager::create).thenReturn(privacyGroupManager);
        ResidentGroupHandler result = ResidentGroupHandlerProvider.provider();
        assertThat(result).isNotNull();
        privacyGroupManagerMockedStatic.verify(PrivacyGroupManager::create);
        privacyGroupManagerMockedStatic.verifyNoMoreInteractions();
        verifyNoInteractions(privacyGroupManager);
    }
}
Also used : PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) ResidentGroupHandler(com.quorum.tessera.privacygroup.ResidentGroupHandler) Test(org.junit.Test)

Example 5 with PrivacyGroupManager

use of com.quorum.tessera.privacygroup.PrivacyGroupManager in project tessera by ConsenSys.

the class BesuTransactionResource method send.

@Hidden
@POST
@Path("send")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
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> optionalPrivacyGroup = Optional.ofNullable(sendRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipientList = optionalPrivacyGroup.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);
    optionalPrivacyGroup.ifPresentOrElse(requestBuilder::withPrivacyGroupId, () -> {
        PrivacyGroup legacyGroup = privacyGroupManager.createLegacyPrivacyGroup(sender, recipientList);
        requestBuilder.withPrivacyGroupId(legacyGroup.getId());
    });
    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 SendResponse sendResponse = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).map(messageHash -> new SendResponse(messageHash, null, null)).get();
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedKey, StandardCharsets.UTF_8)).build();
    return Response.status(Response.Status.OK).type(APPLICATION_JSON).location(location).entity(sendResponse).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) LoggerFactory(org.slf4j.LoggerFactory) Valid(jakarta.validation.Valid) NotNull(jakarta.validation.constraints.NotNull) PrivacyValid(com.quorum.tessera.api.constraint.PrivacyValid) Path(jakarta.ws.rs.Path) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Produces(jakarta.ws.rs.Produces) URI(java.net.URI) MessageHash(com.quorum.tessera.data.MessageHash) Schema(io.swagger.v3.oas.annotations.media.Schema) Consumes(jakarta.ws.rs.Consumes) Logger(org.slf4j.Logger) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Hidden(io.swagger.v3.oas.annotations.Hidden) POST(jakarta.ws.rs.POST) com.quorum.tessera.api(com.quorum.tessera.api) Predicate(java.util.function.Predicate) TransactionManager(com.quorum.tessera.transaction.TransactionManager) PrivacyGroupManager(com.quorum.tessera.privacygroup.PrivacyGroupManager) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) URLEncoder(java.net.URLEncoder) Stream(java.util.stream.Stream) UriBuilder(jakarta.ws.rs.core.UriBuilder) Tag(io.swagger.v3.oas.annotations.tags.Tag) APPLICATION_JSON(jakarta.ws.rs.core.MediaType.APPLICATION_JSON) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) Path(jakarta.ws.rs.Path) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Hidden(io.swagger.v3.oas.annotations.Hidden)

Aggregations

PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)8 TransactionManager (com.quorum.tessera.transaction.TransactionManager)6 URI (java.net.URI)5 Stream (java.util.stream.Stream)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)4 MessageHash (com.quorum.tessera.data.MessageHash)4 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)4 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)4 PublicKey (com.quorum.tessera.encryption.PublicKey)4 Operation (io.swagger.v3.oas.annotations.Operation)4 Content (io.swagger.v3.oas.annotations.media.Content)4 Schema (io.swagger.v3.oas.annotations.media.Schema)4 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)4 Tag (io.swagger.v3.oas.annotations.tags.Tag)4 Valid (jakarta.validation.Valid)4 NotNull (jakarta.validation.constraints.NotNull)4 Response (jakarta.ws.rs.core.Response)4 UriBuilder (jakarta.ws.rs.core.UriBuilder)4