Skip to content

Instantly share code, notes, and snippets.

@dapplion
Created August 21, 2020 17:06
Show Gist options
  • Save dapplion/6beeaca5fded1c745564e3dea57ce1d6 to your computer and use it in GitHub Desktop.
Save dapplion/6beeaca5fded1c745564e3dea57ce1d6 to your computer and use it in GitHub Desktop.
Learn how eth1 deposits proofs are different depending on when they are computed
import {createIBeaconConfig, IBeaconConfig} from "@chainsafe/lodestar-config";
import {params} from "@chainsafe/lodestar-params/lib/presets/mainnet";
import {TreeBacked, List, toHexString} from "@chainsafe/ssz";
import {Root, ValidatorIndex, DepositData} from "@chainsafe/lodestar-types";
import {IDepositEvent} from "./eth1";
import {computeDomain, DomainType, computeSigningRoot} from "@chainsafe/lodestar-beacon-state-transition";
import {interopKeypair} from "@chainsafe/lodestar-validator";
import {initBLS, Keypair, PrivateKey} from "@chainsafe/bls";
async function learnProofs(): Promise<void> {
await initBLS();
const config = createIBeaconConfig(params);
const depositTree: TreeBacked<List<Root>> = config.types.DepositDataRootList.tree.defaultValue();
function generateDeposit(index: ValidatorIndex): DepositData {
const domain = computeDomain(config, DomainType.DEPOSIT);
const keypair = new Keypair(PrivateKey.fromBytes(interopKeypair(index).privkey));
const depositMessage = {
pubkey: keypair.publicKey.toBytesCompressed(),
withdrawalCredentials: Buffer.alloc(32, index),
amount: BigInt(32 * 1000000000000000000),
};
const signingRoot = computeSigningRoot(config, config.types.DepositMessage, depositMessage, domain);
const signature = keypair.privateKey.signMessage(signingRoot);
return {...depositMessage, signature: signature.toBytesCompressed()};
}
function generateDepositEvent(index: ValidatorIndex): IDepositEvent {
return {
...generateDeposit(index),
index: index,
blockNumber: index,
};
}
const depositEvents: IDepositEvent[] = [];
for (let i = 0; i < 2; i++) {
depositEvents.push(generateDepositEvent(i));
}
for (const depositEvent of depositEvents) {
depositTree.push(config.types.DepositData.hashTreeRoot(depositEvent));
const index = depositEvent.index;
const gindex = depositTree.gindexOfProperty(index);
const proof = depositTree.tree().getSingleProof(gindex);
console.log({index, gindex});
console.log(proof.map((buff) => toHexString(buff)).join("\n"));
}
for (const depositEvent of depositEvents) {
const proof = depositTree.tree().getSingleProof(depositTree.gindexOfProperty(depositEvent.index));
console.log(depositEvent.index);
console.log(proof.map((buff) => toHexString(buff)).join("\n"));
}
}
learnProofs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment