#!/usr/bin/env node // SHA-256 of every contract file, so a hand-edited copy is noticed. // node checksums.mjs --check compare the files with CHECKSUMS (exit 1 on any difference) // node checksums.mjs --write rewrite CHECKSUMS (only in the IWDL repository, when the contract changes) // Node only, no dependency. Works on a copy of this folder anywhere. import { createHash } from 'node:crypto' import { readFileSync, readdirSync, writeFileSync, existsSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' const here = dirname(fileURLToPath(import.meta.url)) const FIXED = ['README.md', 'CHANGELOG.md', 'VERSION', 'schema.json', 'runner.mjs', 'checksums.mjs'] function files() { const examples = readdirSync(join(here, 'examples')) .filter((f) => f.endsWith('.json')) .sort() .map((f) => `examples/${f}`) return [...FIXED, ...examples] } function sums() { return files() .map((f) => `${createHash('sha256').update(readFileSync(join(here, f))).digest('hex')} ${f}`) .join('\n') + '\n' } const mode = process.argv[2] if (mode === '--write') { writeFileSync(join(here, 'CHECKSUMS'), sums()) console.log(`CHECKSUMS written (${files().length} files)`) } else if (mode === '--check') { if (!existsSync(join(here, 'CHECKSUMS'))) { console.error('CHECKSUMS is missing') process.exit(1) } const want = new Map( readFileSync(join(here, 'CHECKSUMS'), 'utf8').split('\n').filter(Boolean).map((l) => { const [sum, name] = l.split(' ') return [name, sum] }), ) const have = new Map(sums().split('\n').filter(Boolean).map((l) => { const [sum, name] = l.split(' ') return [name, sum] })) const problems = [] for (const [name, sum] of have) { if (!want.has(name)) problems.push(`not in CHECKSUMS: ${name}`) else if (want.get(name) !== sum) problems.push(`changed: ${name}`) } for (const name of want.keys()) if (!have.has(name)) problems.push(`missing: ${name}`) if (problems.length) { console.error(problems.join('\n')) console.error('\nThe contract files do not match CHECKSUMS. Copy them again from IWDL; do not edit a copy.') process.exit(1) } console.log(`contract ${readFileSync(join(here, 'VERSION'), 'utf8').trim()}: ${have.size} files match CHECKSUMS`) } else { console.error('usage: node checksums.mjs --check | --write') process.exit(2) }