use of com.quorum.tessera.encryption.PublicKey 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.encryption.PublicKey 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.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionResource method sendSignedTransactionStandard.
// 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_OCTET_STREAM)
@Produces(TEXT_PLAIN)
public Response sendSignedTransactionStandard(@Parameter(description = "comma-separated list of recipient public keys (for application/octet-stream requests)", schema = @Schema(format = "base64")) @HeaderParam("c11n-to") final String recipientKeys, @Valid @NotNull @Size(min = 1) final byte[] signedTransaction) {
final List<PublicKey> recipients = Stream.ofNullable(recipientKeys).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.SendSignedRequest request = com.quorum.tessera.transaction.SendSignedRequest.Builder.create().withRecipients(recipients).withSignedData(signedTransaction).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(Collections.emptySet()).withExecHash(new byte[0]).build();
final com.quorum.tessera.transaction.SendResponse response = transactionManager.sendSignedTransaction(request);
final String encodedTransactionHash = base64Encoder.encodeToString(response.getTransactionHash().getHashBytes());
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();
}
use of com.quorum.tessera.encryption.PublicKey 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();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionResource method receive.
// hide this operation from swagger generation; the /transaction/{hash} operation is overloaded
// and must be documented in a single place
@Hidden
@GET
@Path("/transaction/{hash}")
@Produces(APPLICATION_JSON)
public Response receive(@Parameter(description = "hash indicating encrypted payload to retrieve from database", schema = @Schema(format = "base64")) @Valid @ValidBase64 @PathParam("hash") final String hash, @Parameter(description = "(optional) public key of recipient of the encrypted payload; used in decryption; if not provided, decryption is attempted with all known recipient keys in turn", schema = @Schema(format = "base64")) @QueryParam("to") final String toStr, @Parameter(description = "(optional) indicates whether the payload is raw; determines which database the payload is retrieved from; possible values\n* true - for pre-stored payloads in the \"raw\" database\n* false (default) - for already sent payloads in \"standard\" database") @Valid @Pattern(flags = Pattern.Flag.CASE_INSENSITIVE, regexp = "^(true|false)$") @QueryParam("isRaw") final String isRaw) {
final PublicKey recipient = Optional.ofNullable(toStr).filter(Predicate.not(String::isEmpty)).map(base64Decoder::decode).map(PublicKey::from).orElse(null);
final MessageHash transactionHash = Optional.of(hash).map(base64Decoder::decode).map(MessageHash::new).get();
final com.quorum.tessera.transaction.ReceiveRequest request = com.quorum.tessera.transaction.ReceiveRequest.Builder.create().withRecipient(recipient).withTransactionHash(transactionHash).withRaw(Boolean.valueOf(isRaw)).build();
com.quorum.tessera.transaction.ReceiveResponse response = transactionManager.receive(request);
final ReceiveResponse receiveResponse = new ReceiveResponse();
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(base64Encoder::encodeToString).toArray(String[]::new));
Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
response.getPrivacyGroupId().map(PrivacyGroup.Id::getBase64).ifPresent(receiveResponse::setPrivacyGroupId);
return Response.status(Response.Status.OK).type(APPLICATION_JSON).entity(receiveResponse).build();
}
Aggregations