|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Extract sample PSBT/TX data from fixtures for the parser UI. |
| 4 | + * |
| 5 | + * This script reads fixture files and extracts: |
| 6 | + * - psbtBase64: unsigned or partially signed PSBT |
| 7 | + * - psbtBase64Finalized: fully signed PSBT |
| 8 | + * - extractedTransaction: raw transaction hex |
| 9 | + * |
| 10 | + * Usage: node scripts/extract-samples.js |
| 11 | + */ |
| 12 | + |
| 13 | +const fs = require("fs"); |
| 14 | +const path = require("path"); |
| 15 | + |
| 16 | +const FIXTURES_DIR = path.resolve(__dirname, "../src/fixtures/fixed-script"); |
| 17 | +const OUTPUT_FILE = path.resolve(__dirname, "../src/wasm-utxo/parser/samples.ts"); |
| 18 | + |
| 19 | +function extractSamples() { |
| 20 | + const samples = []; |
| 21 | + |
| 22 | + // Read fixture files |
| 23 | + const files = fs.readdirSync(FIXTURES_DIR).filter((f) => f.endsWith(".json") && f.startsWith("psbt")); |
| 24 | + |
| 25 | + for (const file of files) { |
| 26 | + const filePath = path.join(FIXTURES_DIR, file); |
| 27 | + const content = fs.readFileSync(filePath, "utf-8"); |
| 28 | + |
| 29 | + let fixture; |
| 30 | + try { |
| 31 | + fixture = JSON.parse(content); |
| 32 | + } catch { |
| 33 | + console.warn(`Skipping invalid JSON: ${file}`); |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + // Extract name from filename: psbt-lite.bitcoin.unsigned.json -> Bitcoin Lite (unsigned) |
| 38 | + const match = file.match(/^psbt(-lite)?\.(\w+)\.(\w+)\.json$/); |
| 39 | + if (!match) continue; |
| 40 | + |
| 41 | + const [, lite, network, state] = match; |
| 42 | + const networkName = network.charAt(0).toUpperCase() + network.slice(1); |
| 43 | + const stateName = state.charAt(0).toUpperCase() + state.slice(1); |
| 44 | + const liteLabel = lite ? " Lite" : ""; |
| 45 | + |
| 46 | + // Add psbtBase64 (unsigned/halfsigned/fullsigned) |
| 47 | + if (fixture.psbtBase64) { |
| 48 | + samples.push({ |
| 49 | + name: `${networkName}${liteLabel} PSBT (${stateName})`, |
| 50 | + type: "psbt", |
| 51 | + data: fixture.psbtBase64, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Add psbtBase64Finalized (from fullsigned files only) |
| 56 | + if (fixture.psbtBase64Finalized && state === "fullsigned") { |
| 57 | + samples.push({ |
| 58 | + name: `${networkName}${liteLabel} PSBT (Finalized)`, |
| 59 | + type: "psbt", |
| 60 | + data: fixture.psbtBase64Finalized, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Add extractedTransaction |
| 65 | + if (fixture.extractedTransaction) { |
| 66 | + samples.push({ |
| 67 | + name: `${networkName}${liteLabel} TX (Extracted)`, |
| 68 | + type: "tx", |
| 69 | + data: fixture.extractedTransaction, |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Sort by name |
| 75 | + samples.sort((a, b) => a.name.localeCompare(b.name)); |
| 76 | + |
| 77 | + return samples; |
| 78 | +} |
| 79 | + |
| 80 | +function generateTypeScript(samples) { |
| 81 | + const lines = [ |
| 82 | + "/**", |
| 83 | + " * Sample PSBT/TX data extracted from test fixtures.", |
| 84 | + " * Auto-generated by scripts/extract-samples.js", |
| 85 | + " * DO NOT EDIT MANUALLY", |
| 86 | + " */", |
| 87 | + "", |
| 88 | + "export interface Sample {", |
| 89 | + " name: string;", |
| 90 | + ' type: "psbt" | "tx";', |
| 91 | + " data: string;", |
| 92 | + "}", |
| 93 | + "", |
| 94 | + "export const samples: Sample[] = [", |
| 95 | + ]; |
| 96 | + |
| 97 | + for (const sample of samples) { |
| 98 | + lines.push(" {"); |
| 99 | + lines.push(` name: ${JSON.stringify(sample.name)},`); |
| 100 | + lines.push(` type: ${JSON.stringify(sample.type)},`); |
| 101 | + lines.push(` data: ${JSON.stringify(sample.data)},`); |
| 102 | + lines.push(" },"); |
| 103 | + } |
| 104 | + |
| 105 | + lines.push("];"); |
| 106 | + lines.push(""); |
| 107 | + |
| 108 | + return lines.join("\n"); |
| 109 | +} |
| 110 | + |
| 111 | +function main() { |
| 112 | + console.log("Extracting samples from fixtures..."); |
| 113 | + |
| 114 | + const samples = extractSamples(); |
| 115 | + console.log(`Found ${samples.length} samples`); |
| 116 | + |
| 117 | + const typescript = generateTypeScript(samples); |
| 118 | + |
| 119 | + // Ensure output directory exists |
| 120 | + const outputDir = path.dirname(OUTPUT_FILE); |
| 121 | + if (!fs.existsSync(outputDir)) { |
| 122 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 123 | + } |
| 124 | + |
| 125 | + fs.writeFileSync(OUTPUT_FILE, typescript); |
| 126 | + console.log(`Written to ${OUTPUT_FILE}`); |
| 127 | +} |
| 128 | + |
| 129 | +main(); |
| 130 | + |
0 commit comments