Search in sources :

Example 1 with PrivacyGroup

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

the class PrivacyGroupResource method retrievePrivacyGroup.

@Operation(summary = "/retrievePrivacyGroup", operationId = "retrievePrivacyGroup", description = "retrieve privacy group from a privacy group id", requestBody = @RequestBody(content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PrivacyGroupRetrieveRequest.class))))
@ApiResponse(responseCode = "200", description = "A privacy group object", content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PrivacyGroupResponse.class)))
@ApiResponse(responseCode = "404", description = "privacy group not found")
@POST
@Path("retrievePrivacyGroup")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response retrievePrivacyGroup(@NotNull final PrivacyGroupRetrieveRequest retrieveRequest) {
    final PrivacyGroup.Id privacyGroupId = PrivacyGroup.Id.fromBase64String(retrieveRequest.getPrivacyGroupId());
    final PrivacyGroup privacyGroup = privacyGroupManager.retrievePrivacyGroup(privacyGroupId);
    return Response.ok().entity(toResponseObject(privacyGroup)).build();
}
Also used : PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 2 with PrivacyGroup

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

the class PrivacyGroupResource method deletePrivacyGroup.

@Operation(summary = "/deletePrivacyGroup", operationId = "deletePrivacyGroup", description = "mark a privacy group as deleted", requestBody = @RequestBody(content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PrivacyGroupDeleteRequest.class))))
@ApiResponse(responseCode = "200", description = "id of the deleted privacy group", content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = String.class)))
@ApiResponse(responseCode = "404", description = "privacy group not found")
@POST
@Path("deletePrivacyGroup")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response deletePrivacyGroup(@NotNull final PrivacyGroupDeleteRequest request) {
    final PublicKey from = Optional.ofNullable(request.getFrom()).map(Base64.getDecoder()::decode).map(PublicKey::from).orElseGet(privacyGroupManager::defaultPublicKey);
    final PrivacyGroup.Id privacyGroupId = PrivacyGroup.Id.fromBase64String(request.getPrivacyGroupId());
    final PrivacyGroup privacyGroup = privacyGroupManager.deletePrivacyGroup(from, privacyGroupId);
    // Have to output in this format to match what is expected from Besu
    final String output = Json.createArrayBuilder().add(privacyGroup.getId().getBase64()).build().getJsonString(0).toString();
    return Response.ok().entity(output).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 3 with PrivacyGroup

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

the class PrivacyGroupResource method createPrivacyGroup.

@Operation(summary = "/createPrivacyGroup", operationId = "createPrivacyGroup", description = "creates a privacy group, stores data in database, and distribute to members", requestBody = @RequestBody(content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PrivacyGroupRequest.class))))
@ApiResponse(responseCode = "200", description = "created privacy group", content = @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PrivacyGroupResponse.class)))
@ApiResponse(responseCode = "403", description = "privacy group not supported on remote member")
@POST
@Path("createPrivacyGroup")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response createPrivacyGroup(@NotNull final PrivacyGroupRequest request) {
    final PublicKey from = Optional.ofNullable(request.getFrom()).map(Base64.getDecoder()::decode).map(PublicKey::from).orElseGet(privacyGroupManager::defaultPublicKey);
    final List<PublicKey> members = Stream.ofNullable(request.getAddresses()).flatMap(Arrays::stream).map(Base64.getDecoder()::decode).map(PublicKey::from).collect(Collectors.toList());
    final byte[] randomSeed = Optional.ofNullable(request.getSeed()).map(Base64.getDecoder()::decode).orElseGet(generateRandomSeed);
    final String name = Optional.ofNullable(request.getName()).orElse("");
    final String description = Optional.ofNullable(request.getDescription()).orElse("");
    final PrivacyGroup created = privacyGroupManager.createPrivacyGroup(name, description, from, members, randomSeed);
    return Response.status(Response.Status.OK).entity(toResponseObject(created)).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 4 with PrivacyGroup

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

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

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