smartledger-bsv 3.1.0 → 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,367 @@
1
+ /**
2
+ * SmartContract.UTXOGenerator Class
3
+ * =================================
4
+ *
5
+ * Enhanced UTXO generation with real BSV private/public keys
6
+ * for authentic smart contract testing and development.
7
+ *
8
+ * Features:
9
+ * - Generate real BSV keypairs for testing
10
+ * - Create authentic transaction inputs/outputs
11
+ * - Support multiple script types (P2PKH, P2SH, custom)
12
+ * - Integrate with existing SmartUTXO system
13
+ * - Enable local testing with real cryptography
14
+ */
15
+
16
+ 'use strict'
17
+
18
+ var bsv = require('../..')
19
+ var crypto = require('crypto')
20
+
21
+ /**
22
+ * UTXOGenerator Class - Real BSV UTXO generation for testing
23
+ * @param {Object} options - Configuration options
24
+ */
25
+ function UTXOGenerator(options) {
26
+ if (!(this instanceof UTXOGenerator)) {
27
+ return new UTXOGenerator(options)
28
+ }
29
+
30
+ this.options = options || {}
31
+ this.network = this.options.network || bsv.Networks.mainnet
32
+ this.keyRing = {} // Store generated keys
33
+ this.utxoPool = [] // Store generated UTXOs
34
+
35
+ // Initialize with SmartUTXO integration
36
+ if (typeof bsv.SmartUTXO !== 'undefined') {
37
+ this.smartUTXO = new bsv.SmartUTXO(this.options)
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Generate a new BSV keypair for testing
43
+ * @param {string} label - Optional label for the keypair
44
+ * @returns {Object} Keypair with privateKey, publicKey, address
45
+ */
46
+ UTXOGenerator.prototype.generateKeypair = function(label) {
47
+ label = label || 'key_' + Date.now()
48
+
49
+ var privateKey = bsv.PrivateKey.fromRandom(this.network)
50
+ var publicKey = privateKey.toPublicKey()
51
+ var address = privateKey.toAddress(this.network)
52
+
53
+ var keypair = {
54
+ label: label,
55
+ privateKey: privateKey,
56
+ publicKey: publicKey,
57
+ address: address,
58
+ wif: privateKey.toWIF(),
59
+ addressString: address.toString()
60
+ }
61
+
62
+ // Store for later use
63
+ this.keyRing[label] = keypair
64
+
65
+ return keypair
66
+ }
67
+
68
+ /**
69
+ * Create real UTXOs with authentic BSV transactions
70
+ * @param {Object} config - UTXO configuration
71
+ * @returns {Array} Array of real UTXOs
72
+ */
73
+ UTXOGenerator.prototype.createRealUTXOs = function(config) {
74
+ config = config || {}
75
+
76
+ var utxoCount = config.count || 3
77
+ var satoshisPerUTXO = config.satoshis || 100000
78
+ var scriptType = config.scriptType || 'P2PKH'
79
+ var keypair = config.keypair || this.generateKeypair('utxo_owner')
80
+
81
+ var utxos = []
82
+
83
+ for (var i = 0; i < utxoCount; i++) {
84
+ var utxo = this._createSingleUTXO({
85
+ keypair: keypair,
86
+ satoshis: satoshisPerUTXO,
87
+ scriptType: scriptType,
88
+ vout: i
89
+ })
90
+
91
+ utxos.push(utxo)
92
+ this.utxoPool.push(utxo)
93
+
94
+ // Add to SmartUTXO system if available
95
+ if (this.smartUTXO) {
96
+ this.smartUTXO.addUTXO(utxo)
97
+ }
98
+ }
99
+
100
+ return utxos
101
+ }
102
+
103
+ /**
104
+ * Create a single authentic UTXO
105
+ * @private
106
+ */
107
+ UTXOGenerator.prototype._createSingleUTXO = function(config) {
108
+ // Generate realistic transaction ID
109
+ var txid = crypto.randomBytes(32).toString('hex')
110
+
111
+ // Create appropriate script based on type
112
+ var script
113
+ var scriptHex
114
+
115
+ switch (config.scriptType) {
116
+ case 'P2PKH':
117
+ script = bsv.Script.buildPublicKeyHashOut(config.keypair.address)
118
+ scriptHex = script.toHex()
119
+ break
120
+
121
+ case 'P2SH':
122
+ // Create a simple multisig for P2SH example
123
+ var redeemScript = bsv.Script.buildMultisigOut([config.keypair.publicKey], 1)
124
+ script = bsv.Script.buildScriptHashOut(redeemScript)
125
+ scriptHex = script.toHex()
126
+ break
127
+
128
+ case 'CUSTOM':
129
+ // Allow custom script injection
130
+ script = config.customScript || bsv.Script.buildPublicKeyHashOut(config.keypair.address)
131
+ scriptHex = script.toHex()
132
+ break
133
+
134
+ default:
135
+ script = bsv.Script.buildPublicKeyHashOut(config.keypair.address)
136
+ scriptHex = script.toHex()
137
+ }
138
+
139
+ return {
140
+ txid: txid,
141
+ vout: config.vout,
142
+ address: config.keypair.addressString,
143
+ script: scriptHex,
144
+ satoshis: config.satoshis,
145
+ keypair: config.keypair,
146
+ scriptType: config.scriptType,
147
+ scriptObj: script,
148
+ created: new Date().toISOString()
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Create a realistic transaction using generated UTXOs
154
+ * @param {Object} config - Transaction configuration
155
+ * @returns {Object} Transaction and signing details
156
+ */
157
+ UTXOGenerator.prototype.createTestTransaction = function(config) {
158
+ config = config || {}
159
+
160
+ // Get UTXOs to spend
161
+ var inputUTXOs = config.inputs || this.utxoPool.slice(0, 1)
162
+ if (inputUTXOs.length === 0) {
163
+ throw new Error('No UTXOs available for transaction. Call createRealUTXOs() first.')
164
+ }
165
+
166
+ // Calculate input total
167
+ var inputTotal = inputUTXOs.reduce(function(sum, utxo) {
168
+ return sum + utxo.satoshis
169
+ }, 0)
170
+
171
+ // Create transaction
172
+ var transaction = new bsv.Transaction()
173
+
174
+ // Add inputs
175
+ inputUTXOs.forEach(function(utxo) {
176
+ transaction.from({
177
+ txId: utxo.txid,
178
+ outputIndex: utxo.vout,
179
+ address: utxo.address,
180
+ script: utxo.script,
181
+ satoshis: utxo.satoshis
182
+ })
183
+ })
184
+
185
+ // Add outputs
186
+ var outputAddress = config.outputAddress || inputUTXOs[0].keypair.addressString
187
+ var outputAmount = config.outputAmount || (inputTotal - 10000) // Leave 10k sats for fee
188
+ var fee = config.fee || 10000
189
+
190
+ transaction.to(outputAddress, outputAmount)
191
+ transaction.fee(fee)
192
+
193
+ return {
194
+ transaction: transaction,
195
+ inputUTXOs: inputUTXOs,
196
+ unsignedHex: transaction.toString(),
197
+ inputTotal: inputTotal,
198
+ outputAmount: outputAmount,
199
+ fee: fee,
200
+
201
+ // Signing helper
202
+ sign: function() {
203
+ inputUTXOs.forEach(function(utxo) {
204
+ transaction.sign(utxo.keypair.privateKey)
205
+ })
206
+ return {
207
+ signedTransaction: transaction,
208
+ signedHex: transaction.toString(),
209
+ txid: transaction.id
210
+ }
211
+ },
212
+
213
+ // Preimage generation helper
214
+ generatePreimage: function(inputIndex, sighashType) {
215
+ sighashType = sighashType || bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID
216
+ var utxo = inputUTXOs[inputIndex]
217
+
218
+ return bsv.Transaction.sighash.sighashPreimage(
219
+ transaction,
220
+ sighashType,
221
+ inputIndex,
222
+ bsv.Script.fromHex(utxo.script),
223
+ new bsv.crypto.BN(utxo.satoshis)
224
+ )
225
+ }
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Create covenant-ready UTXOs with preimage generation
231
+ * @param {Object} config - Covenant configuration
232
+ * @returns {Object} Covenant test setup
233
+ */
234
+ UTXOGenerator.prototype.createCovenantTest = function(config) {
235
+ config = config || {}
236
+
237
+ // Generate keypair for covenant
238
+ var covenantKeypair = this.generateKeypair('covenant_test')
239
+
240
+ // Create UTXOs
241
+ var utxos = this.createRealUTXOs({
242
+ count: config.utxoCount || 2,
243
+ satoshis: config.satoshis || 50000,
244
+ keypair: covenantKeypair,
245
+ scriptType: config.scriptType || 'P2PKH'
246
+ })
247
+
248
+ // Create test transaction
249
+ var txConfig = {
250
+ inputs: utxos.slice(0, 1), // Use first UTXO
251
+ outputAmount: config.covenantAmount || 40000,
252
+ fee: 10000
253
+ }
254
+
255
+ var testTx = this.createTestTransaction(txConfig)
256
+
257
+ // Generate preimage for covenant analysis
258
+ var preimageBuffer = testTx.generatePreimage(0)
259
+ var preimageHex = preimageBuffer.toString('hex')
260
+
261
+ return {
262
+ keypair: covenantKeypair,
263
+ utxos: utxos,
264
+ transaction: testTx,
265
+ preimage: {
266
+ buffer: preimageBuffer,
267
+ hex: preimageHex,
268
+ length: preimageBuffer.length
269
+ },
270
+
271
+ // Covenant testing helpers
272
+ extractField: function(fieldName) {
273
+ try {
274
+ var Preimage = require('./preimage')
275
+ return Preimage.extractFromHex(preimageHex, fieldName)
276
+ } catch (error) {
277
+ throw new Error('SmartContract.Preimage not available: ' + error.message)
278
+ }
279
+ },
280
+
281
+ validateCovenant: function(covenantLogic) {
282
+ // Placeholder for covenant validation
283
+ return {
284
+ preimageValid: preimageBuffer.length >= 182, // Minimum BIP-143 size
285
+ covenantLogic: covenantLogic,
286
+ testPassed: true
287
+ }
288
+ },
289
+
290
+ getSummary: function() {
291
+ return {
292
+ keypair: covenantKeypair.addressString,
293
+ utxoCount: utxos.length,
294
+ totalValue: utxos.reduce(function(sum, utxo) { return sum + utxo.satoshis }, 0),
295
+ preimageLength: preimageBuffer.length,
296
+ transactionId: testTx.transaction.id
297
+ }
298
+ }
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Get all generated keypairs
304
+ * @returns {Object} Key ring with all keypairs
305
+ */
306
+ UTXOGenerator.prototype.getKeypairs = function() {
307
+ return this.keyRing
308
+ }
309
+
310
+ /**
311
+ * Get all generated UTXOs
312
+ * @returns {Array} UTXO pool
313
+ */
314
+ UTXOGenerator.prototype.getUTXOs = function() {
315
+ return this.utxoPool
316
+ }
317
+
318
+ /**
319
+ * Clear all generated data
320
+ */
321
+ UTXOGenerator.prototype.reset = function() {
322
+ this.keyRing = {}
323
+ this.utxoPool = []
324
+ }
325
+
326
+ /**
327
+ * Static utility methods
328
+ */
329
+
330
+ /**
331
+ * Generate a quick test setup with real BSV keys
332
+ * @param {Object} options - Setup options
333
+ * @returns {Object} Complete test environment
334
+ */
335
+ UTXOGenerator.createTestEnvironment = function(options) {
336
+ var generator = new UTXOGenerator(options)
337
+ var covenantTest = generator.createCovenantTest(options)
338
+
339
+ return {
340
+ generator: generator,
341
+ test: covenantTest,
342
+
343
+ // Quick access methods
344
+ getPreimage: function() {
345
+ return covenantTest.preimage.hex
346
+ },
347
+
348
+ getKeypair: function() {
349
+ return covenantTest.keypair
350
+ },
351
+
352
+ extractField: function(fieldName) {
353
+ return covenantTest.extractField(fieldName)
354
+ },
355
+
356
+ generateASM: function(fieldName) {
357
+ try {
358
+ var Preimage = require('./preimage')
359
+ return Preimage.generateASMFromHex(covenantTest.preimage.hex, fieldName)
360
+ } catch (error) {
361
+ throw new Error('Cannot generate ASM: ' + error.message)
362
+ }
363
+ }
364
+ }
365
+ }
366
+
367
+ module.exports = UTXOGenerator
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smartledger-bsv",
3
- "version": "3.1.0",
4
- "description": "Security-hardened Bitcoin SV library with custom script framework - Complete drop-in replacement for bsv@1.5.6 with zero vulnerabilities",
3
+ "version": "3.2.0",
4
+ "description": "Advanced Bitcoin SV library with JavaScript-to-Bitcoin Script framework - Complete BSV API + BIP143 preimage parsing + 121 opcode mapping + covenant builder + nChain PUSHTX techniques + custom script development",
5
5
  "author": "SmartLedger Technology <hello@smartledger.technology> (https://smartledger.technology)",
6
6
  "homepage": "https://github.com/codenlighten/smartledger-bsv#readme",
7
7
  "bugs": {
@@ -12,14 +12,25 @@
12
12
  "lint": "standard",
13
13
  "test": "standard && mocha",
14
14
  "test:signatures": "node validation_test.js",
15
+ "test:js2script": "node lib/smart_contract/opcode_map.js && node lib/smart_contract/covenant_builder.js",
16
+ "test:opcodes": "node lib/smart_contract/opcode_list.js",
17
+ "test:covenants": "node examples/covenants/advanced_covenant_demo.js",
18
+ "test:scripts": "node examples/scripts/custom_script_signature_test.js",
19
+ "test:basic": "node examples/basic/transaction-creation.js",
20
+ "test:all": "npm test && npm run test:covenants && npm run test:scripts",
15
21
  "coverage": "nyc --reporter=text npm run test",
16
- "build-bsv": "webpack index.js --config webpack.config.js",
17
- "build-ecies": "webpack ecies/index.js --config webpack.subproject.config.js --output-library bsvEcies -o bsv-ecies.min.js",
18
- "build-message": "webpack message/index.js --config webpack.subproject.config.js --output-library bsvMessage -o bsv-message.min.js",
19
- "build-mnemonic": "webpack mnemonic/index.js --config webpack.subproject.config.js --output-library bsvMnemonic -o bsv-mnemonic.min.js",
20
- "build-bundle": "webpack bundle-entry.js --config webpack.bundle.config.js",
22
+ "build-bsv": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack index.js --config webpack.config.js",
23
+ "build-ecies": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack ecies/index.js --config webpack.subproject.config.js --output-library bsvEcies -o bsv-ecies.min.js",
24
+ "build-message": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack message/index.js --config webpack.subproject.config.js --output-library bsvMessage -o bsv-message.min.js",
25
+ "build-mnemonic": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack mnemonic/index.js --config webpack.subproject.config.js --output-library bsvMnemonic -o bsv-mnemonic.min.js",
26
+ "build-bundle": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack bundle-entry.js --config webpack.bundle.config.js",
21
27
  "build": "npm run build-bsv && npm run build-ecies && npm run build-message && npm run build-mnemonic",
22
28
  "build-all": "npm run build && npm run build-bundle",
29
+ "demo": "npm run test:covenants",
30
+ "demo:basic": "npm run test:basic",
31
+ "demo:scripts": "npm run test:scripts",
32
+ "demo:preimage": "echo '🧠 BIP-143 Preimage Extractor Demo' && node examples/preimage/extract_preimage_bidirectional.js",
33
+ "preimage:extract": "node examples/preimage/extract_preimage_bidirectional.js",
23
34
  "prepublishOnly": "NODE_OPTIONS=\"--openssl-legacy-provider\" npm run build"
24
35
  },
25
36
  "unpkg": "bsv.min.js",
@@ -33,11 +44,14 @@
33
44
  "message/",
34
45
  "mnemonic/",
35
46
  "bsv.min.js",
47
+ "bsv.bundle.js",
36
48
  "bsv-ecies.min.js",
37
- "bsv-message.min.js",
49
+ "bsv-message.min.js",
38
50
  "bsv-mnemonic.min.js",
39
51
  "bsv.d.ts",
40
52
  "validation_test.js",
53
+ "docs/",
54
+ "examples/",
41
55
  "LICENSE",
42
56
  "README.md",
43
57
  "SECURITY.md",
@@ -55,7 +69,27 @@
55
69
  "hardened",
56
70
  "vulnerability-free",
57
71
  "drop-in-replacement",
72
+ "covenant",
73
+ "covenants",
74
+ "pushtx",
75
+ "pels",
76
+ "bip143",
77
+ "preimage",
78
+ "nchain",
79
+ "custom-scripts",
80
+ "multisig",
81
+ "timelock",
82
+ "conditional-scripts",
58
83
  "utxo-management",
84
+ "ultra-low-fees",
85
+ "javascript-to-script",
86
+ "opcode-mapping",
87
+ "covenant-builder",
88
+ "script-simulation",
89
+ "bitcoin-script",
90
+ "asm-generation",
91
+ "advanced-features",
92
+ "script-development",
59
93
  "blockchain-simulator",
60
94
  "miner-simulator",
61
95
  "testing-tools",
@@ -67,8 +101,7 @@
67
101
  "bip32",
68
102
  "bip37",
69
103
  "bip69",
70
- "bip70",
71
- "multisig"
104
+ "bip70"
72
105
  ],
73
106
  "repository": {
74
107
  "type": "git",