use of com.quorum.tessera.recovery.resend.ResendResponse 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.recovery.resend.ResendResponse 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));
}
use of com.quorum.tessera.recovery.resend.ResendResponse in project tessera by ConsenSys.
the class LegacyResendManagerImplTest method performResendAll.
@Test
public void performResendAll() {
final PublicKey targetResendKey = PublicKey.from("target".getBytes());
final ResendRequest request = ResendRequest.Builder.create().withType(ResendRequest.ResendRequestType.ALL).withRecipient(targetResendKey).build();
// Not bothered about going through the process, just make sure they are all loaded from the
// database
// We are not testing the workflow itself, only that the workflow gets the right amount of
// transactions
when(dao.transactionCount()).thenReturn(2L);
when(dao.retrieveTransactions(0, 1)).thenReturn(List.of(new EncryptedTransaction()));
when(dao.retrieveTransactions(1, 1)).thenReturn(List.of(new EncryptedTransaction()));
final ResendResponse response = resendManager.resend(request);
assertThat(response).isNotNull();
assertThat(response.getPayload()).isNull();
verify(enclave, times(2)).status();
verify(dao).transactionCount();
verify(dao).retrieveTransactions(0, 1);
verify(dao).retrieveTransactions(1, 1);
}
use of com.quorum.tessera.recovery.resend.ResendResponse in project tessera by ConsenSys.
the class LegacyResendManagerImplTest method targetKeyIsNotSenderOfTransaction.
@Test
public void targetKeyIsNotSenderOfTransaction() {
final MessageHash txHash = new MessageHash("sample-hash".getBytes());
final PublicKey targetResendKey = PublicKey.from("target".getBytes());
final EncodedPayload nonSPPayload = EncodedPayload.Builder.create().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));
final ResendResponse response;
try (var mockStatic = mockStatic(EncodedPayload.Builder.class)) {
EncodedPayload.Builder builder = mock(EncodedPayload.Builder.class);
mockStatic.when(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey)).thenReturn(builder);
when(builder.build()).thenReturn(nonSPPayload);
response = resendManager.resend(request);
mockStatic.verify(() -> EncodedPayload.Builder.forRecipient(nonSPPayload, targetResendKey));
}
assertThat(response).isNotNull();
assertThat(response.getPayload()).isEqualTo(nonSPPayload);
verify(dao).retrieveByHash(txHash);
}
Aggregations