Search in sources :

Example 51 with MessageHash

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

the class ReceiveRequestTest method buildWithRaw.

@Test
public void buildWithRaw() {
    MessageHash messageHash = mock(MessageHash.class);
    PublicKey recipient = mock(PublicKey.class);
    ReceiveRequest req = ReceiveRequest.Builder.create().withTransactionHash(messageHash).withRecipient(recipient).withRaw(true).build();
    assertThat(req).isNotNull();
    assertThat(req.getTransactionHash()).isNotNull().isSameAs(messageHash);
    assertThat(req.getRecipient()).containsSame(recipient);
    assertThat(req.isRaw()).isTrue();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 52 with MessageHash

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

the class ReceiveRequestTest method buildWithTransactionHash.

@Test
public void buildWithTransactionHash() {
    MessageHash messageHash = mock(MessageHash.class);
    ReceiveRequest result = ReceiveRequest.Builder.create().withTransactionHash(messageHash).build();
    assertThat(result).isNotNull();
    assertThat(result.getTransactionHash()).isNotNull().isSameAs(messageHash);
    assertThat(result.getRecipient()).isNotPresent();
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 53 with MessageHash

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

the class SendResponseTest method fromTransactionHash.

@Test
public void fromTransactionHash() {
    MessageHash transactionHash = mock(MessageHash.class);
    Set<PublicKey> arbitraryManagedKeys = Set.of(PublicKey.from("arbitrary key".getBytes()));
    SendResponse response = SendResponse.Builder.create().withMessageHash(transactionHash).withManagedParties(arbitraryManagedKeys).withSender(PublicKey.from("sender".getBytes())).build();
    assertThat(response).isNotNull();
    assertThat(response.getTransactionHash()).isSameAs(transactionHash);
    assertThat(response.getManagedParties()).isSameAs(arbitraryManagedKeys);
    assertThat(response.getSender()).isEqualTo(PublicKey.from("sender".getBytes()));
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 54 with MessageHash

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

the class StoreRawResponseTest method createFromTransactionHash.

@Test
public void createFromTransactionHash() {
    MessageHash transactionHash = mock(MessageHash.class);
    StoreRawResponse result = StoreRawResponse.from(transactionHash);
    assertThat(result).isNotNull();
    assertThat(result.getHash()).isSameAs(transactionHash);
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 55 with MessageHash

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

the class ResendManagerImpl method acceptOwnMessage.

public synchronized void acceptOwnMessage(final EncodedPayload payload) {
    // check the payload can be decrypted to ensure it isn't rubbish being sent to us
    final byte[] newDecrypted;
    if (payload.getPrivacyMode() == PrivacyMode.PRIVATE_STATE_VALIDATION) {
        // if it is PSV, then the enclave would be expected our own box to be available,
        // but it isn't (since we are rebuilding)
        // since we only want to decrypt the tx and not worry about all the other pieces,
        // treat it just like a standard private tx, and remove the other recipients
        final EncodedPayload tempPayload = EncodedPayload.Builder.from(payload).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withExecHash(new byte[0]).withNewRecipientKeys(List.of(payload.getRecipientKeys().get(0))).withRecipientBoxes(List.of(payload.getRecipientBoxes().get(0).getData())).build();
        newDecrypted = enclave.unencryptTransaction(tempPayload, payload.getSenderKey());
    } else {
        newDecrypted = enclave.unencryptTransaction(payload, payload.getSenderKey());
    }
    final MessageHash transactionHash = Optional.of(payload).map(EncodedPayload::getCipherText).map(payloadDigest::digest).map(MessageHash::new).get();
    final PublicKey sender = payload.getSenderKey();
    if (!enclave.getPublicKeys().contains(sender)) {
        throw new IllegalArgumentException("Message " + transactionHash.toString() + " does not have one the nodes own keys as a sender");
    }
    // this is a tx which we created
    final Optional<EncryptedTransaction> tx = this.encryptedTransactionDAO.retrieveByHash(transactionHash);
    if (tx.isPresent()) {
        // we just need to add the recipient
        final EncodedPayload existing = tx.get().getPayload();
        // this is the easiest way to tell if a recipient has already been included
        for (RecipientBox existingBox : existing.getRecipientBoxes()) {
            if (Objects.equals(existingBox, payload.getRecipientBoxes().get(0))) {
                // recipient must already exist, so just act as though things went normally
                return;
            }
        }
        final EncodedPayload.Builder payloadBuilder = EncodedPayload.Builder.from(existing);
        if (!existing.getRecipientKeys().contains(payload.getRecipientKeys().get(0))) {
            // lets compare it against another message received before
            final byte[] oldDecrypted = enclave.unencryptTransaction(existing, existing.getSenderKey());
            final boolean same = Arrays.equals(newDecrypted, oldDecrypted) && Arrays.equals(payload.getCipherText(), existing.getCipherText());
            if (!same) {
                throw new IllegalArgumentException("Invalid payload provided");
            }
            // check recipients
            if (!existing.getRecipientKeys().contains(payload.getRecipientKeys().get(0))) {
                payloadBuilder.withRecipientKey(payload.getRecipientKeys().get(0)).withRecipientBox(payload.getRecipientBoxes().get(0).getData());
            }
            EncryptedTransaction encryptedTransaction = tx.get();
            encryptedTransaction.setPayload(payloadBuilder.build());
            this.encryptedTransactionDAO.update(encryptedTransaction);
        }
    } else {
        final EncodedPayload.Builder payloadBuilder = EncodedPayload.Builder.from(payload);
        final List<PublicKey> recipientKeys = new ArrayList<>(payload.getRecipientKeys());
        // we need to recreate this
        if (!recipientKeys.contains(sender)) {
            recipientKeys.add(sender);
            payloadBuilder.withRecipientKey(sender);
        }
        // add recipient boxes for all recipients (applicable for PSV transactions)
        for (int i = payload.getRecipientBoxes().size(); i < recipientKeys.size(); i++) {
            PublicKey recipient = recipientKeys.get(i);
            byte[] newBox = enclave.createNewRecipientBox(payload, recipient);
            payloadBuilder.withRecipientBox(newBox);
        }
        final EncryptedTransaction txToSave = new EncryptedTransaction(transactionHash, payloadBuilder.build());
        this.encryptedTransactionDAO.save(txToSave);
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

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