-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextract-samples.js
More file actions
130 lines (106 loc) · 3.46 KB
/
Copy pathextract-samples.js
File metadata and controls
130 lines (106 loc) · 3.46 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
#!/usr/bin/env node
/**
* Extract sample PSBT/TX data from fixtures for the parser UI.
*
* This script reads fixture files and extracts:
* - psbtBase64: unsigned or partially signed PSBT
* - psbtBase64Finalized: fully signed PSBT
* - extractedTransaction: raw transaction hex
*
* Usage: node scripts/extract-samples.js
*/
const fs = require("fs");
const path = require("path");
const FIXTURES_DIR = path.resolve(__dirname, "../src/fixtures/fixed-script");
const OUTPUT_FILE = path.resolve(__dirname, "../src/wasm-utxo/parser/samples.ts");
function extractSamples() {
const samples = [];
// Read fixture files
const files = fs.readdirSync(FIXTURES_DIR).filter((f) => f.endsWith(".json") && f.startsWith("psbt"));
for (const file of files) {
const filePath = path.join(FIXTURES_DIR, file);
const content = fs.readFileSync(filePath, "utf-8");
let fixture;
try {
fixture = JSON.parse(content);
} catch {
console.warn(`Skipping invalid JSON: ${file}`);
continue;
}
// Extract name from filename: psbt-lite.bitcoin.unsigned.json -> Bitcoin Lite (unsigned)
const match = file.match(/^psbt(-lite)?\.(\w+)\.(\w+)\.json$/);
if (!match) continue;
const [, lite, network, state] = match;
const networkName = network.charAt(0).toUpperCase() + network.slice(1);
const stateName = state.charAt(0).toUpperCase() + state.slice(1);
const liteLabel = lite ? " Lite" : "";
// Add psbtBase64 (unsigned/halfsigned/fullsigned)
if (fixture.psbtBase64) {
samples.push({
name: `${networkName}${liteLabel} PSBT (${stateName})`,
type: "psbt",
data: fixture.psbtBase64,
});
}
// Add psbtBase64Finalized (from fullsigned files only)
if (fixture.psbtBase64Finalized && state === "fullsigned") {
samples.push({
name: `${networkName}${liteLabel} PSBT (Finalized)`,
type: "psbt",
data: fixture.psbtBase64Finalized,
});
}
// Add extractedTransaction
if (fixture.extractedTransaction) {
samples.push({
name: `${networkName}${liteLabel} TX (Extracted)`,
type: "tx",
data: fixture.extractedTransaction,
});
}
}
// Sort by name
samples.sort((a, b) => a.name.localeCompare(b.name));
return samples;
}
function generateTypeScript(samples) {
const lines = [
"/**",
" * Sample PSBT/TX data extracted from test fixtures.",
" * Auto-generated by scripts/extract-samples.js",
" * DO NOT EDIT MANUALLY",
" */",
"",
"export interface Sample {",
" name: string;",
' type: "psbt" | "tx";',
" data: string;",
"}",
"",
"export const samples: Sample[] = [",
];
for (const sample of samples) {
lines.push(" {");
lines.push(` name: ${JSON.stringify(sample.name)},`);
lines.push(` type: ${JSON.stringify(sample.type)},`);
lines.push(` data: ${JSON.stringify(sample.data)},`);
lines.push(" },");
}
lines.push("];");
lines.push("");
return lines.join("\n");
}
function main() {
console.log("Extracting samples from fixtures...");
const samples = extractSamples();
console.log(`Found ${samples.length} samples`);
const typescript = generateTypeScript(samples);
// Ensure output directory exists
const outputDir = path.dirname(OUTPUT_FILE);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(OUTPUT_FILE, typescript);
console.log(`Written to ${OUTPUT_FILE}`);
}
main();