@smartledger/bsv 3.4.2 โ†’ 3.4.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 (88) hide show
  1. package/CHANGELOG.md +336 -0
  2. package/README.md +72 -72
  3. package/SECURITY.md +88 -0
  4. package/bin/cli.js +13 -8
  5. package/bsv-covenant.min.js +4 -4
  6. package/bsv-gdaf.min.js +5 -5
  7. package/bsv-ltp.min.js +7 -7
  8. package/bsv-smartcontract.min.js +5 -5
  9. package/bsv.bundle.js +5 -5
  10. package/bsv.d.ts +486 -9
  11. package/bsv.min.js +5 -5
  12. package/docs/COVENANT_DEVELOPMENT_RESOLVED.md +2 -2
  13. package/docs/MODULE_REFERENCE_COMPLETE.md +61 -58
  14. package/docs/advanced/LEGAL_TOKEN_PROTOCOL.md +3 -3
  15. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  16. package/docs/getting-started/INSTALLATION.md +30 -30
  17. package/docs/getting-started/QUICK_START.md +18 -18
  18. package/docs/migration/FROM_BSV_1_5_6.md +16 -10
  19. package/gdaf-entry.js +1 -2
  20. package/index.js +20 -7
  21. package/lib/smart_contract/covenant.js +10 -1
  22. package/lib/smartutxo.js +20 -12
  23. package/lib/transaction/transaction.js +7 -0
  24. package/ltp-entry.js +1 -2
  25. package/package.json +3 -3
  26. package/utilities/blockchain-state.js +32 -23
  27. package/demos/README.md +0 -188
  28. package/demos/architecture_demo.js +0 -247
  29. package/demos/browser-test.html +0 -1208
  30. package/demos/bsv_wallet_demo.js +0 -242
  31. package/demos/complete_ltp_demo.js +0 -511
  32. package/demos/debug_tools_demo.js +0 -87
  33. package/demos/demo_features.js +0 -123
  34. package/demos/easy_interface_demo.js +0 -109
  35. package/demos/ecies_demo.js +0 -182
  36. package/demos/gdaf_demo.js +0 -237
  37. package/demos/ltp_demo.js +0 -361
  38. package/demos/ltp_primitives_demo.js +0 -403
  39. package/demos/message_demo.js +0 -209
  40. package/demos/preimage_separation_demo.js +0 -383
  41. package/demos/script_helper_demo.js +0 -289
  42. package/demos/security_demo.js +0 -287
  43. package/demos/shamir_demo.js +0 -121
  44. package/demos/simple_demo.js +0 -204
  45. package/demos/simple_p2pkh_demo.js +0 -169
  46. package/demos/simple_utxo_preimage_demo.js +0 -196
  47. package/demos/smart_contract_demo.html +0 -1347
  48. package/demos/smart_contract_demo.js +0 -910
  49. package/demos/utxo_generator_demo.js +0 -244
  50. package/demos/validation_pipeline_demo.js +0 -155
  51. package/demos/web3keys.html +0 -740
  52. package/examples/README.md +0 -200
  53. package/examples/basic/transaction-creation.js +0 -534
  54. package/examples/basic/transaction_signature_api_gap.js +0 -178
  55. package/examples/complete_workflow_demo.js +0 -783
  56. package/examples/covenants/advanced_covenant_demo.js +0 -219
  57. package/examples/covenants/covenant_interface_demo.js +0 -270
  58. package/examples/covenants/covenant_manual_signature_resolved.js +0 -212
  59. package/examples/covenants/covenant_signature_template.js +0 -117
  60. package/examples/covenants2/covenant_bidirectional_example.js +0 -262
  61. package/examples/covenants2/covenant_utils_demo.js +0 -120
  62. package/examples/covenants2/preimage_covenant_utils.js +0 -287
  63. package/examples/covenants2/production_integration.js +0 -256
  64. package/examples/data/covenant_utxos.json +0 -28
  65. package/examples/data/utxos.json +0 -26
  66. package/examples/definitive_working_demo.js +0 -261
  67. package/examples/final_working_contracts.js +0 -338
  68. package/examples/legacy/README.md +0 -11
  69. package/examples/legacy/smart_contract_test_integration.js +0 -269
  70. package/examples/legacy/test_builtin_verify.js +0 -117
  71. package/examples/legacy/test_debug_integration.js +0 -71
  72. package/examples/legacy/test_ecdsa_little.js +0 -70
  73. package/examples/legacy/test_shamir.js +0 -221
  74. package/examples/legacy/test_smartverify_der.js +0 -110
  75. package/examples/preimage/README.md +0 -178
  76. package/examples/preimage/extract_preimage_bidirectional.js +0 -421
  77. package/examples/preimage/generate_sample_preimage.js +0 -208
  78. package/examples/preimage/generate_sighash_examples.js +0 -152
  79. package/examples/preimage/parse_preimage.js +0 -117
  80. package/examples/preimage/test_preimage_extractor.js +0 -53
  81. package/examples/preimage/test_varint_extraction.js +0 -95
  82. package/examples/scripts/custom_script_helper_example.js +0 -273
  83. package/examples/scripts/script_interpreter.js +0 -193
  84. package/examples/smart_contract/complete_workflow_demo.js +0 -343
  85. package/examples/smart_contract/covenant_builder_demo.js +0 -176
  86. package/examples/smart_contract/script_testing_integration.js +0 -198
  87. package/examples/smart_contract_templates.js +0 -718
  88. package/examples/working_smart_contracts.js +0 -348
@@ -1,176 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Covenant Builder Demo
4
- * ====================
5
- *
6
- * Demonstrates how to use the enhanced SmartContract.Preimage API
7
- * to build practical covenant constraints with field isolation.
8
- *
9
- * This example shows:
10
- * - Extracting preimage fields with JavaScript
11
- * - Generating ASM for stack operations
12
- * - Building covenant logic for common use cases
13
- * - Testing locally before broadcast
14
- */
15
-
16
- const SmartContract = require('../../lib/smart_contract');
17
- const crypto = require('crypto');
18
-
19
- console.log('๐Ÿ—๏ธ Smart Contract Covenant Builder Demo');
20
- console.log('='.repeat(60));
21
-
22
- // Get sample preimage for testing
23
- const preimageHex = require('../preimage/generate_sample_preimage').getLargePreimage();
24
-
25
- console.log('\n๐Ÿ“‹ Step 1: Analyze Preimage Structure');
26
- const analysis = SmartContract.Preimage.analyzeFromHex(preimageHex);
27
- console.log(`Preimage: ${analysis.length} bytes`);
28
- console.log(`Structure: LEFT(${analysis.structure.leftFixed}) + scriptLen(${analysis.structure.scriptLenVarint}) + script(${analysis.structure.scriptCode}) + RIGHT(${analysis.structure.rightFixed})`);
29
- console.log(`Valid BIP-143: ${analysis.validation.valid}`);
30
-
31
- console.log('\n๐ŸŽฏ Step 2: Common Covenant Patterns');
32
-
33
- // Pattern 1: Minimum Amount Covenant
34
- console.log('\n๐Ÿ”น Pattern 1: Minimum Amount Covenant');
35
- const valueExtraction = SmartContract.Preimage.extractFromHex(preimageHex, 'value');
36
- console.log(`Current amount: ${valueExtraction.interpretation.satoshis} satoshis`);
37
- console.log('ASM to extract value:');
38
- console.log(valueExtraction.asm);
39
- console.log('\n๐Ÿ’ก Use case: Ensure outputs meet minimum threshold (e.g., 1000 sats)');
40
-
41
- // Pattern 2: SIGHASH Validation Covenant
42
- console.log('\n๐Ÿ”น Pattern 2: SIGHASH Validation Covenant');
43
- const sighashExtraction = SmartContract.Preimage.extractFromHex(preimageHex, 'sighashType');
44
- console.log(`SIGHASH type: ${sighashExtraction.interpretation.description}`);
45
- console.log('ASM to extract sighash:');
46
- console.log(sighashExtraction.asm);
47
- console.log('\n๐Ÿ’ก Use case: Enforce specific signing behavior (SIGHASH_ALL, etc.)');
48
-
49
- // Pattern 3: Script Template Covenant
50
- console.log('\n๐Ÿ”น Pattern 3: Script Template Covenant');
51
- const scriptExtraction = SmartContract.Preimage.extractFromHex(preimageHex, 'scriptCode');
52
- console.log(`Script type: ${scriptExtraction.interpretation.description}`);
53
- console.log('ASM to extract scriptCode:');
54
- console.log(scriptExtraction.asm);
55
- console.log('\n๐Ÿ’ก Use case: Validate output script patterns (P2PKH, multisig, etc.)');
56
-
57
- console.log('\n๐ŸŽฎ Step 3: Multi-Field Covenant Logic');
58
-
59
- // Extract multiple fields for complex covenant
60
- const covenantFields = SmartContract.Preimage.extractMultipleFromHex(
61
- preimageHex,
62
- ['nVersion', 'hashPrevouts', 'value', 'sighashType', 'scriptCode']
63
- );
64
-
65
- console.log('\nCovenant constraint analysis:');
66
- Object.keys(covenantFields).forEach(field => {
67
- const result = covenantFields[field];
68
- console.log(`โ€ข ${field}: ${result.strategy} extraction โ†’ ${result.interpretation.description || 'Raw data'}`);
69
- });
70
-
71
- console.log('\n๐Ÿ”ง Step 4: Building Complete Covenant Script');
72
-
73
- // Example: Build a covenant that enforces minimum amount + specific SIGHASH
74
- function buildMinimumAmountCovenant(minimumSats, requiredSighash) {
75
- const valueASM = SmartContract.Preimage.generateASMFromHex(preimageHex, 'value');
76
- const sighashASM = SmartContract.Preimage.generateASMFromHex(preimageHex, 'sighashType');
77
-
78
- return `
79
- # ๐Ÿ›๏ธ Minimum Amount + SIGHASH Covenant
80
- # Ensures output has minimum ${minimumSats} satoshis and correct SIGHASH
81
-
82
- # Extract and validate amount
83
- ${valueASM}
84
- # Stack: [value]
85
- OP_BIN2NUM # Convert to number: [value_num]
86
- ${minimumSats} # Push minimum: [value_num, min]
87
- OP_GREATERTHANOREQUAL # Check minimum: [bool]
88
- OP_VERIFY # Verify minimum amount
89
-
90
- # Extract and validate SIGHASH
91
- ${sighashASM}
92
- # Stack: [sighash]
93
- ${requiredSighash} # Push required SIGHASH: [sighash, required]
94
- OP_EQUAL # Check SIGHASH: [bool]
95
- OP_VERIFY # Verify SIGHASH type
96
-
97
- # If we reach here, covenant constraints are satisfied
98
- OP_TRUE # Success: [true]
99
-
100
- # ๐Ÿ“– This covenant ensures:
101
- # 1. Output amount >= ${minimumSats} satoshis
102
- # 2. Transaction uses SIGHASH type ${requiredSighash}
103
- # 3. Can be extended with additional constraints
104
- `.trim();
105
- }
106
-
107
- const covenantScript = buildMinimumAmountCovenant(1000, '0x41000000');
108
- console.log('\nGenerated covenant locking script:');
109
- console.log(covenantScript);
110
-
111
- console.log('\n๐ŸŽฏ Step 5: Covenant Testing Framework');
112
-
113
- // Create a testing framework for covenant validation
114
- function testCovenant(preimageHex, covenantLogic) {
115
- console.log('\n๐Ÿงช Testing covenant logic...');
116
-
117
- try {
118
- // Validate preimage structure
119
- const validation = SmartContract.Preimage.validateFromHex(preimageHex);
120
- if (!validation.valid) {
121
- console.log('โŒ Invalid preimage:', validation.errors.join(', '));
122
- return false;
123
- }
124
-
125
- // Extract fields for testing
126
- const testFields = SmartContract.Preimage.extractMultipleFromHex(
127
- preimageHex,
128
- ['value', 'sighashType']
129
- );
130
-
131
- // Test covenant constraints
132
- const currentAmount = parseInt(testFields.value.interpretation.satoshis);
133
- const currentSighash = testFields.sighashType.value;
134
-
135
- console.log(`Current amount: ${currentAmount} satoshis`);
136
- console.log(`Current SIGHASH: ${testFields.sighashType.interpretation.description}`);
137
-
138
- // Simulate covenant checks
139
- const minimumAmount = 1000;
140
- const requiredSighash = '41000000'; // SIGHASH_ALL | FORKID
141
-
142
- const amountOK = currentAmount >= minimumAmount;
143
- const sighashOK = currentSighash === requiredSighash;
144
-
145
- console.log(`โœ“ Amount check: ${amountOK ? 'PASS' : 'FAIL'} (${currentAmount} >= ${minimumAmount})`);
146
- console.log(`โœ“ SIGHASH check: ${sighashOK ? 'PASS' : 'FAIL'} (${currentSighash} === ${requiredSighash})`);
147
-
148
- const covenantValid = amountOK && sighashOK;
149
- console.log(`\\n๐Ÿ›๏ธ Covenant validation: ${covenantValid ? 'โœ… PASS' : 'โŒ FAIL'}`);
150
-
151
- return covenantValid;
152
-
153
- } catch (error) {
154
- console.log('โŒ Test error:', error.message);
155
- return false;
156
- }
157
- }
158
-
159
- const testResult = testCovenant(preimageHex, covenantScript);
160
-
161
- console.log('\n' + '='.repeat(60));
162
- console.log('๐ŸŽ‰ Covenant Builder Demo Complete!');
163
- console.log('');
164
- console.log('๐Ÿ”— What developers can now do:');
165
- console.log(' โœ… Extract any preimage field with JavaScript');
166
- console.log(' โœ… Generate optimal ASM for stack operations');
167
- console.log(' โœ… Build complex covenant constraints');
168
- console.log(' โœ… Test locally before broadcast');
169
- console.log(' โœ… Validate preimage structure and fields');
170
- console.log(' โœ… Create reusable covenant patterns');
171
- console.log('');
172
- console.log('๐Ÿ“š Next Steps:');
173
- console.log(' โ€ข Add real UTXO generation with BSV keys');
174
- console.log(' โ€ข Build custom locking/unlocking script generators');
175
- console.log(' โ€ข Implement local script verification engine');
176
- console.log(' โ€ข Create broadcast integration for production');
@@ -1,198 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Smart Contract Script Testing Integration Demo
4
- * =============================================
5
- *
6
- * Demonstrates how to integrate the script_interpreter.js with our
7
- * SmartContract development environment for complete covenant testing.
8
- */
9
-
10
- const SmartContract = require('../../lib/smart_contract');
11
- const { execSync } = require('child_process');
12
- const fs = require('fs');
13
- const path = require('path');
14
-
15
- console.log('๐Ÿ”ง Smart Contract Script Testing Integration');
16
- console.log('='.repeat(70));
17
-
18
- // ============================================================================
19
- // PHASE 1: CREATE COVENANT WITH REAL UTXOS
20
- // ============================================================================
21
-
22
- console.log('\n๐Ÿ›๏ธ Phase 1: Create Covenant with Real UTXOs');
23
- console.log('-'.repeat(50));
24
-
25
- // Create test environment
26
- const testEnv = SmartContract.createTestEnvironment({
27
- utxoCount: 1,
28
- satoshis: 100000,
29
- covenantAmount: 80000
30
- });
31
-
32
- // Get preimage for analysis
33
- const preimageHex = testEnv.getPreimage();
34
- const keypair = testEnv.getKeypair();
35
-
36
- console.log('Test Environment:');
37
- console.log(` Address: ${keypair.addressString}`);
38
- console.log(` Preimage: ${preimageHex.substring(0, 40)}...`);
39
-
40
- // ============================================================================
41
- // PHASE 2: BUILD COVENANT LOCKING SCRIPT
42
- // ============================================================================
43
-
44
- console.log('\n๐Ÿ”’ Phase 2: Build Covenant Locking Script');
45
- console.log('-'.repeat(50));
46
-
47
- // Extract fields and generate ASM
48
- const valueField = testEnv.extractField('value');
49
- const sighashField = testEnv.extractField('sighashType');
50
-
51
- // Build a simple amount validation covenant
52
- const covenantASM = `
53
- # Amount Validation Covenant
54
- ${valueField.asm}
55
- # Stack: [value_bytes]
56
- OP_BIN2NUM
57
- # Stack: [value_number]
58
- 75000
59
- # Stack: [value_number, minimum]
60
- OP_GREATERTHANOREQUAL
61
- # Stack: [bool]
62
- `.trim().replace(/^#.*$/gm, '').replace(/\n\n+/g, '\n').trim();
63
-
64
- console.log('Generated Covenant ASM:');
65
- console.log(covenantASM);
66
-
67
- // ============================================================================
68
- // PHASE 3: BUILD UNLOCKING SCRIPT
69
- // ============================================================================
70
-
71
- console.log('\n๐Ÿ”“ Phase 3: Build Unlocking Script');
72
- console.log('-'.repeat(50));
73
-
74
- // Create unlocking script that provides the preimage
75
- const unlockingASM = preimageHex;
76
-
77
- console.log('Unlocking Script (Preimage):');
78
- console.log(` Length: ${preimageHex.length / 2} bytes`);
79
- console.log(` Hex: ${preimageHex.substring(0, 60)}...`);
80
-
81
- // ============================================================================
82
- // PHASE 4: TEST WITH SCRIPT INTERPRETER
83
- // ============================================================================
84
-
85
- console.log('\n๐Ÿงช Phase 4: Test with Script Interpreter');
86
- console.log('-'.repeat(50));
87
-
88
- // Write scripts to temporary files for easier testing
89
- const tempDir = path.join(__dirname, 'temp');
90
- if (!fs.existsSync(tempDir)) {
91
- fs.mkdirSync(tempDir);
92
- }
93
-
94
- const lockingFile = path.join(tempDir, 'locking.asm');
95
- const unlockingFile = path.join(tempDir, 'unlocking.hex');
96
-
97
- fs.writeFileSync(lockingFile, covenantASM);
98
- fs.writeFileSync(unlockingFile, unlockingASM);
99
-
100
- console.log('Script files created:');
101
- console.log(` Locking: ${lockingFile}`);
102
- console.log(` Unlocking: ${unlockingFile}`);
103
-
104
- // ============================================================================
105
- // PHASE 5: AUTOMATED SCRIPT VERIFICATION
106
- // ============================================================================
107
-
108
- console.log('\nโšก Phase 5: Automated Script Verification');
109
- console.log('-'.repeat(50));
110
-
111
- function testCovenantScript(unlockingScript, lockingScript) {
112
- console.log('Testing covenant script execution...');
113
-
114
- try {
115
- // Test using our script interpreter
116
- const interpreterPath = path.join(__dirname, '../scripts/script_interpreter.js');
117
-
118
- // Run truth evaluation
119
- const cmd = `node "${interpreterPath}" --unlocking "${unlockingScript}" --locking "${lockingScript}" --truth`;
120
- console.log('Executing:', cmd.substring(0, 80) + '...');
121
-
122
- const result = execSync(cmd, {
123
- encoding: 'utf8',
124
- timeout: 10000,
125
- stdio: ['pipe', 'pipe', 'pipe']
126
- });
127
-
128
- console.log('\\nScript Execution Result:');
129
- console.log(result);
130
-
131
- // Parse result to determine success/failure
132
- const success = result.includes('TRUE (Success)');
133
- return { success, output: result };
134
-
135
- } catch (error) {
136
- console.log('โŒ Script execution failed:', error.message);
137
- return { success: false, error: error.message };
138
- }
139
- }
140
-
141
- // Test the covenant
142
- const testResult = testCovenantScript(unlockingASM, covenantASM);
143
-
144
- console.log('\\n๐Ÿ“Š Test Results:');
145
- console.log(` Success: ${testResult.success ? 'โœ… PASS' : 'โŒ FAIL'}`);
146
-
147
- if (testResult.success) {
148
- console.log(' โœ… Covenant script executed successfully!');
149
- console.log(' โœ… Amount constraint validated');
150
- console.log(' โœ… Preimage extraction working');
151
- console.log(' โœ… Ready for mainnet deployment');
152
- } else {
153
- console.log(' โŒ Covenant validation failed');
154
- console.log(' ๐Ÿ”ง Debug with step-by-step execution:');
155
- console.log(` node examples/scripts/script_interpreter.js --unlocking "${unlockingASM.substring(0, 40)}..." --locking "${covenantASM.replace(/\n/g, ' ').substring(0, 40)}..." --step`);
156
- }
157
-
158
- // ============================================================================
159
- // PHASE 6: INTEGRATION SUMMARY
160
- // ============================================================================
161
-
162
- console.log('\n' + '='.repeat(70));
163
- console.log('๐ŸŽฏ SCRIPT TESTING INTEGRATION COMPLETE');
164
- console.log('='.repeat(70));
165
-
166
- console.log('\n๐Ÿ”— Integration Benefits:');
167
- console.log(' โœ… Generate covenants with real BSV UTXOs');
168
- console.log(' โœ… Extract preimage fields automatically');
169
- console.log(' โœ… Build ASM scripts with field validation');
170
- console.log(' โœ… Test covenant execution with script interpreter');
171
- console.log(' โœ… Debug step-by-step when issues arise');
172
- console.log(' โœ… Verify locally before expensive broadcast');
173
-
174
- console.log('\n๐Ÿš€ Complete Workflow Available:');
175
- console.log(' 1. SmartContract.UTXOGenerator โ†’ Real UTXOs');
176
- console.log(' 2. SmartContract.Preimage โ†’ Field extraction + ASM');
177
- console.log(' 3. Custom covenant logic โ†’ Locking scripts');
178
- console.log(' 4. script_interpreter.js โ†’ Local verification');
179
- console.log(' 5. Mainnet broadcast โ†’ Production deployment');
180
-
181
- console.log('\n๐Ÿ’ก Debug Commands:');
182
- console.log(' # Step-by-step execution:');
183
- console.log(' node examples/scripts/script_interpreter.js --combined "YOUR_ASM" --step');
184
- console.log('');
185
- console.log(' # Truth evaluation:');
186
- console.log(' node examples/scripts/script_interpreter.js --unlocking "HEX" --locking "ASM" --truth');
187
-
188
- console.log('\nโœ… Smart contract development ecosystem with script testing is complete!');
189
-
190
- // Cleanup
191
- try {
192
- fs.unlinkSync(lockingFile);
193
- fs.unlinkSync(unlockingFile);
194
- fs.rmdirSync(tempDir);
195
- console.log('\\n๐Ÿงน Temporary files cleaned up');
196
- } catch (e) {
197
- // Ignore cleanup errors
198
- }