Search in sources :

Example 11 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class SearchRecipientKeyForPayload method execute.

@Override
public boolean execute(BatchWorkflowContext event) {
    final Set<EncodedPayload> encodedPayloads = event.getPayloadsToPublish();
    final boolean keysAreEmpty = encodedPayloads.stream().map(EncodedPayload::getRecipientKeys).allMatch(List::isEmpty);
    // if the payload has someone in it, then we are good
    if (!keysAreEmpty) {
        return true;
    }
    // the keys are not present, so we need to search for the relevant recipient
    final Set<EncodedPayload> adjustedPayloads = encodedPayloads.stream().map(payload -> {
        // this is a pre-PE tx, so find the recipient key
        final PublicKey recipientKey = searchForRecipientKey(payload).orElseThrow(() -> {
            final EncryptedTransaction encryptedTransaction = event.getEncryptedTransaction();
            final MessageHash hash = encryptedTransaction.getHash();
            final String message = String.format("No key found as recipient of message %s", hash);
            return new RecipientKeyNotFoundException(message);
        });
        return EncodedPayload.Builder.from(payload).withRecipientKeys(List.of(recipientKey)).build();
    }).collect(Collectors.toSet());
    event.setPayloadsToPublish(adjustedPayloads);
    return true;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Collectors(java.util.stream.Collectors) EnclaveNotAvailableException(com.quorum.tessera.enclave.EnclaveNotAvailableException) Objects(java.util.Objects) EncryptorException(com.quorum.tessera.encryption.EncryptorException) List(java.util.List) Enclave(com.quorum.tessera.enclave.Enclave) Optional(java.util.Optional) MessageHash(com.quorum.tessera.data.MessageHash) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) List(java.util.List) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Example 12 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class BatchWorkflowFactoryImplTest method workflowExecutedReturnFalse.

@Test
public void workflowExecutedReturnFalse() {
    BatchWorkflowFactoryImpl batchWorkflowFactory = new BatchWorkflowFactoryImpl(enclave, discovery, resendBatchPublisher);
    BatchWorkflow batchWorkflow = batchWorkflowFactory.create(999L);
    assertThat(batchWorkflow).isNotNull();
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    PublicKey publicKey = mock(PublicKey.class);
    batchWorkflowContext.setRecipientKey(publicKey);
    EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
    batchWorkflowContext.setEncryptedTransaction(encryptedTransaction);
    batchWorkflowContext.setEncodedPayload(mock(EncodedPayload.class));
    when(enclave.status()).thenReturn(Service.Status.STARTED);
    assertThat(batchWorkflow.execute(batchWorkflowContext)).isFalse();
    assertThat(batchWorkflow.getPublishedMessageCount()).isZero();
    verify(enclave).status();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 13 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method targetIsSenderOfTransaction.

@Test
public void targetIsSenderOfTransaction() {
    final MessageHash txHash = new MessageHash("sample-hash".getBytes());
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final PublicKey localRecipientKey = PublicKey.from("local-recipient".getBytes());
    final EncodedPayload nonSPPayload = EncodedPayload.Builder.create().withSenderKey(targetResendKey).withRecipientBox("testBox".getBytes()).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).build();
    final EncryptedTransaction databaseTx = new EncryptedTransaction();
    databaseTx.setPayload(nonSPPayload);
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.INDIVIDUAL).withHash(txHash).withRecipient(targetResendKey).build();
    when(dao.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.of(databaseTx));
    when(enclave.getPublicKeys()).thenReturn(Set.of(localRecipientKey));
    when(enclave.unencryptTransaction(any(), eq(localRecipientKey))).thenReturn(new byte[0]);
    final ResendResponse response = resendManager.resend(request);
    final EncodedPayload expected = EncodedPayload.Builder.from(nonSPPayload).withRecipientKey(localRecipientKey).build();
    assertThat(response).isNotNull();
    assertThat(response.getPayload()).isEqualToComparingFieldByFieldRecursively(expected);
    verify(dao).retrieveByHash(txHash);
    verify(enclave).getPublicKeys();
    verify(enclave).unencryptTransaction(any(), eq(localRecipientKey));
}
Also used : ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 14 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class ResendManagerImplTest method storePayloadAsSenderWhenTxIsPresent.

@Test
public void storePayloadAsSenderWhenTxIsPresent() {
    final PublicKey senderKey = PublicKey.from("SENDER".getBytes());
    final PublicKey recipientKey1 = PublicKey.from("RECIPIENT-KEY1".getBytes());
    final RecipientBox recipientBox1 = RecipientBox.from("BOX1".getBytes());
    final PublicKey recipientKey2 = PublicKey.from("RECIPIENT-KEY2".getBytes());
    final RecipientBox recipientBox2 = RecipientBox.from("BOX2".getBytes());
    final EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
    when(encodedPayload.getSenderKey()).thenReturn(senderKey);
    when(encodedPayload.getRecipientKeys()).thenReturn(List.of(recipientKey2));
    when(encodedPayload.getRecipientBoxes()).thenReturn(List.of(recipientBox2));
    final EncodedPayload existingEncodedPayload = mock(EncodedPayload.class);
    when(existingEncodedPayload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
    when(existingEncodedPayload.getSenderKey()).thenReturn(senderKey);
    when(existingEncodedPayload.getRecipientKeys()).thenReturn(List.of(recipientKey1));
    when(existingEncodedPayload.getRecipientBoxes()).thenReturn(List.of(recipientBox1));
    final EncryptedTransaction et = new EncryptedTransaction(mock(MessageHash.class), existingEncodedPayload);
    when(enclave.getPublicKeys()).thenReturn(singleton(senderKey));
    when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.of(et));
    resendManager.acceptOwnMessage(encodedPayload);
    assertThat(encodedPayload.getRecipientKeys()).containsExactly(recipientKey2);
    assertThat(encodedPayload.getRecipientBoxes()).containsExactly(recipientBox2);
    ArgumentCaptor<EncryptedTransaction> updatedTxCaptor = ArgumentCaptor.forClass(EncryptedTransaction.class);
    verify(encryptedTransactionDAO).update(updatedTxCaptor.capture());
    final EncodedPayload updated = updatedTxCaptor.getValue().getPayload();
    // Check recipients are being added
    assertThat(updated.getRecipientKeys()).hasSize(2).containsExactlyInAnyOrder(recipientKey1, recipientKey2);
    // Check boxes are being added
    assertThat(updated.getRecipientBoxes()).hasSize(2);
    verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave).getPublicKeys();
    verify(enclave).unencryptTransaction(encodedPayload, senderKey);
    verify(enclave).unencryptTransaction(existingEncodedPayload, senderKey);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 15 with EncryptedTransaction

use of com.quorum.tessera.data.EncryptedTransaction in project tessera by ConsenSys.

the class ResendManagerImplTest method storePayloadAsSenderWhenTxIsNotPresent.

@Test
public void storePayloadAsSenderWhenTxIsNotPresent() {
    final PublicKey senderKey = PublicKey.from("SENDER".getBytes());
    // A legacy payload has empty recipient and box
    final EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getCipherText()).thenReturn("CIPHERTEXT".getBytes());
    when(encodedPayload.getSenderKey()).thenReturn(senderKey);
    final byte[] newEncryptedMasterKey = "newbox".getBytes();
    when(enclave.getPublicKeys()).thenReturn(singleton(senderKey));
    when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    when(enclave.createNewRecipientBox(any(), any())).thenReturn(newEncryptedMasterKey);
    resendManager.acceptOwnMessage(encodedPayload);
    ArgumentCaptor<EncryptedTransaction> updatedTxCaptor = ArgumentCaptor.forClass(EncryptedTransaction.class);
    verify(encryptedTransactionDAO).save(updatedTxCaptor.capture());
    final EncodedPayload updatedPayload = updatedTxCaptor.getValue().getPayload();
    assertThat(updatedPayload).isNotNull();
    // The sender was added
    assertThat(updatedPayload.getRecipientKeys()).containsExactly(senderKey);
    // New box was created
    assertThat(updatedPayload.getRecipientBoxes()).containsExactly(RecipientBox.from(newEncryptedMasterKey));
    verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    verify(enclave).getPublicKeys();
    verify(enclave).createNewRecipientBox(any(), any());
    verify(enclave).unencryptTransaction(encodedPayload, senderKey);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Aggregations

EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)32 Test (org.junit.Test)24 PublicKey (com.quorum.tessera.encryption.PublicKey)21 MessageHash (com.quorum.tessera.data.MessageHash)19 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)5 BatchWorkflow (com.quorum.tessera.recovery.workflow.BatchWorkflow)5 BatchWorkflowContext (com.quorum.tessera.recovery.workflow.BatchWorkflowContext)5 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)4 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 After (org.junit.After)4 Before (org.junit.Before)4 Base64Codec (com.quorum.tessera.base64.Base64Codec)3 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)3 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)3 com.quorum.tessera.enclave (com.quorum.tessera.enclave)3 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)3