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