Search in sources :

Example 1 with PrivacyMode

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

the class MigrationTest method generateEncodedPayload.

static EncodedPayload generateEncodedPayload() {
    PrivacyMode privacyMode = Arrays.stream(PrivacyMode.values()).skip((int) (PrivacyMode.values().length * Math.random())).findAny().get();
    PublicKey senderKey = PublicKey.from("SenderKey".getBytes());
    EncodedPayload.Builder encodedPayloadBuilder = EncodedPayload.Builder.create().withSenderKey(senderKey).withCipherText("cipherText".getBytes()).withCipherTextNonce("CipherTextNonce".getBytes()).withPrivacyMode(privacyMode).withRecipientNonce("RecipientNonce".getBytes()).withRecipientKeys(List.of(senderKey, PublicKey.from("Recipient".getBytes())));
    if (privacyMode != PrivacyMode.PRIVATE_STATE_VALIDATION) {
        if (privacyMode == PrivacyMode.MANDATORY_RECIPIENTS) {
            encodedPayloadBuilder.withMandatoryRecipients(Set.of(PublicKey.from("Recipient".getBytes())));
        }
        encodedPayloadBuilder.withExecHash(new byte[0]);
    } else {
        encodedPayloadBuilder.withExecHash("execHash".getBytes());
    }
    return encodedPayloadBuilder.build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 2 with PrivacyMode

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

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

the class TransactionResource method sendSignedTransactionEnhanced.

// hide this operation from swagger generation; the /sendsignedtx operation is overloaded and must
// be documented in a single place
@Hidden
@POST
@Path("sendsignedtx")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response sendSignedTransactionEnhanced(@NotNull @Valid @PrivacyValid final SendSignedRequest sendSignedRequest) {
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendSignedRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipients = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Optional.ofNullable(sendSignedRequest.getTo()).map(Arrays::stream).orElse(Stream.empty()).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendSignedRequest.getPrivacyFlag());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendSignedRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendSignedRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final com.quorum.tessera.transaction.SendSignedRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withSignedData(sendSignedRequest.getHash()).withRecipients(recipients).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withExecHash(execHash);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(requestBuilder.build());
    final String endcodedTransactionHash = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    LOGGER.debug("Encoded key: {}", endcodedTransactionHash);
    URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(endcodedTransactionHash, StandardCharsets.UTF_8)).build();
    SendResponse sendResponse = new SendResponse();
    sendResponse.setKey(endcodedTransactionHash);
    return Response.status(Response.Status.CREATED).type(APPLICATION_JSON).location(location).entity(sendResponse).build();
}
Also used : 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 4 with PrivacyMode

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

the class TransactionResource3 method sendSignedTransaction.

// path /sendsignedtx is overloaded (application/octet-stream, application/json and
// application/vnd.tessera-2.1+json); swagger annotations cannot handle situations like this so
// this operation
// documents both
@Operation(operationId = "sendStored", summary = "/sendsignedtx", description = "re-wraps a pre-stored & pre-encrypted payload, stores result in database, and publishes result to recipients", requestBody = @RequestBody(content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendSignedRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendSignedRequest.class)), @Content(mediaType = APPLICATION_OCTET_STREAM, array = @ArraySchema(schema = @Schema(description = "hash of pre-stored payload", type = "string", format = "base64"))) }))
@ApiResponse(responseCode = "200", description = "hash of rewrapped payload (for application/octet-stream requests)", content = @Content(mediaType = APPLICATION_OCTET_STREAM, schema = @Schema(description = "hash of rewrapped payload", type = "string", format = "base64")))
@ApiResponse(responseCode = "201", description = "hash of rewrapped payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendResponse.class, description = "hash of rewrapped payload")), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendResponse.class, description = "hash of rewrapped payload")) })
@POST
@Path("sendsignedtx")
@Consumes({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
@Produces({ MIME_TYPE_JSON_2_1, MIME_TYPE_JSON_3 })
public Response sendSignedTransaction(@NotNull @Valid @PrivacyValid final SendSignedRequest sendSignedRequest) {
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendSignedRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipients = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Optional.ofNullable(sendSignedRequest.getTo()).stream().flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendSignedRequest.getPrivacyFlag());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendSignedRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendSignedRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final com.quorum.tessera.transaction.SendSignedRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withSignedData(sendSignedRequest.getHash()).withRecipients(recipients).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withExecHash(execHash);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(requestBuilder.build());
    final String encodedTransactionHash = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedTransactionHash, StandardCharsets.UTF_8)).build();
    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 responseEntity = new SendResponse();
    responseEntity.setKey(encodedTransactionHash);
    responseEntity.setManagedParties(managedParties);
    responseEntity.setSenderKey(response.getSender().encodeToBase64());
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    return Response.created(location).entity(responseEntity).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 5 with PrivacyMode

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

the class TransactionResource4 method sendSignedTransaction.

@POST
@Path("sendsignedtx")
@Consumes({ MIME_TYPE_JSON_4 })
@Produces({ MIME_TYPE_JSON_4 })
public Response sendSignedTransaction(@NotNull @Valid @PrivacyValid final SendSignedRequest sendSignedRequest) {
    final Optional<PrivacyGroup.Id> privacyGroupId = Optional.ofNullable(sendSignedRequest.getPrivacyGroupId()).map(PrivacyGroup.Id::fromBase64String);
    final List<PublicKey> recipients = privacyGroupId.map(privacyGroupManager::retrievePrivacyGroup).map(PrivacyGroup::getMembers).orElse(Optional.ofNullable(sendSignedRequest.getTo()).stream().flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList()));
    final PrivacyMode privacyMode = PrivacyMode.fromFlag(sendSignedRequest.getPrivacyFlag());
    final Set<MessageHash> affectedTransactions = Stream.ofNullable(sendSignedRequest.getAffectedContractTransactions()).flatMap(Arrays::stream).map(base64Decoder::decode).map(MessageHash::new).collect(Collectors.toSet());
    final byte[] execHash = Optional.ofNullable(sendSignedRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
    final Set<PublicKey> mandatoryRecipients = Stream.ofNullable(sendSignedRequest.getMandatoryRecipients()).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toUnmodifiableSet());
    final com.quorum.tessera.transaction.SendSignedRequest.Builder requestBuilder = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withSignedData(sendSignedRequest.getHash()).withRecipients(recipients).withPrivacyMode(privacyMode).withAffectedContractTransactions(affectedTransactions).withExecHash(execHash).withMandatoryRecipients(mandatoryRecipients);
    privacyGroupId.ifPresent(requestBuilder::withPrivacyGroupId);
    final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(requestBuilder.build());
    final String encodedTransactionHash = Optional.of(response).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    final URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedTransactionHash, StandardCharsets.UTF_8)).build();
    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 responseEntity = new SendResponse();
    responseEntity.setKey(encodedTransactionHash);
    responseEntity.setManagedParties(managedParties);
    responseEntity.setSenderKey(response.getSender().encodeToBase64());
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    return Response.created(location).entity(responseEntity).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest)

Aggregations

PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 PublicKey (com.quorum.tessera.encryption.PublicKey)10 MessageHash (com.quorum.tessera.data.MessageHash)7 URI (java.net.URI)7 SendSignedRequest (com.quorum.tessera.api.SendSignedRequest)5 Operation (io.swagger.v3.oas.annotations.Operation)5 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)5 Response (jakarta.ws.rs.core.Response)5 SendResponse (com.quorum.tessera.api.SendResponse)4 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)4 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)4 PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)4 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4 Content (io.swagger.v3.oas.annotations.media.Content)4 Schema (io.swagger.v3.oas.annotations.media.Schema)4 Tag (io.swagger.v3.oas.annotations.tags.Tag)4 Valid (jakarta.validation.Valid)4 NotNull (jakarta.validation.constraints.NotNull)4 UriBuilder (jakarta.ws.rs.core.UriBuilder)4 URLEncoder (java.net.URLEncoder)4