Java Code Examples for org.apache.tuweni.bytes.Bytes#toArrayUnsafe()

The following examples show how to use org.apache.tuweni.bytes.Bytes#toArrayUnsafe() . 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: RLPxConnectionFactory.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
private static EthereumIESEncryptionEngine forDecryption(
    SecretKey privateKey,
    PublicKey ephemeralPublicKey,
    Bytes iv,
    Bytes commonMac) {
  CipherParameters pubParam = new ECPublicKeyParameters(ephemeralPublicKey.asEcPoint(), CURVE);
  CipherParameters privParam = new ECPrivateKeyParameters(privateKey.bytes().toUnsignedBigInteger(), CURVE);

  BasicAgreement agreement = new ECDHBasicAgreement();
  agreement.init(privParam);
  byte[] agreementValue =
      BigIntegers.asUnsignedByteArray(agreement.getFieldSize(), agreement.calculateAgreement(pubParam));

  IESWithCipherParameters iesWithCipherParameters = new IESWithCipherParameters(new byte[0], new byte[0], 128, 128);

  EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction kdf =
      new EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction(1, new SHA256Digest());
  kdf.init(new KDFParameters(agreementValue, iesWithCipherParameters.getDerivationV()));
  EthereumIESEncryptionEngine engine = new EthereumIESEncryptionEngine(
      agreement,
      kdf,
      new HMac(new SHA256Digest()),
      commonMac.toArrayUnsafe(),
      new BufferedBlockCipher(new SICBlockCipher(new AESEngine())));
  ParametersWithIV cipherParameters = new ParametersWithIV(iesWithCipherParameters, iv.toArrayUnsafe());
  engine.init(false, privParam, pubParam, cipherParameters);
  return engine;
}
 
Example 2
Source File: LegacyPrivateStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Hash> getLatestStateRoot(final Bytes privacyId) {
  final byte[] id = privacyId.toArrayUnsafe();

  if (keyValueStorage.get(id).isPresent()) {
    return Optional.of(Hash.wrap(Bytes32.wrap(keyValueStorage.get(id).get())));
  } else {
    return Optional.empty();
  }
}
 
Example 3
Source File: PrivateStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
private Predicate<byte[]> containsSuffix(final Bytes suffix) {
  final byte[] suffixArray = suffix.toArrayUnsafe();
  return key ->
      key.length > suffixArray.length
          && Arrays.equals(
              Arrays.copyOfRange(key, key.length - suffixArray.length, key.length), suffixArray);
}
 
Example 4
Source File: MockInputStream.java    From teku with Apache License 2.0 5 votes vote down vote up
public synchronized void deliverBytes(final Bytes bytes) throws IOException {
  if (closed) {
    throw new IOException("Attempt to write bytes to closed stream");
  }
  final boolean pendingBytesWasEmpty = unconsumedBytes.size() == 0;
  final byte[] byteArray = bytes.toArrayUnsafe();
  for (byte b : byteArray) {
    unconsumedBytes.add(b);
  }

  if (pendingBytesWasEmpty) {
    nextReadResultReady.complete(null);
    nextReadResultReady = new CompletableFuture<>();
  }
}
 
Example 5
Source File: DepositsFromBlockEventSerializer.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(final DepositsFromBlockEvent value) {
  Bytes bytes =
      SSZ.encode(
          writer -> {
            writer.writeUInt64(value.getBlockNumber().longValue());
            writer.writeFixedBytes(value.getBlockHash());
            writer.writeUInt64(value.getBlockTimestamp().longValue());
            writer.writeBytesList(
                value.getDeposits().stream().map(this::encodeDeposit).collect(toList()));
          });
  return bytes.toArrayUnsafe();
}
 
Example 6
Source File: MinGenesisTimeBlockEventSerializer.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(final MinGenesisTimeBlockEvent value) {
  Bytes bytes =
      SSZ.encode(
          writer -> {
            writer.writeUInt64(value.getTimestamp().longValue());
            writer.writeUInt64(value.getBlockNumber().longValue());
            writer.writeFixedBytes(value.getBlockHash());
          });
  return bytes.toArrayUnsafe();
}
 
Example 7
Source File: RLPxConnectionFactory.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
private static EthereumIESEncryptionEngine forEncryption(
    PublicKey pubKey,
    Bytes iv,
    Bytes commonMac,
    KeyPair ephemeralKeyPair) {
  CipherParameters pubParam = new ECPublicKeyParameters(pubKey.asEcPoint(), CURVE);
  CipherParameters privParam =
      new ECPrivateKeyParameters(ephemeralKeyPair.secretKey().bytes().toUnsignedBigInteger(), CURVE);

  BasicAgreement agree = new ECDHBasicAgreement();
  agree.init(privParam);
  BigInteger z = agree.calculateAgreement(pubParam);
  byte[] zbytes = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);

  IESWithCipherParameters iesWithCipherParameters = new IESWithCipherParameters(new byte[0], new byte[0], 128, 128);

  // Initialise the KDF.
  EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction kdf =
      new EthereumIESEncryptionEngine.ECIESHandshakeKDFFunction(1, new SHA256Digest());
  kdf.init(new KDFParameters(zbytes, iesWithCipherParameters.getDerivationV()));
  EthereumIESEncryptionEngine engine = new EthereumIESEncryptionEngine(
      agree,
      kdf,
      new HMac(new SHA256Digest()),
      commonMac.toArrayUnsafe(),
      new BufferedBlockCipher(new SICBlockCipher(new AESEngine())));
  ParametersWithIV cipherParameters = new ParametersWithIV(iesWithCipherParameters, iv.toArrayUnsafe());
  engine.init(true, privParam, pubParam, cipherParameters);

  return engine;
}
 
Example 8
Source File: UInt256.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
/**
 * Return a {@link UInt256} containing the value described by the specified bytes.
 *
 * @param bytes The bytes containing a {@link UInt256}.
 * @return A {@link UInt256} containing the specified value.
 * @throws IllegalArgumentException if {@code bytes.size() &gt; 32}.
 */
public static UInt256 fromBytes(final Bytes bytes) {
  if (bytes instanceof Bytes32) {
    final byte[] array = bytes.toArrayUnsafe();
    return new UInt256(
        new int[] {
            (Byte.toUnsignedInt(array[0])) << 24
                | (Byte.toUnsignedInt(array[1]) << 16)
                | (Byte.toUnsignedInt(array[2]) << 8)
                | (Byte.toUnsignedInt(array[3])),
            (Byte.toUnsignedInt(array[4]) << 24)
                | (Byte.toUnsignedInt(array[5]) << 16)
                | (Byte.toUnsignedInt(array[6]) << 8)
                | (Byte.toUnsignedInt(array[7])),
            (Byte.toUnsignedInt(array[8]) << 24)
                | (Byte.toUnsignedInt(array[9]) << 16)
                | (Byte.toUnsignedInt(array[10]) << 8)
                | (Byte.toUnsignedInt(array[11])),
            (Byte.toUnsignedInt(array[12]) << 24)
                | (Byte.toUnsignedInt(array[13]) << 16)
                | (Byte.toUnsignedInt(array[14]) << 8)
                | (Byte.toUnsignedInt(array[15])),
            (Byte.toUnsignedInt(array[16]) << 24)
                | (Byte.toUnsignedInt(array[17]) << 16)
                | (Byte.toUnsignedInt(array[18]) << 8)
                | (Byte.toUnsignedInt(array[19])),
            (Byte.toUnsignedInt(array[20]) << 24)
                | (Byte.toUnsignedInt(array[21]) << 16)
                | (Byte.toUnsignedInt(array[22]) << 8)
                | (Byte.toUnsignedInt(array[23])),
            (Byte.toUnsignedInt(array[24]) << 24)
                | (Byte.toUnsignedInt(array[25]) << 16)
                | (Byte.toUnsignedInt(array[26]) << 8)
                | (Byte.toUnsignedInt(array[27])),
            (Byte.toUnsignedInt(array[28]) << 24)
                | (Byte.toUnsignedInt(array[29]) << 16)
                | (Byte.toUnsignedInt(array[30]) << 8)
                | (Byte.toUnsignedInt(array[31]))});
  } else {
    return new UInt256(Bytes32.leftPad(bytes));
  }
}