Skip to content

Instantly share code, notes, and snippets.

@ajsutton ajsutton/GenerateKeys.java
Last active Aug 13, 2019

Embed
What would you like to do?
package tech.pegasys.artemis;
import com.google.common.primitives.UnsignedLong;
import java.security.MessageDigest;
import java.security.Security;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import tech.pegasys.artemis.datastructures.Constants;
import tech.pegasys.artemis.datastructures.operations.DepositData;
import tech.pegasys.artemis.util.bls.BLSKeyPair;
import tech.pegasys.artemis.util.bls.BLSSignature;
import tech.pegasys.artemis.util.mikuli.KeyPair;
public class GenerateKeys {
@SuppressWarnings("DoNotInvokeMessageDigestDirectly")
public static void main(String[] args) throws Exception {
// Create providers
Security.addProvider(new BouncyCastleProvider());
final MessageDigest sha256 = MessageDigest.getInstance("sha256");
// Create the two key pairs we need.
final BLSKeyPair validatorKeyPair = BLSKeyPair.random();
final BLSKeyPair withdrawalKeyPair = new BLSKeyPair(KeyPair.random());
System.out.println(
"Validator secret key: "
+ validatorKeyPair.getSecretKey().getSecretKey().toBytes().reverse().toHexString());
System.out.println(
"Withdrawal secret key: "
+ withdrawalKeyPair.getSecretKey().getSecretKey().toBytes().toHexString());
// Calulate pubKey param
final String pubKeyParam =
validatorKeyPair.getPublicKey().getPublicKey().toBytesCompressed().reverse().toHexString();
// Calculate withdrawalCredentials param
withdrawalKeyPair.getPublicKey().toBytes().update(sha256);
final byte[] digest = Bytes.wrap(sha256.digest()).reverse().toArray();
digest[0] = 0; // BLS_WITHDRAWAL_PREFIX_BYTE
final Bytes rawWithdrawalCredentials = Bytes.wrap(digest);
final String withdrawalCredentialsParam = rawWithdrawalCredentials.toHexString();
// Calculate signature param
final Bytes32 depositDataSigningRoot =
new DepositData(
validatorKeyPair.getPublicKey(),
Bytes32.wrap(rawWithdrawalCredentials),
UnsignedLong.valueOf(32),
null)
.signing_root("signature");
BLSSignature.sign(validatorKeyPair, depositDataSigningRoot, Constants.DOMAIN_DEPOSIT);
final String signatureParam = BLSSignature.random().toBytes().reverse().toHexString();
System.out.println();
System.out.println();
System.out.println("pubKey: " + pubKeyParam);
System.out.println("Withdrawal credentials: " + withdrawalCredentialsParam);
System.out.println("Signature: " + signatureParam);
System.out.println();
System.out.println(
"Deposit params:\n\""
+ pubKeyParam
+ "\",\""
+ withdrawalCredentialsParam
+ "\",\""
+ signatureParam
+ "\"");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.