Search in sources :

Example 31 with EncodedPayload

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();
}
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 32 with EncodedPayload

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);
}
Also used : StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) RecoveryResult(com.quorum.tessera.recovery.RecoveryResult) Test(org.junit.Test)

Example 33 with EncodedPayload

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;
}
Also used : Objects(java.util.Objects) PublicKey(com.quorum.tessera.encryption.PublicKey) List(java.util.List) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Set(java.util.Set) Collectors(java.util.stream.Collectors) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 34 with EncodedPayload

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;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Collectors(java.util.stream.Collectors) EnclaveNotAvailableException(com.quorum.tessera.enclave.EnclaveNotAvailableException) Objects(java.util.Objects) EncryptorException(com.quorum.tessera.encryption.EncryptorException) List(java.util.List) Enclave(com.quorum.tessera.enclave.Enclave) Optional(java.util.Optional) MessageHash(com.quorum.tessera.data.MessageHash) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) List(java.util.List) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Example 35 with EncodedPayload

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));
}
Also used : ResendResponse(com.quorum.tessera.recovery.resend.ResendResponse) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) ResendRequest(com.quorum.tessera.recovery.resend.ResendRequest) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Aggregations

EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)91 Test (org.junit.Test)60 PublicKey (com.quorum.tessera.encryption.PublicKey)50 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)23 Response (jakarta.ws.rs.core.Response)20 MessageHash (com.quorum.tessera.data.MessageHash)13 Collectors (java.util.stream.Collectors)12 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)11 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)8 Recipient (com.quorum.tessera.partyinfo.node.Recipient)8 java.util (java.util)8 Invocation (jakarta.ws.rs.client.Invocation)6 WebTarget (jakarta.ws.rs.client.WebTarget)6 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)5 PrivacyMetadata (com.quorum.tessera.enclave.PrivacyMetadata)5 RecipientBox (com.quorum.tessera.enclave.RecipientBox)5 TxHash (com.quorum.tessera.enclave.TxHash)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)5