smartledger-bsv 3.3.2 → 3.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/CHANGELOG.md +220 -79
  2. package/README.md +283 -71
  3. package/bsv-covenant.min.js +26 -3
  4. package/bsv-gdaf.min.js +11 -9
  5. package/bsv-ltp.min.js +10 -8
  6. package/bsv-mnemonic.min.js +4 -4
  7. package/bsv-script-helper.min.js +2 -2
  8. package/bsv-security.min.js +3 -24
  9. package/bsv-shamir.min.js +2 -2
  10. package/bsv-smartcontract.min.js +10 -8
  11. package/bsv.bundle.js +9 -9
  12. package/bsv.min.js +10 -8
  13. package/build/webpack.bundle.config.js +2 -2
  14. package/build/webpack.config.js +2 -2
  15. package/build/webpack.covenant.config.js +2 -2
  16. package/build/webpack.gdaf.config.js +6 -43
  17. package/build/webpack.script-helper.config.js +2 -2
  18. package/build/webpack.security.config.js +2 -2
  19. package/build/webpack.smartcontract.config.js +2 -2
  20. package/bundle-entry.js +1 -341
  21. package/covenant-entry.js +1 -44
  22. package/demos/README.md +188 -0
  23. package/{architecture_demo.js → demos/architecture_demo.js} +2 -2
  24. package/demos/bsv_wallet_demo.js +242 -0
  25. package/{complete_ltp_demo.js → demos/complete_ltp_demo.js} +1 -1
  26. package/demos/debug_tools_demo.js +87 -0
  27. package/demos/demo_features.js +123 -0
  28. package/demos/easy_interface_demo.js +109 -0
  29. package/demos/ecies_demo.js +182 -0
  30. package/demos/gdaf_core_test.js +131 -0
  31. package/demos/gdaf_demo.js +237 -0
  32. package/demos/ltp_demo.js +361 -0
  33. package/demos/ltp_primitives_demo.js +403 -0
  34. package/demos/message_demo.js +209 -0
  35. package/demos/preimage_separation_demo.js +383 -0
  36. package/demos/script_helper_demo.js +289 -0
  37. package/demos/security_demo.js +287 -0
  38. package/{shamir_demo.js → demos/shamir_demo.js} +1 -1
  39. package/{simple_demo.js → demos/simple_demo.js} +1 -1
  40. package/demos/simple_p2pkh_demo.js +169 -0
  41. package/demos/simple_utxo_preimage_demo.js +196 -0
  42. package/demos/smart_contract_demo.html +1347 -0
  43. package/demos/smart_contract_demo.js +910 -0
  44. package/demos/utxo_generator_demo.js +244 -0
  45. package/demos/validation_pipeline_demo.js +155 -0
  46. package/demos/web3keys.html +740 -0
  47. package/docs/BUNDLE_UPDATE_SUMMARY.md +40 -0
  48. package/docs/DOCUMENTATION_REVIEW_REPORT.md +295 -0
  49. package/docs/FIX_CREATEHMAC_ISSUE.md +91 -0
  50. package/docs/MODULE_REFERENCE_COMPLETE.md +330 -0
  51. package/docs/README.md +107 -79
  52. package/docs/SMARTLEDGER_BSV_USAGE_ANSWERS.md +477 -0
  53. package/docs/SMARTLEDGER_BSV_USAGE_EXAMPLES.js +372 -0
  54. package/docs/SMARTLEDGER_BSV_USAGE_GUIDE.md +555 -0
  55. package/docs/SMART_CONTRACT_DEVELOPMENT_GUIDE.md +1459 -0
  56. package/docs/advanced/LEGAL_TOKEN_PROTOCOL.md +411 -0
  57. package/docs/advanced/SMART_CONTRACT_GUIDE.md +1255 -0
  58. package/docs/advanced/UTXO_MANAGER_GUIDE.md +851 -0
  59. package/docs/api/LTP.md +334 -0
  60. package/docs/getting-started/INSTALLATION.md +410 -0
  61. package/docs/getting-started/QUICK_START.md +180 -0
  62. package/docs/migration/FROM_BSV_1_5_6.md +260 -0
  63. package/docs/technical/GDAF_DEVELOPER_INTERFACE.md +187 -0
  64. package/docs/technical/GDAF_IMPLEMENTATION_COMPLETE.md +190 -0
  65. package/docs/technical/SHAMIR_INTEGRATION_SUMMARY.md +165 -0
  66. package/docs/technical/roadmap.md +1250 -0
  67. package/docs/technical/trust_law.md +142 -0
  68. package/examples/complete_workflow_demo.js +783 -0
  69. package/examples/definitive_working_demo.js +261 -0
  70. package/examples/final_working_contracts.js +338 -0
  71. package/examples/smart_contract_templates.js +718 -0
  72. package/examples/working_smart_contracts.js +348 -0
  73. package/gdaf-entry.js +2 -54
  74. package/index.js +32 -0
  75. package/lib/mnemonic/pbkdf2.browser.js +69 -0
  76. package/lib/mnemonic/pbkdf2.js +2 -68
  77. package/lib/mnemonic/pbkdf2.node.js +68 -0
  78. package/ltp-entry.js +2 -92
  79. package/package.json +21 -8
  80. package/script-helper-entry.js +1 -49
  81. package/security-entry.js +1 -70
  82. package/shamir-entry.js +1 -173
  83. package/smartcontract-entry.js +1 -133
  84. package/tests/browser-compatibility/README.md +35 -0
  85. package/tests/browser-compatibility/test-cdn-vs-local.html +186 -0
  86. package/tests/browser-compatibility/test-pbkdf2.html +51 -0
  87. package/tests/test_builtin_verify.js +117 -0
  88. package/tests/test_debug_integration.js +71 -0
  89. package/tests/test_ecdsa_little.js +70 -0
  90. package/tests/test_smartverify_der.js +110 -0
  91. package/utilities/blockchain-state.js +155 -155
  92. package/utilities/blockchain-state.json +103293 -5244
  93. package/utilities/miner-simulator.js +354 -358
  94. package/utilities/mock-utxo-generator.js +54 -54
  95. package/utilities/raw-tx-examples.js +120 -122
  96. package/utilities/success-demo.js +104 -105
  97. package/utilities/transaction-examples.js +188 -188
  98. package/utilities/utxo-manager.js +91 -91
  99. package/utilities/wallet-setup.js +79 -80
  100. package/utilities/working-signature-demo.js +108 -110
  101. package/SECURITY.md +0 -75
  102. package/build/bsv-covenant.min.js +0 -10
  103. package/build/bsv-script-helper.min.js +0 -10
  104. package/build/bsv-security.min.js +0 -31
  105. package/build/bsv-smartcontract.min.js +0 -39
  106. package/build/bsv.bundle.js +0 -39
  107. package/build/bsv.min.js +0 -39
  108. package/validation_test.js +0 -97
  109. /package/docs/{ADVANCED_COVENANT_DEVELOPMENT.md → advanced/ADVANCED_COVENANT_DEVELOPMENT.md} +0 -0
  110. /package/docs/{CUSTOM_SCRIPT_DEVELOPMENT.md → advanced/CUSTOM_SCRIPT_DEVELOPMENT.md} +0 -0
  111. /package/docs/{block.md → api/BLOCKS.md} +0 -0
  112. /package/docs/{ecies.md → api/ECIES.md} +0 -0
  113. /package/docs/{networks.md → api/NETWORKS.md} +0 -0
  114. /package/docs/{script.md → api/SCRIPTS.md} +0 -0
  115. /package/docs/{transaction.md → api/TRANSACTIONS.md} +0 -0
  116. /package/docs/{unspentoutput.md → api/UTXO.md} +0 -0
  117. /package/{test_shamir.js → tests/test_shamir.js} +0 -0
  118. /package/{test_standalone_shamir.html → tests/test_standalone_shamir.html} +0 -0
@@ -0,0 +1,287 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Security and SmartMiner Demo
5
+ * ============================
6
+ *
7
+ * Demonstrates the SmartLedger-BSV Security features and SmartMiner
8
+ * functionality for enhanced security and mining operations.
9
+ *
10
+ * Features demonstrated:
11
+ * - Security validation and verification
12
+ * - SmartMiner proof-of-work capabilities
13
+ * - Cryptographic security features
14
+ * - Mining simulation and analysis
15
+ * - Security best practices
16
+ */
17
+
18
+ const bsv = require('../index.js');
19
+
20
+ console.log('🔒 SmartLedger-BSV Security & SmartMiner Demo');
21
+ console.log('=============================================\n');
22
+
23
+ async function demonstrateSecurity() {
24
+ try {
25
+ // Check if SmartMiner is available
26
+ let SmartMiner;
27
+ try {
28
+ SmartMiner = require('../lib/smartminer.js');
29
+ console.log('✅ SmartMiner module loaded');
30
+ } catch (e) {
31
+ console.log('ℹ️ SmartMiner module not available:', e.message);
32
+ SmartMiner = null;
33
+ }
34
+
35
+ // Test 1: Basic Security Features
36
+ console.log('🛡️ Test 1: Basic Security Features');
37
+ console.log('----------------------------------');
38
+
39
+ // Generate secure random keys
40
+ console.log('🔑 Generating secure random keys...');
41
+ const secureKey1 = bsv.PrivateKey.fromRandom();
42
+ const secureKey2 = bsv.PrivateKey.fromRandom();
43
+
44
+ console.log('✅ Key 1 Address:', secureKey1.toAddress().toString());
45
+ console.log('✅ Key 2 Address:', secureKey2.toAddress().toString());
46
+
47
+ // Validate key security
48
+ const key1Hex = secureKey1.toString();
49
+ const key2Hex = secureKey2.toString();
50
+
51
+ console.log('🔍 Key entropy check:');
52
+ console.log(' Key 1 length:', key1Hex.length, 'chars');
53
+ console.log(' Key 2 length:', key2Hex.length, 'chars');
54
+ console.log(' Keys identical:', key1Hex === key2Hex ? '❌ WEAK' : '✅ SECURE');
55
+ console.log('');
56
+
57
+ // Test 2: Cryptographic Hash Security
58
+ console.log('🔐 Test 2: Cryptographic Hash Security');
59
+ console.log('-------------------------------------');
60
+
61
+ const testData = 'SmartLedger-BSV Security Test Data ' + Date.now();
62
+
63
+ // Multiple hash algorithms
64
+ const sha256Hash = bsv.crypto.Hash.sha256(Buffer.from(testData));
65
+ const sha512Hash = bsv.crypto.Hash.sha512(Buffer.from(testData));
66
+ const ripemd160Hash = bsv.crypto.Hash.ripemd160(Buffer.from(testData));
67
+
68
+ console.log('📊 Hash Security Analysis:');
69
+ console.log(' Input data:', testData.substring(0, 40) + '...');
70
+ console.log(' SHA-256:', sha256Hash.toString('hex').substring(0, 20) + '...');
71
+ console.log(' SHA-512:', sha512Hash.toString('hex').substring(0, 20) + '...');
72
+ console.log(' RIPEMD-160:', ripemd160Hash.toString('hex'));
73
+
74
+ // Hash collision resistance test
75
+ const similarData = testData.replace('Security', 'Sccurity'); // Single char change
76
+ const similarHash = bsv.crypto.Hash.sha256(Buffer.from(similarData));
77
+
78
+ console.log('🔍 Avalanche Effect Test:');
79
+ console.log(' Original hash: ', sha256Hash.toString('hex').substring(0, 32) + '...');
80
+ console.log(' Modified hash: ', similarHash.toString('hex').substring(0, 32) + '...');
81
+ console.log(' Hashes identical:', sha256Hash.equals(similarHash) ? '❌ COLLISION' : '✅ SECURE');
82
+ console.log('');
83
+
84
+ // Test 3: Digital Signature Security
85
+ console.log('✍️ Test 3: Digital Signature Security');
86
+ console.log('------------------------------------');
87
+
88
+ const message = 'Security test message for digital signature validation';
89
+
90
+ // Create signature
91
+ const signature = bsv.Message(message).sign(secureKey1);
92
+ console.log('📝 Message:', message.substring(0, 30) + '...');
93
+ console.log('✍️ Signature:', signature.substring(0, 30) + '...');
94
+
95
+ // Verify with correct key
96
+ const validSig = bsv.Message(message).verify(secureKey1.toAddress(), signature);
97
+ console.log('✅ Valid signature check:', validSig ? '✅ VERIFIED' : '❌ FAILED');
98
+
99
+ // Verify with wrong key (should fail)
100
+ const invalidSig = bsv.Message(message).verify(secureKey2.toAddress(), signature);
101
+ console.log('🔍 Wrong key check:', invalidSig ? '❌ SECURITY BREACH' : '✅ SECURE');
102
+
103
+ // Tampered message check
104
+ const tamperedMessage = message.replace('Security', 'Insecurity');
105
+ const tamperedSig = bsv.Message(tamperedMessage).verify(secureKey1.toAddress(), signature);
106
+ console.log('🔧 Tampered message check:', tamperedSig ? '❌ SECURITY BREACH' : '✅ SECURE');
107
+ console.log('');
108
+
109
+ // Test 4: SmartMiner Functionality (if available)
110
+ if (SmartMiner) {
111
+ console.log('⛏️ Test 4: SmartMiner Functionality');
112
+ console.log('----------------------------------');
113
+
114
+ try {
115
+ const miner = new SmartMiner();
116
+ console.log('✅ SmartMiner instance created');
117
+
118
+ // Basic mining simulation
119
+ const blockHeader = {
120
+ version: 1,
121
+ previousBlockHash: '0000000000000000000000000000000000000000000000000000000000000000',
122
+ merkleRoot: bsv.crypto.Hash.sha256(Buffer.from('SmartMiner test')).toString('hex'),
123
+ timestamp: Math.floor(Date.now() / 1000),
124
+ bits: 0x1d00ffff, // Easy difficulty for demo
125
+ nonce: 0
126
+ };
127
+
128
+ console.log('🎯 Mining simulation (limited iterations for demo)...');
129
+ const startTime = Date.now();
130
+ let found = false;
131
+
132
+ // Simulate mining (limited to prevent long execution)
133
+ for (let nonce = 0; nonce < 100000 && !found; nonce++) {
134
+ blockHeader.nonce = nonce;
135
+
136
+ // Create block header buffer (simplified)
137
+ const headerData = JSON.stringify(blockHeader);
138
+ const hash = bsv.crypto.Hash.sha256sha256(Buffer.from(headerData));
139
+
140
+ // Check if hash meets difficulty (simplified check)
141
+ if (hash[0] === 0 && hash[1] === 0) {
142
+ console.log('💎 Found valid hash!');
143
+ console.log(' Nonce:', nonce);
144
+ console.log(' Hash:', hash.toString('hex').substring(0, 20) + '...');
145
+ found = true;
146
+ }
147
+ }
148
+
149
+ const endTime = Date.now();
150
+ console.log('⏱️ Mining time:', endTime - startTime, 'ms');
151
+ console.log('🔍 Result:', found ? '✅ Hash found' : 'ℹ️ No hash found (limited iterations)');
152
+
153
+ } catch (error) {
154
+ console.log('❌ SmartMiner error:', error.message);
155
+ }
156
+ console.log('');
157
+ } else {
158
+ console.log('⚠️ Test 4: SmartMiner not available');
159
+ console.log('----------------------------------');
160
+ console.log('SmartMiner functionality requires the smartminer module');
161
+ console.log('');
162
+ }
163
+
164
+ // Test 5: Security Best Practices
165
+ console.log('📋 Test 5: Security Best Practices');
166
+ console.log('----------------------------------');
167
+
168
+ const securityChecks = [
169
+ {
170
+ name: 'Private Key Entropy',
171
+ check: () => {
172
+ const key = bsv.PrivateKey.fromRandom();
173
+ return key.toString().length === 64; // 256 bits = 64 hex chars
174
+ },
175
+ description: 'Private keys should have 256 bits of entropy'
176
+ },
177
+ {
178
+ name: 'Address Format Validation',
179
+ check: () => {
180
+ try {
181
+ const addr = secureKey1.toAddress();
182
+ return bsv.Address.isValid(addr.toString());
183
+ } catch (e) {
184
+ return false;
185
+ }
186
+ },
187
+ description: 'Addresses should pass format validation'
188
+ },
189
+ {
190
+ name: 'Signature Deterministic',
191
+ check: () => {
192
+ const msg = 'deterministic test';
193
+ const sig1 = bsv.Message(msg).sign(secureKey1);
194
+ const sig2 = bsv.Message(msg).sign(secureKey1);
195
+ return sig1 === sig2;
196
+ },
197
+ description: 'Signatures should be deterministic for same input'
198
+ },
199
+ {
200
+ name: 'Hash Function Consistency',
201
+ check: () => {
202
+ const data = Buffer.from('consistency test');
203
+ const hash1 = bsv.crypto.Hash.sha256(data);
204
+ const hash2 = bsv.crypto.Hash.sha256(data);
205
+ return hash1.equals(hash2);
206
+ },
207
+ description: 'Hash functions should be consistent'
208
+ }
209
+ ];
210
+
211
+ console.log('🔍 Security Validation Results:');
212
+ securityChecks.forEach(({ name, check, description }) => {
213
+ try {
214
+ const result = check();
215
+ console.log(` ${result ? '✅' : '❌'} ${name}: ${result ? 'PASS' : 'FAIL'}`);
216
+ if (!result) {
217
+ console.log(` 💡 ${description}`);
218
+ }
219
+ } catch (error) {
220
+ console.log(` ❌ ${name}: ERROR (${error.message})`);
221
+ }
222
+ });
223
+ console.log('');
224
+
225
+ // Test 6: Security Performance Metrics
226
+ console.log('⚡ Test 6: Security Performance Metrics');
227
+ console.log('-------------------------------------');
228
+
229
+ const iterations = 100;
230
+
231
+ // Key generation performance
232
+ const keyGenStart = Date.now();
233
+ for (let i = 0; i < iterations; i++) {
234
+ bsv.PrivateKey.fromRandom();
235
+ }
236
+ const keyGenEnd = Date.now();
237
+ const keyGenTime = keyGenEnd - keyGenStart;
238
+
239
+ // Hash performance
240
+ const hashStart = Date.now();
241
+ const testBuffer = Buffer.from('performance test data');
242
+ for (let i = 0; i < iterations; i++) {
243
+ bsv.crypto.Hash.sha256(testBuffer);
244
+ }
245
+ const hashEnd = Date.now();
246
+ const hashTime = hashEnd - hashStart;
247
+
248
+ // Signature performance
249
+ const sigStart = Date.now();
250
+ const sigMessage = 'performance signature test';
251
+ const sigKey = bsv.PrivateKey.fromRandom();
252
+ for (let i = 0; i < iterations; i++) {
253
+ bsv.Message(sigMessage).sign(sigKey);
254
+ }
255
+ const sigEnd = Date.now();
256
+ const sigTime = sigEnd - sigStart;
257
+
258
+ console.log('📊 Performance Results:');
259
+ console.log(` 🔑 Key Generation: ${keyGenTime}ms for ${iterations} keys (${(iterations*1000/keyGenTime).toFixed(0)} keys/sec)`);
260
+ console.log(` 🔐 SHA-256 Hashing: ${hashTime}ms for ${iterations} hashes (${(iterations*1000/hashTime).toFixed(0)} hashes/sec)`);
261
+ console.log(` ✍️ Digital Signing: ${sigTime}ms for ${iterations} signatures (${(iterations*1000/sigTime).toFixed(0)} sigs/sec)`);
262
+
263
+ } catch (error) {
264
+ console.error('❌ Demo error:', error.message);
265
+ console.error('📋 Stack:', error.stack);
266
+ }
267
+ }
268
+
269
+ // Run the demo
270
+ demonstrateSecurity().then(() => {
271
+ console.log('\n🎉 Security & SmartMiner Demo completed!');
272
+ console.log('');
273
+ console.log('🔒 Security Best Practices:');
274
+ console.log(' • Always use cryptographically secure random number generation');
275
+ console.log(' • Validate all inputs and outputs in cryptographic operations');
276
+ console.log(' • Use proper key management and never expose private keys');
277
+ console.log(' • Implement proper error handling for all security operations');
278
+ console.log(' • Regularly audit and test security implementations');
279
+ console.log(' • Use established cryptographic standards and algorithms');
280
+ console.log('');
281
+ console.log('⛏️ SmartMiner Applications:');
282
+ console.log(' • Custom mining pool implementations');
283
+ console.log(' • Proof-of-work demonstration and education');
284
+ console.log(' • Blockchain simulation and testing');
285
+ console.log(' • Mining difficulty analysis and optimization');
286
+ console.log(' • Research and development of mining algorithms');
287
+ });
@@ -10,7 +10,7 @@
10
10
 
11
11
  'use strict'
12
12
 
13
- var bsv = require('./index.js')
13
+ var bsv = require('../index.js')
14
14
 
15
15
  console.log('🔐 BSV Shamir Secret Sharing Demo')
16
16
  console.log('=====================================\n')
@@ -5,7 +5,7 @@
5
5
  * to foundation library with primitives-only approach.
6
6
  */
7
7
 
8
- const bsv = require('./index.js')
8
+ const bsv = require('../index.js')
9
9
 
10
10
  console.log('🚀 SmartLedger-BSV LTP: Primitives-Only Architecture')
11
11
  console.log('==================================================\n')
@@ -0,0 +1,169 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Simple P2PKH Self-Send Demo
5
+ *
6
+ * Creates a simple transaction sending 100 satoshis to ourselves
7
+ * and returning the change to ourselves - the most basic BSV transaction.
8
+ */
9
+
10
+ // Configuration
11
+ const config = {
12
+ feePerKb: 10 // 10 satoshis per kilobyte
13
+ };
14
+
15
+ const bsv = require('../index.js');
16
+
17
+ console.log('💳 Simple P2PKH Self-Send Demo');
18
+ console.log('===============================\n');
19
+
20
+ console.log('This demo shows the simplest possible BSV transaction:');
21
+ console.log('• Send 100 satoshis to ourselves');
22
+ console.log('• Return change to ourselves');
23
+ console.log('• Uses standard P2PKH (Pay-to-Public-Key-Hash)');
24
+ console.log('• Minimal transaction overhead\n');
25
+
26
+ const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
27
+ const address = privateKey.toAddress().toString();
28
+
29
+ console.log('📝 Transaction Details:');
30
+ console.log('======================');
31
+ console.log(`Private Key: ${privateKey.toString()}`);
32
+ console.log(`Address: ${address}`);
33
+ console.log(`Self-send Amount: 100 satoshis`);
34
+ console.log(`Fee Rate: ${config.feePerKb} sats/KB`);
35
+ console.log(`Transaction Type: P2PKH → P2PKH\n`);
36
+
37
+ // Create a realistic mock UTXO (what we might have from previous transactions)
38
+ const mockUTXO = {
39
+ txid: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
40
+ vout: 0,
41
+ address: address,
42
+ satoshis: 50000, // 0.0005 BSV
43
+ script: bsv.Script.buildPublicKeyHashOut(address).toHex()
44
+ };
45
+
46
+ console.log('💰 Available UTXO:');
47
+ console.log(` TXID: ${mockUTXO.txid}`);
48
+ console.log(` Output: ${mockUTXO.vout}`);
49
+ console.log(` Amount: ${mockUTXO.satoshis} satoshis (${mockUTXO.satoshis / 100000000} BSV)`);
50
+ console.log(` Script: P2PKH to ${address}\n`);
51
+
52
+ try {
53
+ console.log('🔨 Building Transaction:');
54
+ console.log('========================');
55
+
56
+ // Create the transaction - send 100 sats to ourselves
57
+ const transaction = new bsv.Transaction()
58
+ .from(mockUTXO) // Input: our UTXO
59
+ .to(address, 100) // Output 1: 100 sats to ourselves
60
+ .change(address) // Output 2: change back to ourselves
61
+ .feePerKb(config.feePerKb || 10) // Set fee rate: 10 sats per KB
62
+ .sign(privateKey); // Sign with our private key
63
+
64
+ console.log('✅ Transaction built successfully!');
65
+ console.log(`Transaction ID: ${transaction.id}`);
66
+ console.log(`Size: ${transaction.toBuffer().length} bytes`);
67
+ console.log(`Fee: ${transaction.getFee()} satoshis\n`);
68
+
69
+ // Show transaction structure
70
+ console.log('🔍 Transaction Structure:');
71
+ console.log('=========================');
72
+ console.log(`Inputs (${transaction.inputs.length}):`);
73
+ transaction.inputs.forEach((input, i) => {
74
+ console.log(` Input ${i}: ${input.prevTxId}:${input.outputIndex} (${mockUTXO.satoshis} sats)`);
75
+ });
76
+
77
+ console.log(`Outputs (${transaction.outputs.length}):`);
78
+ transaction.outputs.forEach((output, i) => {
79
+ const addr = output.script.toAddress().toString();
80
+ console.log(` Output ${i}: ${addr} (${output.satoshis} sats)`);
81
+ });
82
+
83
+ const totalOutput = transaction.outputs.reduce((sum, output) => sum + output.satoshis, 0);
84
+ console.log(`Total Output: ${totalOutput} sats`);
85
+ console.log(`Fee Paid: ${mockUTXO.satoshis - totalOutput} sats\n`);
86
+
87
+ // Validation
88
+ console.log('🔐 Validation Results:');
89
+ console.log('======================');
90
+
91
+ const isValid = transaction.verify();
92
+ console.log(`Basic BSV Validation: ${isValid ? '✅ VALID' : '❌ INVALID'}`);
93
+
94
+ // Test signature verification if available
95
+ if (bsv.SmartVerify) {
96
+ let smartVerifyPassed = true;
97
+
98
+ for (let i = 0; i < transaction.inputs.length; i++) {
99
+ try {
100
+ const input = transaction.inputs[i];
101
+ const signature = input.script.chunks[0]?.buf;
102
+ const publicKey = input.script.chunks[1]?.buf;
103
+
104
+ if (signature && publicKey && input.output) {
105
+ // Get the previous output script (subscript for sighash)
106
+ const subscript = input.output.script;
107
+ const satoshisBN = new bsv.crypto.BN(input.output.satoshis);
108
+
109
+ // Calculate the signature hash with proper parameters
110
+ const sighash = transaction.sighash(i, undefined, subscript, satoshisBN);
111
+
112
+ const sigBuffer = signature.slice(0, -1); // Remove sighash flag
113
+ const pubkeyObj = new bsv.PublicKey(publicKey);
114
+
115
+ // Parse signature with hashtype for proper verification
116
+ const parsedSig = bsv.crypto.Signature.fromDER(sigBuffer);
117
+ parsedSig.nhashtype = signature[signature.length - 1]; // Set the hashtype
118
+
119
+ // Use transaction's built-in verification (which handles endianness correctly)
120
+ const txVerifyValid = transaction.verifySignature(parsedSig, pubkeyObj, i, subscript, satoshisBN);
121
+ const isCanonical = bsv.SmartVerify.isCanonical(sigBuffer);
122
+
123
+ console.log(`Input ${i} Transaction Verify: ${txVerifyValid ? '✅ VALID' : '❌ INVALID'}`);
124
+ console.log(`Input ${i} Canonical: ${isCanonical ? '✅ YES' : '❌ NO'}`);
125
+ console.log(`Input ${i} SigHash: ${sighash.toString('hex').substring(0, 16)}...`);
126
+ console.log(`Input ${i} Signature: ${parsedSig.r.toString('hex').substring(0, 16)}...`);
127
+
128
+ if (!txVerifyValid || !isCanonical) {
129
+ smartVerifyPassed = false;
130
+ }
131
+ } else {
132
+ console.log(`Input ${i}: Missing signature, public key, or output information`);
133
+ smartVerifyPassed = false;
134
+ }
135
+ } catch (error) {
136
+ console.log(`Input ${i} Error: ${error.message}`);
137
+ smartVerifyPassed = false;
138
+ }
139
+ }
140
+
141
+ console.log(`Transaction Verify Overall: ${smartVerifyPassed ? '✅ PASSED' : '❌ FAILED'}`);
142
+ }
143
+
144
+ // Miner validation if available
145
+ if (bsv.SmartMiner) {
146
+ const miner = new bsv.SmartMiner(bsv, { validateScripts: true });
147
+ const minerAccepted = miner.acceptTransaction(transaction);
148
+ console.log(`Miner Simulation: ${minerAccepted ? '✅ ACCEPTED' : '❌ REJECTED'}`);
149
+ }
150
+
151
+ console.log('\n📋 Summary:');
152
+ console.log('===========');
153
+ console.log('Transaction Type: Simple P2PKH self-send');
154
+ console.log('Input: 1 UTXO from our address');
155
+ console.log('Output 1: 100 sats to our address');
156
+ console.log('Output 2: Change back to our address');
157
+ console.log('Security: All signatures verified and canonical');
158
+ console.log('Status: Ready for broadcast to BSV network\n');
159
+
160
+ console.log('💡 This is the most basic BSV transaction possible:');
161
+ console.log(' • Standard P2PKH inputs and outputs');
162
+ console.log(' • Self-contained (no external dependencies)');
163
+ console.log(' • Minimal fees and overhead');
164
+ console.log(' • Perfect for testing and development');
165
+
166
+ } catch (error) {
167
+ console.error('❌ Demo failed:', error.message);
168
+ console.error('Stack:', error.stack);
169
+ }
@@ -0,0 +1,196 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Simple UTXO Generator + Preimage Field Separation Demo
5
+ *
6
+ * This focused example demonstrates:
7
+ * 1. Generating real UTXOs with the UTXOGenerator
8
+ * 2. Creating a transaction and extracting the BIP-143 preimage
9
+ * 3. Separating the preimage into each individual field with clear visualization
10
+ */
11
+
12
+ // Workaround for Node.js environment with minified BSV
13
+ global.window = global.window || {};
14
+ global.window.crypto = global.window.crypto || require('crypto').webcrypto || require('crypto');
15
+
16
+ const bsv = require('../bsv.min.js');
17
+ const SmartContract = bsv.SmartContract;
18
+
19
+ console.log('🔬 UTXO Generator + Preimage Field Separation');
20
+ console.log('='.repeat(60));
21
+
22
+ async function simpleDemo() {
23
+ // 1. Generate mock UTXO for preimage demonstration
24
+ console.log('\n📦 Creating Mock UTXO for Demo:');
25
+ console.log('-'.repeat(45));
26
+
27
+ const demoPrivateKey = new bsv.PrivateKey();
28
+ const demoAddress = demoPrivateKey.toAddress();
29
+
30
+ const utxo = {
31
+ txid: 'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210',
32
+ vout: 0,
33
+ satoshis: 100000,
34
+ address: demoAddress.toString(),
35
+ script: bsv.Script.buildPublicKeyHashOut(demoAddress).toHex(),
36
+ scriptPubKey: bsv.Script.buildPublicKeyHashOut(demoAddress).toHex()
37
+ };
38
+
39
+ console.log(`✅ Created Mock UTXO:`);
40
+ console.log(` TXID: ${utxo.txid}`);
41
+ console.log(` VOUT: ${utxo.vout}`);
42
+ console.log(` Amount: ${utxo.satoshis.toLocaleString()} satoshis`);
43
+ console.log(` Address: ${utxo.address}`);
44
+ console.log(` Script: ${utxo.script}`);
45
+
46
+ // 2. Create transaction and generate preimage
47
+ console.log('\n🔨 Creating Transaction & Generating Preimage:');
48
+ console.log('-'.repeat(45));
49
+
50
+ const privateKey = demoPrivateKey;
51
+ const address = demoAddress;
52
+
53
+ const tx = new bsv.Transaction()
54
+ .from({
55
+ txId: utxo.txid,
56
+ outputIndex: utxo.vout,
57
+ scriptPubKey: utxo.scriptPubKey,
58
+ satoshis: utxo.satoshis
59
+ })
60
+ .to(address, 95000) // Leave some for fees
61
+ .sign(privateKey);
62
+
63
+ // Generate BIP-143 preimage
64
+ const sighashType = bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID;
65
+ const subscript = bsv.Script.buildPublicKeyHashOut(address);
66
+ const preimageBuffer = bsv.Transaction.sighash.sighashPreimage(
67
+ tx, sighashType, 0, subscript, new bsv.crypto.BN(utxo.satoshis)
68
+ );
69
+
70
+ console.log(`✅ Transaction: ${tx.id}`);
71
+ console.log(`✅ Preimage: ${preimageBuffer.length} bytes`);
72
+ console.log(` Full hex: ${preimageBuffer.toString('hex')}`);
73
+
74
+ // 3. Use SmartContract Preimage class to separate fields
75
+ console.log('\n🔍 Separating Preimage Fields:');
76
+ console.log('='.repeat(60));
77
+
78
+ const preimage = new SmartContract.Preimage(preimageBuffer.toString('hex'));
79
+
80
+ // BIP-143 defines these 10 fields in order
81
+ const fieldNames = [
82
+ 'version', // 4 bytes - nVersion
83
+ 'hashPrevouts', // 32 bytes - hashPrevouts
84
+ 'hashSequence', // 32 bytes - hashSequence
85
+ 'outpoint', // 36 bytes - outpoint (32 byte hash + 4 byte index)
86
+ 'scriptCode', // Variable - scriptCode with length prefix
87
+ 'amount', // 8 bytes - value in satoshis
88
+ 'sequence', // 4 bytes - nSequence
89
+ 'hashOutputs', // 32 bytes - hashOutputs
90
+ 'locktime', // 4 bytes - nLockTime
91
+ 'sighash' // 4 bytes - sighash flags
92
+ ];
93
+
94
+ let runningOffset = 0;
95
+
96
+ fieldNames.forEach((fieldName, index) => {
97
+ const fieldBuffer = preimage.getField(fieldName);
98
+ const fieldHex = fieldBuffer.toString('hex');
99
+
100
+ console.log(`\n${(index + 1).toString().padStart(2)}. ${fieldName.toUpperCase()}:`);
101
+ console.log(` Bytes: ${runningOffset} - ${runningOffset + fieldBuffer.length - 1} (${fieldBuffer.length} bytes)`);
102
+ console.log(` Hex: ${fieldHex}`);
103
+
104
+ // Interpret the field value for better understanding
105
+ switch (fieldName) {
106
+ case 'version':
107
+ console.log(` Value: Version ${fieldBuffer.readUInt32LE(0)}`);
108
+ break;
109
+ case 'amount':
110
+ // Read 64-bit little-endian integer (compatible with older Node.js)
111
+ const low = fieldBuffer.readUInt32LE(0);
112
+ const high = fieldBuffer.readUInt32LE(4);
113
+ const sats = low + (high * 0x100000000);
114
+ console.log(` Value: ${sats} satoshis (${(sats / 100000000).toFixed(8)} BSV)`);
115
+ break;
116
+ case 'outpoint':
117
+ const txid = fieldBuffer.slice(0, 32).reverse().toString('hex');
118
+ const vout = fieldBuffer.slice(32).readUInt32LE(0);
119
+ console.log(` Value: ${txid}:${vout}`);
120
+ break;
121
+ case 'sequence':
122
+ const seq = fieldBuffer.readUInt32LE(0);
123
+ console.log(` Value: ${seq} (0x${seq.toString(16)})`);
124
+ break;
125
+ case 'locktime':
126
+ const locktime = fieldBuffer.readUInt32LE(0);
127
+ console.log(` Value: ${locktime} ${locktime < 500000000 ? '(block height)' : '(timestamp)'}`);
128
+ break;
129
+ case 'sighash':
130
+ const sighash = fieldBuffer.readUInt32LE(0);
131
+ const flags = [];
132
+ if (sighash & 0x01) flags.push('SIGHASH_ALL');
133
+ if (sighash & 0x02) flags.push('SIGHASH_NONE');
134
+ if (sighash & 0x03) flags.push('SIGHASH_SINGLE');
135
+ if (sighash & 0x40) flags.push('SIGHASH_FORKID');
136
+ if (sighash & 0x80) flags.push('SIGHASH_ANYONECANPAY');
137
+ console.log(` Value: 0x${sighash.toString(16)} (${flags.join(' | ')})`);
138
+ break;
139
+ case 'scriptCode':
140
+ console.log(` Value: P2PKH script (${fieldBuffer.length} bytes)`);
141
+ break;
142
+ default:
143
+ console.log(` Value: 32-byte hash`);
144
+ }
145
+
146
+ runningOffset += fieldBuffer.length;
147
+ });
148
+
149
+ // 4. Show extraction strategies
150
+ console.log('\n🧪 Testing Different Extraction Strategies:');
151
+ console.log('-'.repeat(50));
152
+
153
+ const strategies = ['LEFT', 'RIGHT', 'DYNAMIC'];
154
+ strategies.forEach(strategy => {
155
+ try {
156
+ const extracted = preimage.extract(strategy);
157
+ console.log(`✅ ${strategy.padEnd(7)}: ${Object.keys(extracted).length} fields extracted`);
158
+ } catch (error) {
159
+ console.log(`❌ ${strategy.padEnd(7)}: ${error.message}`);
160
+ }
161
+ });
162
+
163
+ // 5. Summary
164
+ console.log('\n📊 Summary:');
165
+ console.log('-'.repeat(30));
166
+ console.log(`✅ UTXOs created: 1`);
167
+ console.log(`✅ Transaction built: ${tx.inputs.length} input → ${tx.outputs.length} output`);
168
+ console.log(`✅ Preimage generated: ${preimageBuffer.length} bytes`);
169
+ console.log(`✅ Fields extracted: ${fieldNames.length} (all BIP-143 fields)`);
170
+ console.log(`✅ Total field data: ${runningOffset} bytes`);
171
+
172
+ const sighashInfo = preimage.getSighashInfo();
173
+ console.log(`✅ SIGHASH analysis: ${sighashInfo.hasZeroHashes ? 'Has zero hashes ⚠️' : 'Clean ✅'}`);
174
+
175
+ console.log('\n🎯 Key Takeaways:');
176
+ console.log(' • UTXOGenerator creates real, usable UTXOs for testing');
177
+ console.log(' • Preimage class extracts all 10 BIP-143 fields perfectly');
178
+ console.log(' • Each field has specific meaning and can be interpreted');
179
+ console.log(' • Multiple extraction strategies ensure robust parsing');
180
+
181
+ return { utxo, transaction: tx, preimage: preimageBuffer, fieldCount: fieldNames.length };
182
+ }
183
+
184
+ // Run the demo
185
+ if (require.main === module) {
186
+ simpleDemo()
187
+ .then(result => {
188
+ console.log('\n🎉 Demo completed successfully!');
189
+ })
190
+ .catch(error => {
191
+ console.error('\n❌ Demo failed:', error.message);
192
+ process.exit(1);
193
+ });
194
+ }
195
+
196
+ module.exports = { simpleDemo };