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);
}
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);
});
}
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();
}
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;
}
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));
}
Aggregations