use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveNoTransactionInDatabase.
@Test
public void receiveNoTransactionInDatabase() {
PublicKey recipient = PublicKey.from("recipient".getBytes());
MessageHash messageHash = mock(MessageHash.class);
when(messageHash.getHashBytes()).thenReturn("KEY".getBytes());
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
EncodedPayload payload = mock(EncodedPayload.class);
when(encryptedTransactionDAO.retrieveByHash(any(MessageHash.class))).thenReturn(Optional.empty());
try {
transactionManager.receive(receiveRequest);
failBecauseExceptionWasNotThrown(TransactionNotFoundException.class);
} catch (TransactionNotFoundException ex) {
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
}
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method sendSignedTransactionWithMandatoryRecipients.
@Test
public void sendSignedTransactionWithMandatoryRecipients() {
EncodedPayload payload = mock(EncodedPayload.class);
MessageHash hash = new MessageHash("HASH".getBytes());
EncryptedRawTransaction encryptedRawTransaction = mock(EncryptedRawTransaction.class);
when(encryptedRawTransaction.getHash()).thenReturn(hash);
when(encryptedRawTransaction.toRawTransaction()).thenReturn(mock((RawTransaction.class)));
when(encryptedRawTransactionDAO.retrieveByHash(eq(hash))).thenReturn(Optional.of(encryptedRawTransaction));
when(payload.getCipherText()).thenReturn("ENCRYPTED_PAYLOAD".getBytes());
when(enclave.encryptPayload(any(RawTransaction.class), any(), any())).thenReturn(payload);
PublicKey receiver = PublicKey.from("RECEIVER".getBytes());
when(enclave.getPublicKeys()).thenReturn(Set.of(receiver));
SendSignedRequest sendSignedRequest = mock(SendSignedRequest.class);
when(sendSignedRequest.getRecipients()).thenReturn(List.of(receiver));
when(sendSignedRequest.getSignedData()).thenReturn("HASH".getBytes());
when(sendSignedRequest.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(sendSignedRequest.getMandatoryRecipients()).thenReturn(Set.of(receiver));
SendResponse result = transactionManager.sendSignedTransaction(sendSignedRequest);
assertThat(result).isNotNull();
assertThat(result.getTransactionHash()).isEqualTo(new MessageHash("HASH".getBytes()));
assertThat(result.getManagedParties()).containsExactly(receiver);
ArgumentCaptor<PrivacyMetadata> data = ArgumentCaptor.forClass(PrivacyMetadata.class);
verify(enclave).encryptPayload(any(RawTransaction.class), any(), data.capture());
verify(encryptedTransactionDAO).save(any(EncryptedTransaction.class), any(Callable.class));
verify(encryptedRawTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave).getForwardingKeys();
verify(enclave).getPublicKeys();
final PrivacyMetadata passingData = data.getValue();
assertThat(passingData.getPrivacyMode()).isEqualTo(PrivacyMode.MANDATORY_RECIPIENTS);
assertThat(passingData.getPrivacyGroupId()).isNotPresent();
assertThat(passingData.getMandatoryRecipients()).containsExactly(receiver);
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveWithRecipientsPresent.
@Test
public void receiveWithRecipientsPresent() {
final PublicKey sender = PublicKey.from("sender".getBytes());
final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
MessageHash messageHash = mock(MessageHash.class);
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getExecHash()).thenReturn("execHash".getBytes());
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
when(payload.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
when(payload.getSenderKey()).thenReturn(sender);
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
byte[] expectedOutcome = "Encrypted payload".getBytes();
when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenReturn(expectedOutcome);
when(enclave.getPublicKeys()).thenReturn(Set.of(recipient1, recipient2));
ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
assertThat(receiveResponse).isNotNull();
assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
assertThat(receiveResponse.getManagedParties()).containsExactlyInAnyOrder(recipient1, recipient2);
assertThat(receiveResponse.sender()).isEqualTo(sender);
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave, times(2)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
verify(enclave, times(2)).getPublicKeys();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveWithMandatoryRecipients.
@Test
public void receiveWithMandatoryRecipients() {
PublicKey sender = PublicKey.from("sender".getBytes());
byte[] randomData = Base64.getEncoder().encode("odd-data".getBytes());
MessageHash messageHash = mock(MessageHash.class);
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
PublicKey mandReceiver1 = PublicKey.from("mandatory1".getBytes());
PublicKey mandReceiver2 = PublicKey.from("mandatory2".getBytes());
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getMandatoryRecipients()).thenReturn(Set.of(mandReceiver1, mandReceiver2));
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(payload.getSenderKey()).thenReturn(sender);
when(payload.getPrivacyGroupId()).thenReturn(Optional.of(PrivacyGroup.Id.fromBytes("group".getBytes())));
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
byte[] expectedOutcome = "Encrypted payload".getBytes();
when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenReturn(expectedOutcome);
PublicKey publicKey = mock(PublicKey.class);
when(enclave.getPublicKeys()).thenReturn(Set.of(publicKey));
ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
assertThat(receiveResponse).isNotNull();
assertThat(receiveResponse.sender()).isEqualTo(sender);
assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
assertThat(receiveResponse.getPrivacyGroupId()).isPresent();
assertThat(receiveResponse.getPrivacyGroupId().get()).isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave, times(2)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
verify(enclave).getPublicKeys();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class TransactionManagerTest method receiveWithNoRecipientsPresent.
@Test
public void receiveWithNoRecipientsPresent() {
final PublicKey sender = PublicKey.from("sender".getBytes());
final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
MessageHash messageHash = mock(MessageHash.class);
ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
when(receiveRequest.getRecipient()).thenReturn(Optional.of(mock(PublicKey.class)));
when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getSenderKey()).thenReturn(sender);
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
when(payload.getRecipientBoxes()).thenReturn(List.of(RecipientBox.from("box1".getBytes())));
EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
when(encryptedTransaction.getHash()).thenReturn(messageHash);
when(encryptedTransaction.getPayload()).thenReturn(payload);
when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
byte[] expectedOutcome = "Encrypted payload".getBytes();
when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenReturn(expectedOutcome);
when(enclave.getPublicKeys()).thenReturn(Set.of(recipient1));
ReceiveResponse receiveResponse = transactionManager.receive(receiveRequest);
assertThat(receiveResponse).isNotNull();
assertThat(receiveResponse.getUnencryptedTransactionData()).isEqualTo(expectedOutcome);
assertThat(receiveResponse.getManagedParties()).containsExactly(recipient1);
assertThat(receiveResponse.sender()).isEqualTo(sender);
verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
verify(enclave, times(3)).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
verify(enclave, times(2)).getPublicKeys();
}
Aggregations