Search in sources :

Example 41 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class SendSignedRequestTest method build.

@Test
public void build() {
    byte[] signedData = "SignedData".getBytes();
    List<PublicKey> recipients = List.of(mock(PublicKey.class));
    MessageHash affectedTransaction = mock(MessageHash.class);
    PrivacyGroup.Id groupId = mock(PrivacyGroup.Id.class);
    SendSignedRequest request = SendSignedRequest.Builder.create().withSignedData(signedData).withExecHash("Exehash".getBytes()).withRecipients(recipients).withAffectedContractTransactions(Set.of(affectedTransaction)).withPrivacyMode(PrivacyMode.PRIVATE_STATE_VALIDATION).withPrivacyGroupId(groupId).build();
    assertThat(request).isNotNull();
    assertThat(request.getAffectedContractTransactions()).containsOnly(affectedTransaction);
    assertThat(request.getSignedData()).containsExactly(signedData);
    assertThat(request.getExecHash()).containsExactly("Exehash".getBytes());
    assertThat(request.getRecipients()).hasSize(1).containsAll(recipients);
    assertThat(request.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
    assertThat(request.getPrivacyGroupId()).isPresent().get().isSameAs(groupId);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 42 with MessageHash

use of com.quorum.tessera.data.MessageHash in project tessera by ConsenSys.

the class SearchRecipientKeyForPayloadTest method executeHandleEnclaveExceptions.

@Test
public void executeHandleEnclaveExceptions() {
    final List<Class<? extends Exception>> handledExceptionTypes = List.of(EnclaveNotAvailableException.class, IndexOutOfBoundsException.class, EncryptorException.class);
    handledExceptionTypes.forEach(t -> {
        final BatchWorkflowContext workflowEvent = new BatchWorkflowContext();
        final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
        encryptedTransaction.setHash(new MessageHash("sampleHash".getBytes()));
        workflowEvent.setEncryptedTransaction(encryptedTransaction);
        final EncodedPayload encodedPayload = EncodedPayload.Builder.create().build();
        workflowEvent.setPayloadsToPublish(Set.of(encodedPayload));
        final PublicKey publicKey = PublicKey.from("sample-public-key".getBytes());
        when(enclave.getPublicKeys()).thenReturn(Set.of(publicKey));
        when(enclave.unencryptTransaction(encodedPayload, publicKey)).thenThrow(t);
        final Throwable throwable = catchThrowable(() -> searchRecipientKeyForPayload.execute(workflowEvent));
        assertThat(throwable).isInstanceOf(RecipientKeyNotFoundException.class).hasMessage("No key found as recipient of message c2FtcGxlSGFzaA==");
        verify(enclave).unencryptTransaction(encodedPayload, publicKey);
        verify(enclave).getPublicKeys();
        verifyNoMoreInteractions(enclave);
        reset(enclave);
    });
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) EnclaveNotAvailableException(com.quorum.tessera.enclave.EnclaveNotAvailableException) EncryptorException(com.quorum.tessera.encryption.EncryptorException) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 43 with MessageHash

use of com.quorum.tessera.data.MessageHash 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 44 with MessageHash

use of com.quorum.tessera.data.MessageHash 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 45 with MessageHash

use of com.quorum.tessera.data.MessageHash 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

MessageHash (com.quorum.tessera.data.MessageHash)81 PublicKey (com.quorum.tessera.encryption.PublicKey)56 Test (org.junit.Test)51 Response (jakarta.ws.rs.core.Response)44 Operation (io.swagger.v3.oas.annotations.Operation)21 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)21 SendResponse (com.quorum.tessera.api.SendResponse)17 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)16 SendRequest (com.quorum.tessera.api.SendRequest)15 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)15 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)15 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)13 java.util (java.util)13 Collectors (java.util.stream.Collectors)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)12 TransactionManager (com.quorum.tessera.transaction.TransactionManager)12 Tag (io.swagger.v3.oas.annotations.tags.Tag)11 Valid (jakarta.validation.Valid)11