use of com.quorum.tessera.enclave.EncodedPayload 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.enclave.EncodedPayload in project tessera by ConsenSys.
the class RecoveryImplTest method testSyncPsvTransactionOnlySentOnce.
@Test
public void testSyncPsvTransactionOnlySentOnce() {
StagingTransaction version1 = mock(StagingTransaction.class);
StagingTransaction version2 = mock(StagingTransaction.class);
StagingTransaction anotherTx = mock(StagingTransaction.class);
when(version1.getHash()).thenReturn("TXN1");
when(version2.getHash()).thenReturn("TXN1");
when(anotherTx.getHash()).thenReturn("TXN2");
EncodedPayload encodedPayload = mock(EncodedPayload.class);
EncodedPayload encodedPayload2 = mock(EncodedPayload.class);
when(version1.getEncodedPayload()).thenReturn(encodedPayload);
when(version2.getEncodedPayload()).thenReturn(encodedPayload);
when(anotherTx.getEncodedPayload()).thenReturn(encodedPayload2);
when(version1.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(version2.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(anotherTx.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
when(stagingEntityDAO.retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt())).thenReturn(List.of(version1, version2, anotherTx));
when(stagingEntityDAO.countAll()).thenReturn(3L);
when(transactionManager.storePayload(any())).thenThrow(PrivacyViolationException.class);
RecoveryResult result = recovery.sync();
assertThat(result).isEqualTo(RecoveryResult.FAILURE);
verify(stagingEntityDAO).retrieveTransactionBatchOrderByStageAndHash(anyInt(), anyInt());
verify(stagingEntityDAO, times(2)).countAll();
verify(transactionManager).storePayload(encodedPayload);
verify(transactionManager).storePayload(encodedPayload2);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class PreparePayloadForRecipient method execute.
@Override
public boolean execute(final BatchWorkflowContext event) {
final EncodedPayload payload = event.getEncodedPayload();
final PublicKey targetPublicKey = event.getRecipientKey();
if (!Objects.equals(payload.getSenderKey(), targetPublicKey)) {
// we are the sender, so need to format the payload for the recipient
// which is: for PSV, all recipients and one box, or just one box and one recipient
final EncodedPayload adjustedPayload = EncodedPayload.Builder.forRecipient(payload, targetPublicKey).build();
event.setPayloadsToPublish(Set.of(adjustedPayload));
return true;
}
// we have the keys, so just matching keys to boxes
if (!payload.getRecipientKeys().isEmpty()) {
final int numberOfBoxes = payload.getRecipientBoxes().size();
// we know the recipients, we just need to format them per recipient we have
// but only for ones we have boxes for
final Set<EncodedPayload> formattedPayloads = payload.getRecipientKeys().stream().filter(key -> payload.getRecipientKeys().indexOf(key) < numberOfBoxes).map(key -> EncodedPayload.Builder.forRecipient(payload, key).build()).collect(Collectors.toSet());
event.setPayloadsToPublish(formattedPayloads);
return true;
}
// We only have boxes, no recipients (pre-1.0 standard private)
// Create individual payloads with each box and search for each box's key.
final Set<EncodedPayload> formattedPayloads = payload.getRecipientBoxes().stream().map(box -> EncodedPayload.Builder.from(payload).withRecipientBoxes(List.of(box.getData())).build()).collect(Collectors.toSet());
event.setPayloadsToPublish(formattedPayloads);
return true;
}
use of com.quorum.tessera.enclave.EncodedPayload 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.enclave.EncodedPayload 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));
}
Aggregations