public static KeyPair generate(final KeyPairGenerator keyPairGenerator, final String algorithm) {
    final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair();
    final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
    final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();

    final BigInteger privateKeyValue = privateKey.getD();

    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    final BigInteger publicKeyValue =
        new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));

    return new KeyPair(
        SECPPrivateKey.create(privateKeyValue, algorithm),
        SECPPublicKey.create(publicKeyValue, algorithm));
  }
Generate key pair.

@param keyPairGenerator the key pair generator
@param algorithm the algorithm
@return the key pair
crypto/algorithms/src/main/java/org/hyperledger/besu/crypto/KeyPair.java