use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException in project tessera by ConsenSys.
the class TransactionManagerTest method receiveNoRecipientKeyFound.
@Test
public void receiveNoRecipientKeyFound() {
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);
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
PublicKey publicKey = mock(PublicKey.class);
when(enclave.getPublicKeys()).thenReturn(Collections.singleton(publicKey));
when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenThrow(EncryptorException.class);
try {
transactionManager.receive(receiveRequest);
failBecauseExceptionWasNotThrown(RecipientKeyNotFoundException.class);
} catch (RecipientKeyNotFoundException ex) {
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave).getPublicKeys();
verify(enclave).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
}
}
use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException 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();
}
use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException 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.RecipientKeyNotFoundException 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;
}
Aggregations