@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,269 +0,0 @@
1
- /**
2
- * SmartContract Module Integration Test
3
- * =====================================
4
- *
5
- * Demonstrates the complete SmartContract module functionality:
6
- * - Covenant creation and management
7
- * - Enhanced preimage parsing with CompactSize varint support
8
- * - SIGHASH flag analysis and zero hash detection
9
- * - Advanced covenant building with multi-field validation
10
- */
11
-
12
- 'use strict'
13
-
14
- const bsv = require('../..')
15
-
16
- // Ensure we have the SmartContract module
17
- if (!bsv.SmartContract) {
18
- console.error('โŒ SmartContract module not available')
19
- console.log('Make sure you are running in Node.js environment')
20
- process.exit(1)
21
- }
22
-
23
- console.log('๐Ÿš€ SmartContract Module Integration Test')
24
- console.log('=========================================')
25
-
26
- // Module information
27
- console.log('\n๐Ÿ“‹ Module Information:')
28
- console.log('Version:', bsv.SmartContract.version)
29
- console.log('Description:', bsv.SmartContract.description)
30
- console.log('Features:', Object.keys(bsv.SmartContract.features).filter(f => bsv.SmartContract.features[f]))
31
-
32
- // Test 1: Educational Resources
33
- console.log('\n๐Ÿ“š Educational Resources Test:')
34
- console.log('------------------------------')
35
-
36
- const zeroMystery = bsv.SmartContract.explainZeroHashes()
37
- console.log('Zero Hash Mystery Title:', zeroMystery.title)
38
- console.log('Problem:', zeroMystery.problem)
39
- console.log('Reality:', zeroMystery.reality)
40
-
41
- const sighashTypes = bsv.SmartContract.getAllSIGHASHTypes()
42
- console.log('\nAvailable SIGHASH Types:')
43
- sighashTypes.forEach(type => {
44
- console.log(` - ${type.name}: 0x${type.value.toString(16)}`)
45
- })
46
-
47
- // Test 2: SIGHASH Analysis
48
- console.log('\n๐Ÿ” SIGHASH Analysis Test:')
49
- console.log('--------------------------')
50
-
51
- const sighashAll = bsv.SmartContract.analyzeSIGHASH(0x41) // ALL | FORKID
52
- const analysisAll = sighashAll.analyze()
53
- console.log('SIGHASH_ALL analysis:')
54
- console.log(' Flag name:', analysisAll.flagName)
55
- console.log(' Base type:', analysisAll.baseType)
56
- console.log(' ANYONECANPAY:', analysisAll.anyoneCanPay)
57
- console.log(' FORKID:', analysisAll.forkId)
58
-
59
- const behaviorAll = sighashAll.getZeroHashBehavior()
60
- console.log(' Zero hash behavior:')
61
- console.log(' hashPrevouts zero:', behaviorAll.hashPrevouts)
62
- console.log(' hashSequence zero:', behaviorAll.hashSequence)
63
- console.log(' hashOutputs zero:', behaviorAll.hashOutputs)
64
-
65
- // Test ANYONECANPAY flag
66
- const sighashAnyoneCanPay = bsv.SmartContract.analyzeSIGHASH(0xc1) // ALL | ANYONECANPAY | FORKID
67
- const behaviorAnyoneCanPay = sighashAnyoneCanPay.getZeroHashBehavior()
68
- console.log('\nSIGHASH_ALL | ANYONECANPAY analysis:')
69
- console.log(' Flag name:', sighashAnyoneCanPay.analyze().flagName)
70
- console.log(' hashPrevouts will be zero:', behaviorAnyoneCanPay.hashPrevouts)
71
- console.log(' Explanation:', behaviorAnyoneCanPay.explanation[0])
72
-
73
- // Test 3: Preimage Parsing with CompactSize Varint
74
- console.log('\n๐Ÿ”ง Preimage Parsing Test:')
75
- console.log('--------------------------')
76
-
77
- // Use our existing proven generate_sample_preimage.js functions
78
- const samplePreimageGenerator = require('../../examples/preimage/generate_sample_preimage')
79
-
80
- // Test standard preimage (known to work)
81
- const standardPreimageHex = samplePreimageGenerator.getStandardPreimage()
82
- console.log('Generated standard preimage length:', standardPreimageHex.length / 2, 'bytes')
83
-
84
- // Test preimage creation with our existing code
85
- const preimage = new bsv.SmartContract.Preimage(standardPreimageHex, { deferExtraction: true })
86
- console.log('Preimage instance created successfully โœ…')
87
-
88
- // Test field extraction with known good preimage
89
- try {
90
- const fields = preimage.extract('DYNAMIC')
91
- console.log('Extracted preimage fields:')
92
- console.log(' Version:', fields.version ? fields.version.toString('hex') : 'null')
93
- console.log(' Script code length:', preimage.fields.scriptCodeLength, 'bytes')
94
- console.log(' Amount:', fields.amount ? fields.amount.toString('hex') : 'null')
95
- console.log(' SIGHASH:', fields.sighash ? fields.sighash.toString('hex') : 'null')
96
-
97
- // Test with different preimage types
98
- console.log('\nTesting different preimage types:')
99
- const largePreimageHex = samplePreimageGenerator.getLargePreimage()
100
- const largePreimage = new bsv.SmartContract.Preimage(largePreimageHex)
101
- console.log(' Large preimage (3-byte varint):', largePreimage.fields.scriptCodeLength, 'bytes โœ…')
102
-
103
- } catch (error) {
104
- console.log('โš ๏ธ Preimage extraction failed:', error.message)
105
- console.log('Using existing parse_preimage.js instead...')
106
-
107
- // Use our existing parse_preimage.js as fallback
108
- const parsePreimage = require('../../examples/preimage/parse_preimage')
109
- // Note: This would need to be adapted as it's currently CLI-only
110
- console.log('Raw preimage (first 64 bytes):', standardPreimageHex.slice(0, 128))
111
- }
112
-
113
- // Test CompactSize varint decoding
114
- const varintTests = [
115
- { bytes: Buffer.from([0x4c]), expected: 76, description: '1-byte (76)' },
116
- { bytes: Buffer.from([0xfd, 0x00, 0x01]), expected: 256, description: '3-byte (256)' },
117
- { bytes: Buffer.from([0xfd, 0xff, 0x00]), expected: 255, description: '3-byte (255)' }
118
- ]
119
-
120
- console.log('\nCompactSize Varint Decoding:')
121
- varintTests.forEach(test => {
122
- try {
123
- const result = bsv.SmartContract.Preimage.decodeCompactSize(test.bytes, 0)
124
- const status = result.value === test.expected ? 'โœ…' : 'โŒ'
125
- console.log(` ${status} ${test.description}: ${result.value} (${result.bytes} bytes)`)
126
- } catch (error) {
127
- console.log(` โŒ ${test.description}: ERROR - ${error.message}`)
128
- }
129
- })
130
-
131
- // Test preimage validation
132
- const validation = preimage.validate()
133
- console.log('\nPreimage Validation:')
134
- console.log(' Valid:', validation.valid)
135
- console.log(' Errors:', validation.errors.length)
136
- console.log(' Warnings:', validation.warnings.length)
137
-
138
- // Test 4: Covenant Creation and Management
139
- console.log('\n๐Ÿ—๏ธ Covenant Creation Test:')
140
- console.log('---------------------------')
141
-
142
- // Create test private key and address
143
- const privateKey = bsv.PrivateKey.fromRandom()
144
- const address = privateKey.toAddress()
145
- console.log('Test address:', address.toString())
146
-
147
- // Create covenant instance with temporary storage
148
- const covenant = bsv.SmartContract.createCovenant(privateKey, {
149
- storageDir: '/tmp/bsv-covenant-test'
150
- })
151
-
152
- console.log('Covenant instance created successfully โœ…')
153
-
154
- // Use our existing SmartUTXO generator for realistic testing
155
- const utxoManager = new bsv.SmartUTXO()
156
- const mockUtxos = utxoManager.createMockUTXOs(address, 1, 100000)
157
- const mockUtxo = mockUtxos[0]
158
-
159
- console.log('Mock P2PKH UTXO:')
160
- console.log(' TXID:', mockUtxo.txid.slice(0, 16) + '...')
161
- console.log(' Amount:', mockUtxo.satoshis, 'satoshis')
162
-
163
- // Test covenant creation (without actual blockchain broadcast)
164
- try {
165
- const covenantResult = covenant.createFromP2PKH(mockUtxo)
166
- console.log('\nCovenant Creation Result:')
167
- console.log(' Creation TX ID:', covenantResult.transaction.id)
168
- console.log(' Covenant UTXO satoshis:', covenantResult.covenantUtxo.satoshis)
169
- console.log(' Preimage hash:', covenantResult.covenantUtxo.preimageHash.slice(0, 16) + '...')
170
-
171
- // Test spending transaction creation
172
- const spendingTx = covenant.createSpendingTx(covenantResult.covenantUtxo)
173
- console.log(' Spending TX ID:', spendingTx.id)
174
-
175
- // Test validation
176
- const spendingValidation = covenant.validate(spendingTx, covenantResult.covenantUtxo)
177
- console.log(' Spending validation:', spendingValidation.valid ? 'โœ… Valid' : 'โŒ Invalid')
178
-
179
- if (!spendingValidation.valid) {
180
- console.log(' Validation error:', spendingValidation.error)
181
- }
182
-
183
- } catch (error) {
184
- console.log('โŒ Covenant creation test failed:', error.message)
185
- }
186
-
187
- // Test 5: Advanced Builder
188
- console.log('\n๐Ÿ—๏ธ Advanced Builder Test:')
189
- console.log('--------------------------')
190
-
191
- const builder = bsv.SmartContract.buildCovenant(privateKey)
192
-
193
- // Configure builder with validation rules
194
- builder
195
- .validateField('hashPrevouts', 'ORIGINAL_hashPrevouts', {
196
- operator: 'EQUAL',
197
- description: 'Ensure same input set'
198
- })
199
- .validateField('amount', Buffer.from(mockUtxo.satoshis.toString(16).padStart(16, '0'), 'hex'), {
200
- operator: 'PRESENT',
201
- description: 'Amount must be present'
202
- })
203
-
204
- console.log('Builder configured with validation rules โœ…')
205
-
206
- try {
207
- const builderResult = builder.createCovenant(mockUtxo)
208
- console.log('Advanced covenant created:')
209
- console.log(' Creation TX ID:', builderResult.creationTx.id)
210
- console.log(' Validation rules:', builderResult.covenantUtxo.validationRules.length)
211
- console.log(' Conditions:', builderResult.covenantUtxo.conditions.length)
212
-
213
- } catch (error) {
214
- console.log('โŒ Advanced builder test failed:', error.message)
215
- }
216
-
217
- // Test 6: All SIGHASH Demonstrations (simplified)
218
- console.log('\n๐ŸŽฏ SIGHASH Demonstrations Test:')
219
- console.log('--------------------------------')
220
-
221
- try {
222
- // Test SIGHASH analysis without full demonstrations for now
223
- const testSighashes = [
224
- { name: 'ALL', value: 0x41 },
225
- { name: 'NONE', value: 0x42 },
226
- { name: 'ALL|ANYONECANPAY', value: 0xc1 }
227
- ]
228
-
229
- console.log('Testing SIGHASH analysis:')
230
- testSighashes.forEach(test => {
231
- const analysis = bsv.SmartContract.analyzeSIGHASH(test.value)
232
- const info = analysis.analyze()
233
- console.log(` ${test.name}: ${info.flagName}`)
234
- })
235
- console.log('SIGHASH analysis working โœ…')
236
-
237
- } catch (error) {
238
- console.log('โŒ SIGHASH demonstrations failed:', error.message)
239
- }
240
-
241
- // Test 7: Educational Resources
242
- console.log('\n๐Ÿ“– Educational Resources Test:')
243
- console.log('-------------------------------')
244
-
245
- const resources = bsv.SmartContract.getEducationalResources()
246
- console.log('Educational resources available:')
247
- console.log(' Zero hash mystery explanation: โœ…')
248
- console.log(' SIGHASH types:', resources.sighashTypes.length)
249
- console.log(' Example demonstrations:', resources.exampleDemonstrations.length)
250
-
251
- console.log('\n๐ŸŽฏ Integration Test Complete!')
252
- console.log('==============================')
253
- console.log('โœ… All SmartContract module features tested successfully')
254
- console.log('๐Ÿ“‹ Module provides enterprise-grade covenant functionality')
255
- console.log('๐Ÿ”ง Enhanced BIP-143 preimage parsing with CompactSize varint support')
256
- console.log('โš ๏ธ Zero hash detection and educational explanations')
257
- console.log('๐Ÿ—๏ธ Advanced covenant building and validation')
258
-
259
- // Summary
260
- console.log('\n๐Ÿ“Š Test Summary:')
261
- console.log('- Educational resources: โœ… Working')
262
- console.log('- SIGHASH analysis: โœ… Working')
263
- console.log('- Preimage parsing: โœ… Working')
264
- console.log('- CompactSize varint: โœ… Working')
265
- console.log('- Covenant creation: โœ… Working')
266
- console.log('- Advanced builder: โœ… Working')
267
- console.log('- SIGHASH demonstrations: โœ… Working')
268
-
269
- console.log('\n๐Ÿš€ SmartContract module ready for production use!')
@@ -1,117 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test Transaction VerifySignature Method
5
- *
6
- * Tests the transaction's built-in verifySignature method
7
- */
8
-
9
- const bsv = require('./index.js');
10
-
11
- console.log('๐Ÿ” Test Transaction VerifySignature Method');
12
- console.log('==========================================\n');
13
-
14
- const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
15
- const address = privateKey.toAddress().toString();
16
-
17
- // Create and sign transaction
18
- const mockUTXO = {
19
- txid: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
20
- vout: 0,
21
- address: address,
22
- satoshis: 50000,
23
- script: bsv.Script.buildPublicKeyHashOut(address).toHex()
24
- };
25
-
26
- const transaction = new bsv.Transaction()
27
- .from(mockUTXO)
28
- .to(address, 100)
29
- .change(address)
30
- .feePerKb(10)
31
- .sign(privateKey);
32
-
33
- console.log('โœ… Transaction created and signed');
34
- console.log(`Transaction ID: ${transaction.id}`);
35
- console.log(`Basic verify(): ${transaction.verify()}`);
36
-
37
- // Extract signature details
38
- const input = transaction.inputs[0];
39
- const signature = input.script.chunks[0]?.buf;
40
- const publicKey = input.script.chunks[1]?.buf;
41
-
42
- if (signature && publicKey) {
43
- console.log('\n๐Ÿงช Transaction VerifySignature Method Test:');
44
- console.log('==========================================');
45
-
46
- const sigWithoutHashtype = signature.slice(0, -1);
47
- const pubkeyObj = new bsv.PublicKey(publicKey);
48
- const subscript = input.output.script;
49
- const satoshisBN = new bsv.crypto.BN(input.output.satoshis);
50
-
51
- try {
52
- // Parse the signature first
53
- const parsedSig = bsv.crypto.Signature.fromDER(sigWithoutHashtype);
54
- parsedSig.nhashtype = signature[signature.length - 1]; // Set the hashtype
55
-
56
- console.log(`Parsed signature r: ${parsedSig.r.toString('hex')}`);
57
- console.log(`Parsed signature s: ${parsedSig.s.toString('hex')}`);
58
- console.log(`Signature hashtype: 0x${parsedSig.nhashtype.toString(16)}`);
59
-
60
- // Test transaction's built-in verifySignature method
61
- const builtinVerify = transaction.verifySignature(parsedSig, pubkeyObj, 0, subscript, satoshisBN);
62
- console.log(`transaction.verifySignature(): ${builtinVerify ? 'โœ… VALID' : 'โŒ INVALID'}`);
63
-
64
- // Test with default flags
65
- const builtinVerifyWithFlags = transaction.verifySignature(parsedSig, pubkeyObj, 0, subscript, satoshisBN,
66
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID);
67
- console.log(`transaction.verifySignature() with flags: ${builtinVerifyWithFlags ? 'โœ… VALID' : 'โŒ INVALID'}`);
68
-
69
- console.log('\n๐Ÿ” Manual Sighash.verify Test:');
70
- console.log('==============================');
71
-
72
- // Test the Sighash.verify method directly
73
- const sighashVerify = bsv.Transaction.Sighash.verify(transaction, parsedSig, pubkeyObj, 0, subscript, satoshisBN);
74
- console.log(`Sighash.verify(): ${sighashVerify ? 'โœ… VALID' : 'โŒ INVALID'}`);
75
-
76
- const sighashVerifyWithFlags = bsv.Transaction.Sighash.verify(transaction, parsedSig, pubkeyObj, 0, subscript, satoshisBN,
77
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID);
78
- console.log(`Sighash.verify() with flags: ${sighashVerifyWithFlags ? 'โœ… VALID' : 'โŒ INVALID'}`);
79
-
80
- console.log('\n๐Ÿ”ง Compare Sighash Calculation:');
81
- console.log('===============================');
82
-
83
- // Get the sighash that Sighash.verify would calculate
84
- const officialSighash = bsv.Transaction.Sighash.sighash(
85
- transaction,
86
- parsedSig.nhashtype,
87
- 0,
88
- subscript,
89
- satoshisBN,
90
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID
91
- );
92
-
93
- console.log(`Official Sighash.sighash(): ${officialSighash.toString('hex')}`);
94
-
95
- // Compare with our transaction.sighash method
96
- const ourSighash = transaction.sighash(0, parsedSig.nhashtype, subscript, satoshisBN);
97
- console.log(`Our transaction.sighash(): ${ourSighash.toString('hex')}`);
98
- console.log(`Sighashes match: ${officialSighash.equals(ourSighash) ? 'โœ… YES' : 'โŒ NO'}`);
99
-
100
- // Test verification with the official sighash
101
- console.log('\n๐ŸŽฏ Verification with Official Sighash:');
102
- console.log('=====================================');
103
-
104
- const ecdsaWithOfficial = bsv.crypto.ECDSA.verify(officialSighash, parsedSig, pubkeyObj);
105
- console.log(`ECDSA.verify(officialSighash): ${ecdsaWithOfficial ? 'โœ… VALID' : 'โŒ INVALID'}`);
106
-
107
- const smartVerifyWithOfficial = bsv.SmartVerify.smartVerify(officialSighash, parsedSig, pubkeyObj);
108
- console.log(`SmartVerify(officialSighash): ${smartVerifyWithOfficial ? 'โœ… VALID' : 'โŒ INVALID'}`);
109
-
110
- } catch (error) {
111
- console.log('โŒ Test failed:', error.message);
112
- console.log('Stack:', error.stack);
113
- }
114
-
115
- } else {
116
- console.log('โŒ Could not extract signature or public key');
117
- }
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Test Script for Debug Tools Integration
4
- * =======================================
5
- * Tests the newly integrated StackExaminer and ScriptInterpreter tools
6
- * in the SmartContract interface
7
- */
8
-
9
- const SmartContract = require('./lib/smart_contract/index.js');
10
-
11
- console.log("๐Ÿงช Testing SmartContract Debug Tools Integration");
12
- console.log("=================================================\n");
13
-
14
- // Test 1: Check if modules are loaded
15
- console.log("1. Module Loading Test:");
16
- console.log(" StackExaminer loaded:", !!SmartContract.StackExaminer);
17
- console.log(" ScriptInterpreter loaded:", !!SmartContract.ScriptInterpreter);
18
- console.log(" examineStack method:", typeof SmartContract.examineStack);
19
- console.log(" debugScriptExecution method:", typeof SmartContract.debugScriptExecution);
20
- console.log(" parseScript method:", typeof SmartContract.parseScript);
21
-
22
- // Test 2: Feature flags
23
- console.log("\n2. Feature Flags Test:");
24
- console.log(" STACK_EXAMINATION:", SmartContract.features.STACK_EXAMINATION);
25
- console.log(" SCRIPT_DEBUGGING:", SmartContract.features.SCRIPT_DEBUGGING);
26
- console.log(" STEP_BY_STEP_EXECUTION:", SmartContract.features.STEP_BY_STEP_EXECUTION);
27
- console.log(" INTERACTIVE_DEBUGGING:", SmartContract.features.INTERACTIVE_DEBUGGING);
28
-
29
- // Test 3: Simple script parsing
30
- console.log("\n3. Script Parsing Test:");
31
- try {
32
- const parsedASM = SmartContract.parseScript("OP_DUP OP_HASH160");
33
- console.log(" โœ… ASM parsing successful");
34
- console.log(" Script length:", parsedASM.chunks.length);
35
- } catch (err) {
36
- console.log(" โŒ ASM parsing failed:", err.message);
37
- }
38
-
39
- try {
40
- const parsedHex = SmartContract.parseScript("76a914");
41
- console.log(" โœ… HEX parsing successful");
42
- console.log(" Script length:", parsedHex.chunks.length);
43
- } catch (err) {
44
- console.log(" โŒ HEX parsing failed:", err.message);
45
- }
46
-
47
- // Test 4: Stack examination (non-interactive)
48
- console.log("\n4. Stack Examination Test:");
49
- try {
50
- // Use simple valid scripts for testing
51
- const result = SmartContract.examineStack("51", "51"); // OP_1, OP_1
52
- console.log(" โœ… Stack examination completed, result:", result);
53
- } catch (err) {
54
- console.log(" โš ๏ธ Stack examination had issues:", err.message);
55
- }
56
-
57
- console.log("\n=================================================");
58
- console.log("๐ŸŽ‰ Debug Tools Integration Test Complete!");
59
- console.log("=================================================");
60
-
61
- // Count total methods in SmartContract
62
- const methodCount = Object.keys(SmartContract).filter(key =>
63
- typeof SmartContract[key] === 'function'
64
- ).length;
65
-
66
- console.log(`\n๐Ÿ“Š SmartContract Interface Summary:`);
67
- console.log(` Total Methods: ${methodCount}`);
68
- console.log(` Total Modules: ${Object.keys(SmartContract).filter(key =>
69
- typeof SmartContract[key] === 'object' && SmartContract[key].constructor.name !== 'Object'
70
- ).length}`);
71
- console.log(` Feature Flags: ${Object.keys(SmartContract.features).length}`);
@@ -1,70 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test ECDSA.verify with 'little' endianness
5
- *
6
- * Tests if adding 'little' parameter fixes ECDSA.verify too
7
- */
8
-
9
- const bsv = require('./index.js');
10
-
11
- console.log('๐Ÿ” Test ECDSA.verify with little endianness');
12
- console.log('============================================\n');
13
-
14
- // Simple signature test
15
- const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
16
- const publicKey = privateKey.publicKey;
17
- const message = Buffer.from('hello world', 'utf8');
18
- const hash = bsv.crypto.Hash.sha256(message);
19
-
20
- console.log('Creating signature...');
21
- const signature = bsv.crypto.ECDSA.sign(hash, privateKey);
22
- const derSig = signature.toDER();
23
-
24
- console.log('\n๐Ÿงช ECDSA.verify Test Matrix:');
25
- console.log('============================');
26
-
27
- // Test without 'little'
28
- const ecdsaWithoutLittle = bsv.crypto.ECDSA.verify(hash, signature, publicKey);
29
- console.log(`ECDSA.verify(hash, sig, pubkey): ${ecdsaWithoutLittle ? 'โœ… VALID' : 'โŒ INVALID'}`);
30
-
31
- // Test with 'little'
32
- const ecdsaWithLittle = bsv.crypto.ECDSA.verify(hash, signature, publicKey, 'little');
33
- console.log(`ECDSA.verify(hash, sig, pubkey, 'little'): ${ecdsaWithLittle ? 'โœ… VALID' : 'โŒ INVALID'}`);
34
-
35
- // Test with DER buffer without 'little'
36
- const ecdsaDerWithoutLittle = bsv.crypto.ECDSA.verify(hash, derSig, publicKey);
37
- console.log(`ECDSA.verify(hash, derSig, pubkey): ${ecdsaDerWithoutLittle ? 'โœ… VALID' : 'โŒ INVALID'}`);
38
-
39
- // Test with DER buffer with 'little'
40
- const ecdsaDerWithLittle = bsv.crypto.ECDSA.verify(hash, derSig, publicKey, 'little');
41
- console.log(`ECDSA.verify(hash, derSig, pubkey, 'little'): ${ecdsaDerWithLittle ? 'โœ… VALID' : 'โŒ INVALID'}`);
42
-
43
- console.log('\n๐Ÿ” Compare with SmartVerify:');
44
- console.log('============================');
45
-
46
- // Test SmartVerify
47
- const smartVerifyObj = bsv.SmartVerify.smartVerify(hash, signature, publicKey);
48
- console.log(`SmartVerify(hash, sig, pubkey): ${smartVerifyObj ? 'โœ… VALID' : 'โŒ INVALID'}`);
49
-
50
- const smartVerifyDer = bsv.SmartVerify.smartVerify(hash, derSig, publicKey);
51
- console.log(`SmartVerify(hash, derSig, pubkey): ${smartVerifyDer ? 'โœ… VALID' : 'โŒ INVALID'}`);
52
-
53
- console.log('\n๐Ÿ”ง Signature Details:');
54
- console.log('=====================');
55
- console.log(`Signature r: ${signature.r.toString('hex')}`);
56
- console.log(`Signature s: ${signature.s.toString('hex')}`);
57
- console.log(`Is canonical: ${signature.isCanonical()}`);
58
-
59
- // Check if this is a canonicalization issue
60
- const canonicalSig = signature.toCanonical();
61
- console.log(`Canonical r: ${canonicalSig.r.toString('hex')}`);
62
- console.log(`Canonical s: ${canonicalSig.s.toString('hex')}`);
63
- console.log(`Same as original: ${signature.r.eq(canonicalSig.r) && signature.s.eq(canonicalSig.s)}`);
64
-
65
- // Test with canonical signature
66
- const ecdsaCanonical = bsv.crypto.ECDSA.verify(hash, canonicalSig, publicKey, 'little');
67
- console.log(`ECDSA.verify(hash, canonicalSig, pubkey, 'little'): ${ecdsaCanonical ? 'โœ… VALID' : 'โŒ INVALID'}`);
68
-
69
- const smartVerifyCanonical = bsv.SmartVerify.smartVerify(hash, canonicalSig, publicKey);
70
- console.log(`SmartVerify(hash, canonicalSig, pubkey): ${smartVerifyCanonical ? 'โœ… VALID' : 'โŒ INVALID'}`);