Skip to content

Instantly share code, notes, and snippets.

@dapplion
Created May 30, 2023 17:55
Show Gist options
  • Save dapplion/2e75344415d07b527a92df0707ba0444 to your computer and use it in GitHub Desktop.
Save dapplion/2e75344415d07b527a92df0707ba0444 to your computer and use it in GitHub Desktop.
Verify signature of example payload from beacon chain
import { ssz } from "@lodestar/types";
import { createBeaconConfig } from "@lodestar/config";
import { fromHexString, toHexString } from "@chainsafe/ssz";
import bls from "@chainsafe/bls";
// To run, first install dependencies:
// $ npm install @lodestar/types @lodestar/config @chainsafe/ssz @chainsafe/bls
//
// Then
// $ node signature_verify.mjs
const payload = {
version: "capella",
data: {
message: {
header: {
parent_hash:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
fee_recipient: "0xabcf8e0d4e9587369b2301d0790347320302cc09",
state_root:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
receipts_root:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
logs_bloom:
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
prev_randao:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
block_number: "1",
gas_limit: "1",
gas_used: "1",
timestamp: "1",
extra_data:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
base_fee_per_gas: "1",
block_hash:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
transactions_root:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
withdrawals_root:
"0xcf8e0d4e9587369b2301d0790347320302cc0943d5a1884560367e8208d920f2",
},
value: "1",
pubkey:
"0x93247f2209abcacf57b75a51dafae777f9dd38bc7053d1af526f220a7489a6d3a2753e5f3e8b1cfe39b56f43611df74a",
},
signature:
"0x1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505cc411d61252fb6cb3fa0017b679f8bb2305b26a285fa2737f175668d0dff91cc1b66ac1fb663c9bc59509846d6ec05345bd908eda73e670af888da41af171505",
},
};
const headerRoot = ssz.capella.ExecutionPayloadHeader.hashTreeRoot(
ssz.capella.ExecutionPayloadHeader.fromJson(payload.data.message.header)
);
// TODO: Set correct slot for domain's fork
const signingSlot = 0;
// TODO: Set to correct signing domain
const DOMAIN_EXECUTION_HEADER = new Uint8Array([0, 0, 0, 0]);
// TODO: Set to correct network's genesis validators root
const GENESIS_VALIDATORS_ROOT =
"0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95";
const config = createBeaconConfig({}, fromHexString(GENESIS_VALIDATORS_ROOT));
const domain = config.getDomain(signingSlot, DOMAIN_EXECUTION_HEADER);
const signingRoot = ssz.phase0.SigningData.hashTreeRoot({
objectRoot: headerRoot,
domain,
});
const isValid = bls.verify(
fromHexString(payload.data.message.pubkey),
signingRoot,
fromHexString(payload.data.signature)
);
console.log({
signingRoot: toHexString(signingRoot),
isValid,
});
@wanderingelephants
Copy link

returns false. any sample data where it passes ?

{
signingRoot: '0xcfa871ec6f6f46bccf5cbbc4660d812e51418f80929710b46e7509a5e502a097',
isValid: false
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment