use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.
the class CustomPayloadEncryptionIT method payloadDecryptionFailsOnBadMessage.
@Test
public void payloadDecryptionFailsOnBadMessage() {
final Party sender = partyHelper.findByAlias(NodeAlias.A);
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(Base64.getEncoder().encode("Test Payload".getBytes()));
Response result = sender.getRestClientWebTarget().path("/encodedpayload/create").request().post(Entity.entity(sendRequest, mediaType));
assertThat(result.getStatus()).isEqualTo(200);
final PayloadEncryptResponse payloadEncryptResponse = result.readEntity(PayloadEncryptResponse.class);
// edit the cipher text to something rubbish, so it can't be decrypted
payloadEncryptResponse.setCipherText("Unexpected data".getBytes());
// attempt to decrypt it
final Response decryptResultForSender = sender.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
assertThat(decryptResultForSender.getStatus()).isEqualTo(500);
}
use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.
the class CustomPayloadEncryptionIT method createAndDecryptPayload.
@Test
public void createAndDecryptPayload() {
final Party sender = partyHelper.findByAlias(NodeAlias.A);
final Party recipient = partyHelper.findByAlias(NodeAlias.B);
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(Base64.getEncoder().encode("Test Payload".getBytes()));
sendRequest.setTo(recipient.getPublicKey());
final Response encryptResult = sender.getRestClientWebTarget().path("/encodedpayload/create").request().post(Entity.entity(sendRequest, mediaType));
assertThat(encryptResult.getStatus()).isEqualTo(200);
final PayloadEncryptResponse payloadEncryptResponse = encryptResult.readEntity(PayloadEncryptResponse.class);
// decrypt it again with the sender
final Response decryptResultForSender = sender.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
final ReceiveResponse decryptedPayload = decryptResultForSender.readEntity(ReceiveResponse.class);
assertThat(Base64.getDecoder().decode(decryptedPayload.getPayload())).isEqualTo("Test Payload".getBytes());
// decrypt it using the recipient
final String firstRecipientInList = Base64.getEncoder().encodeToString(payloadEncryptResponse.getRecipientKeys().get(0));
if (Objects.equals(firstRecipientInList, sender.getPublicKey())) {
payloadEncryptResponse.getRecipientBoxes().remove(0);
} else {
payloadEncryptResponse.getRecipientBoxes().remove(1);
}
payloadEncryptResponse.setRecipientKeys(Collections.emptyList());
final Response decryptResultForRecipient = recipient.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
final ReceiveResponse decryptedPayloadForRecipient = decryptResultForRecipient.readEntity(ReceiveResponse.class);
assertThat(Base64.getDecoder().decode(decryptedPayloadForRecipient.getPayload())).isEqualTo("Test Payload".getBytes());
}
use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.
the class RequestPrivacyValidatorTest method testPrivacyValidationOnSendRequest.
@Test
public void testPrivacyValidationOnSendRequest() {
SendRequest request = new SendRequest();
request.setPrivacyFlag(-1000);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(0);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(1);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(2);
assertThat(validator.isValid(request, context)).isTrue();
request.setPrivacyFlag(3);
assertThat(validator.isValid(request, context)).isFalse();
verify(context).buildConstraintViolationWithTemplate("Exec hash missing");
verify(builder).addConstraintViolation();
request.setExecHash("execHash");
assertThat(validator.isValid(request, context)).isTrue();
}
use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.
the class RequestPrivacyValidator method isValid.
@Override
public boolean isValid(Object request, ConstraintValidatorContext context) {
PrivacyMode privacyMode;
String execHash;
if (request instanceof SendRequest) {
privacyMode = PrivacyMode.fromFlag(((SendRequest) request).getPrivacyFlag());
execHash = ((SendRequest) request).getExecHash();
} else if (request instanceof SendSignedRequest) {
privacyMode = PrivacyMode.fromFlag(((SendSignedRequest) request).getPrivacyFlag());
execHash = ((SendSignedRequest) request).getExecHash();
} else {
context.buildConstraintViolationWithTemplate("Invalid usage. This validator can only be apply to SendRequest or SendSignedRequest").addConstraintViolation();
return false;
}
if (PrivacyMode.PRIVATE_STATE_VALIDATION == privacyMode) {
if (Objects.isNull(execHash) || execHash.length() == 0) {
context.buildConstraintViolationWithTemplate("Exec hash missing").addConstraintViolation();
return false;
}
}
return true;
}
use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.
the class BesuTransactionResourceTest method send.
@Test
public void send() {
final Base64.Encoder base64Encoder = Base64.getEncoder();
final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
final SendRequest sendRequest = new SendRequest();
sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
sendRequest.setTo(base64Key);
final PublicKey sender = mock(PublicKey.class);
when(transactionManager.defaultPublicKey()).thenReturn(sender);
final byte[] recipientKeyBytes = Base64.getDecoder().decode(base64Key);
final com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
final MessageHash messageHash = mock(MessageHash.class);
final byte[] txnData = "TxnData".getBytes();
when(messageHash.getHashBytes()).thenReturn(txnData);
when(sendResponse.getTransactionHash()).thenReturn(messageHash);
when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
PrivacyGroup legacy = mock(PrivacyGroup.class);
when(legacy.getId()).thenReturn(PrivacyGroup.Id.fromBytes("group".getBytes()));
when(privacyGroupManager.createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))))).thenReturn(legacy);
final Response result = besuTransactionResource.send(sendRequest);
// jersey.target("send").request().post(Entity.entity(sendRequest,
// MediaType.APPLICATION_JSON));
assertThat(result.getStatus()).isEqualTo(200);
assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
SendResponse resultSendResponse = (SendResponse) result.getEntity();
assertThat(resultSendResponse.getKey()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
verify(transactionManager).send(argumentCaptor.capture());
verify(transactionManager).defaultPublicKey();
verify(privacyGroupManager).createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))));
com.quorum.tessera.transaction.SendRequest businessObject = argumentCaptor.getValue();
assertThat(businessObject).isNotNull();
assertThat(businessObject.getPayload()).isEqualTo(sendRequest.getPayload());
assertThat(businessObject.getSender()).isEqualTo(sender);
assertThat(businessObject.getRecipients()).hasSize(1);
assertThat(businessObject.getRecipients().get(0).encodeToBase64()).isEqualTo(base64Key);
assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
assertThat(businessObject.getExecHash()).isEmpty();
assertThat(businessObject.getPrivacyGroupId()).isPresent().get().isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
}
Aggregations