-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchittyfix-id-patterns.sh
More file actions
executable file
·228 lines (191 loc) · 6.85 KB
/
chittyfix-id-patterns.sh
File metadata and controls
executable file
·228 lines (191 loc) · 6.85 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# ChittyFix ID Patterns - Automatic Rogue ID Pattern Fixer
# Fixes crypto.randomUUID(), Math.random(), and other local ID generation patterns
# Replaces with proper ChittyID service calls
set -euo pipefail
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
echo -e "${CYAN}🔧 ChittyFix ID Patterns - Automated ID Pattern Fixer${NC}"
echo "════════════════════════════════════════════════════════"
# Configuration
DRY_RUN=${1:-true}
TARGET_DIR="${2:-.}"
FIXES_APPLIED=0
FILES_MODIFIED=0
# Exclude patterns
EXCLUDE_DIRS="node_modules|archive|deprecated|backup|legacy|\.git"
EXCLUDE_FILES="\.test\.|\.spec\.|\.backup"
log_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
log_success() {
echo -e "${GREEN}✅${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
log_error() {
echo -e "${RED}❌${NC} $1"
}
# Fix crypto.randomUUID() pattern
fix_crypto_random_uuid() {
local file="$1"
local context="$2" # 'session', 'version', 'transaction', etc.
log_info "Fixing crypto.randomUUID() in $file (context: $context)"
# Backup original
cp "$file" "$file.chittyfix.bak"
# Determine appropriate domain/subtype based on context
local domain="unknown"
local subtype="generated"
case "$context" in
session*)
domain="session"
subtype="coordination"
;;
version*)
domain="state"
subtype="version"
;;
transaction*)
domain="transaction"
subtype="event"
;;
*)
domain="entity"
subtype="generated"
;;
esac
# Create fix pattern based on file type
if [[ "$file" == *.ts || "$file" == *.tsx ]]; then
# TypeScript fix
cat > /tmp/chittyfix_replace.txt << EOF
// Request ChittyID from service (§36 compliant)
const chittyIdResponse = await fetch('https://id.chitty.cc/v1/mint', {
method: 'POST',
headers: {
'Authorization': \`Bearer \${env.CHITTY_ID_TOKEN || process.env.CHITTY_ID_TOKEN}\`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
domain: '$domain',
subtype: '$subtype',
metadata: { source: '$(basename $file)' }
})
});
if (!chittyIdResponse.ok) {
throw new Error(\`ChittyID service unavailable: \${chittyIdResponse.status}\`);
}
const { chitty_id } = await chittyIdResponse.json();
// Use chitty_id instead of crypto.randomUUID()
EOF
else
# JavaScript fix
cat > /tmp/chittyfix_replace.txt << EOF
// Request ChittyID from service (§36 compliant)
const chittyIdResponse = await fetch('https://id.chitty.cc/v1/mint', {
method: 'POST',
headers: {
'Authorization': \`Bearer \${process.env.CHITTY_ID_TOKEN}\`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
domain: '$domain',
subtype: '$subtype',
metadata: { source: '$(basename $file)' }
})
});
if (!chittyIdResponse.ok) {
throw new Error(\`ChittyID service unavailable: \${chittyIdResponse.status}\`);
}
const { chitty_id } = await chittyIdResponse.json();
// Use chitty_id instead of crypto.randomUUID()
EOF
fi
if [ "$DRY_RUN" = "false" ]; then
# Note: This is a template - actual sed replacement would need precise context
log_warning "Manual review required: $file"
log_warning " Replace crypto.randomUUID() with ChittyID service call"
log_warning " Template saved to /tmp/chittyfix_replace.txt"
FILES_MODIFIED=$((FILES_MODIFIED + 1))
else
log_warning "[DRY RUN] Would fix crypto.randomUUID() in $file"
fi
FIXES_APPLIED=$((FIXES_APPLIED + 1))
}
# Fix Math.random() pattern
fix_math_random() {
local file="$1"
log_info "Fixing Math.random().toString(36) in $file"
if [ "$DRY_RUN" = "false" ]; then
log_warning "Manual review required: $file"
log_warning " Replace Math.random() ID generation with ChittyID service"
FILES_MODIFIED=$((FILES_MODIFIED + 1))
else
log_warning "[DRY RUN] Would fix Math.random() in $file"
fi
FIXES_APPLIED=$((FIXES_APPLIED + 1))
}
# Scan and fix files
scan_and_fix() {
log_info "Scanning for rogue ID patterns in $TARGET_DIR..."
# Find crypto.randomUUID() violations
while IFS= read -r file; do
# Skip excluded paths
if echo "$file" | grep -qE "$EXCLUDE_DIRS|$EXCLUDE_FILES"; then
continue
fi
# Detect context from surrounding code
local context=$(grep -B 3 "crypto.randomUUID()" "$file" | grep -i "session\|version\|transaction" | head -1 || echo "unknown")
if [[ "$context" =~ session ]]; then
fix_crypto_random_uuid "$file" "session"
elif [[ "$context" =~ version ]]; then
fix_crypto_random_uuid "$file" "version"
elif [[ "$context" =~ transaction ]]; then
fix_crypto_random_uuid "$file" "transaction"
else
fix_crypto_random_uuid "$file" "unknown"
fi
done < <(grep -rl "crypto\.randomUUID()" "$TARGET_DIR" --include="*.js" --include="*.ts" --include="*.tsx" 2>/dev/null || true)
# Find Math.random() violations
while IFS= read -r file; do
# Skip excluded paths
if echo "$file" | grep -qE "$EXCLUDE_DIRS|$EXCLUDE_FILES"; then
continue
fi
fix_math_random "$file"
done < <(grep -rl "Math\.random()\.toString(36)" "$TARGET_DIR" --include="*.js" --include="*.ts" 2>/dev/null || true)
}
# Main execution
if [ "$DRY_RUN" = "true" ]; then
log_warning "Running in DRY RUN mode - no files will be modified"
log_warning "Run with: $0 false <directory> to apply fixes"
echo ""
fi
scan_and_fix
# Summary
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}📊 ChittyFix ID Patterns Summary${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e " Fixes identified: ${YELLOW}$FIXES_APPLIED${NC}"
echo -e " Files requiring manual review: ${YELLOW}$FILES_MODIFIED${NC}"
if [ "$DRY_RUN" = "true" ]; then
echo ""
log_warning "This was a DRY RUN - no changes were made"
log_info "To apply fixes: $0 false"
else
echo ""
log_success "Fixes have been identified"
log_warning "⚠️ IMPORTANT: Manual code review required!"
log_warning " - Review backup files (*.chittyfix.bak)"
log_warning " - Check /tmp/chittyfix_replace.txt for templates"
log_warning " - Verify ChittyID service integration"
log_warning " - Test functionality after changes"
fi
echo ""
exit 0