package com.quorum.tessera.enclave;

import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.mockito.Mockito.*;

import com.quorum.tessera.enclave.encoder.LegacyEncodedPayload;
import com.quorum.tessera.enclave.encoder.LegacyPayloadEncoder;
import com.quorum.tessera.encryption.Nonce;
import com.quorum.tessera.encryption.PublicKey;
import java.util.*;
import java.util.stream.Stream;
import org.junit.Test;
import org.mockito.Mockito;

public class PayloadEncoderTest {

  private final PayloadEncoder payloadEncoder = new PayloadEncoderImpl();

  private final LegacyPayloadEncoder legacyPayloadEncoder = new LegacyPayloadEncoder();

  // This tests a payload that has no data for the recipient list
  // NOT the case where the list is present but empty
  @Test
  public void decodeLegacyPayloadMissingRecipient() {
    final byte[] input =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 120, 111, 63, -100, 97, -12, -103, 20, 2, -48, 37, -86, -115, -112, -75,
          -27, 55, 12, -1, 120, 13, 0, 86, 92, 52, 77, -4, 45, 0, 0, 0, 0, 0, 0, 0, 24, -115, -84,
          -58, 14, 82, 118, 4, -118, -53, 86, 3, 14, 112, 70, -4, 81, 121, 84, -24, -3, -73, -17, 6,
          124, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, -87, -102, 0, 95, -13, 48, 76, -115,
          -115, 62, 54, -55, -78, 125, -54, -34, -71, -11, -95, -85, 78, -24, -30, 47, 65, 5, 88,
          38, -111, -12, -41, -97, 103, -60, -101, 43, -57, -68, 68, -109, 36, 49, -63, -123, 62,
          21, 67, -28, 0, 0, 0, 0, 0, 0, 0, 24, -63, 5, 86, 42, -85, -12, -36, 16, -108, 48, 26, 36,
          44, -82, 15, -38, -19, 6, -101, 107, 110, -30, 95, 5
        };

    final byte[] senderKey =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          120, 111, 63, -100, 97, -12, -103, 20, 2, -48, 37, -86, -115, -112, -75, -27, 55, 12, -1,
          120, 13, 0, 86, 92, 52, 77, -4, 45
        };
    final byte[] nonce =
        new byte[] {
          -115, -84, -58, 14, 82, 118, 4, -118, -53, 86, 3, 14, 112, 70, -4, 81, 121, 84, -24, -3,
          -73, -17, 6, 124
        };
    final byte[] recipientNonce =
        new byte[] {
          -63, 5, 86, 42, -85, -12, -36, 16, -108, 48, 26, 36, 44, -82, 15, -38, -19, 6, -101, 107,
          110, -30, 95, 5
        };
    final byte[] recipient =
        new byte[] {
          -87, -102, 0, 95, -13, 48, 76, -115, -115, 62, 54, -55, -78, 125, -54, -34, -71, -11, -95,
              -85, 78,
          -24, -30, 47, 65, 5, 88, 38, -111, -12, -41, -97, 103, -60, -101, 43, -57, -68, 68, -109,
              36, 49,
          -63, -123, 62, 21, 67, -28
        };

    final EncodedPayload output = payloadEncoder.decode(input);

    assertThat(output.getSenderKey()).isEqualTo(PublicKey.from(senderKey));
    assertThat(output.getCipherText()).containsExactly(cipherText);
    assertThat(output.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(output.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(output.getRecipientBoxes()).hasSize(1);
    assertThat(output.getRecipientBoxes().get(0).getData()).containsExactly(recipient);
    assertThat(output.getRecipientKeys()).isEmpty();
  }

  @Test
  public void decodeLegacyPayloadEmptyRecipients() {

    final byte[] input =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 120, 111, 63, -100, 97, -12, -103, 20, 2, -48, 37, -86, -115, -112, -75,
          -27, 55, 12, -1, 120, 13, 0, 86, 92, 52, 77, -4, 45, 0, 0, 0, 0, 0, 0, 0, 24, -115, -84,
          -58, 14, 82, 118, 4, -118, -53, 86, 3, 14, 112, 70, -4, 81, 121, 84, -24, -3, -73, -17, 6,
          124, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, -87, -102, 0, 95, -13, 48, 76, -115,
          -115, 62, 54, -55, -78, 125, -54, -34, -71, -11, -95, -85, 78, -24, -30, 47, 65, 5, 88,
          38, -111, -12, -41, -97, 103, -60, -101, 43, -57, -68, 68, -109, 36, 49, -63, -123, 62,
          21, 67, -28, 0, 0, 0, 0, 0, 0, 0, 24, -63, 5, 86, 42, -85, -12, -36, 16, -108, 48, 26, 36,
          44, -82, 15, -38, -19, 6, -101, 107, 110, -30, 95, 5, 0, 0, 0, 0, 0, 0, 0, 0
        };

    final byte[] senderKey =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          120, 111, 63, -100, 97, -12, -103, 20, 2, -48, 37, -86, -115, -112, -75, -27, 55, 12, -1,
          120, 13, 0, 86, 92, 52, 77, -4, 45
        };
    final byte[] nonce =
        new byte[] {
          -115, -84, -58, 14, 82, 118, 4, -118, -53, 86, 3, 14, 112, 70, -4, 81, 121, 84, -24, -3,
          -73, -17, 6, 124
        };
    final byte[] recipientNonce =
        new byte[] {
          -63, 5, 86, 42, -85, -12, -36, 16, -108, 48, 26, 36, 44, -82, 15, -38, -19, 6, -101, 107,
          110, -30, 95, 5
        };
    final byte[] recipient =
        new byte[] {
          -87, -102, 0, 95, -13, 48, 76, -115, -115, 62, 54, -55, -78, 125, -54, -34, -71, -11, -95,
              -85, 78,
          -24, -30, 47, 65, 5, 88, 38, -111, -12, -41, -97, 103, -60, -101, 43, -57, -68, 68, -109,
              36, 49,
          -63, -123, 62, 21, 67, -28
        };

    final EncodedPayload output = payloadEncoder.decode(input);

    assertThat(output.getSenderKey()).isEqualTo(PublicKey.from(senderKey));
    assertThat(output.getCipherText()).containsExactly(cipherText);
    assertThat(output.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(output.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(output.getRecipientBoxes()).hasSize(1);
    assertThat(output.getRecipientBoxes().get(0).getData()).containsExactly(recipient);
    assertThat(output.getRecipientKeys()).isEmpty();
    assertThat(output.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
  }

  @Test
  public void decodeLegacyPayloadWithRecipients() {

    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 32, 35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30,
          -67, 115, -38, 43, 36, 57, 90, 100, 3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75, 0,
          0, 0, 0, 0, 0, 0, 32, 53, -51, 72, -87, 21, -70, -41, 41, 9, -6, -29, -30, -67, 15, -38,
          43, 36, 57, 90, 100, 3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };
    final byte[] recipientKey1 =
        new byte[] {
          35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30, -67, 115, -38, 43, 36, 57, 90, 100,
          3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };
    final byte[] recipientKey2 =
        new byte[] {
          53, -51, 72, -87, 21, -70, -41, 41, 9, -6, -29, -30, -67, 15, -38, 43, 36, 57, 90, 100, 3,
          80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys())
        .hasSize(2)
        .containsExactly(PublicKey.from(recipientKey1), PublicKey.from(recipientKey2));
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
  }

  @Test
  public void decodePartyProtectionPayloadNoRecipientNoAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys()).isEmpty();
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PARTY_PROTECTION);
    assertThat(payload.getAffectedContractTransactions()).isEmpty();
    assertThat(payload.getExecHash()).isNullOrEmpty();
  }

  @Test
  public void decodePartyProtectionPayloadNoRecipientWithAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 4, 116,
          101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 4, 116, 101, 115, 116
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys()).isEmpty();
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PARTY_PROTECTION);
    assertThat(payload.getAffectedContractTransactions()).hasSize(1);
    assertThat(payload.getAffectedContractTransactions().values().iterator().next().getData())
        .containsExactly("test".getBytes());
    assertThat(payload.getExecHash()).isNullOrEmpty();
  }

  @Test
  public void decodePartyProtectionPayloadWithRecipientsNoAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 32, 35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30,
          -67, 115, -38, 43, 36, 57, 90, 100, 3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75, 0,
          0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };
    final byte[] recipientKey =
        new byte[] {
          35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30, -67, 115, -38, 43, 36, 57, 90, 100,
          3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys()).hasSize(1).containsExactly(PublicKey.from(recipientKey));
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PARTY_PROTECTION);
    assertThat(payload.getAffectedContractTransactions()).isEmpty();
    assertThat(payload.getExecHash()).isNullOrEmpty();
  }

  @Test
  public void decodePartyProtectionPayloadWithRecipientsWithAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 32, 35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30,
          -67, 115, -38, 43, 36, 57, 90, 100, 3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75, 0,
          0, 0, 0, 0, 0, 0, 32, 53, -51, 72, -87, 21, -70, -41, 41, 9, -6, -29, -30, -67, 15, -38,
          43, 36, 57, 90, 100, 3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75, 0, 0, 0, 0, 0, 0,
          0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 4, 116, 101, 115, 116, 0, 0, 0, 0,
          0, 0, 0, 4, 116, 101, 115, 116
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };
    final byte[] recipientKey1 =
        new byte[] {
          35, -15, 27, -78, 21, -70, -41, 41, 9, -6, -92, -30, -67, 115, -38, 43, 36, 57, 90, 100,
          3, 80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };
    final byte[] recipientKey2 =
        new byte[] {
          53, -51, 72, -87, 21, -70, -41, 41, 9, -6, -29, -30, -67, 15, -38, 43, 36, 57, 90, 100, 3,
          80, 87, -52, 52, 97, 1, -113, 97, 54, -71, 75
        };

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys())
        .hasSize(2)
        .containsExactly(PublicKey.from(recipientKey1), PublicKey.from(recipientKey2));
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PARTY_PROTECTION);
    assertThat(payload.getAffectedContractTransactions()).hasSize(1);
    assertThat(payload.getAffectedContractTransactions().keySet())
        .containsExactly(new TxHash("test".getBytes()));
    assertThat(payload.getExecHash()).isNullOrEmpty();
  }

  @Test
  public void decodePsvPayloadNoRecipientsNoAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 101,
          120, 101, 99, 117, 116, 105, 111, 110, 72, 97, 115, 104
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };
    final byte[] execHash = "executionHash".getBytes();

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys()).isEmpty();
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
    assertThat(payload.getAffectedContractTransactions()).isEmpty();
    assertThat(payload.getExecHash()).isEqualTo(execHash);
  }

  @Test
  public void decodePsvPayloadNoRecipientsWithAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
          118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1, -86, 74, -25, -30, -88, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 4, 116,
          101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 4, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 13, 101,
          120, 101, 99, 117, 116, 105, 111, 110, 72, 97, 115, 104
        };

    final byte[] sender =
        new byte[] {
          -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78, 30, 85, -61, 56, 22,
          -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8
        };
    final byte[] cipherText =
        new byte[] {
          121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116, 97, 108, 48, -108,
          87, 124, -71, 87, -79, 15, -117, -23
        };
    final byte[] nonce =
        new byte[] {
          26, 117, -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65,
          -83, 53, -77, -82, 104
        };
    final byte[] recipientBox =
        new byte[] {
          61, 89, -19, -11, 82, 81, -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122,
              -7, -31,
          -36, 51, -12, 14, -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86,
              -78, 31, 86,
          61, -103, -92, -128, -74, 81
        };
    final byte[] recipientNonce =
        new byte[] {
          -92, 8, 108, -75, -70, 59, 77, 113, 118, 118, 118, -48, 13, -36, -116, 41, 127, 86, 1,
          -86, 74, -25, -30, -88
        };
    final byte[] execHash = "executionHash".getBytes();

    final EncodedPayload payload = payloadEncoder.decode(encoded);

    assertThat(payload.getSenderKey()).isEqualTo(PublicKey.from(sender));
    assertThat(payload.getCipherText()).containsExactly(cipherText);
    assertThat(payload.getCipherTextNonce()).isEqualTo(new Nonce(nonce));
    assertThat(payload.getRecipientBoxes()).hasSize(1);
    assertThat(payload.getRecipientBoxes().get(0).getData()).containsExactly(recipientBox);
    assertThat(payload.getRecipientNonce()).isEqualTo(new Nonce(recipientNonce));
    assertThat(payload.getRecipientKeys()).isEmpty();
    assertThat(payload.getPrivacyMode()).isEqualTo(PrivacyMode.PRIVATE_STATE_VALIDATION);
    assertThat(payload.getAffectedContractTransactions()).hasSize(1);
    assertThat(payload.getAffectedContractTransactions().values().iterator().next().getData())
        .containsExactly("test".getBytes());
    assertThat(payload.getExecHash()).isEqualTo(execHash);
  }

  @Test
  public void decodePsvPayloadWithRecipientsNoAffectedTx() {
    final byte[] encoded =
        new byte[] {
          0, 0, 0, 0, 0, 0, 0, 32, -51, 40, -97, 78, 121, 47, -26, -66, 10, -21, -80, -22, -33, 78,
          30, 85, -61, 56, 22, -100, 70, 124, 114, -34, -41, 36, -62, 6, 109, 63, -17, 8, 0, 0, 0,
          0, 0, 0, 0, 28, 121, 84, 87, 65, -41, -37, 105, -73, 74, 33, -86, 53, 42, 39, 51, -116,
          97, 108, 48, -108, 87, 124, -71, 87, -79, 15, -117, -23, 0, 0, 0, 0, 0, 0, 0, 24, 26, 117,
          -64, -69, -4, -127, 50, 16, -40, 92, -15, 52, 60, 119, -5, 117, -22, 121, 65, -83, 53,
          -77, -82, 104, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 48, 61, 89, -19, -11, 82, 81,
          -84, 119, -103, -104, 70, 34, 117, 1, 29, -100, -108, 112, 122, -7, -31, -36, 51, -12, 14,
          -120, -86, 49, -29, -41, 99, 106, -56, -35, -1, -117, -89, -45, -86, -78, 31, 86, 61,
          -103, -92, -128, -74, 81, 0, 0, 0, 0, 0, 0, 0, 24, -92, 8, 108, -75, -70, 59, 77, 113,
