-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit-units.ts
More file actions
78 lines (66 loc) · 2.08 KB
/
audit-units.ts
File metadata and controls
78 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Audit script to check for lingering milliseconds comparisons
*
* Usage: npm run audit:units
*
* Fails if any unlockTime/remainingUnlockTime comparisons use 1000n/5000n
* (which would be milliseconds, not seconds)
*/
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'
const srcDir = join(process.cwd(), 'src')
function collectTsFiles(dir: string, base: string = dir): string[] {
const entries = readdirSync(dir, { withFileTypes: true })
const files: string[] = []
for (const entry of entries) {
const fullPath = join(dir, entry.name)
if (entry.isDirectory()) {
if (['node_modules', 'dist', '.git'].includes(entry.name)) {
continue
}
files.push(...collectTsFiles(fullPath, base))
} else if (entry.isFile() && entry.name.endsWith('.ts')) {
files.push(fullPath.substring(base.length + 1))
}
}
return files
}
const files = collectTsFiles(srcDir)
// Patterns that indicate ms comparisons (should be < 1n or < 5n for seconds)
const patterns = [
/unlockTime\s*[<>]\s*1000n/g,
/unlockTime\s*[<>]\s*5000n/g,
/remainingUnlockTime\s*[<>]\s*1000n/g,
/remainingUnlockTime\s*[<>]\s*5000n/g,
]
let found = false
const errors: Array<{ file: string; line: number; match: string }> = []
files.forEach(file => {
const content = readFileSync(join(srcDir, file), 'utf8')
const lines = content.split('\n')
patterns.forEach(pattern => {
lines.forEach((line, index) => {
if (pattern.test(line)) {
found = true
errors.push({
file,
line: index + 1,
match: line.trim(),
})
}
})
})
})
if (found) {
console.error('❌ ERROR: Found milliseconds comparisons (should be seconds):\n')
errors.forEach(({ file, line, match }) => {
console.error(` ${file}:${line}`)
console.error(` ${match}`)
console.error('')
})
console.error('Fix: Replace 1000n with 1n, 5000n with 5n (unlockTime is in seconds)')
process.exit(1)
} else {
console.log('✓ No milliseconds comparisons found - all unlockTime checks use seconds')
process.exit(0)
}