@smartledger/bsv 3.1.1 → 3.2.1

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 (69) hide show
  1. package/CHANGELOG.md +123 -1
  2. package/README.md +233 -277
  3. package/bsv.bundle.js +39 -0
  4. package/bsv.min.js +8 -8
  5. package/docs/ADVANCED_COVENANT_DEVELOPMENT.md +533 -0
  6. package/docs/COVENANT_DEVELOPMENT_RESOLVED.md +169 -0
  7. package/docs/CUSTOM_SCRIPT_DEVELOPMENT.md +320 -0
  8. package/docs/README.md +201 -0
  9. package/docs/block.md +46 -0
  10. package/docs/ecies.md +102 -0
  11. package/docs/index.md +104 -0
  12. package/docs/nchain.md +958 -0
  13. package/docs/networks.md +55 -0
  14. package/docs/preimage.md +126 -0
  15. package/docs/script.md +139 -0
  16. package/docs/transaction.md +174 -0
  17. package/docs/unspentoutput.md +32 -0
  18. package/examples/README.md +200 -0
  19. package/examples/basic/transaction-creation.js +534 -0
  20. package/examples/basic/transaction_signature_api_gap.js +178 -0
  21. package/examples/covenants/advanced_covenant_demo.js +219 -0
  22. package/examples/covenants/covenant_interface_demo.js +270 -0
  23. package/examples/covenants/covenant_manual_signature_resolved.js +212 -0
  24. package/examples/covenants/covenant_signature_template.js +117 -0
  25. package/examples/covenants2/covenant_bidirectional_example.js +262 -0
  26. package/examples/covenants2/covenant_utils_demo.js +120 -0
  27. package/examples/covenants2/preimage_covenant_utils.js +287 -0
  28. package/examples/covenants2/production_integration.js +256 -0
  29. package/examples/data/covenant_utxos.json +28 -0
  30. package/examples/data/utxos.json +26 -0
  31. package/examples/preimage/README.md +178 -0
  32. package/examples/preimage/extract_preimage_bidirectional.js +421 -0
  33. package/examples/preimage/generate_sample_preimage.js +208 -0
  34. package/examples/preimage/generate_sighash_examples.js +152 -0
  35. package/examples/preimage/parse_preimage.js +117 -0
  36. package/examples/preimage/test_preimage_extractor.js +53 -0
  37. package/examples/preimage/test_varint_extraction.js +95 -0
  38. package/examples/scripts/custom_script_helper_example.js +273 -0
  39. package/examples/scripts/custom_script_signature_test.js +344 -0
  40. package/examples/scripts/script_interpreter.js +193 -0
  41. package/examples/smart_contract/complete_workflow_demo.js +343 -0
  42. package/examples/smart_contract/covenant_builder_demo.js +176 -0
  43. package/examples/smart_contract/script_testing_integration.js +198 -0
  44. package/index.js +3 -0
  45. package/lib/covenant-interface.js +713 -0
  46. package/lib/opcode.js +14 -7
  47. package/lib/smart_contract/API_REFERENCE.md +862 -0
  48. package/lib/smart_contract/DOCUMENTATION_SUMMARY.md +201 -0
  49. package/lib/smart_contract/EXAMPLES.md +751 -0
  50. package/lib/smart_contract/QUICK_START.md +549 -0
  51. package/lib/smart_contract/README.md +395 -0
  52. package/lib/smart_contract/builder.js +452 -0
  53. package/lib/smart_contract/covenant.js +336 -0
  54. package/lib/smart_contract/covenant_builder.js +512 -0
  55. package/lib/smart_contract/index.js +350 -0
  56. package/lib/smart_contract/opcode_list.js +30 -0
  57. package/lib/smart_contract/opcode_map.js +1174 -0
  58. package/lib/smart_contract/opcodes.md +1173 -0
  59. package/lib/smart_contract/preimage.js +903 -0
  60. package/lib/smart_contract/script_interpreter.js +236 -0
  61. package/lib/smart_contract/script_tester.js +487 -0
  62. package/lib/smart_contract/script_utils.js +621 -0
  63. package/lib/smart_contract/sighash.js +310 -0
  64. package/lib/smart_contract/smartledger-opcode_review.md +70 -0
  65. package/lib/smart_contract/stack_examiner.js +129 -0
  66. package/lib/smart_contract/test_integration.js +269 -0
  67. package/lib/smart_contract/utxo_generator.js +367 -0
  68. package/package.json +43 -10
  69. package/utilities/blockchain-state.json +20478 -3
@@ -0,0 +1,350 @@
1
+ /**
2
+ * SmartContract Module
3
+ * ===================
4
+ *
5
+ * Production-ready covenant functionality for Bitcoin SV (BSV).
6
+ * Implements BIP-143 transaction preimage parsing and covenant operations
7
+ * with comprehensive SIGHASH flag support and error handling.
8
+ *
9
+ * Classes:
10
+ * - Covenant: Core covenant creation and spending logic
11
+ * - Preimage: BIP-143 preimage parsing and validation with field extraction
12
+ * - SIGHASH: SIGHASH flag detection and analysis
13
+ * - Builder: High-level covenant construction utilities
14
+ * - UTXOGenerator: Real BSV UTXO generation for authentic testing
15
+ * - ScriptTester: Local script execution and debugging capabilities
16
+ * - CovenantBuilder: JavaScript-to-Bitcoin Script covenant generator
17
+ * - OpcodeMap: Comprehensive Bitcoin Script opcode mapping
18
+ * - ScriptUtils: Script analysis, conversion, and optimization utilities
19
+ * - StackExaminer: Stack state examination and step-by-step execution debugging
20
+ * - ScriptInterpreter: Interactive script debugging with full verification support
21
+ */
22
+
23
+ 'use strict'
24
+
25
+ var bsv = require('../..')
26
+
27
+ var SmartContract = {
28
+ Covenant: require('./covenant'),
29
+ Preimage: require('./preimage'),
30
+ SIGHASH: require('./sighash'),
31
+ Builder: require('./builder'),
32
+ UTXOGenerator: require('./utxo_generator'),
33
+ ScriptTester: require('./script_tester'),
34
+ CovenantBuilder: require('./covenant_builder').CovenantBuilder,
35
+ CovenantTemplates: require('./covenant_builder').CovenantTemplates,
36
+ OpcodeMap: require('./opcode_map'),
37
+ ScriptUtils: require('./script_utils'),
38
+ StackExaminer: require('./stack_examiner'),
39
+ ScriptInterpreter: require('./script_interpreter')
40
+ }
41
+
42
+ // Convenience constructor methods
43
+ SmartContract.createCovenant = function(privateKey, options) {
44
+ return new SmartContract.Covenant(privateKey, options)
45
+ }
46
+
47
+ SmartContract.extractPreimage = function(preimage, options) {
48
+ return new SmartContract.Preimage(preimage, options)
49
+ }
50
+
51
+ SmartContract.analyzeSIGHASH = function(sighashType) {
52
+ return new SmartContract.SIGHASH(sighashType)
53
+ }
54
+
55
+ SmartContract.buildCovenant = function(privateKey, options) {
56
+ return new SmartContract.Builder(privateKey, options)
57
+ }
58
+
59
+ SmartContract.createUTXOGenerator = function(options) {
60
+ return new SmartContract.UTXOGenerator(options)
61
+ }
62
+
63
+ SmartContract.createTestEnvironment = function(options) {
64
+ return SmartContract.UTXOGenerator.createTestEnvironment(options)
65
+ }
66
+
67
+ SmartContract.testScript = function(unlocking, locking, options) {
68
+ return SmartContract.ScriptTester.test(unlocking, locking, options)
69
+ }
70
+
71
+ SmartContract.testCovenant = function(preimageHex, constraints, options) {
72
+ return SmartContract.ScriptTester.testCovenant(preimageHex, constraints, options)
73
+ }
74
+
75
+ SmartContract.testFieldExtraction = function(preimageHex, fieldName, options) {
76
+ return SmartContract.ScriptTester.testFieldExtraction(preimageHex, fieldName, options)
77
+ }
78
+
79
+ SmartContract.debugScript = function(config, options) {
80
+ var tester = new SmartContract.ScriptTester(options)
81
+ return tester.debug(config)
82
+ }
83
+
84
+ // Educational and utility functions
85
+ SmartContract.explainZeroHashes = function() {
86
+ return SmartContract.SIGHASH.explainZeroMystery()
87
+ }
88
+
89
+ SmartContract.getAllSIGHASHTypes = function() {
90
+ return SmartContract.SIGHASH.getAllTypes()
91
+ }
92
+
93
+ SmartContract.demonstrateAllSIGHASH = function() {
94
+ return SmartContract.SIGHASH.generateAllDemonstrations()
95
+ }
96
+
97
+ // JavaScript-to-Bitcoin Script covenant generation
98
+ SmartContract.createCovenantBuilder = function() {
99
+ return new SmartContract.CovenantBuilder()
100
+ }
101
+
102
+ SmartContract.createValueLockCovenant = function(expectedValue) {
103
+ return SmartContract.CovenantTemplates.valueLock(expectedValue)
104
+ }
105
+
106
+ SmartContract.createHashLockCovenant = function(expectedHash) {
107
+ return SmartContract.CovenantTemplates.hashLock(expectedHash)
108
+ }
109
+
110
+ SmartContract.createComplexValidationCovenant = function(rules) {
111
+ return SmartContract.CovenantTemplates.complexValidation(rules)
112
+ }
113
+
114
+ // Opcode mapping utilities
115
+ SmartContract.getOpcodeMap = function() {
116
+ return SmartContract.OpcodeMap.opcodeMap
117
+ }
118
+
119
+ SmartContract.simulateScript = function(operations, initialStack) {
120
+ return SmartContract.OpcodeMap.utils.simulate(operations, initialStack)
121
+ }
122
+
123
+ SmartContract.createASMFromJS = function(operations) {
124
+ return SmartContract.OpcodeMap.utils.createASM(operations)
125
+ }
126
+
127
+ // Script analysis and conversion utilities
128
+ SmartContract.scriptToASM = function(scriptBuffer) {
129
+ return SmartContract.ScriptUtils.scriptToASM(scriptBuffer)
130
+ }
131
+
132
+ SmartContract.asmToScript = function(asmString) {
133
+ return SmartContract.ScriptUtils.asmToScript(asmString)
134
+ }
135
+
136
+ SmartContract.asmToHex = function(asmString) {
137
+ return SmartContract.ScriptUtils.asmToHex(asmString)
138
+ }
139
+
140
+ SmartContract.hexToASM = function(hexString) {
141
+ return SmartContract.ScriptUtils.hexToASM(hexString)
142
+ }
143
+
144
+ SmartContract.scriptToHex = function(script) {
145
+ return SmartContract.ScriptUtils.scriptToHex(script)
146
+ }
147
+
148
+ SmartContract.validateASM = function(asmString) {
149
+ return SmartContract.ScriptUtils.validateASM(asmString)
150
+ }
151
+
152
+ SmartContract.validateScript = function(script) {
153
+ return SmartContract.ScriptUtils.validateScript(script)
154
+ }
155
+
156
+ SmartContract.estimateScriptSize = function(script) {
157
+ return SmartContract.ScriptUtils.estimateScriptSize(script)
158
+ }
159
+
160
+ SmartContract.optimizeScript = function(script) {
161
+ return SmartContract.ScriptUtils.optimizeScript(script)
162
+ }
163
+
164
+ SmartContract.compareScripts = function(scriptA, scriptB) {
165
+ return SmartContract.ScriptUtils.compareScripts(scriptA, scriptB)
166
+ }
167
+
168
+ SmartContract.explainScript = function(script) {
169
+ return SmartContract.ScriptUtils.explainScript(script)
170
+ }
171
+
172
+ SmartContract.scriptMetrics = function(script) {
173
+ return SmartContract.ScriptUtils.scriptMetrics(script)
174
+ }
175
+
176
+ SmartContract.analyzeComplexity = function(script) {
177
+ return SmartContract.ScriptUtils.analyzeComplexity(script)
178
+ }
179
+
180
+ SmartContract.findOptimizations = function(script) {
181
+ return SmartContract.ScriptUtils.findOptimizations(script)
182
+ }
183
+
184
+ SmartContract.covenantToEnglish = function(covenant) {
185
+ return SmartContract.ScriptUtils.covenantToEnglish(covenant)
186
+ }
187
+
188
+ SmartContract.batchTestScripts = function(scripts, options) {
189
+ return SmartContract.ScriptUtils.batchTestScripts(scripts, options)
190
+ }
191
+
192
+ // Debug and analysis tools
193
+ SmartContract.examineStack = function(lockingHex, unlockingHex) {
194
+ return SmartContract.StackExaminer.runScript(lockingHex, unlockingHex)
195
+ }
196
+
197
+ SmartContract.debugScriptExecution = function(unlockingScript, lockingScript, options) {
198
+ options = options || {}
199
+ if (options.stepMode) {
200
+ return SmartContract.ScriptInterpreter.stepThroughScript(
201
+ SmartContract.ScriptInterpreter.parseScript(unlockingScript + ' ' + lockingScript),
202
+ new bsv.Transaction(),
203
+ true
204
+ )
205
+ } else {
206
+ return SmartContract.ScriptInterpreter.runFullEvaluation(
207
+ SmartContract.ScriptInterpreter.parseScript(unlockingScript),
208
+ SmartContract.ScriptInterpreter.parseScript(lockingScript),
209
+ new bsv.Transaction()
210
+ )
211
+ }
212
+ }
213
+
214
+ SmartContract.parseScript = function(scriptInput) {
215
+ return SmartContract.ScriptInterpreter.parseScript(scriptInput)
216
+ }
217
+
218
+ SmartContract.printStack = function(stack, altstack) {
219
+ return SmartContract.ScriptInterpreter.printStack(stack, altstack)
220
+ }
221
+
222
+ // Quick covenant creation utility
223
+ SmartContract.createQuickCovenant = function(type, params) {
224
+ const builder = SmartContract.createCovenantBuilder()
225
+
226
+ switch (type) {
227
+ case 'value_lock':
228
+ return builder
229
+ .comment(`Value lock: minimum ${params.value} satoshis`)
230
+ .extractField('value')
231
+ .push(params.value)
232
+ .greaterThanOrEqual()
233
+ .verify()
234
+ .build()
235
+
236
+ case 'hash_lock':
237
+ return builder
238
+ .comment(`Hash lock: requires preimage of ${params.hash}`)
239
+ .push('secret_placeholder')
240
+ .sha256()
241
+ .push(params.hash)
242
+ .equalVerify()
243
+ .build()
244
+
245
+ case 'time_lock':
246
+ return builder
247
+ .comment(`Time lock: requires nLockTime > ${params.timestamp}`)
248
+ .extractField('nLockTime')
249
+ .push(params.timestamp)
250
+ .greaterThan()
251
+ .verify()
252
+ .build()
253
+
254
+ case 'multi_condition':
255
+ let covenant = builder.comment('Multi-condition covenant')
256
+ params.conditions.forEach(condition => {
257
+ switch (condition.type) {
258
+ case 'value':
259
+ covenant = covenant
260
+ .extractField('value')
261
+ .push(condition.value)
262
+ .greaterThanOrEqual()
263
+ .verify()
264
+ break
265
+ case 'hash':
266
+ covenant = covenant
267
+ .push('secret_placeholder')
268
+ .sha256()
269
+ .push(condition.hash)
270
+ .equalVerify()
271
+ break
272
+ case 'time':
273
+ covenant = covenant
274
+ .extractField('nLockTime')
275
+ .push(condition.timestamp)
276
+ .greaterThan()
277
+ .verify()
278
+ break
279
+ }
280
+ })
281
+ return covenant.build()
282
+
283
+ default:
284
+ throw new Error(`Unknown covenant type: ${type}`)
285
+ }
286
+ }
287
+
288
+ // Complete workflow helpers
289
+ SmartContract.completeCovenantFlow = function(privateKey, p2pkhUtxo, broadcastCallback) {
290
+ var covenant = new SmartContract.Covenant(privateKey)
291
+ return covenant.completeFlow(p2pkhUtxo, broadcastCallback)
292
+ }
293
+
294
+ // Educational utilities
295
+ SmartContract.getEducationalResources = function() {
296
+ return {
297
+ zeroHashMystery: SmartContract.SIGHASH.explainZeroMystery(),
298
+ sighashTypes: SmartContract.SIGHASH.getAllTypes(),
299
+ exampleDemonstrations: SmartContract.SIGHASH.generateAllDemonstrations()
300
+ }
301
+ }
302
+
303
+ // Utility functions
304
+ SmartContract.utils = {}
305
+ SmartContract.utils.reconstructP2pkhScript = function(address) {
306
+ return bsv.Script.buildPublicKeyHashOut(address).toHex()
307
+ }
308
+
309
+ SmartContract.utils.createCovenantAddress = function(script) {
310
+ // Create P2SH address for covenant script
311
+ var scriptHash = bsv.crypto.Hash.sha256ripemd160(script.toBuffer())
312
+ return bsv.Address.fromScriptHash(scriptHash)
313
+ }
314
+
315
+ SmartContract.utils.decodeCompactSize = function(buffer, offset) {
316
+ return SmartContract.Preimage.decodeCompactSize(buffer, offset)
317
+ }
318
+
319
+ // Version information
320
+ SmartContract.version = 'v1.0.0'
321
+ SmartContract.description = 'Enterprise Smart Contract Framework for Bitcoin SV'
322
+
323
+ // Feature flags
324
+ SmartContract.features = {
325
+ BIP143_PREIMAGE: true,
326
+ COMPACT_SIZE_VARINT: true,
327
+ BIDIRECTIONAL_EXTRACTION: true,
328
+ SIGHASH_DETECTION: true,
329
+ ZERO_HASH_WARNINGS: true,
330
+ MULTI_FIELD_VALIDATION: true,
331
+ REAL_UTXO_GENERATION: true,
332
+ SCRIPT_TESTING: true,
333
+ LOCAL_VERIFICATION: true,
334
+ JAVASCRIPT_TO_SCRIPT: true,
335
+ OPCODE_MAPPING: true,
336
+ COVENANT_BUILDER: true,
337
+ SCRIPT_ANALYSIS: true,
338
+ SCRIPT_OPTIMIZATION: true,
339
+ SCRIPT_CONVERSION: true,
340
+ BATCH_TESTING: true,
341
+ QUICK_COVENANTS: true,
342
+ SCRIPT_EXPLANATIONS: true,
343
+ STACK_EXAMINATION: true,
344
+ SCRIPT_DEBUGGING: true,
345
+ STEP_BY_STEP_EXECUTION: true,
346
+ INTERACTIVE_DEBUGGING: true,
347
+ PRODUCTION_READY: true
348
+ }
349
+
350
+ module.exports = SmartContract
@@ -0,0 +1,30 @@
1
+ const bsv = require('../..')
2
+ const opcode = bsv.Opcode;
3
+ console.log('Opcode List:');
4
+ for (let key in opcode.map) {
5
+ const code = opcode.map[key];
6
+ console.log(`${key}: 0x${code.toString(16).padStart(2, '0')}`);
7
+ }
8
+
9
+ const Script = bsv.Script;
10
+ console.log(Script);
11
+ console.log('\nSample Script using OP_CHECKSIG and OP_RETURN:');
12
+ const script = new Script()
13
+ .add(opcode.fromString('OP_DUP'))
14
+ .add(opcode.fromString('OP_CHECKSIG'))
15
+ .add(opcode.fromString('OP_RETURN'));
16
+ console.log(script.toASM());
17
+
18
+ //scriptToASM() - Convert script buffer to readable ASM
19
+ // asmToScript() - Convert ASM string to script buffer
20
+ // validateASM() - Check if ASM is valid Bitcoin Script
21
+ // estimateScriptSize() - Predict script size in bytes
22
+ // optimizeScript() - Remove redundant operations
23
+ // compareScripts() - Check if two scripts are equivalent
24
+ // explainScript() - Human-readable script explanation
25
+ // scriptMetrics() - Analyze script complexity and costs
26
+
27
+ // 🔗 Conversion Utilities:
28
+ // hexToASM() - Convert hex script to ASM
29
+ // asmToHex() - Convert ASM to hex script
30
+ // scriptToHex() - Convert script object to hex