-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.js
More file actions
executable file
·215 lines (177 loc) · 7.33 KB
/
Copy pathdemo.js
File metadata and controls
executable file
·215 lines (177 loc) · 7.33 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
#!/usr/bin/env node
/**
* Complete TEST_OPI demonstration script
*
* This script demonstrates all framework features
* without requiring a real Bitcoin connection
*/
import pc from 'picocolors';
import PsbtBuilder from './src/core/PsbtBuilder.js';
import TestOpiOperation from './src/operations/TestOpiOperation.js';
import OperationRegistry from './src/operations/OperationRegistry.js';
import Logger from './src/utils/Logger.js';
async function runDemo() {
console.log(pc.bold(pc.blue('\n🚀 TEST_OPI - Complete Demonstration\n')));
try {
// 1. Operations demonstration
console.log(pc.yellow('📋 1. OPI Operations Demonstration'));
console.log('─'.repeat(50));
const testOpi = new TestOpiOperation();
// Create different operations
const operations = [
testOpi.createDefaultOperation('test_opi', '10'),
testOpi.createDefaultOperation('demo', '100'),
testOpi.createCustomOperation({ tick: 'custom', amt: '50' })
];
operations.forEach((op, index) => {
console.log(pc.green(` Operation ${index + 1}:`));
console.log(pc.gray(` ${JSON.stringify(op, null, 2)}`));
});
// 2. OP_RETURN messages demonstration
console.log(pc.yellow('\n📝 2. OP_RETURN Messages'));
console.log('─'.repeat(50));
const opReturnMessages = operations.map(op =>
testOpi.createOpReturnMessage(op.tick, op.amt)
);
opReturnMessages.forEach((message, index) => {
console.log(pc.green(` Message ${index + 1}:`));
console.log(pc.gray(` ${message}`));
console.log(pc.gray(` Size: ${message.length} characters`));
});
// 3. PSBT builder demonstration
console.log(pc.yellow('\n🔧 3. PSBT Construction'));
console.log('─'.repeat(50));
const psbtBuilder = new PsbtBuilder();
// Address type detection
const addresses = [
'bc1p2p6d0pzwhz5g0u002em0uj303hmhkh3gqs2nh6hxuw90rwkzg5nsatsphn',
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx',
'1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
];
addresses.forEach(addr => {
const type = psbtBuilder.determineAddressType(addr);
console.log(pc.green(` ${addr.substring(0, 20)}... → ${type}`));
});
// 4. Test scenarios demonstration
console.log(pc.yellow('\n🎯 4. Test Scenarios'));
console.log('─'.repeat(50));
const scenarios = [
{
name: 'Single OP_RETURN',
description: 'Transaction with only OP_RETURN',
outputs: 1
},
{
name: 'Two Outputs',
description: 'OP_RETURN + output to test address',
outputs: 2
},
{
name: 'External Address',
description: 'OP_RETURN + output to external address',
outputs: 2
},
{
name: 'Three Outputs',
description: 'OP_RETURN in position 0 + 2 other outputs',
outputs: 3
}
];
scenarios.forEach((scenario, index) => {
console.log(pc.green(` ${index + 1}. ${scenario.name}`));
console.log(pc.gray(` ${scenario.description}`));
console.log(pc.gray(` Outputs: ${scenario.outputs}`));
});
// 5. Detailed PSBT construction demonstration
console.log(pc.yellow('\n🏗️ 5. Detailed PSBT Construction'));
console.log('─'.repeat(50));
// Scenario 1: Single OP_RETURN
const opReturnScript = psbtBuilder.createOpReturnScript(opReturnMessages[0]);
const psbt1 = psbtBuilder.buildPsbt([], [{
script: opReturnScript,
value: 0
}]);
console.log(pc.green(' Scenario 1 - Single OP_RETURN:'));
console.log(pc.gray(` Inputs: ${psbt1.txInputs.length}`));
console.log(pc.gray(` Outputs: ${psbt1.txOutputs.length}`));
console.log(pc.gray(` PSBT: ${psbt1.toHex().substring(0, 50)}...`));
// Scenario 2: Two Outputs (uses configured test address)
const testAddress = 'bc1p2p6d0pzwhz5g0u002em0uj303hmhkh3gqs2nh6hxuw90rwkzg5nsatsphn';
const outputScript = psbtBuilder.createOutputScript(testAddress);
const psbt2 = psbtBuilder.buildPsbt([], [
{ script: opReturnScript, value: 0 },
{ script: outputScript, value: 1200 }
]);
console.log(pc.green('\n Scenario 2 - Two Outputs:'));
console.log(pc.gray(` Inputs: ${psbt2.txInputs.length}`));
console.log(pc.gray(` Outputs: ${psbt2.txOutputs.length}`));
console.log(pc.gray(` PSBT: ${psbt2.toHex().substring(0, 50)}...`));
// 6. Size and fee estimations demonstration
console.log(pc.yellow('\n📊 6. Size and Fee Estimations'));
console.log('─'.repeat(50));
const sizes = [
{ inputs: 1, outputs: 1, type: 'P2WPKH' },
{ inputs: 2, outputs: 2, type: 'P2WPKH' },
{ inputs: 1, outputs: 1, type: 'P2TR' },
{ inputs: 3, outputs: 3, type: 'P2WPKH' }
];
sizes.forEach((config, index) => {
const size = psbtBuilder.estimateTransactionSize(
config.inputs,
config.outputs,
config.type
);
const fee = psbtBuilder.calculateFee(size, 10);
console.log(pc.green(` Configuration ${index + 1}:`));
console.log(pc.gray(` ${config.inputs} inputs, ${config.outputs} outputs, ${config.type}`));
console.log(pc.gray(` Estimated size: ${size} vbytes`));
console.log(pc.gray(` Fee (10 sat/vB): ${fee} sats`));
});
// 7. Operations registry demonstration
console.log(pc.yellow('\n📚 7. Operations Registry'));
console.log('─'.repeat(50));
const operationsList = OperationRegistry.listOperations();
console.log(pc.green(` Registered operations: ${operationsList.length}`));
operationsList.forEach(op => {
console.log(pc.gray(` - ${op.name}: ${op.info.operation}`));
});
const stats = OperationRegistry.getOperationStats();
console.log(pc.green('\n Statistics:'));
console.log(pc.gray(` ${JSON.stringify(stats, null, 2)}`));
// 8. Data validation demonstration
console.log(pc.yellow('\n✅ 8. Data Validation'));
console.log('─'.repeat(50));
const testData = {
p: 'brc-20',
op: 'test_opi',
tick: 'test',
amt: '10'
};
try {
OperationRegistry.validateOperation('test_opi', testData);
console.log(pc.green(' ✅ Validation successful'));
} catch (error) {
console.log(pc.red(` ❌ Validation failed: ${error.message}`));
}
// 9. Final summary
console.log(pc.yellow('\n📋 9. Demonstration Summary'));
console.log('─'.repeat(50));
console.log(pc.green(' ✅ OPI operations created and validated'));
console.log(pc.green(' ✅ OP_RETURN messages generated'));
console.log(pc.green(' ✅ PSBTs built for different scenarios'));
console.log(pc.green(' ✅ Size and fee estimations calculated'));
console.log(pc.green(' ✅ Address types detected'));
console.log(pc.green(' ✅ Data validation functional'));
console.log(pc.bold(pc.blue('\n🎉 Demonstration completed successfully!')));
console.log(pc.gray('\nTo use the framework with real Bitcoin transactions:'));
console.log(pc.gray('1. Configure your .env file'));
console.log(pc.gray('2. Run: npm run start test-connection'));
console.log(pc.gray('3. Run: npm run start interactive'));
} catch (error) {
console.log(pc.red(`\n❌ Error during demonstration: ${error.message}`));
Logger.error('Demo failed', { error: error.message });
process.exit(1);
}
}
// Run the demonstration
runDemo();