use of com.quorum.tessera.encryption.PublicKey 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();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class BesuTransactionResource method receive.
@Operation(summary = "/receive", operationId = "getDecryptedPayloadJson", description = "get payload from database, decrypt, and return. This endpoint is only to be used by Besu")
@ApiResponse(responseCode = "200", description = "decrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = BesuReceiveResponse.class)), @Content(mediaType = ORION, schema = @Schema(implementation = BesuReceiveResponse.class)) })
@POST
@Path("/receive")
@Consumes({ APPLICATION_JSON, ORION })
@Produces(APPLICATION_JSON)
public Response receive(@Valid final ReceiveRequest request) {
LOGGER.debug("Received receive request");
MessageHash transactionHash = Optional.of(request).map(ReceiveRequest::getKey).map(base64Decoder::decode).map(MessageHash::new).get();
PublicKey recipient = Optional.of(request).map(ReceiveRequest::getTo).filter(Predicate.not(String::isEmpty)).filter(Objects::nonNull).map(base64Decoder::decode).map(PublicKey::from).orElse(null);
com.quorum.tessera.transaction.ReceiveRequest receiveRequest = com.quorum.tessera.transaction.ReceiveRequest.Builder.create().withTransactionHash(transactionHash).withRecipient(recipient).withRaw(request.isRaw()).build();
com.quorum.tessera.transaction.ReceiveResponse response = transactionManager.receive(receiveRequest);
BesuReceiveResponse receiveResponse = new BesuReceiveResponse();
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setSenderKey(response.sender().encodeToBase64());
response.getPrivacyGroupId().map(PrivacyGroup.Id::getBase64).ifPresent(receiveResponse::setPrivacyGroupId);
return Response.status(Response.Status.OK).type(APPLICATION_JSON).entity(receiveResponse).build();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class EncodedPayloadResource method createEncodedPayload.
// hide this operation from swagger generation; the /encodedpayload/create operation is overloaded
// and must be documented in a single place
@Hidden
@POST
@Path("create")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response createEncodedPayload(@NotNull @Valid final SendRequest sendRequest) {
LOGGER.info("Received request for custom payload encryption");
final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
final List<PublicKey> recipientList = 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(Base64.getDecoder()::decode).map(MessageHash::new).collect(Collectors.toSet());
final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
final com.quorum.tessera.transaction.SendRequest request = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(PrivacyMode.fromFlag(sendRequest.getPrivacyFlag())).withAffectedContractTransactions(affectedTransactions).build();
final EncodedPayload encodedPayload = encodedPayloadManager.create(request);
final Map<String, String> affectedContractTransactionMap = encodedPayload.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().encodeToBase64(), e -> Base64.getEncoder().encodeToString(e.getValue().getData())));
final PayloadEncryptResponse response = new PayloadEncryptResponse();
response.setSenderKey(encodedPayload.getSenderKey().getKeyBytes());
response.setCipherText(encodedPayload.getCipherText());
response.setCipherTextNonce(encodedPayload.getCipherTextNonce().getNonceBytes());
response.setRecipientBoxes(encodedPayload.getRecipientBoxes().stream().map(RecipientBox::getData).collect(Collectors.toList()));
response.setRecipientNonce(encodedPayload.getRecipientNonce().getNonceBytes());
response.setRecipientKeys(encodedPayload.getRecipientKeys().stream().map(PublicKey::getKeyBytes).collect(Collectors.toList()));
response.setPrivacyMode(encodedPayload.getPrivacyMode().getPrivacyFlag());
response.setAffectedContractTransactions(affectedContractTransactionMap);
response.setExecHash(encodedPayload.getExecHash());
return Response.ok(response).type(APPLICATION_JSON).build();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class EncodedPayloadResource method createEncodedPayload21.
// path /encodedpayload/create is overloaded (application/json and
// application/vnd.tessera-2.1+json); swagger annotations cannot handle situations like this so
// this operation documents both
@POST
@Path("create")
@Operation(summary = "/encodedpayload/create", operationId = "encrypt", description = "encrypt a payload and return the result; does not store to the database or push to peers", requestBody = @RequestBody(content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = SendRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = SendRequest.class)) }))
@ApiResponse(responseCode = "200", description = "encrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = PayloadEncryptResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = PayloadEncryptResponse.class)) })
@Consumes(MIME_TYPE_JSON_2_1)
@Produces(MIME_TYPE_JSON_2_1)
public Response createEncodedPayload21(@NotNull @Valid final SendRequest sendRequest) {
LOGGER.info("Received request for custom payload encryption");
final PublicKey sender = Optional.ofNullable(sendRequest.getFrom()).map(base64Decoder::decode).map(PublicKey::from).orElseGet(transactionManager::defaultPublicKey);
final List<PublicKey> recipientList = 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(Base64.getDecoder()::decode).map(MessageHash::new).collect(Collectors.toSet());
final byte[] execHash = Optional.ofNullable(sendRequest.getExecHash()).map(String::getBytes).orElse(new byte[0]);
final com.quorum.tessera.transaction.SendRequest request = com.quorum.tessera.transaction.SendRequest.Builder.create().withRecipients(recipientList).withSender(sender).withPayload(sendRequest.getPayload()).withExecHash(execHash).withPrivacyMode(PrivacyMode.fromFlag(sendRequest.getPrivacyFlag())).withAffectedContractTransactions(affectedTransactions).build();
final EncodedPayload encodedPayload = encodedPayloadManager.create(request);
final Map<String, String> affectedContractTransactionMap = encodedPayload.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().encodeToBase64(), e -> Base64.getEncoder().encodeToString(e.getValue().getData())));
final PayloadEncryptResponse response = new PayloadEncryptResponse();
response.setSenderKey(encodedPayload.getSenderKey().getKeyBytes());
response.setCipherText(encodedPayload.getCipherText());
response.setCipherTextNonce(encodedPayload.getCipherTextNonce().getNonceBytes());
response.setRecipientBoxes(encodedPayload.getRecipientBoxes().stream().map(RecipientBox::getData).collect(Collectors.toList()));
response.setRecipientNonce(encodedPayload.getRecipientNonce().getNonceBytes());
response.setRecipientKeys(encodedPayload.getRecipientKeys().stream().map(PublicKey::getKeyBytes).collect(Collectors.toList()));
response.setPrivacyMode(encodedPayload.getPrivacyMode().getPrivacyFlag());
response.setAffectedContractTransactions(affectedContractTransactionMap);
response.setExecHash(encodedPayload.getExecHash());
return Response.ok(response).type(MIME_TYPE_JSON_2_1).build();
}
use of com.quorum.tessera.encryption.PublicKey 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();
}
Aggregations