@smartledger/bsv 3.1.1 → 3.2.0

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 (67) 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 +754 -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 +311 -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_tester.js +487 -0
  61. package/lib/smart_contract/script_utils.js +609 -0
  62. package/lib/smart_contract/sighash.js +310 -0
  63. package/lib/smart_contract/smartledger-opcode_review.md +70 -0
  64. package/lib/smart_contract/test_integration.js +269 -0
  65. package/lib/smart_contract/utxo_generator.js +367 -0
  66. package/package.json +43 -10
  67. package/utilities/blockchain-state.json +20478 -3
@@ -0,0 +1,311 @@
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
+ */
19
+
20
+ 'use strict'
21
+
22
+ var bsv = require('../..')
23
+
24
+ var SmartContract = {
25
+ Covenant: require('./covenant'),
26
+ Preimage: require('./preimage'),
27
+ SIGHASH: require('./sighash'),
28
+ Builder: require('./builder'),
29
+ UTXOGenerator: require('./utxo_generator'),
30
+ ScriptTester: require('./script_tester'),
31
+ CovenantBuilder: require('./covenant_builder').CovenantBuilder,
32
+ CovenantTemplates: require('./covenant_builder').CovenantTemplates,
33
+ OpcodeMap: require('./opcode_map'),
34
+ ScriptUtils: require('./script_utils')
35
+ }
36
+
37
+ // Convenience constructor methods
38
+ SmartContract.createCovenant = function(privateKey, options) {
39
+ return new SmartContract.Covenant(privateKey, options)
40
+ }
41
+
42
+ SmartContract.extractPreimage = function(preimage, options) {
43
+ return new SmartContract.Preimage(preimage, options)
44
+ }
45
+
46
+ SmartContract.analyzeSIGHASH = function(sighashType) {
47
+ return new SmartContract.SIGHASH(sighashType)
48
+ }
49
+
50
+ SmartContract.buildCovenant = function(privateKey, options) {
51
+ return new SmartContract.Builder(privateKey, options)
52
+ }
53
+
54
+ SmartContract.createUTXOGenerator = function(options) {
55
+ return new SmartContract.UTXOGenerator(options)
56
+ }
57
+
58
+ SmartContract.createTestEnvironment = function(options) {
59
+ return SmartContract.UTXOGenerator.createTestEnvironment(options)
60
+ }
61
+
62
+ SmartContract.testScript = function(unlocking, locking, options) {
63
+ return SmartContract.ScriptTester.test(unlocking, locking, options)
64
+ }
65
+
66
+ SmartContract.testCovenant = function(preimageHex, constraints, options) {
67
+ return SmartContract.ScriptTester.testCovenant(preimageHex, constraints, options)
68
+ }
69
+
70
+ SmartContract.testFieldExtraction = function(preimageHex, fieldName, options) {
71
+ return SmartContract.ScriptTester.testFieldExtraction(preimageHex, fieldName, options)
72
+ }
73
+
74
+ SmartContract.debugScript = function(config, options) {
75
+ var tester = new SmartContract.ScriptTester(options)
76
+ return tester.debug(config)
77
+ }
78
+
79
+ // Educational and utility functions
80
+ SmartContract.explainZeroHashes = function() {
81
+ return SmartContract.SIGHASH.explainZeroMystery()
82
+ }
83
+
84
+ SmartContract.getAllSIGHASHTypes = function() {
85
+ return SmartContract.SIGHASH.getAllTypes()
86
+ }
87
+
88
+ SmartContract.demonstrateAllSIGHASH = function() {
89
+ return SmartContract.SIGHASH.generateAllDemonstrations()
90
+ }
91
+
92
+ // JavaScript-to-Bitcoin Script covenant generation
93
+ SmartContract.createCovenantBuilder = function() {
94
+ return new SmartContract.CovenantBuilder()
95
+ }
96
+
97
+ SmartContract.createValueLockCovenant = function(expectedValue) {
98
+ return SmartContract.CovenantTemplates.valueLock(expectedValue)
99
+ }
100
+
101
+ SmartContract.createHashLockCovenant = function(expectedHash) {
102
+ return SmartContract.CovenantTemplates.hashLock(expectedHash)
103
+ }
104
+
105
+ SmartContract.createComplexValidationCovenant = function(rules) {
106
+ return SmartContract.CovenantTemplates.complexValidation(rules)
107
+ }
108
+
109
+ // Opcode mapping utilities
110
+ SmartContract.getOpcodeMap = function() {
111
+ return SmartContract.OpcodeMap.opcodeMap
112
+ }
113
+
114
+ SmartContract.simulateScript = function(operations, initialStack) {
115
+ return SmartContract.OpcodeMap.utils.simulate(operations, initialStack)
116
+ }
117
+
118
+ SmartContract.createASMFromJS = function(operations) {
119
+ return SmartContract.OpcodeMap.utils.createASM(operations)
120
+ }
121
+
122
+ // Script analysis and conversion utilities
123
+ SmartContract.scriptToASM = function(scriptBuffer) {
124
+ return SmartContract.ScriptUtils.scriptToASM(scriptBuffer)
125
+ }
126
+
127
+ SmartContract.asmToScript = function(asmString) {
128
+ return SmartContract.ScriptUtils.asmToScript(asmString)
129
+ }
130
+
131
+ SmartContract.asmToHex = function(asmString) {
132
+ return SmartContract.ScriptUtils.asmToHex(asmString)
133
+ }
134
+
135
+ SmartContract.hexToASM = function(hexString) {
136
+ return SmartContract.ScriptUtils.hexToASM(hexString)
137
+ }
138
+
139
+ SmartContract.scriptToHex = function(script) {
140
+ return SmartContract.ScriptUtils.scriptToHex(script)
141
+ }
142
+
143
+ SmartContract.validateASM = function(asmString) {
144
+ return SmartContract.ScriptUtils.validateASM(asmString)
145
+ }
146
+
147
+ SmartContract.validateScript = function(script) {
148
+ return SmartContract.ScriptUtils.validateScript(script)
149
+ }
150
+
151
+ SmartContract.estimateScriptSize = function(script) {
152
+ return SmartContract.ScriptUtils.estimateScriptSize(script)
153
+ }
154
+
155
+ SmartContract.optimizeScript = function(script) {
156
+ return SmartContract.ScriptUtils.optimizeScript(script)
157
+ }
158
+
159
+ SmartContract.compareScripts = function(scriptA, scriptB) {
160
+ return SmartContract.ScriptUtils.compareScripts(scriptA, scriptB)
161
+ }
162
+
163
+ SmartContract.explainScript = function(script) {
164
+ return SmartContract.ScriptUtils.explainScript(script)
165
+ }
166
+
167
+ SmartContract.scriptMetrics = function(script) {
168
+ return SmartContract.ScriptUtils.scriptMetrics(script)
169
+ }
170
+
171
+ SmartContract.analyzeComplexity = function(script) {
172
+ return SmartContract.ScriptUtils.analyzeComplexity(script)
173
+ }
174
+
175
+ SmartContract.findOptimizations = function(script) {
176
+ return SmartContract.ScriptUtils.findOptimizations(script)
177
+ }
178
+
179
+ SmartContract.covenantToEnglish = function(covenant) {
180
+ return SmartContract.ScriptUtils.covenantToEnglish(covenant)
181
+ }
182
+
183
+ SmartContract.batchTestScripts = function(scripts, options) {
184
+ return SmartContract.ScriptUtils.batchTestScripts(scripts, options)
185
+ }
186
+
187
+ // Quick covenant creation utility
188
+ SmartContract.createQuickCovenant = function(type, params) {
189
+ const builder = SmartContract.createCovenantBuilder()
190
+
191
+ switch (type) {
192
+ case 'value_lock':
193
+ return builder
194
+ .comment(`Value lock: minimum ${params.value} satoshis`)
195
+ .extractField('value')
196
+ .push(params.value)
197
+ .greaterThanOrEqual()
198
+ .verify()
199
+ .build()
200
+
201
+ case 'hash_lock':
202
+ return builder
203
+ .comment(`Hash lock: requires preimage of ${params.hash}`)
204
+ .push('secret_placeholder')
205
+ .sha256()
206
+ .push(params.hash)
207
+ .equalVerify()
208
+ .build()
209
+
210
+ case 'time_lock':
211
+ return builder
212
+ .comment(`Time lock: requires nLockTime > ${params.timestamp}`)
213
+ .extractField('nLockTime')
214
+ .push(params.timestamp)
215
+ .greaterThan()
216
+ .verify()
217
+ .build()
218
+
219
+ case 'multi_condition':
220
+ let covenant = builder.comment('Multi-condition covenant')
221
+ params.conditions.forEach(condition => {
222
+ switch (condition.type) {
223
+ case 'value':
224
+ covenant = covenant
225
+ .extractField('value')
226
+ .push(condition.value)
227
+ .greaterThanOrEqual()
228
+ .verify()
229
+ break
230
+ case 'hash':
231
+ covenant = covenant
232
+ .push('secret_placeholder')
233
+ .sha256()
234
+ .push(condition.hash)
235
+ .equalVerify()
236
+ break
237
+ case 'time':
238
+ covenant = covenant
239
+ .extractField('nLockTime')
240
+ .push(condition.timestamp)
241
+ .greaterThan()
242
+ .verify()
243
+ break
244
+ }
245
+ })
246
+ return covenant.build()
247
+
248
+ default:
249
+ throw new Error(`Unknown covenant type: ${type}`)
250
+ }
251
+ }
252
+
253
+ // Complete workflow helpers
254
+ SmartContract.completeCovenantFlow = function(privateKey, p2pkhUtxo, broadcastCallback) {
255
+ var covenant = new SmartContract.Covenant(privateKey)
256
+ return covenant.completeFlow(p2pkhUtxo, broadcastCallback)
257
+ }
258
+
259
+ // Educational utilities
260
+ SmartContract.getEducationalResources = function() {
261
+ return {
262
+ zeroHashMystery: SmartContract.SIGHASH.explainZeroMystery(),
263
+ sighashTypes: SmartContract.SIGHASH.getAllTypes(),
264
+ exampleDemonstrations: SmartContract.SIGHASH.generateAllDemonstrations()
265
+ }
266
+ }
267
+
268
+ // Utility functions
269
+ SmartContract.utils = {}
270
+ SmartContract.utils.reconstructP2pkhScript = function(address) {
271
+ return bsv.Script.buildPublicKeyHashOut(address).toHex()
272
+ }
273
+
274
+ SmartContract.utils.createCovenantAddress = function(script) {
275
+ // Create P2SH address for covenant script
276
+ var scriptHash = bsv.crypto.Hash.sha256ripemd160(script.toBuffer())
277
+ return bsv.Address.fromScriptHash(scriptHash)
278
+ }
279
+
280
+ SmartContract.utils.decodeCompactSize = function(buffer, offset) {
281
+ return SmartContract.Preimage.decodeCompactSize(buffer, offset)
282
+ }
283
+
284
+ // Version information
285
+ SmartContract.version = 'v1.0.0'
286
+ SmartContract.description = 'Enterprise Smart Contract Framework for Bitcoin SV'
287
+
288
+ // Feature flags
289
+ SmartContract.features = {
290
+ BIP143_PREIMAGE: true,
291
+ COMPACT_SIZE_VARINT: true,
292
+ BIDIRECTIONAL_EXTRACTION: true,
293
+ SIGHASH_DETECTION: true,
294
+ ZERO_HASH_WARNINGS: true,
295
+ MULTI_FIELD_VALIDATION: true,
296
+ REAL_UTXO_GENERATION: true,
297
+ SCRIPT_TESTING: true,
298
+ LOCAL_VERIFICATION: true,
299
+ JAVASCRIPT_TO_SCRIPT: true,
300
+ OPCODE_MAPPING: true,
301
+ COVENANT_BUILDER: true,
302
+ SCRIPT_ANALYSIS: true,
303
+ SCRIPT_OPTIMIZATION: true,
304
+ SCRIPT_CONVERSION: true,
305
+ BATCH_TESTING: true,
306
+ QUICK_COVENANTS: true,
307
+ SCRIPT_EXPLANATIONS: true,
308
+ PRODUCTION_READY: true
309
+ }
310
+
311
+ 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