Search in sources :

Example 31 with MessageHash

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

the class TransactionResourceTest method sendWithPrivacy.

@Test
public void sendWithPrivacy() {
    final Base64.Encoder base64Encoder = Base64.getEncoder();
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final String base64Hash = "yKNxAAPdBMiEZFkyQifH1PShwHTHTdE92T3hAfSQ3RtGce9IB8jrsrXxGuCe+Vu3Wyv2zgSbUnt+QBN2Rf48qQ==";
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
    sendRequest.setTo(base64Key);
    sendRequest.setPrivacyFlag(3);
    sendRequest.setAffectedContractTransactions(base64Hash);
    sendRequest.setExecHash("executionHash");
    final PublicKey sender = mock(PublicKey.class);
    when(transactionManager.defaultPublicKey()).thenReturn(sender);
    final com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    final MessageHash messageHash = mock(MessageHash.class);
    final byte[] txnData = "TxnData".getBytes();
    when(messageHash.getHashBytes()).thenReturn(txnData);
    when(sendResponse.getTransactionHash()).thenReturn(messageHash);
    when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
    final Response result = transactionResource.send(sendRequest);
    // jersey.target("send").request().post(Entity.entity(sendRequest,
    // MediaType.APPLICATION_JSON));
    assertThat(result.getStatus()).isEqualTo(201);
    assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
    SendResponse resultSendResponse = SendResponse.class.cast(result.getEntity());
    assertThat(resultSendResponse.getKey());
    ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
    verify(transactionManager).send(argumentCaptor.capture());
    verify(transactionManager).defaultPublicKey();
    com.quorum.tessera.transaction.SendRequest businessObject = argumentCaptor.getValue();
    assertThat(businessObject).isNotNull();
    assertThat(businessObject.getPayload()).isEqualTo(sendRequest.getPayload());
    assertThat(businessObject.getSender()).isEqualTo(sender);
    assertThat(businessObject.getRecipients()).hasSize(1);
    assertThat(businessObject.getRecipients().get(0).encodeToBase64()).isEqualTo(base64Key);
    assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
    assertThat(businessObject.getAffectedContractTransactions()).hasSize(1);
    final MessageHash hash = businessObject.getAffectedContractTransactions().iterator().next();
    assertThat(Base64.getEncoder().encodeToString(hash.getHashBytes())).isEqualTo(base64Hash);
    assertThat(businessObject.getExecHash()).isEqualTo("executionHash".getBytes());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 32 with MessageHash

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

the class TransactionResourceTest method sendRawEmptyRecipients.

@Test
public void sendRawEmptyRecipients() throws UnsupportedEncodingException {
    byte[] txnData = "KEY".getBytes();
    com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn(txnData);
    when(sendResponse.getTransactionHash()).thenReturn(messageHash);
    when(transactionManager.defaultPublicKey()).thenReturn(mock(PublicKey.class));
    when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
    Response result = transactionResource.sendRaw("", "", "foo".getBytes());
    // jersey.target("sendraw")
    // .request()
    // .header("c11n-from", "")
    // .header("c11n-to", "")
    // .post(Entity.entity("foo".getBytes(),
    // MediaType.APPLICATION_OCTET_STREAM_TYPE));
    assertThat(result.getStatus()).isEqualTo(200);
    assertThat(result.getEntity()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
    verify(transactionManager).send(any(com.quorum.tessera.transaction.SendRequest.class));
    verify(transactionManager).defaultPublicKey();
}
Also used : Response(jakarta.ws.rs.core.Response) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash) Test(org.junit.Test)

Example 33 with MessageHash

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

the class TransactionResourceTest method sendSignedTransaction.

@Test
public void sendSignedTransaction() throws Exception {
    com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    byte[] transactionHashData = "I Love Sparrows".getBytes();
    final String base64EncodedTransactionHAshData = Base64.getEncoder().encodeToString(transactionHashData);
    MessageHash transactionHash = mock(MessageHash.class);
    when(transactionHash.getHashBytes()).thenReturn(transactionHashData);
    when(sendResponse.getTransactionHash()).thenReturn(transactionHash);
    when(transactionManager.sendSignedTransaction(any(com.quorum.tessera.transaction.SendSignedRequest.class))).thenReturn(sendResponse);
    SendSignedRequest sendSignedRequest = new SendSignedRequest();
    sendSignedRequest.setHash("SOMEDATA".getBytes());
    sendSignedRequest.setTo("recipient1", "recipient2");
    Response result = transactionResource.sendSignedTransactionEnhanced(sendSignedRequest);
    // jersey.target("sendsignedtx")
    // .request()
    // .post(Entity.entity(sendSignedRequest,
    // MediaType.APPLICATION_JSON_TYPE));
    assertThat(result.getStatus()).isEqualTo(201);
    SendResponse resultResponse = SendResponse.class.cast(result.getEntity());
    assertThat(resultResponse.getKey()).isEqualTo(base64EncodedTransactionHAshData);
    assertThat(result.getLocation()).hasPath("transaction/".concat(base64EncodedTransactionHAshData));
    ArgumentCaptor<com.quorum.tessera.transaction.SendSignedRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendSignedRequest.class);
    verify(transactionManager).sendSignedTransaction(argumentCaptor.capture());
    com.quorum.tessera.transaction.SendSignedRequest obj = argumentCaptor.getValue();
    assertThat(obj).isNotNull();
    assertThat(obj.getSignedData()).isEqualTo("SOMEDATA".getBytes());
    assertThat(obj.getRecipients()).hasSize(2);
    assertThat(obj.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(obj.getAffectedContractTransactions()).isEmpty();
    assertThat(obj.getExecHash()).isEmpty();
}
Also used : MessageHash(com.quorum.tessera.data.MessageHash) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Example 34 with MessageHash

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

the class EncodedPayloadManagerImpl method decrypt.

@Override
public ReceiveResponse decrypt(final EncodedPayload payload, final PublicKey maybeDefaultRecipient) {
    final MessageHash customPayloadHash = new MessageHash(payloadDigest.digest(payload.getCipherText()));
    LOGGER.debug("Decrypt request for custom message with hash {}", customPayloadHash);
    final PublicKey recipientKey = Optional.ofNullable(maybeDefaultRecipient).orElseGet(() -> searchForRecipientKey(payload).orElseThrow(() -> new RecipientKeyNotFoundException("No suitable recipient keys found to decrypt payload for " + customPayloadHash)));
    LOGGER.debug("Decryption key found: {}", recipientKey.encodeToBase64());
    final byte[] decryptedTransactionData = enclave.unencryptTransaction(payload, recipientKey);
    final Set<MessageHash> affectedTransaction = payload.getAffectedContractTransactions().keySet().stream().map(TxHash::getBytes).map(MessageHash::new).collect(Collectors.toSet());
    ReceiveResponse.Builder builder = ReceiveResponse.Builder.create().withUnencryptedTransactionData(decryptedTransactionData).withPrivacyMode(payload.getPrivacyMode()).withAffectedTransactions(affectedTransaction).withExecHash(payload.getExecHash()).withSender(payload.getSenderKey());
    payload.getPrivacyGroupId().ifPresent(builder::withPrivacyGroupId);
    return builder.build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)

Example 35 with MessageHash

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

the class EncodedPayloadManagerImpl method searchForRecipientKey.

private Optional<PublicKey> searchForRecipientKey(final EncodedPayload payload) {
    final MessageHash customPayloadHash = new MessageHash(payloadDigest.digest(payload.getCipherText()));
    for (final PublicKey potentialMatchingKey : enclave.getPublicKeys()) {
        try {
            LOGGER.debug("Attempting to decrypt {} using key {}", customPayloadHash, potentialMatchingKey.encodeToBase64());
            enclave.unencryptTransaction(payload, potentialMatchingKey);
            LOGGER.debug("Succeeded decrypting {} using key {}", customPayloadHash, potentialMatchingKey.encodeToBase64());
            return Optional.of(potentialMatchingKey);
        } catch (EnclaveException | IndexOutOfBoundsException | EncryptorException ex) {
            LOGGER.debug("Attempted payload decryption using wrong key, discarding.");
        }
    }
    return Optional.empty();
}
Also used : EncryptorException(com.quorum.tessera.encryption.EncryptorException) PublicKey(com.quorum.tessera.encryption.PublicKey) MessageHash(com.quorum.tessera.data.MessageHash)

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