Search in sources :

Example 1 with ReceiveResponse

use of com.quorum.tessera.transaction.ReceiveResponse 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());
}
Also used : PayloadDecryptRequest(com.quorum.tessera.api.PayloadDecryptRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Base64(java.util.Base64) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 2 with ReceiveResponse

use of com.quorum.tessera.transaction.ReceiveResponse in project tessera by ConsenSys.

the class TransactionResource3Test method receive.

@Test
public void receive() {
    PublicKey sender = PublicKey.from("sender".getBytes());
    ReceiveResponse response = mock(ReceiveResponse.class);
    when(response.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(response.getUnencryptedTransactionData()).thenReturn("Success".getBytes());
    when(response.sender()).thenReturn(sender);
    when(transactionManager.receive(any(com.quorum.tessera.transaction.ReceiveRequest.class))).thenReturn(response);
    String transactionHash = Base64.getEncoder().encodeToString("transactionHash".getBytes());
    Response result = transactionResource.receive(transactionHash, null, Boolean.FALSE.toString());
    assertThat(result.getStatus()).isEqualTo(200);
    com.quorum.tessera.api.ReceiveResponse resultResponse = com.quorum.tessera.api.ReceiveResponse.class.cast(result.getEntity());
    assertThat(resultResponse.getExecHash()).isNull();
    assertThat(resultResponse.getPrivacyFlag()).isEqualTo(PrivacyMode.STANDARD_PRIVATE.getPrivacyFlag());
    assertThat(resultResponse.getExecHash()).isNull();
    assertThat(resultResponse.getAffectedContractTransactions()).isNullOrEmpty();
    assertThat(resultResponse.getPayload()).isEqualTo("Success".getBytes());
    assertThat(resultResponse.getSenderKey()).isEqualTo(sender.encodeToBase64());
    verify(transactionManager).receive(any(com.quorum.tessera.transaction.ReceiveRequest.class));
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Test(org.junit.Test)

Example 3 with ReceiveResponse

use of com.quorum.tessera.transaction.ReceiveResponse in project tessera by ConsenSys.

the class EncodedPayloadManagerImpl method decrypt.

@Override
public ReceiveResponse decrypt(final EncodedPayload payload, final PublicKey maybeDefaultRecipient) {
    final MessageHash customPayloadHash = new MessageHash(payloadDigest.digest(payload.getCipherText()));
    LOGGER.debug("Decrypt request for custom message with hash {}", customPayloadHash);
    final PublicKey recipientKey = Optional.ofNullable(maybeDefaultRecipient).orElseGet(() -> searchForRecipientKey(payload).orElseThrow(() -> new RecipientKeyNotFoundException("No suitable recipient keys found to decrypt payload for " + customPayloadHash)));
    LOGGER.debug("Decryption key found: {}", recipientKey.encodeToBase64());
    final byte[] decryptedTransactionData = enclave.unencryptTransaction(payload, recipientKey);
    final Set<MessageHash> affectedTransaction = payload.getAffectedContractTransactions().keySet().stream().map(TxHash::getBytes).map(MessageHash::new).collect(Collectors.toSet());
    ReceiveResponse.Builder builder = ReceiveResponse.Builder.create().withUnencryptedTransactionData(decryptedTransactionData).withPrivacyMode(payload.getPrivacyMode()).withAffectedTransactions(affectedTransaction).withExecHash(payload.getExecHash()).withSender(payload.getSenderKey());
    payload.getPrivacyGroupId().ifPresent(builder::withPrivacyGroupId);
    return builder.build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)

Example 4 with ReceiveResponse

use of com.quorum.tessera.transaction.ReceiveResponse in project tessera by ConsenSys.

the class EncodedPayloadManagerImplTest method decryptTransactionSucceeds.

@Test
public void decryptTransactionSucceeds() {
    final byte[] testPayload = "test payload".getBytes();
    final String senderKeyBase64 = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final PublicKey sender = PublicKey.from(Base64.getDecoder().decode(senderKeyBase64));
    final String singleRecipientBase64 = "QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=";
    final PublicKey singleRecipient = PublicKey.from(Base64.getDecoder().decode(singleRecipientBase64));
    final List<PublicKey> recipients = List.of(singleRecipient);
    final EncodedPayload samplePayload = mock(EncodedPayload.class);
    when(samplePayload.getSenderKey()).thenReturn(sender);
    when(samplePayload.getRecipientKeys()).thenReturn(recipients);
    when(samplePayload.getCipherText()).thenReturn(testPayload);
    when(samplePayload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(samplePayload.getAffectedContractTransactions()).thenReturn(emptyMap());
    when(samplePayload.getExecHash()).thenReturn(new byte[0]);
    when(payloadDigest.digest(any())).thenReturn("test hash".getBytes());
    when(enclave.getPublicKeys()).thenReturn(Set.of(singleRecipient));
    when(enclave.unencryptTransaction(samplePayload, singleRecipient)).thenReturn("decrypted data".getBytes());
    final ReceiveResponse response = encodedPayloadManager.decrypt(samplePayload, null);
    assertThat(response.getUnencryptedTransactionData()).isEqualTo("decrypted data".getBytes());
    assertThat(response.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(response.getAffectedTransactions()).isEmpty();
    assertThat(response.getExecHash()).isEmpty();
    verify(payloadDigest, times(2)).digest(any());
    verify(enclave).getPublicKeys();
    verify(enclave, times(2)).unencryptTransaction(samplePayload, singleRecipient);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Test(org.junit.Test)

Example 5 with ReceiveResponse

use of com.quorum.tessera.transaction.ReceiveResponse in project tessera by ConsenSys.

the class BesuTransactionResourceTest method receive.

@Test
public void receive() {
    String key = Base64.getEncoder().encodeToString("KEY".getBytes());
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(key);
    String recipient = Base64.getEncoder().encodeToString("Bobby Sixkiller".getBytes());
    receiveRequest.setTo(recipient);
    ReceiveResponse receiveResponse = mock(ReceiveResponse.class);
    when(receiveResponse.getAffectedTransactions()).thenReturn(Set.of());
    when(receiveResponse.getUnencryptedTransactionData()).thenReturn("Result".getBytes());
    when(receiveResponse.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(receiveResponse.sender()).thenReturn(PublicKey.from("sender".getBytes()));
    when(receiveResponse.getPrivacyGroupId()).thenReturn(Optional.of(PrivacyGroup.Id.fromBytes("group".getBytes())));
    when(transactionManager.receive(any(com.quorum.tessera.transaction.ReceiveRequest.class))).thenReturn(receiveResponse);
    BesuTransactionResource resource = new BesuTransactionResource(transactionManager, privacyGroupManager);
    final Response result = resource.receive(receiveRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    BesuReceiveResponse resultResponse = (BesuReceiveResponse) result.getEntity();
    assertThat(resultResponse.getPayload()).isEqualTo("Result".getBytes());
    assertThat(resultResponse.getSenderKey()).isEqualTo(PublicKey.from("sender".getBytes()).encodeToBase64());
    assertThat(resultResponse.getPrivacyGroupId()).isEqualTo(PublicKey.from("group".getBytes()).encodeToBase64());
    verify(transactionManager).receive(any(com.quorum.tessera.transaction.ReceiveRequest.class));
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Aggregations

ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)9 Test (org.junit.Test)8 Response (jakarta.ws.rs.core.Response)7 SendResponse (com.quorum.tessera.api.SendResponse)5 PublicKey (com.quorum.tessera.encryption.PublicKey)5 MessageHash (com.quorum.tessera.data.MessageHash)3 BesuReceiveResponse (com.quorum.tessera.api.BesuReceiveResponse)2 PayloadDecryptRequest (com.quorum.tessera.api.PayloadDecryptRequest)2 PayloadEncryptResponse (com.quorum.tessera.api.PayloadEncryptResponse)2 ReceiveRequest (com.quorum.tessera.api.ReceiveRequest)2 Base64 (java.util.Base64)2 RecipientKeyNotFoundException (com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)1