Search in sources :

Example 6 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class CBOREncoder method decode.

@Override
public EncodedPayload decode(byte[] input) {
    EncodedPayload.Builder payloadBuilder = EncodedPayload.Builder.create();
    try (final CBORParser parser = cborFactory.createParser(input)) {
        validateToken(JsonToken.START_OBJECT, parser.nextToken());
        while (parser.nextFieldName() != null) {
            if (parser.getCurrentName().equals("sender")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] senderKey = parser.getBinaryValue();
                payloadBuilder.withSenderKey(PublicKey.from(senderKey));
                continue;
            }
            if (parser.getCurrentName().equals("cipherText")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] cipherText = parser.getBinaryValue();
                payloadBuilder.withCipherText(cipherText);
                continue;
            }
            if (parser.getCurrentName().equals("nonce")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] nonceBytes = parser.getBinaryValue();
                payloadBuilder.withCipherTextNonce(nonceBytes);
                continue;
            }
            if (parser.getCurrentName().equals("recipientNonce")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] recipientNonceBytes = parser.getBinaryValue();
                payloadBuilder.withRecipientNonce(recipientNonceBytes);
                continue;
            }
            if (parser.getCurrentName().equals("recipients")) {
                validateToken(JsonToken.START_ARRAY, parser.nextToken());
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    final byte[] recipientBytes = parser.getBinaryValue();
                    payloadBuilder.withRecipientKey(PublicKey.from(recipientBytes));
                }
                continue;
            }
            if (parser.getCurrentName().equals("recipientBoxes")) {
                validateToken(JsonToken.START_ARRAY, parser.nextToken());
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    final byte[] box = parser.getBinaryValue();
                    payloadBuilder.withRecipientBox(box);
                }
                continue;
            }
            if (parser.getCurrentName().equals("privacyFlag")) {
                final int flag = parser.nextIntValue(0);
                payloadBuilder.withPrivacyFlag(flag);
                continue;
            }
            if (parser.getCurrentName().equals("affected")) {
                validateToken(JsonToken.START_OBJECT, parser.nextToken());
                final Map<TxHash, byte[]> affectedTxs = new HashMap<>();
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    final TxHash txHash = new TxHash(parser.currentName());
                    validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                    final byte[] securityHashBytes = parser.getBinaryValue();
                    affectedTxs.put(txHash, securityHashBytes);
                }
                payloadBuilder.withAffectedContractTransactions(affectedTxs);
                continue;
            }
            if (parser.getCurrentName().equals("execHash")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] execHash = parser.getBinaryValue();
                payloadBuilder.withExecHash(execHash);
                continue;
            }
            if (parser.getCurrentName().equals("mandatoryFor")) {
                validateToken(JsonToken.START_ARRAY, parser.nextToken());
                final Set<PublicKey> mandatoryRecipients = new HashSet<>();
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    final byte[] recipient = parser.getBinaryValue();
                    mandatoryRecipients.add(PublicKey.from(recipient));
                }
                payloadBuilder.withMandatoryRecipients(mandatoryRecipients);
                continue;
            }
            if (parser.getCurrentName().equals("privacyGroupId")) {
                validateToken(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
                final byte[] groupId = parser.getBinaryValue();
                if (groupId.length > 0)
                    payloadBuilder.withPrivacyGroupId(PrivacyGroup.Id.fromBytes(groupId));
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Unable to decode payload data. ", ex);
    }
    return payloadBuilder.build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser)

Example 7 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class EncryptedTransactionMigratorTest method spPETxWithBothRecipients.

@Test
public void spPETxWithBothRecipients() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final byte[] recipient1Box = "box1".getBytes();
    final byte[] recipient2Box = "box2".getBytes();
    final EncodedPayload primaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withRecipientKey(recipient1).withRecipientBox(recipient1Box).build();
    final EncodedPayload secondaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withRecipientKey(recipient2).withRecipientBox(recipient2Box).build();
    final EncodedPayload result = migrator.handleSingleTransaction(primaryPayload, secondaryPayload);
    final EncodedPayload expected = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withRecipientKeys(List.of(recipient1, recipient2)).withRecipientBoxes(List.of(recipient1Box, recipient2Box)).build();
    assertThat(result).isEqualToComparingFieldByFieldRecursively(expected);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 8 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class EncryptedTransactionMigratorTest method spPrePETxWithSecondaryAsSender.

@Test
public void spPrePETxWithSecondaryAsSender() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final byte[] recipient1Box = "box1".getBytes();
    final EncodedPayload secondaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withNewRecipientKeys(List.of(sender, recipient1, recipient2)).withRecipientBoxes(List.of("boxSender".getBytes(), recipient1Box, "box2".getBytes())).build();
    final EncodedPayload primaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withRecipientBox(recipient1Box).build();
    final EncodedPayload result = migrator.handleSingleTransaction(primaryPayload, secondaryPayload);
    assertThat(result).isEqualToComparingFieldByFieldRecursively(secondaryPayload);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 9 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class EncryptedTransactionMigratorTest method ppTxWithBothAsRecipients.

@Test
public void ppTxWithBothAsRecipients() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final byte[] recipient1Box = "box1".getBytes();
    final byte[] recipient2Box = "box2".getBytes();
    final EncodedPayload primaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withSenderKey(sender).withNewRecipientKeys(List.of(recipient1)).withRecipientBoxes(List.of(recipient1Box)).build();
    final EncodedPayload secondaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withSenderKey(sender).withNewRecipientKeys(List.of(recipient2)).withRecipientBox(recipient2Box).build();
    final EncodedPayload result = migrator.handleSingleTransaction(primaryPayload, secondaryPayload);
    final EncodedPayload expected = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.PARTY_PROTECTION).withSenderKey(sender).withNewRecipientKeys(List.of(recipient1, recipient2)).withRecipientBoxes(List.of(recipient1Box, recipient2Box)).build();
    assertThat(result).isEqualToComparingFieldByFieldRecursively(expected);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 10 with PublicKey

use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.

the class EncryptedTransactionMigratorTest method spPrimarySenderDoesntHaveOwnKeyInList.

// From a pre-0.8 tx
@Test
public void spPrimarySenderDoesntHaveOwnKeyInList() {
    final PublicKey sender = PublicKey.from("sender".getBytes());
    final PublicKey recipient1 = PublicKey.from("recipient1".getBytes());
    final PublicKey recipient2 = PublicKey.from("recipient2".getBytes());
    final byte[] recipient1Box = "box1".getBytes();
    final EncodedPayload primaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withNewRecipientKeys(List.of(recipient1, recipient2)).withRecipientBoxes(List.of(recipient1Box, "box2".getBytes())).build();
    final EncodedPayload secondaryPayload = EncodedPayload.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withSenderKey(sender).withRecipientBox(recipient1Box).build();
    final EncodedPayload result = migrator.handleSingleTransaction(primaryPayload, secondaryPayload);
    assertThat(result).isEqualToComparingFieldByFieldRecursively(primaryPayload);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)281 Test (org.junit.Test)213 Response (jakarta.ws.rs.core.Response)59 MessageHash (com.quorum.tessera.data.MessageHash)57 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)48 Collectors (java.util.stream.Collectors)32 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)28 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)25 java.util (java.util)23 SendResponse (com.quorum.tessera.api.SendResponse)21 Nonce (com.quorum.tessera.encryption.Nonce)20 Recipient (com.quorum.tessera.partyinfo.node.Recipient)20 Operation (io.swagger.v3.oas.annotations.Operation)20 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)20 Stream (java.util.stream.Stream)19 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)18 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)17 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)17 URI (java.net.URI)17 SendRequest (com.quorum.tessera.api.SendRequest)15