Search in sources :

Example 1 with ValidBase64

use of com.quorum.tessera.config.constraints.ValidBase64 in project tessera by ConsenSys.

the class TransactionResource method sendRaw.

@Operation(summary = "/sendraw", operationId = "encryptStoreAndSendOctetStream", description = "encrypts a payload, stores result in database, and publishes result to recipients")
@ApiResponse(responseCode = "200", description = "encrypted payload hash", content = @Content(schema = @Schema(type = "string", format = "base64", description = "encrypted payload hash")))
@POST
@Path("sendraw")
@Consumes(APPLICATION_OCTET_STREAM)
@Produces(TEXT_PLAIN)
public Response sendRaw(@HeaderParam("c11n-from") @Parameter(description = "public key identifying the server's key pair that will be used in the encryption; if not set, default used", schema = @Schema(format = "base64")) @Valid @ValidBase64 final String sender, @HeaderParam("c11n-to") @Parameter(description = "comma-separated list of recipient public keys", schema = @Schema(format = "base64")) final String recipientKeys, @Schema(description = "data to be encrypted") @NotNull @Size(min = 1) @Valid final byte[] payload) {
    final PublicKey senderKey = Optional.ofNullable(sender).filter(Predicate.not(String::isEmpty)).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
    final List<PublicKey> recipients = Stream.of(recipientKeys).filter(Objects::nonNull).filter(s -> !Objects.equals("", s)).map(v -> v.split(",")).flatMap(Arrays::stream).map(base64Decoder::decode).map(PublicKey::from).collect(Collectors.toList());
    final com.quorum.tessera.transaction.SendRequest request = com.quorum.tessera.transaction.SendRequest.Builder.create().withSender(senderKey).withRecipients(recipients).withPayload(payload).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(Collections.emptySet()).withExecHash(new byte[0]).build();
    final com.quorum.tessera.transaction.SendResponse sendResponse = transactionManager.send(request);
    final String encodedTransactionHash = Optional.of(sendResponse).map(com.quorum.tessera.transaction.SendResponse::getTransactionHash).map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).get();
    LOGGER.debug("Encoded key: {}", encodedTransactionHash);
    URI location = UriBuilder.fromPath("transaction").path(URLEncoder.encode(encodedTransactionHash, StandardCharsets.UTF_8)).build();
    return Response.status(Response.Status.OK).entity(encodedTransactionHash).location(location).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) MessageHash(com.quorum.tessera.data.MessageHash) URI(java.net.URI) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 2 with ValidBase64

use of com.quorum.tessera.config.constraints.ValidBase64 in project tessera by ConsenSys.

the class InlineKeypair method getPrivateKey.

@Override
@NotNull
@Size(min = 1)
@ValidBase64(message = "Invalid Base64 key provided")
@Pattern(regexp = "^((?!NACL_FAILURE).)*$", message = "Could not decrypt the private key with the provided password, please double check the passwords provided")
public String getPrivateKey() {
    final PrivateKeyData pkd = privateKeyConfig.getPrivateKeyData();
    if (privateKeyConfig.getType() == UNLOCKED) {
        return privateKeyConfig.getValue();
    }
    if (this.cachedValue == null || !Objects.equals(this.cachedPassword, this.password)) {
        if (password != null) {
            try {
                this.cachedValue = keyEncryptor.decryptPrivateKey(pkd, password).encodeToBase64();
            } catch (final EncryptorException ex) {
                this.cachedValue = "NACL_FAILURE";
            }
        }
    }
    this.cachedPassword = this.password;
    return this.cachedValue;
}
Also used : PrivateKeyData(com.quorum.tessera.config.PrivateKeyData) EncryptorException(com.quorum.tessera.encryption.EncryptorException) Pattern(jakarta.validation.constraints.Pattern) Size(jakarta.validation.constraints.Size) ValidBase64(com.quorum.tessera.config.constraints.ValidBase64) NotNull(jakarta.validation.constraints.NotNull)

Aggregations

ValidBase64 (com.quorum.tessera.config.constraints.ValidBase64)2 NotNull (jakarta.validation.constraints.NotNull)2 Pattern (jakarta.validation.constraints.Pattern)2 Size (jakarta.validation.constraints.Size)2 com.quorum.tessera.api (com.quorum.tessera.api)1 PrivacyValid (com.quorum.tessera.api.constraint.PrivacyValid)1 PrivateKeyData (com.quorum.tessera.config.PrivateKeyData)1 MessageHash (com.quorum.tessera.data.MessageHash)1 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)1 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)1 EncryptorException (com.quorum.tessera.encryption.EncryptorException)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 PrivacyGroupManager (com.quorum.tessera.privacygroup.PrivacyGroupManager)1 TransactionManager (com.quorum.tessera.transaction.TransactionManager)1 Hidden (io.swagger.v3.oas.annotations.Hidden)1 Operation (io.swagger.v3.oas.annotations.Operation)1 Parameter (io.swagger.v3.oas.annotations.Parameter)1 ArraySchema (io.swagger.v3.oas.annotations.media.ArraySchema)1 Content (io.swagger.v3.oas.annotations.media.Content)1 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)1