Search in sources :

Example 1 with TransactionNotFoundException

use of com.quorum.tessera.transaction.exception.TransactionNotFoundException in project tessera by ConsenSys.

the class TransactionManagerTest method receiveNoTransactionInDatabase.

@Test
public void receiveNoTransactionInDatabase() {
    PublicKey recipient = PublicKey.from("recipient".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn("KEY".getBytes());
    ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
    when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
    when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
    EncodedPayload payload = mock(EncodedPayload.class);
    when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    try {
        transactionManager.receive(receiveRequest);
        failBecauseExceptionWasNotThrown(TransactionNotFoundException.class);
    } catch (TransactionNotFoundException ex) {
        verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) Test(org.junit.Test)

Example 2 with TransactionNotFoundException

use of com.quorum.tessera.transaction.exception.TransactionNotFoundException in project tessera by ConsenSys.

the class TransactionManagerImpl method receive.

@Override
public ReceiveResponse receive(ReceiveRequest request) {
    final MessageHash hash = request.getTransactionHash();
    LOGGER.info("Lookup transaction {}", hash);
    if (request.isRaw()) {
        final EncryptedRawTransaction encryptedRawTransaction = encryptedRawTransactionDAO.retrieveByHash(hash).orElseThrow(() -> new TransactionNotFoundException("Raw Message with hash " + hash + " was not found"));
        final PublicKey senderKey = PublicKey.from(encryptedRawTransaction.getSender());
        final RawTransaction rawTransaction = new RawTransaction(encryptedRawTransaction.getEncryptedPayload(), encryptedRawTransaction.getEncryptedKey(), new Nonce(encryptedRawTransaction.getNonce()), senderKey);
        final byte[] response = enclave.unencryptRawPayload(rawTransaction);
        return ReceiveResponse.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withUnencryptedTransactionData(response).withManagedParties(Set.of(senderKey)).withSender(senderKey).build();
    }
    final EncryptedTransaction encryptedTransaction = encryptedTransactionDAO.retrieveByHash(hash).orElseThrow(() -> new TransactionNotFoundException("Message with hash " + hash + " was not found"));
    final EncodedPayload payload = Optional.of(encryptedTransaction).map(EncryptedTransaction::getPayload).orElseThrow(() -> new IllegalStateException("Unable to decode previously encoded payload"));
    PublicKey recipientKey = request.getRecipient().orElse(searchForRecipientKey(payload).orElseThrow(() -> new RecipientKeyNotFoundException("No suitable recipient keys found to decrypt payload for : " + hash)));
    byte[] unencryptedTransactionData = enclave.unencryptTransaction(payload, recipientKey);
    Set<MessageHash> affectedTransactions = payload.getAffectedContractTransactions().keySet().stream().map(TxHash::getBytes).map(MessageHash::new).collect(Collectors.toSet());
    Set<PublicKey> managedParties = new HashSet<>();
    if (payload.getRecipientKeys().isEmpty()) {
        // legacy tx
        for (RecipientBox box : payload.getRecipientBoxes()) {
            EncodedPayload singleBoxPayload = EncodedPayload.Builder.from(payload).withRecipientBoxes(List.of(box.getData())).build();
            Optional<PublicKey> possibleRecipient = searchForRecipientKey(singleBoxPayload);
            possibleRecipient.ifPresent(managedParties::add);
        }
    } else {
        managedParties = enclave.getPublicKeys().stream().filter(payload.getRecipientKeys()::contains).collect(Collectors.toSet());
    }
    final ReceiveResponse.Builder responseBuilder = ReceiveResponse.Builder.create();
    payload.getPrivacyGroupId().ifPresent(responseBuilder::withPrivacyGroupId);
    return responseBuilder.withUnencryptedTransactionData(unencryptedTransactionData).withPrivacyMode(payload.getPrivacyMode()).withAffectedTransactions(affectedTransactions).withExecHash(payload.getExecHash()).withManagedParties(managedParties).withSender(payload.getSenderKey()).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) Nonce(com.quorum.tessera.encryption.Nonce) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)

Example 3 with TransactionNotFoundException

use of com.quorum.tessera.transaction.exception.TransactionNotFoundException 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 4 with TransactionNotFoundException

use of com.quorum.tessera.transaction.exception.TransactionNotFoundException in project tessera by ConsenSys.

the class TransactionManagerTest method sendSignedTransactionNoRawTransactionFoundException.

@Test
public void sendSignedTransactionNoRawTransactionFoundException() {
    when(encryptedRawTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
    PublicKey receiver = PublicKey.from("RECEIVER".getBytes());
    SendSignedRequest sendSignedRequest = mock(SendSignedRequest.class);
    when(sendSignedRequest.getRecipients()).thenReturn(List.of(receiver));
    when(sendSignedRequest.getSignedData()).thenReturn("HASH".getBytes());
    try {
        transactionManager.sendSignedTransaction(sendSignedRequest);
        failBecauseExceptionWasNotThrown(TransactionNotFoundException.class);
    } catch (TransactionNotFoundException ex) {
        verify(encryptedRawTransactionDAO).retrieveByHash(any(MessageHash.class));
        verify(enclave).getForwardingKeys();
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) Test(org.junit.Test)

Example 5 with TransactionNotFoundException

use of com.quorum.tessera.transaction.exception.TransactionNotFoundException in project tessera by ConsenSys.

the class TransactionManagerImpl method sendSignedTransaction.

@Override
public SendResponse sendSignedTransaction(final SendSignedRequest sendRequest) {
    final List<PublicKey> recipientList = new ArrayList<>(sendRequest.getRecipients());
    recipientList.addAll(enclave.getForwardingKeys());
    final MessageHash messageHash = new MessageHash(sendRequest.getSignedData());
    EncryptedRawTransaction encryptedRawTransaction = encryptedRawTransactionDAO.retrieveByHash(messageHash).orElseThrow(() -> new TransactionNotFoundException("Raw Transaction with hash " + messageHash + " was not found"));
    recipientList.add(PublicKey.from(encryptedRawTransaction.getSender()));
    final PrivacyMode privacyMode = sendRequest.getPrivacyMode();
    final byte[] execHash = sendRequest.getExecHash();
    final List<AffectedTransaction> affectedContractTransactions = privacyHelper.findAffectedContractTransactionsFromSendRequest(sendRequest.getAffectedContractTransactions());
    privacyHelper.validateSendRequest(privacyMode, recipientList, affectedContractTransactions, sendRequest.getMandatoryRecipients());
    final List<PublicKey> recipientListNoDuplicate = recipientList.stream().distinct().collect(Collectors.toList());
    final PrivacyMetadata.Builder privacyMetaDataBuilder = PrivacyMetadata.Builder.create().withPrivacyMode(privacyMode).withAffectedTransactions(affectedContractTransactions).withExecHash(execHash).withMandatoryRecipients(sendRequest.getMandatoryRecipients());
    sendRequest.getPrivacyGroupId().ifPresent(privacyMetaDataBuilder::withPrivacyGroupId);
    final EncodedPayload payload = enclave.encryptPayload(encryptedRawTransaction.toRawTransaction(), recipientListNoDuplicate, privacyMetaDataBuilder.build());
    final EncryptedTransaction newTransaction = new EncryptedTransaction(messageHash, payload);
    final Set<PublicKey> managedPublicKeys = enclave.getPublicKeys();
    final Set<PublicKey> managedParties = recipientListNoDuplicate.stream().filter(managedPublicKeys::contains).collect(Collectors.toSet());
    final List<PublicKey> recipientListRemotesOnly = recipientListNoDuplicate.stream().filter(not(managedPublicKeys::contains)).collect(Collectors.toList());
    this.encryptedTransactionDAO.save(newTransaction, () -> {
        batchPayloadPublisher.publishPayload(payload, recipientListRemotesOnly);
        return null;
    });
    return SendResponse.Builder.create().withMessageHash(messageHash).withManagedParties(managedParties).withSender(PublicKey.from(encryptedRawTransaction.getSender())).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException)

Aggregations

TransactionNotFoundException (com.quorum.tessera.transaction.exception.TransactionNotFoundException)6 PublicKey (com.quorum.tessera.encryption.PublicKey)5 Test (org.junit.Test)3 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)1 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)1 MessageHash (com.quorum.tessera.data.MessageHash)1 Discovery (com.quorum.tessera.discovery.Discovery)1 Enclave (com.quorum.tessera.enclave.Enclave)1 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)1 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)1 Nonce (com.quorum.tessera.encryption.Nonce)1 ResendRequest (com.quorum.tessera.recovery.resend.ResendRequest)1 ResendResponse (com.quorum.tessera.recovery.resend.ResendResponse)1 com.quorum.tessera.recovery.workflow (com.quorum.tessera.recovery.workflow)1 EnhancedPrivacyNotSupportedException (com.quorum.tessera.transaction.exception.EnhancedPrivacyNotSupportedException)1 RecipientKeyNotFoundException (com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)1 PayloadPublisher (com.quorum.tessera.transaction.publish.PayloadPublisher)1 Response (jakarta.ws.rs.core.Response)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1