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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations