Java Code Examples for org.apache.tuweni.bytes.Bytes32#fromHexStringLenient()

The following examples show how to use org.apache.tuweni.bytes.Bytes32#fromHexStringLenient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PongPacketDataTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void readFrom() {
  final long time = System.currentTimeMillis();
  final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty());
  final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");

  BytesValueRLPOutput out = new BytesValueRLPOutput();
  out.startList();
  to.encodeStandalone(out);
  out.writeBytes(hash);
  out.writeLongScalar(time);
  out.endList();
  final Bytes encoded = out.encoded();

  final PongPacketData deserialized = PongPacketData.readFrom(RLP.input(encoded));
  assertThat(deserialized.getTo()).isEqualTo(to);
  assertThat(deserialized.getPingHash()).isEqualTo(hash);
  assertThat(deserialized.getExpiration()).isEqualTo(time);
}
 
Example 2
Source File: PongPacketDataTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void readFrom_withExtraFields() {
  final long time = System.currentTimeMillis();
  final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty());
  final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");

  BytesValueRLPOutput out = new BytesValueRLPOutput();
  out.startList();
  to.encodeStandalone(out);
  out.writeBytes(hash);
  out.writeLongScalar(time);
  // Add random fields
  out.writeLong(1234L);
  out.endList();
  final Bytes encoded = out.encoded();

  final PongPacketData deserialized = PongPacketData.readFrom(RLP.input(encoded));
  assertThat(deserialized.getTo()).isEqualTo(to);
  assertThat(deserialized.getPingHash()).isEqualTo(hash);
  assertThat(deserialized.getExpiration()).isEqualTo(time);
}
 
Example 3
Source File: Scalars.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Bytes32 parseLiteral(final Object input) throws CoercingParseLiteralException {
  if (!(input instanceof StringValue)) {
    throw new CoercingParseLiteralException("Value is not any Bytes32 : '" + input + "'");
  }
  try {
    return Bytes32.fromHexStringLenient(((StringValue) input).getValue());
  } catch (final IllegalArgumentException e) {
    throw new CoercingParseLiteralException("Value is not any Bytes32 : '" + input + "'");
  }
}
 
Example 4
Source File: TrieNodeDecoderTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void breadthFirstDecode_unknownTrie() {

  Bytes32 randomRootHash = Bytes32.fromHexStringLenient("0x12");
  List<Node<Bytes>> result =
      TrieNodeDecoder.breadthFirstDecoder((h) -> Optional.empty(), randomRootHash)
          .collect(Collectors.toList());
  assertThat(result.size()).isEqualTo(0);
}
 
Example 5
Source File: PongPacketDataTest.java    From besu with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeDeserialize() {
  final long currentTimeSec = Instant.now().getEpochSecond();
  final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty());
  final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");

  final PongPacketData packet = PongPacketData.create(to, hash);
  final Bytes serialized = RLP.encode(packet::writeTo);
  final PongPacketData deserialized = PongPacketData.readFrom(RLP.input(serialized));

  assertThat(deserialized.getTo()).isEqualTo(to);
  assertThat(deserialized.getPingHash()).isEqualTo(hash);
  assertThat(deserialized.getExpiration()).isGreaterThan(currentTimeSec);
}
 
Example 6
Source File: StatusMessageTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRoundTripViaSsz() {
  final StatusMessage message =
      new StatusMessage(
          Constants.GENESIS_FORK_VERSION,
          Bytes32.fromHexStringLenient("0x01"),
          UnsignedLong.valueOf(2),
          Bytes32.fromHexStringLenient("0x03"),
          UnsignedLong.valueOf(4));

  final Bytes data = SimpleOffsetSerializer.serialize(message);
  final StatusMessage result = SimpleOffsetSerializer.deserialize(data, StatusMessage.class);
  assertThat(result).isEqualToComparingFieldByField(message);
}
 
Example 7
Source File: Hash.java    From besu with Apache License 2.0 4 votes vote down vote up
public static Hash fromHexStringLenient(final String str) {
  return new Hash(Bytes32.fromHexStringLenient(str));
}
 
Example 8
Source File: Hash.java    From incubator-tuweni with Apache License 2.0 2 votes vote down vote up
/**
 * Parse a hexadecimal string into a {@link Hash}.
 *
 * @param str The hexadecimal string to parse, which may or may not start with "0x". That representation may contain
 *        less than 32 bytes, in which case the result is left padded with zeros.
 * @return The value corresponding to {@code str}.
 * @throws IllegalArgumentException if {@code str} does not correspond to a valid hexadecimal representation or
 *         contains more than 32 bytes.
 */
public static Hash fromHexString(String str) {
  return new Hash(Bytes32.fromHexStringLenient(str));
}
 
Example 9
Source File: UInt256.java    From incubator-tuweni with Apache License 2.0 2 votes vote down vote up
/**
 * Parse a hexadecimal string into a {@link UInt256}.
 *
 * @param str The hexadecimal string to parse, which may or may not start with "0x". That representation may contain
 *        less than 32 bytes, in which case the result is left padded with zeros.
 * @return The value corresponding to {@code str}.
 * @throws IllegalArgumentException if {@code str} does not correspond to a valid hexadecimal representation or
 *         contains more than 32 bytes.
 */
public static UInt256 fromHexString(String str) {
  return new UInt256(Bytes32.fromHexStringLenient(str));
}