use of com.quorum.tessera.api.PayloadDecryptRequest in project tessera by ConsenSys.
the class EncodedPayloadResourceTest method decryptPayloadVersion21.
@Test
public void decryptPayloadVersion21() {
final Base64.Decoder decoder = Base64.getDecoder();
final PayloadDecryptRequest request = new PayloadDecryptRequest();
request.setSenderKey(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo="));
request.setCipherText(decoder.decode("h7av/vhPlaPFECB1K30hNWugv/Bu"));
request.setCipherTextNonce(decoder.decode("8MVXAESCQuRHWxrQ6b5MXuYApjia+2h0"));
request.setRecipientBoxes(List.of(decoder.decode("FNirZRc2ayMaYopCBaWQ/1I7VWFiCM0lNw533Hckzxb+qpvngdWVVzJlsE05dbxl")));
request.setRecipientNonce(decoder.decode("p9gYDJlEoBvLdUQ+ZoONl2Jl9AirV1en"));
request.setRecipientKeys(List.of(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")));
request.setPrivacyMode(3);
request.setAffectedContractTransactions(Map.of("dHgx", "dHgxdmFs", "dHgy", "dHgydmFs"));
request.setExecHash("execHash".getBytes());
final ReceiveResponse response = ReceiveResponse.Builder.create().withUnencryptedTransactionData("decryptedData".getBytes()).withPrivacyMode(PrivacyMode.PRIVATE_STATE_VALIDATION).withAffectedTransactions(Set.of(new MessageHash("tx1val".getBytes()), new MessageHash("tx2val".getBytes()))).withExecHash("execHash".getBytes()).withSender(PublicKey.from(request.getSenderKey())).build();
when(encodedPayloadManager.decrypt(any(), eq(null))).thenReturn(response);
final Response result = encodedPayloadResource.receive21(request);
assertThat(result.getStatus()).isEqualTo(200);
final com.quorum.tessera.api.ReceiveResponse payloadEncryptResponse = Optional.of(result).map(Response::getEntity).map(com.quorum.tessera.api.ReceiveResponse.class::cast).get();
assertThat(payloadEncryptResponse.getPayload()).isEqualTo("decryptedData".getBytes());
assertThat(payloadEncryptResponse.getPrivacyFlag()).isEqualTo(3);
assertThat(payloadEncryptResponse.getAffectedContractTransactions()).containsExactlyInAnyOrder("dHgxdmFs", "dHgydmFs");
assertThat(payloadEncryptResponse.getExecHash()).isEqualTo("execHash");
final ArgumentCaptor<EncodedPayload> argumentCaptor = ArgumentCaptor.forClass(EncodedPayload.class);
verify(encodedPayloadManager).decrypt(argumentCaptor.capture(), eq(null));
final EncodedPayload payloadBeforeDecryption = argumentCaptor.getValue();
assertThat(payloadBeforeDecryption.getSenderKey().encodeToBase64()).isEqualTo("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=");
assertThat(payloadBeforeDecryption.getCipherText()).isEqualTo(decoder.decode("h7av/vhPlaPFECB1K30hNWugv/Bu"));
assertThat(payloadBeforeDecryption.getCipherTextNonce().getNonceBytes()).isEqualTo(decoder.decode("8MVXAESCQuRHWxrQ6b5MXuYApjia+2h0"));
assertThat(payloadBeforeDecryption.getRecipientBoxes()).containsExactly(RecipientBox.from(decoder.decode("FNirZRc2ayMaYopCBaWQ/1I7VWFiCM0lNw533Hckzxb+qpvngdWVVzJlsE05dbxl")));
assertThat(payloadBeforeDecryption.getRecipientNonce().getNonceBytes()).isEqualTo(decoder.decode("p9gYDJlEoBvLdUQ+ZoONl2Jl9AirV1en"));
assertThat(payloadBeforeDecryption.getRecipientKeys()).containsExactly(PublicKey.from(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")));
assertThat(payloadBeforeDecryption.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
assertThat(payloadBeforeDecryption.getAffectedContractTransactions()).contains(entry(TxHash.from("tx1".getBytes()), SecurityHash.from("tx1val".getBytes())), entry(TxHash.from("tx2".getBytes()), SecurityHash.from("tx2val".getBytes())));
assertThat(payloadBeforeDecryption.getExecHash()).isEqualTo("execHash".getBytes());
}
use of com.quorum.tessera.api.PayloadDecryptRequest in project tessera by ConsenSys.
the class EncodedPayloadResourceTest method decryptPayload.
@Test
public void decryptPayload() {
final PrivacyMode privacyMode = PrivacyMode.PRIVATE_STATE_VALIDATION;
final Base64.Decoder decoder = Base64.getDecoder();
final PayloadDecryptRequest request = new PayloadDecryptRequest();
request.setSenderKey(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo="));
request.setCipherText(decoder.decode("h7av/vhPlaPFECB1K30hNWugv/Bu"));
request.setCipherTextNonce(decoder.decode("8MVXAESCQuRHWxrQ6b5MXuYApjia+2h0"));
request.setRecipientBoxes(List.of(decoder.decode("FNirZRc2ayMaYopCBaWQ/1I7VWFiCM0lNw533Hckzxb+qpvngdWVVzJlsE05dbxl")));
request.setRecipientNonce(decoder.decode("p9gYDJlEoBvLdUQ+ZoONl2Jl9AirV1en"));
request.setRecipientKeys(List.of(decoder.decode("BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")));
request.setPrivacyMode(privacyMode.getPrivacyFlag());
request.setAffectedContractTransactions(Map.of("dHgx", "dHgxdmFs", "dHgy", "dHgydmFs"));
request.setExecHash("execHash".getBytes());
final ReceiveResponse response = mock(ReceiveResponse.class);
when(response.getPrivacyMode()).thenReturn(privacyMode);
when(response.getUnencryptedTransactionData()).thenReturn("decryptedData".getBytes());
when(response.getExecHash()).thenReturn("I Love sparrows".getBytes());
MessageHash messageHash = mock(MessageHash.class);
when(messageHash.getHashBytes()).thenReturn("SomeMessageHashBytes".getBytes());
when(response.getAffectedTransactions()).thenReturn(Set.of(messageHash));
when(encodedPayloadManager.decrypt(any(), eq(null))).thenReturn(response);
final Response result = encodedPayloadResource.decryptEncodedPayload(request);
assertThat(result.getStatus()).isEqualTo(200);
final com.quorum.tessera.api.ReceiveResponse payloadEncryptResponse = com.quorum.tessera.api.ReceiveResponse.class.cast(result.getEntity());
assertThat(payloadEncryptResponse.getPayload()).isEqualTo("decryptedData".getBytes());
assertThat(payloadEncryptResponse.getPrivacyFlag()).isEqualTo(privacyMode.getPrivacyFlag());
assertThat(payloadEncryptResponse.getAffectedContractTransactions()).contains(Base64.getEncoder().encodeToString("SomeMessageHashBytes".getBytes()));
assertThat(payloadEncryptResponse.getExecHash()).isEqualTo("I Love sparrows");
verify(encodedPayloadManager).decrypt(any(), eq(null));
}
use of com.quorum.tessera.api.PayloadDecryptRequest in project tessera by ConsenSys.
the class EncodedPayloadResource method decryptEncodedPayload.
// hide this operation from swagger generation; the /encodedpayload/decrypt operation is
// overloaded and must be documented in a single place
@Hidden
@POST
@Path("decrypt")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response decryptEncodedPayload(@Valid @NotNull final PayloadDecryptRequest request) {
LOGGER.info("Received request to decrypt custom transaction");
final Base64.Decoder decoder = Base64.getDecoder();
final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
final ReceiveResponse receiveResponse = new ReceiveResponse();
receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
return Response.ok(receiveResponse).type(APPLICATION_JSON).build();
}
use of com.quorum.tessera.api.PayloadDecryptRequest in project tessera by ConsenSys.
the class EncodedPayloadResource method receive21.
// path /encodedpayload/decrypt 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("decrypt")
@Operation(summary = "/encodedpayload/decrypt", operationId = "decrypt", description = "decrypt an encrypted 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 = PayloadDecryptRequest.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = PayloadDecryptRequest.class)) }))
@ApiResponse(responseCode = "200", description = "decrypted payload", content = { @Content(mediaType = APPLICATION_JSON, schema = @Schema(implementation = ReceiveResponse.class)), @Content(mediaType = MIME_TYPE_JSON_2_1, schema = @Schema(implementation = ReceiveResponse.class)) })
@Consumes(MIME_TYPE_JSON_2_1)
@Produces(MIME_TYPE_JSON_2_1)
public Response receive21(@Valid @NotNull final PayloadDecryptRequest request) {
LOGGER.info("Received request to decrypt custom transaction");
final Base64.Decoder decoder = Base64.getDecoder();
final Map<TxHash, byte[]> affectedTxns = request.getAffectedContractTransactions().entrySet().stream().collect(Collectors.toMap(e -> TxHash.from(decoder.decode(e.getKey())), e -> decoder.decode(e.getValue())));
final EncodedPayload requestAsPayload = EncodedPayload.Builder.create().withSenderKey(PublicKey.from(request.getSenderKey())).withCipherText(request.getCipherText()).withCipherTextNonce(request.getCipherTextNonce()).withRecipientBoxes(request.getRecipientBoxes()).withRecipientNonce(request.getRecipientNonce()).withRecipientKeys(request.getRecipientKeys().stream().map(PublicKey::from).collect(Collectors.toList())).withPrivacyFlag(request.getPrivacyMode()).withAffectedContractTransactions(affectedTxns).withExecHash(request.getExecHash()).build();
final com.quorum.tessera.transaction.ReceiveResponse response = encodedPayloadManager.decrypt(requestAsPayload, null);
final ReceiveResponse receiveResponse = new ReceiveResponse();
receiveResponse.setPrivacyFlag(response.getPrivacyMode().getPrivacyFlag());
receiveResponse.setPayload(response.getUnencryptedTransactionData());
receiveResponse.setAffectedContractTransactions(response.getAffectedTransactions().stream().map(MessageHash::getHashBytes).map(Base64.getEncoder()::encodeToString).toArray(String[]::new));
Optional.ofNullable(response.getExecHash()).map(String::new).ifPresent(receiveResponse::setExecHash);
return Response.ok(receiveResponse).type(MIME_TYPE_JSON_2_1).build();
}
Aggregations