import {BeaconConfig} from "@lodestar/config";
import {DOMAIN_SELECTION_PROOF} from "@lodestar/params";
import {ISignatureSet, SignatureSetType, computeSigningRoot} from "@lodestar/state-transition";
import {Slot, ValidatorIndex, phase0, ssz} from "@lodestar/types";

export function getSelectionProofSigningRoot(config: BeaconConfig, slot: Slot): Uint8Array {
  // previously, we call `const selectionProofDomain = config.getDomain(state.slot, DOMAIN_SELECTION_PROOF, slot)`
  // at fork boundary, it's required to dial to target epoch https://github.com/ChainSafe/lodestar/blob/v1.11.3/packages/beacon-node/src/chain/validation/attestation.ts#L573
  // instead of that, just use the fork of slot in the attestation data
  const fork = config.getForkName(slot);
  const selectionProofDomain = config.getDomainAtFork(fork, DOMAIN_SELECTION_PROOF);
  return computeSigningRoot(ssz.Slot, slot, selectionProofDomain);
}

export function getSelectionProofSignatureSet(
  config: BeaconConfig,
  slot: Slot,
  aggregatorIndex: ValidatorIndex,
  aggregateAndProof: phase0.SignedAggregateAndProof
): ISignatureSet {
  return {
    type: SignatureSetType.indexed,
    index: aggregatorIndex,
    signingRoot: getSelectionProofSigningRoot(config, slot),
    signature: aggregateAndProof.message.selectionProof,
  };
}
