Search in sources :

Example 1 with ResendResponse

use of com.quorum.tessera.recovery.resend.ResendResponse in project tessera by ConsenSys.

the class LegacyResendManagerImpl method resendIndividual.

protected ResendResponse resendIndividual(final PublicKey targetResendKey, final MessageHash messageHash) {
    final EncryptedTransaction encryptedTransaction = encryptedTransactionDAO.retrieveByHash(messageHash).orElseThrow(() -> new TransactionNotFoundException("Message with hash " + messageHash + " was not found"));
    final EncodedPayload payload = encryptedTransaction.getPayload();
    if (payload.getPrivacyMode() != PrivacyMode.STANDARD_PRIVATE) {
        throw new EnhancedPrivacyNotSupportedException("Cannot resend enhanced privacy transaction in legacy resend");
    }
    if (!Objects.equals(payload.getSenderKey(), targetResendKey)) {
        final EncodedPayload formattedPayload = EncodedPayload.Builder.forRecipient(payload, targetResendKey).build();
        return ResendResponse.Builder.create().withPayload(formattedPayload).build();
    }
    // split all the boxes out into their own payload
    final Set<EncodedPayload> allTxns = payload.getRecipientBoxes().stream().map(box -> EncodedPayload.Builder.from(payload).withNewRecipientKeys(Collections.emptyList()).withRecipientBoxes(List.of(box.getData())).build()).collect(Collectors.toSet());
    final BatchWorkflowContext context = new BatchWorkflowContext();
    context.setPayloadsToPublish(allTxns);
    context.setEncryptedTransaction(encryptedTransaction);
    new SearchRecipientKeyForPayload(enclave).execute(context);
    final EncodedPayload.Builder builder = EncodedPayload.Builder.from(payload).withNewRecipientKeys(new ArrayList<>()).withRecipientBoxes(new ArrayList<>());
    context.getPayloadsToPublish().forEach(formattedPayload -> {
        builder.withRecipientKey(formattedPayload.getRecipientKeys().get(0));
        builder.withRecipientBox(formattedPayload.getRecipientBoxes().get(0).getData());
    });
    return ResendResponse.Builder.create().withPayload(builder.build()).build();
}
Also used : IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) java.util(java.util) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Collectors(java.util.stream.Collectors) com.quorum.tessera.recovery.workflow(com.quorum.tessera.recovery.workflow) Discovery(com.quorum.tessera.discovery.Discovery) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) PayloadPublisher(com.quorum.tessera.transaction.publish.PayloadPublisher) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException) Enclave(com.quorum.tessera.enclave.Enclave) MessageHash(com.quorum.tessera.data.MessageHash) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) EnhancedPrivacyNotSupportedException(com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)

Example 2 with ResendResponse

use of com.quorum.tessera.recovery.resend.ResendResponse 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 3 with ResendResponse

use of com.quorum.tessera.recovery.resend.ResendResponse in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method performResendAll.

@Test
public void performResendAll() {
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.ALL).withRecipient(targetResendKey).build();
    // Not bothered about going through the process, just make sure they are all loaded from the
    // database
    // We are not testing the workflow itself, only that the workflow gets the right amount of
    // transactions
    when(dao.transactionCount()).thenReturn(2L);
    when(dao.retrieveTransactions(0, 1)).thenReturn(List.of(new EncryptedTransaction()));
    when(dao.retrieveTransactions(1, 1)).thenReturn(List.of(new EncryptedTransaction()));
    final ResendResponse response = resendManager.resend(request);
    assertThat(response).isNotNull();
    assertThat(response.getPayload()).isNull();
    verify(enclave, times(2)).status();
    verify(dao).transactionCount();
    verify(dao).retrieveTransactions(0, 1);
    verify(dao).retrieveTransactions(1, 1);
}
Also used : ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 4 with ResendResponse

use of com.quorum.tessera.recovery.resend.ResendResponse in project tessera by ConsenSys.

the class LegacyResendManagerImplTest method targetKeyIsNotSenderOfTransaction.

@Test
public void targetKeyIsNotSenderOfTransaction() {
    final MessageHash txHash = new MessageHash("sample-hash".getBytes());
    final PublicKey targetResendKey = PublicKey.from("target".getBytes());
    final EncodedPayload nonSPPayload = EncodedPayload.Builder.create().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));
    final ResendResponse response;
    try (var mockStatic = mockStatic(EncodedPayload.Builder.class)) {
        EncodedPayload.Builder builder = mock(EncodedPayload.Builder.class);
        mockStatic.when(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey)).thenReturn(builder);
        when(builder.build()).thenReturn(nonSPPayload);
        response = resendManager.resend(request);
        mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey));
    }
    assertThat(response).isNotNull();
    assertThat(response.getPayload()).isEqualTo(nonSPPayload);
    verify(dao).retrieveByHash(txHash);
}
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)

Aggregations

EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)4 PublicKey (com.quorum.tessera.encryption.PublicKey)4 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)4 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)4 MessageHash (com.quorum.tessera.data.MessageHash)3 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)3 Test (org.junit.Test)3 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)1 Discovery (com.quorum.tessera.discovery.Discovery)1 Enclave (com.quorum.tessera.enclave.Enclave)1 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)1 com.quorum.tessera.recovery.workflow (com.quorum.tessera.recovery.workflow)1 EnhancedPrivacyNotSupportedException (com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)1 TransactionNotFoundException (com.quorum.tessera.transaction.exception.TransactionNotFoundException)1 PayloadPublisher (com.quorum.tessera.transaction.publish.PayloadPublisher)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1