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));
}
}
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();
}
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();
}
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();
}
}
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();
}
Aggregations