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