Skip to content

Instantly share code, notes, and snippets.

@nflaig
Last active July 30, 2024 14:21
Show Gist options
  • Save nflaig/2c39a657900e0fd5500d059d3e381ba6 to your computer and use it in GitHub Desktop.
Save nflaig/2c39a657900e0fd5500d059d3e381ba6 to your computer and use it in GitHub Desktop.
import bls from "@chainsafe/bls";
import { deriveKeyFromMnemonic, deriveEth2ValidatorKeys } from "@chainsafe/bls-keygen";
import { fromHexString, toHexString } from "@chainsafe/ssz";
import { computeDomain, computeSigningRoot, ZERO_HASH } from "@lodestar/state-transition";
import { DOMAIN_DEPOSIT } from "@lodestar/params";
import { mainnetChainConfig } from "@lodestar/config/networks";
import { ssz } from "@lodestar/types/phase0";
const masterSK = deriveKeyFromMnemonic("your mnemonic phrase goes here");
const sk = bls.SecretKey.fromBytes(deriveEth2ValidatorKeys(masterSK, 0).signing);
console.log("Pubkey: " + sk.toPublicKey().toHex());
const pubkey = sk.toPublicKey().toBytes();
const eth1Address = "your eth1 address goes here";
const withdrawalCredentials = fromHexString("0x010000000000000000000000" + eth1Address.replace("0x", ""));
const depositMessage = { pubkey, withdrawalCredentials, amount: 32e9 };
const domain = computeDomain(DOMAIN_DEPOSIT, mainnetChainConfig.GENESIS_FORK_VERSION, ZERO_HASH);
const signingRoot = computeSigningRoot(ssz.DepositMessage, depositMessage, domain);
const depositData = { ...depositMessage, signature: sk.sign(signingRoot).toBytes() };
console.log("Deposit data: %o", ssz.DepositData.toJson(depositData));
const depositDataRoot = ssz.DepositData.hashTreeRoot(depositData);
console.log("Deposit data root: " + toHexString(depositDataRoot));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment