-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrade.sh
More file actions
executable file
·560 lines (462 loc) · 15.5 KB
/
Copy pathgrade.sh
File metadata and controls
executable file
·560 lines (462 loc) · 15.5 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env bash
set -euo pipefail
###############################################################################
# grade.sh — Coin Smith PSBT Builder Grader (Week 2)
#
# Validates cli.sh output against all public fixtures.
# Dependencies: bash, jq, base64
###############################################################################
# ── Constants & colors ───────────────────────────────────────────────────────
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
BOLD='\033[1m'
RESET='\033[0m'
FIXTURES_DIR="fixtures"
OUT_DIR="out"
TOTAL_FIXTURES=0
PASSED_FIXTURES=0
FAILED_FIXTURES=0
# ── Locate jq ────────────────────────────────────────────────────────────────
JQ=""
for candidate in jq /opt/homebrew/bin/jq /usr/local/bin/jq; do
if command -v "$candidate" &>/dev/null; then
JQ="$candidate"
break
fi
done
if [[ -z "$JQ" ]]; then
echo "Error: jq is required but not found. Install it with: brew install jq" >&2
exit 1
fi
# ── Detect base64 decode flag (macOS = -D, Linux = -d) ───────────────────────
B64_DECODE="-d"
if base64 -D </dev/null &>/dev/null 2>&1; then
B64_DECODE="-D"
fi
# ── Utility functions ────────────────────────────────────────────────────────
# Per-fixture counters (set by caller, modified by pass/fail)
# fixture_pass and fixture_fail are managed in run_fixture
pass() {
local msg="$1"
printf " ${GREEN}✓${RESET} %s\n" "$msg"
((fixture_pass++)) || true
}
fail() {
local msg="$1"
printf " ${RED}✗${RESET} %s\n" "$msg"
((fixture_fail++)) || true
}
warn() {
local msg="$1"
printf " ${YELLOW}!${RESET} %s\n" "$msg"
}
# ── Check functions ──────────────────────────────────────────────────────────
check_cli_exit() {
local fixture_path="$1"
if ./cli.sh "$fixture_path" >/dev/null 2>&1; then
pass "cli.sh exits 0"
return 0
else
fail "cli.sh exits non-zero"
return 1
fi
}
check_output_exists() {
local output_path="$1"
if [[ ! -f "$output_path" ]]; then
fail "Output file missing: $output_path"
return 1
fi
if ! $JQ empty "$output_path" 2>/dev/null; then
fail "Output file is not valid JSON"
return 1
fi
local ok
ok=$($JQ -r '.ok // empty' "$output_path")
if [[ "$ok" != "true" ]]; then
fail "Output .ok is not true (got: ${ok:-null})"
return 1
fi
pass "Output file exists and is valid JSON"
return 0
}
check_required_fields() {
local output_path="$1"
local missing
missing=$($JQ -r '
["ok","network","strategy","selected_inputs","outputs","change_index",
"fee_sats","fee_rate_sat_vb","vbytes","rbf_signaling","locktime",
"locktime_type","psbt_base64","warnings"] - keys | .[]
' "$output_path")
if [[ -z "$missing" ]]; then
pass "Required fields present"
return 0
else
fail "Missing fields: $(echo "$missing" | tr '\n' ', ' | sed 's/,$//')"
return 1
fi
}
check_selected_inputs() {
local output_path="$1"
local fixture_path="$2"
# Non-empty array
local count
count=$($JQ '.selected_inputs | length' "$output_path")
if [[ "$count" -eq 0 ]]; then
fail "selected_inputs is empty"
return 1
fi
# Every selected input matches a fixture UTXO by txid+vout
local invalid
invalid=$($JQ --slurpfile fixture "$fixture_path" '
[.selected_inputs[] |
{txid, vout} as $sel |
select(
[$fixture[0].utxos[] | select(.txid == $sel.txid and .vout == $sel.vout)] | length == 0
)
] | length
' "$output_path")
if [[ "$invalid" -ne 0 ]]; then
fail "selected_inputs contains $invalid input(s) not in fixture UTXOs"
return 1
fi
# Respect policy.max_inputs
local max_inputs
max_inputs=$($JQ '.policy.max_inputs // empty' "$fixture_path")
if [[ -n "$max_inputs" ]] && [[ "$count" -gt "$max_inputs" ]]; then
fail "selected_inputs count ($count) exceeds policy.max_inputs ($max_inputs)"
return 1
fi
pass "Selected inputs valid ($count input(s))"
return 0
}
check_outputs() {
local output_path="$1"
local fixture_path="$2"
# All fixture payments appear in outputs (multiset-aware: sort both, compare)
local payments_missing
payments_missing=$($JQ --slurpfile fixture "$fixture_path" '
# Build sorted list of expected payments as "spk:value"
def payment_keys: [.[] | "\(.script_pubkey_hex):\(.value_sats)"] | sort;
# Expected from fixture
($fixture[0].payments | payment_keys) as $expected |
# Actual non-change outputs
([.outputs[] | select(.is_change != true)] | payment_keys) as $actual |
# Check each expected appears in actual (multiset check)
# Reduce: for each expected key, remove first match from remaining actual
reduce $expected[] as $e (
{ missing: [], remaining: $actual };
(.remaining | index($e)) as $idx |
if $idx != null then
.remaining |= (.[0:$idx] + .[$idx+1:])
else
.missing += [$e]
end
) | .missing | length
' "$output_path")
if [[ "$payments_missing" -ne 0 ]]; then
fail "Missing $payments_missing fixture payment(s) in outputs"
return 1
fi
# At most 1 change output
local change_count
change_count=$($JQ '[.outputs[] | select(.is_change == true)] | length' "$output_path")
if [[ "$change_count" -gt 1 ]]; then
fail "Multiple change outputs found ($change_count)"
return 1
fi
# change_index consistency
local change_index
change_index=$($JQ -r '.change_index // "null"' "$output_path")
if [[ "$change_count" -eq 0 ]]; then
if [[ "$change_index" != "null" ]]; then
fail "change_index should be null when no change output (got: $change_index)"
return 1
fi
else
# Find actual position of change output
local actual_index
actual_index=$($JQ '
[.outputs | to_entries[] | select(.value.is_change == true) | .key] | first
' "$output_path")
if [[ "$change_index" != "$actual_index" ]]; then
fail "change_index ($change_index) doesn't match actual change position ($actual_index)"
return 1
fi
fi
pass "Outputs valid (${change_count} change output(s))"
return 0
}
check_balance_and_fee() {
local output_path="$1"
local fixture_path="$2"
local result
result=$($JQ --slurpfile fixture "$fixture_path" '
# Sum selected input values (look up from fixture UTXOs)
(.selected_inputs | map(
. as $sel |
$fixture[0].utxos[] | select(.txid == $sel.txid and .vout == $sel.vout) | .value_sats
) | add // 0) as $input_sum |
# Sum output values
(.outputs | map(.value_sats) | add // 0) as $output_sum |
.fee_sats as $fee |
.fee_rate_sat_vb as $reported_rate |
.vbytes as $vbytes |
$fixture[0].fee_rate_sat_vb as $target_rate |
# Balance check: inputs == outputs + fee
($input_sum == $output_sum + $fee) as $balance_ok |
# Fee meets target: fee >= ceil(vbytes * target_rate)
# ceil(a*b) = integer part, but we allow equality with ceiling
(($vbytes * $target_rate) | ceil) as $min_fee |
($fee >= $min_fee) as $fee_target_ok |
# Fee rate accuracy: |fee/vbytes - reported_rate| <= 0.01
(($fee / $vbytes) - $reported_rate | fabs) as $rate_diff |
($rate_diff <= 0.01) as $rate_ok |
# No dust: every output >= 546 sats
([.outputs[] | select(.value_sats < 546)] | length) as $dust_count |
{
balance_ok: $balance_ok,
fee_target_ok: $fee_target_ok,
rate_ok: $rate_ok,
dust_count: $dust_count,
input_sum: $input_sum,
output_sum: $output_sum,
fee: $fee,
min_fee: $min_fee,
reported_rate: $reported_rate,
actual_rate: ($fee / $vbytes),
vbytes: $vbytes
}
' "$output_path")
local balance_ok fee_target_ok rate_ok dust_count
balance_ok=$(echo "$result" | $JQ -r '.balance_ok')
fee_target_ok=$(echo "$result" | $JQ -r '.fee_target_ok')
rate_ok=$(echo "$result" | $JQ -r '.rate_ok')
dust_count=$(echo "$result" | $JQ -r '.dust_count')
local all_ok=true
if [[ "$balance_ok" == "true" ]]; then
pass "Balance equation: sum(inputs) == sum(outputs) + fee"
else
local input_sum output_sum fee
input_sum=$(echo "$result" | $JQ -r '.input_sum')
output_sum=$(echo "$result" | $JQ -r '.output_sum')
fee=$(echo "$result" | $JQ -r '.fee')
fail "Balance mismatch: inputs=$input_sum, outputs=$output_sum, fee=$fee"
all_ok=false
fi
if [[ "$fee_target_ok" == "true" ]]; then
pass "Fee meets target rate"
else
local fee min_fee
fee=$(echo "$result" | $JQ -r '.fee')
min_fee=$(echo "$result" | $JQ -r '.min_fee')
fail "Fee too low: $fee < minimum $min_fee"
all_ok=false
fi
if [[ "$rate_ok" == "true" ]]; then
pass "Fee rate accuracy within tolerance"
else
local reported_rate actual_rate
reported_rate=$(echo "$result" | $JQ -r '.reported_rate')
actual_rate=$(echo "$result" | $JQ -r '.actual_rate')
fail "Fee rate inaccurate: reported=$reported_rate, actual=$actual_rate"
all_ok=false
fi
if [[ "$dust_count" -eq 0 ]]; then
pass "No dust outputs"
else
fail "$dust_count output(s) below dust threshold (546 sats)"
all_ok=false
fi
[[ "$all_ok" == "true" ]]
}
check_rbf_locktime() {
local output_path="$1"
local all_ok=true
# rbf_signaling is boolean
local rbf_type
rbf_type=$($JQ -r '.rbf_signaling | type' "$output_path")
if [[ "$rbf_type" == "boolean" ]]; then
pass "rbf_signaling is boolean"
else
fail "rbf_signaling is $rbf_type (expected boolean)"
all_ok=false
fi
# locktime is number
local lt_type
lt_type=$($JQ -r '.locktime | type' "$output_path")
if [[ "$lt_type" == "number" ]]; then
pass "locktime is number"
else
fail "locktime is $lt_type (expected number)"
all_ok=false
fi
# locktime_type is one of the valid values
local lt_type_val
lt_type_val=$($JQ -r '.locktime_type' "$output_path")
case "$lt_type_val" in
none|block_height|unix_timestamp)
pass "locktime_type is valid (\"$lt_type_val\")"
;;
*)
fail "locktime_type is \"$lt_type_val\" (expected none|block_height|unix_timestamp)"
all_ok=false
;;
esac
[[ "$all_ok" == "true" ]]
}
check_warnings() {
local output_path="$1"
local all_ok=true
# Extract warning codes from output
local warning_codes
warning_codes=$($JQ -r '[.warnings[]?.code] | join(",")' "$output_path")
has_warning() {
echo ",$warning_codes," | grep -q ",$1,"
}
# No change → SEND_ALL required
local change_count
change_count=$($JQ '[.outputs[] | select(.is_change == true)] | length' "$output_path")
if [[ "$change_count" -eq 0 ]]; then
if has_warning "SEND_ALL"; then
pass "SEND_ALL warning present (no change output)"
else
fail "SEND_ALL warning missing (no change output)"
all_ok=false
fi
fi
# HIGH_FEE checks
local needs_high_fee
needs_high_fee=$($JQ '
(.fee_sats > 1000000) or (.fee_rate_sat_vb > 200)
' "$output_path")
if [[ "$needs_high_fee" == "true" ]]; then
if has_warning "HIGH_FEE"; then
pass "HIGH_FEE warning present"
else
fail "HIGH_FEE warning missing (fee_sats > 1M or fee_rate > 200)"
all_ok=false
fi
fi
# RBF_SIGNALING check
local rbf_signaling
rbf_signaling=$($JQ -r '.rbf_signaling' "$output_path")
if [[ "$rbf_signaling" == "true" ]]; then
if has_warning "RBF_SIGNALING"; then
pass "RBF_SIGNALING warning present"
else
fail "RBF_SIGNALING warning missing (rbf_signaling is true)"
all_ok=false
fi
fi
# If none of the conditional checks triggered, just validate warnings is an array
local warnings_type
warnings_type=$($JQ -r '.warnings | type' "$output_path")
if [[ "$warnings_type" == "array" ]]; then
pass "warnings is a valid array"
else
fail "warnings is $warnings_type (expected array)"
all_ok=false
fi
[[ "$all_ok" == "true" ]]
}
check_psbt() {
local output_path="$1"
local psbt
psbt=$($JQ -r '.psbt_base64' "$output_path")
if [[ -z "$psbt" ]] || [[ "$psbt" == "null" ]]; then
fail "psbt_base64 is empty or null"
return 1
fi
# Base64 decode succeeds
local raw
if ! raw=$(echo "$psbt" | base64 $B64_DECODE 2>/dev/null); then
fail "psbt_base64 is not valid base64"
return 1
fi
# First 5 bytes = psbt magic: 70736274ff
local magic
magic=$(echo "$psbt" | base64 $B64_DECODE 2>/dev/null | xxd -p -l 5)
if [[ "$magic" == "70736274ff" ]]; then
pass "PSBT magic bytes valid (psbt\\xff)"
else
fail "PSBT magic bytes invalid: $magic (expected 70736274ff)"
return 1
fi
return 0
}
# ── Per-fixture driver ───────────────────────────────────────────────────────
run_fixture() {
local fixture_path="$1"
local name
name=$(basename "$fixture_path" .json)
local output_path="$OUT_DIR/$name.json"
printf "\n${BOLD}=== %s ===${RESET}\n" "$name"
local fixture_pass=0
local fixture_fail=0
# A. CLI exit
if ! check_cli_exit "$fixture_path"; then
printf " ${RED}FAIL${RESET} (%d/%d)\n" "$fixture_pass" "$((fixture_pass + fixture_fail))"
((FAILED_FIXTURES++)) || true
return
fi
# B. Output exists and is valid JSON
if ! check_output_exists "$output_path"; then
printf " ${RED}FAIL${RESET} (%d/%d)\n" "$fixture_pass" "$((fixture_pass + fixture_fail))"
((FAILED_FIXTURES++)) || true
return
fi
# C. Required fields
check_required_fields "$output_path" || true
# D. Selected inputs
check_selected_inputs "$output_path" "$fixture_path" || true
# E. Outputs
check_outputs "$output_path" "$fixture_path" || true
# F. Balance and fee
check_balance_and_fee "$output_path" "$fixture_path" || true
# G. RBF / locktime types
check_rbf_locktime "$output_path" || true
# H. Warnings
check_warnings "$output_path" || true
# I. PSBT validation
check_psbt "$output_path" || true
# Per-fixture summary
local total=$((fixture_pass + fixture_fail))
if [[ "$fixture_fail" -eq 0 ]]; then
printf " ${GREEN}PASS${RESET} (%d/%d)\n" "$fixture_pass" "$total"
((PASSED_FIXTURES++)) || true
else
printf " ${RED}FAIL${RESET} (%d/%d)\n" "$fixture_pass" "$total"
((FAILED_FIXTURES++)) || true
fi
}
# ── Main ─────────────────────────────────────────────────────────────────────
printf "\n${BOLD}Coin Smith PSBT Builder — Grader${RESET}\n"
printf "=================================\n"
# Clean output directory
rm -rf "$OUT_DIR"
mkdir -p "$OUT_DIR"
# Run setup if exists
if [[ -f "setup.sh" ]]; then
printf "\nRunning setup.sh...\n"
bash setup.sh >/dev/null 2>&1 || true
fi
# Make cli.sh executable
chmod +x cli.sh 2>/dev/null || true
# Loop over all fixtures
for fixture in "$FIXTURES_DIR"/*.json; do
[[ -f "$fixture" ]] || continue
((TOTAL_FIXTURES++)) || true
run_fixture "$fixture"
done
# Summary
printf "\n${BOLD}=== SUMMARY ===${RESET}\n"
printf " %d/%d fixtures passed\n" "$PASSED_FIXTURES" "$TOTAL_FIXTURES"
if [[ "$FAILED_FIXTURES" -eq 0 ]]; then
printf " ${GREEN}All fixtures passed!${RESET}\n\n"
exit 0
else
printf " ${RED}%d fixture(s) failed.${RESET}\n\n" "$FAILED_FIXTURES"
exit 1
fi