Search in sources :

Example 1 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser 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 2 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class GeneratorSimpleTest method testCopyCurrentStructureWithTaggedBinary.

public void testCopyCurrentStructureWithTaggedBinary() {
    final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
    final CBORGenerator sourceGen = cborGenerator(sourceBytes);
    sourceGen.writeNumber(BigInteger.ZERO);
    sourceGen.close();
    final ByteArrayOutputStream targetBytes = new ByteArrayOutputStream();
    final CBORGenerator gen = cborGenerator(targetBytes);
    final CBORParser cborParser = cborParser(sourceBytes);
    cborParser.nextToken();
    gen.copyCurrentStructure(cborParser);
    gen.close();
    cborParser.close();
    Assert.assertArrayEquals(sourceBytes.toByteArray(), targetBytes.toByteArray());
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Example 3 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class GeneratorSimpleTest method testCopyCurrentEventWithTag.

public void testCopyCurrentEventWithTag() {
    final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
    final CBORGenerator sourceGen = cborGenerator(sourceBytes);
    sourceGen.writeNumber(BigDecimal.ONE);
    sourceGen.close();
    final ByteArrayOutputStream targetBytes = new ByteArrayOutputStream();
    final CBORGenerator gen = cborGenerator(targetBytes);
    final CBORParser cborParser = cborParser(sourceBytes);
    while (cborParser.nextToken() != null) {
        gen.copyCurrentEvent(cborParser);
    }
    gen.close();
    cborParser.close();
    // copyCurrentEvent doesn't preserve fixed arrays, so we can't
    // compare with the source bytes.
    Assert.assertArrayEquals(new byte[] { CBORConstants.BYTE_TAG_DECIMAL_FRACTION, CBORConstants.BYTE_ARRAY_2_ELEMENTS, 0, 1 }, targetBytes.toByteArray());
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Example 4 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class GeneratorSimpleTest method testCopyCurrentStructureWithTaggedArray.

public void testCopyCurrentStructureWithTaggedArray() {
    final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
    final CBORGenerator sourceGen = cborGenerator(sourceBytes);
    sourceGen.writeNumber(BigDecimal.ONE);
    sourceGen.close();
    final ByteArrayOutputStream targetBytes = new ByteArrayOutputStream();
    final CBORGenerator gen = cborGenerator(targetBytes);
    final CBORParser cborParser = cborParser(sourceBytes);
    cborParser.nextToken();
    gen.copyCurrentStructure(cborParser);
    gen.close();
    cborParser.close();
    // copyCurrentEvent doesn't preserve fixed arrays, so we can't
    // compare with the source bytes.
    Assert.assertArrayEquals(new byte[] { CBORConstants.BYTE_TAG_DECIMAL_FRACTION, CBORConstants.BYTE_ARRAY_2_ELEMENTS, 0, 1 }, targetBytes.toByteArray());
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Example 5 with CBORParser

use of com.fasterxml.jackson.dataformat.cbor.CBORParser in project jackson-dataformats-binary by FasterXML.

the class BasicParserTest method testBufferRelease.

public void testBufferRelease() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CBORGenerator generator = cborGenerator(out);
    generator.writeStartObject();
    generator.writeStringProperty("a", "1");
    generator.writeEndObject();
    generator.flush();
    // add stuff that is NOT part of the Object
    out.write(new byte[] { 1, 2, 3 });
    generator.close();
    CBORParser parser = cborParser(out.toByteArray());
    assertEquals(JsonToken.START_OBJECT, parser.nextToken());
    assertEquals(JsonToken.PROPERTY_NAME, parser.nextToken());
    assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
    assertEquals(JsonToken.END_OBJECT, parser.nextToken());
    // Fine; but now should be able to retrieve 3 bytes that are (likely)
    // to have been  buffered
    ByteArrayOutputStream extra = new ByteArrayOutputStream();
    assertEquals(3, parser.releaseBuffered(extra));
    byte[] extraBytes = extra.toByteArray();
    assertEquals((byte) 1, extraBytes[0]);
    assertEquals((byte) 2, extraBytes[1]);
    assertEquals((byte) 3, extraBytes[2]);
    parser.close();
}
Also used : CBORParser(com.fasterxml.jackson.dataformat.cbor.CBORParser) CBORGenerator(com.fasterxml.jackson.dataformat.cbor.CBORGenerator)

Aggregations

CBORParser (com.fasterxml.jackson.dataformat.cbor.CBORParser)22 StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)9 CBORGenerator (com.fasterxml.jackson.dataformat.cbor.CBORGenerator)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonLocation (com.fasterxml.jackson.core.JsonLocation)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 Fido2RuntimeException (io.jans.fido2.exception.Fido2RuntimeException)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1