@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.
- package/CHANGELOG.md +123 -1
- package/README.md +233 -277
- package/bsv.bundle.js +39 -0
- package/bsv.min.js +8 -8
- package/docs/ADVANCED_COVENANT_DEVELOPMENT.md +533 -0
- package/docs/COVENANT_DEVELOPMENT_RESOLVED.md +169 -0
- package/docs/CUSTOM_SCRIPT_DEVELOPMENT.md +320 -0
- package/docs/README.md +201 -0
- package/docs/block.md +46 -0
- package/docs/ecies.md +102 -0
- package/docs/index.md +104 -0
- package/docs/nchain.md +958 -0
- package/docs/networks.md +55 -0
- package/docs/preimage.md +126 -0
- package/docs/script.md +139 -0
- package/docs/transaction.md +174 -0
- package/docs/unspentoutput.md +32 -0
- package/examples/README.md +200 -0
- package/examples/basic/transaction-creation.js +534 -0
- package/examples/basic/transaction_signature_api_gap.js +178 -0
- package/examples/covenants/advanced_covenant_demo.js +219 -0
- package/examples/covenants/covenant_interface_demo.js +270 -0
- package/examples/covenants/covenant_manual_signature_resolved.js +212 -0
- package/examples/covenants/covenant_signature_template.js +117 -0
- package/examples/covenants2/covenant_bidirectional_example.js +262 -0
- package/examples/covenants2/covenant_utils_demo.js +120 -0
- package/examples/covenants2/preimage_covenant_utils.js +287 -0
- package/examples/covenants2/production_integration.js +256 -0
- package/examples/data/covenant_utxos.json +28 -0
- package/examples/data/utxos.json +26 -0
- package/examples/preimage/README.md +178 -0
- package/examples/preimage/extract_preimage_bidirectional.js +421 -0
- package/examples/preimage/generate_sample_preimage.js +208 -0
- package/examples/preimage/generate_sighash_examples.js +152 -0
- package/examples/preimage/parse_preimage.js +117 -0
- package/examples/preimage/test_preimage_extractor.js +53 -0
- package/examples/preimage/test_varint_extraction.js +95 -0
- package/examples/scripts/custom_script_helper_example.js +273 -0
- package/examples/scripts/custom_script_signature_test.js +344 -0
- package/examples/scripts/script_interpreter.js +193 -0
- package/examples/smart_contract/complete_workflow_demo.js +343 -0
- package/examples/smart_contract/covenant_builder_demo.js +176 -0
- package/examples/smart_contract/script_testing_integration.js +198 -0
- package/index.js +3 -0
- package/lib/covenant-interface.js +713 -0
- package/lib/opcode.js +14 -7
- package/lib/smart_contract/API_REFERENCE.md +862 -0
- package/lib/smart_contract/DOCUMENTATION_SUMMARY.md +201 -0
- package/lib/smart_contract/EXAMPLES.md +751 -0
- package/lib/smart_contract/QUICK_START.md +549 -0
- package/lib/smart_contract/README.md +395 -0
- package/lib/smart_contract/builder.js +452 -0
- package/lib/smart_contract/covenant.js +336 -0
- package/lib/smart_contract/covenant_builder.js +512 -0
- package/lib/smart_contract/index.js +350 -0
- package/lib/smart_contract/opcode_list.js +30 -0
- package/lib/smart_contract/opcode_map.js +1174 -0
- package/lib/smart_contract/opcodes.md +1173 -0
- package/lib/smart_contract/preimage.js +903 -0
- package/lib/smart_contract/script_interpreter.js +236 -0
- package/lib/smart_contract/script_tester.js +487 -0
- package/lib/smart_contract/script_utils.js +621 -0
- package/lib/smart_contract/sighash.js +310 -0
- package/lib/smart_contract/smartledger-opcode_review.md +70 -0
- package/lib/smart_contract/stack_examiner.js +129 -0
- package/lib/smart_contract/test_integration.js +269 -0
- package/lib/smart_contract/utxo_generator.js +367 -0
- package/package.json +43 -10
- package/utilities/blockchain-state.json +20478 -3
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
# JavaScript-to-Bitcoin Script Quick Start Guide
|
|
2
|
+
|
|
3
|
+
**SmartLedger-BSV v3.2.0** - Revolutionary covenant development framework
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The JavaScript-to-Bitcoin Script framework allows you to write complex covenant logic in familiar JavaScript syntax and automatically generates optimized Bitcoin Script ASM and hex output.
|
|
8
|
+
|
|
9
|
+
## 🚀 30-Second Quick Start
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
const SmartContract = require('bsv-elliptic-fix').SmartContract
|
|
13
|
+
|
|
14
|
+
// Write covenant logic in JavaScript
|
|
15
|
+
const builder = SmartContract.createCovenantBuilder()
|
|
16
|
+
const covenant = builder
|
|
17
|
+
.extractField('value') // Get transaction value
|
|
18
|
+
.push('1000000') // 1M satoshis minimum
|
|
19
|
+
.greaterThanOrEqual() // Ensure value >= minimum
|
|
20
|
+
.verify() // Assert condition
|
|
21
|
+
.build()
|
|
22
|
+
|
|
23
|
+
// Get Bitcoin Script output
|
|
24
|
+
console.log('ASM:', covenant.cleanedASM)
|
|
25
|
+
console.log('Hex:', covenant.hex)
|
|
26
|
+
console.log('Operations:', covenant.operations.length)
|
|
27
|
+
|
|
28
|
+
// Test execution
|
|
29
|
+
const simulation = SmartContract.simulateScript(covenant.operations)
|
|
30
|
+
console.log('Script valid:', simulation.success)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Core Concepts
|
|
36
|
+
|
|
37
|
+
### 1. CovenantBuilder Fluent API
|
|
38
|
+
|
|
39
|
+
The CovenantBuilder provides a fluent interface with 61 methods for building Bitcoin Script:
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
const builder = SmartContract.createCovenantBuilder()
|
|
43
|
+
|
|
44
|
+
// Chain operations fluently
|
|
45
|
+
const script = builder
|
|
46
|
+
.comment('This is a comment for documentation')
|
|
47
|
+
.push('deadbeef') // Push data onto stack
|
|
48
|
+
.sha256() // Hash the data
|
|
49
|
+
.push('expected_hash') // Push expected result
|
|
50
|
+
.equal() // Compare hashes
|
|
51
|
+
.verify() // Assert equality
|
|
52
|
+
.build() // Generate final script
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 2. Preimage Field Extraction
|
|
56
|
+
|
|
57
|
+
Access BIP-143 preimage fields directly in your covenant logic:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
const covenant = builder
|
|
61
|
+
.extractField('value') // Extract transaction value
|
|
62
|
+
.extractField('hashPrevouts') // Extract input hash
|
|
63
|
+
.extractField('nLockTime') // Extract lock time
|
|
64
|
+
.extractField('scriptCode') // Extract script code
|
|
65
|
+
.build()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 3. Real-Time Simulation
|
|
69
|
+
|
|
70
|
+
Test your scripts immediately without blockchain interaction:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
const simulation = SmartContract.simulateScript(['OP_1', 'OP_2', 'OP_ADD'])
|
|
74
|
+
|
|
75
|
+
console.log('Final stack:', simulation.finalStack) // ['03']
|
|
76
|
+
console.log('Steps executed:', simulation.history.length)
|
|
77
|
+
console.log('Success:', simulation.success) // true
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Common Patterns
|
|
83
|
+
|
|
84
|
+
### Pattern 1: Value Lock Covenant
|
|
85
|
+
|
|
86
|
+
Ensure transactions maintain minimum value:
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
const valueLock = SmartContract.createCovenantBuilder()
|
|
90
|
+
.comment('Require minimum transaction value')
|
|
91
|
+
.extractField('value')
|
|
92
|
+
.push('1000000') // 1M satoshis
|
|
93
|
+
.greaterThanOrEqual()
|
|
94
|
+
.verify()
|
|
95
|
+
.build()
|
|
96
|
+
|
|
97
|
+
// Or use the template:
|
|
98
|
+
const template = SmartContract.createValueLockCovenant('1000000')
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Pattern 2: Hash Lock Covenant
|
|
102
|
+
|
|
103
|
+
Require knowledge of a secret preimage:
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const hashLock = SmartContract.createCovenantBuilder()
|
|
107
|
+
.comment('Require secret preimage')
|
|
108
|
+
.push('secret_preimage') // Secret data
|
|
109
|
+
.sha256() // Hash it
|
|
110
|
+
.push('expected_hash') // Expected result
|
|
111
|
+
.equalVerify() // Must match
|
|
112
|
+
.build()
|
|
113
|
+
|
|
114
|
+
// Or use the template:
|
|
115
|
+
const template = SmartContract.createHashLockCovenant('expected_hash')
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Pattern 3: Time Lock Covenant
|
|
119
|
+
|
|
120
|
+
Enforce time-based spending conditions:
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
const timeLock = SmartContract.createCovenantBuilder()
|
|
124
|
+
.comment('Require time lock expiry')
|
|
125
|
+
.extractField('nLockTime')
|
|
126
|
+
.push('1640995200') // Jan 1, 2022 timestamp
|
|
127
|
+
.greaterThan()
|
|
128
|
+
.verify()
|
|
129
|
+
.build()
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Pattern 4: Multi-Condition Covenant
|
|
133
|
+
|
|
134
|
+
Combine multiple validation rules:
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
const multiCondition = SmartContract.createCovenantBuilder()
|
|
138
|
+
.comment('Multi-condition validation')
|
|
139
|
+
|
|
140
|
+
// Condition 1: Minimum value
|
|
141
|
+
.extractField('value')
|
|
142
|
+
.push('1000000')
|
|
143
|
+
.greaterThanOrEqual()
|
|
144
|
+
.verify()
|
|
145
|
+
|
|
146
|
+
// Condition 2: Hash validation
|
|
147
|
+
.push('secret')
|
|
148
|
+
.sha256()
|
|
149
|
+
.push('expected_hash')
|
|
150
|
+
.equalVerify()
|
|
151
|
+
|
|
152
|
+
// Condition 3: Time lock
|
|
153
|
+
.extractField('nLockTime')
|
|
154
|
+
.push('1640995200')
|
|
155
|
+
.greaterThan()
|
|
156
|
+
.verify()
|
|
157
|
+
|
|
158
|
+
.build()
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Available Operations
|
|
164
|
+
|
|
165
|
+
### Stack Operations
|
|
166
|
+
```javascript
|
|
167
|
+
builder
|
|
168
|
+
.push('data') // Push data
|
|
169
|
+
.dup() // Duplicate top
|
|
170
|
+
.drop() // Remove top
|
|
171
|
+
.swap() // Swap top two
|
|
172
|
+
.over() // Copy second to top
|
|
173
|
+
.rot() // Rotate top three
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Arithmetic Operations
|
|
177
|
+
```javascript
|
|
178
|
+
builder
|
|
179
|
+
.add() // Add two numbers
|
|
180
|
+
.sub() // Subtract
|
|
181
|
+
.mul() // Multiply
|
|
182
|
+
.div() // Divide
|
|
183
|
+
.mod() // Modulo
|
|
184
|
+
.min() // Minimum
|
|
185
|
+
.max() // Maximum
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Comparison Operations
|
|
189
|
+
```javascript
|
|
190
|
+
builder
|
|
191
|
+
.equal() // Test equality
|
|
192
|
+
.equalVerify() // Test and verify
|
|
193
|
+
.lessThan() // Less than
|
|
194
|
+
.greaterThan() // Greater than
|
|
195
|
+
.lessThanOrEqual() // Less than or equal
|
|
196
|
+
.greaterThanOrEqual() // Greater than or equal
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Cryptographic Operations
|
|
200
|
+
```javascript
|
|
201
|
+
builder
|
|
202
|
+
.sha256() // SHA256 hash
|
|
203
|
+
.hash160() // RIPEMD160(SHA256(x))
|
|
204
|
+
.hash256() // Double SHA256
|
|
205
|
+
.ripemd160() // RIPEMD160 hash
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Data Manipulation
|
|
209
|
+
```javascript
|
|
210
|
+
builder
|
|
211
|
+
.size() // Get data size
|
|
212
|
+
.split() // Split at position
|
|
213
|
+
.cat() // Concatenate
|
|
214
|
+
.left(n) // Take left n bytes
|
|
215
|
+
.right(n) // Take right n bytes
|
|
216
|
+
.substr(start, len) // Extract substring
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Control Flow
|
|
220
|
+
```javascript
|
|
221
|
+
builder
|
|
222
|
+
.if() // Conditional execution
|
|
223
|
+
.else() // Else branch
|
|
224
|
+
.endif() // End conditional
|
|
225
|
+
.verify() // Assert true
|
|
226
|
+
.return() // Return from script
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Advanced Examples
|
|
232
|
+
|
|
233
|
+
### Example 1: Recursive Covenant
|
|
234
|
+
|
|
235
|
+
Create a covenant that enforces its own continuation:
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
const recursive = SmartContract.createCovenantBuilder()
|
|
239
|
+
.comment('Recursive covenant - enforces continuation')
|
|
240
|
+
|
|
241
|
+
// Extract current script hash
|
|
242
|
+
.extractField('scriptCode')
|
|
243
|
+
.hash160()
|
|
244
|
+
|
|
245
|
+
// Extract output script hash
|
|
246
|
+
.extractField('hashOutputs')
|
|
247
|
+
.push(32) // Skip to script hash position
|
|
248
|
+
.split()
|
|
249
|
+
.drop()
|
|
250
|
+
.push(20) // Take 20 bytes (script hash)
|
|
251
|
+
.split()
|
|
252
|
+
.swap()
|
|
253
|
+
.drop()
|
|
254
|
+
|
|
255
|
+
// Ensure script hash matches (recursive)
|
|
256
|
+
.equalVerify()
|
|
257
|
+
|
|
258
|
+
.build()
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Example 2: Oracle-Based Covenant
|
|
262
|
+
|
|
263
|
+
Covenant that validates against signed oracle data:
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
const oracle = SmartContract.createCovenantBuilder()
|
|
267
|
+
.comment('Oracle-validated covenant')
|
|
268
|
+
|
|
269
|
+
// Verify oracle signature
|
|
270
|
+
.push('oracle_message')
|
|
271
|
+
.push('oracle_signature')
|
|
272
|
+
.push('oracle_pubkey')
|
|
273
|
+
// ... signature verification logic ...
|
|
274
|
+
|
|
275
|
+
// Extract price from oracle message
|
|
276
|
+
.push('oracle_message')
|
|
277
|
+
.push(8) // Price is at position 8
|
|
278
|
+
.split()
|
|
279
|
+
.drop()
|
|
280
|
+
.push(8) // Price is 8 bytes
|
|
281
|
+
.split()
|
|
282
|
+
.swap()
|
|
283
|
+
.drop()
|
|
284
|
+
|
|
285
|
+
// Ensure transaction value matches oracle price
|
|
286
|
+
.extractField('value')
|
|
287
|
+
.greaterThanOrEqual()
|
|
288
|
+
.verify()
|
|
289
|
+
|
|
290
|
+
.build()
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Example 3: Multi-Path Spending
|
|
294
|
+
|
|
295
|
+
Create a covenant with multiple valid spending paths:
|
|
296
|
+
|
|
297
|
+
```javascript
|
|
298
|
+
const multiPath = SmartContract.createCovenantBuilder()
|
|
299
|
+
.comment('Multi-path spending covenant')
|
|
300
|
+
|
|
301
|
+
// Path 1: Owner can spend immediately
|
|
302
|
+
.push('owner_signature')
|
|
303
|
+
.push('owner_pubkey')
|
|
304
|
+
// ... signature check ...
|
|
305
|
+
|
|
306
|
+
// OR Path 2: Anyone can spend after time delay
|
|
307
|
+
.if()
|
|
308
|
+
.push(1) // Valid signature path
|
|
309
|
+
.else()
|
|
310
|
+
.extractField('nLockTime')
|
|
311
|
+
.push('1640995200') // Delay timestamp
|
|
312
|
+
.greaterThan()
|
|
313
|
+
.endif()
|
|
314
|
+
|
|
315
|
+
.verify()
|
|
316
|
+
.build()
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## Testing and Debugging
|
|
322
|
+
|
|
323
|
+
### 1. Script Simulation
|
|
324
|
+
|
|
325
|
+
Test your covenant logic before deployment:
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
const simulation = SmartContract.simulateScript(covenant.operations)
|
|
329
|
+
|
|
330
|
+
if (simulation.success) {
|
|
331
|
+
console.log('✅ Script executed successfully')
|
|
332
|
+
console.log('Final stack:', simulation.finalStack)
|
|
333
|
+
} else {
|
|
334
|
+
console.log('❌ Script failed:', simulation.error)
|
|
335
|
+
console.log('Failed at step:', simulation.failedStep)
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### 2. Step-by-Step Debugging
|
|
340
|
+
|
|
341
|
+
Debug script execution with detailed traces:
|
|
342
|
+
|
|
343
|
+
```javascript
|
|
344
|
+
const debug = SmartContract.debugScript({
|
|
345
|
+
script: covenant.operations,
|
|
346
|
+
enableTrace: true,
|
|
347
|
+
breakpoints: [5, 10] // Stop at operations 5 and 10
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
debug.history.forEach((step, index) => {
|
|
351
|
+
console.log(`Step ${index}: ${step.operation}`)
|
|
352
|
+
console.log(`Stack: [${step.stack.join(', ')}]`)
|
|
353
|
+
})
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### 3. Covenant Testing
|
|
357
|
+
|
|
358
|
+
Test complete covenant validation:
|
|
359
|
+
|
|
360
|
+
```javascript
|
|
361
|
+
const testResult = SmartContract.testCovenant(preimageHex, {
|
|
362
|
+
value: { min: 1000000 },
|
|
363
|
+
hashPrevouts: { notZero: true }
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
console.log('Covenant valid:', testResult.success)
|
|
367
|
+
console.log('Validation details:', testResult.validations)
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
---
|
|
371
|
+
|
|
372
|
+
## Templates and Utilities
|
|
373
|
+
|
|
374
|
+
### Pre-Built Templates
|
|
375
|
+
|
|
376
|
+
Use common covenant patterns:
|
|
377
|
+
|
|
378
|
+
```javascript
|
|
379
|
+
// Value lock template
|
|
380
|
+
const valueLock = SmartContract.createValueLockCovenant('1000000')
|
|
381
|
+
|
|
382
|
+
// Hash lock template
|
|
383
|
+
const hashLock = SmartContract.createHashLockCovenant('expected_hash')
|
|
384
|
+
|
|
385
|
+
// Complex validation template
|
|
386
|
+
const complex = SmartContract.createComplexValidationCovenant({
|
|
387
|
+
fields: {
|
|
388
|
+
value: { equals: '1000000' },
|
|
389
|
+
nLockTime: { greaterThan: '1640995200' }
|
|
390
|
+
}
|
|
391
|
+
})
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Opcode Reference
|
|
395
|
+
|
|
396
|
+
Access the complete opcode mapping:
|
|
397
|
+
|
|
398
|
+
```javascript
|
|
399
|
+
const opcodes = SmartContract.getOpcodeMap()
|
|
400
|
+
|
|
401
|
+
// Get specific opcode details
|
|
402
|
+
console.log('OP_ADD:', opcodes.OP_ADD)
|
|
403
|
+
// { code: 0x93, action: function, description: 'Add two numbers' }
|
|
404
|
+
|
|
405
|
+
// List all arithmetic opcodes
|
|
406
|
+
Object.keys(opcodes)
|
|
407
|
+
.filter(op => opcodes[op].category === 'arithmetic')
|
|
408
|
+
.forEach(op => console.log(op))
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
## Integration Examples
|
|
414
|
+
|
|
415
|
+
### With BSV Library
|
|
416
|
+
|
|
417
|
+
```javascript
|
|
418
|
+
const bsv = require('bsv-elliptic-fix')
|
|
419
|
+
const SmartContract = bsv.SmartContract
|
|
420
|
+
|
|
421
|
+
// Create covenant from private key
|
|
422
|
+
const privateKey = bsv.PrivateKey.fromRandom()
|
|
423
|
+
const covenant = SmartContract.createCovenant(privateKey)
|
|
424
|
+
|
|
425
|
+
// Build custom script
|
|
426
|
+
const builder = SmartContract.createCovenantBuilder()
|
|
427
|
+
const script = builder
|
|
428
|
+
.extractField('value')
|
|
429
|
+
.push('1000000')
|
|
430
|
+
.greaterThanOrEqual()
|
|
431
|
+
.verify()
|
|
432
|
+
.build()
|
|
433
|
+
|
|
434
|
+
// Use in transaction
|
|
435
|
+
const tx = new bsv.Transaction()
|
|
436
|
+
tx.addOutput(new bsv.Transaction.Output({
|
|
437
|
+
script: bsv.Script.fromASM(script.cleanedASM),
|
|
438
|
+
satoshis: 100000
|
|
439
|
+
}))
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Production Workflow
|
|
443
|
+
|
|
444
|
+
```javascript
|
|
445
|
+
// Complete covenant workflow
|
|
446
|
+
const result = SmartContract.completeCovenantFlow(
|
|
447
|
+
privateKey,
|
|
448
|
+
p2pkhUtxo,
|
|
449
|
+
(transaction, phase) => {
|
|
450
|
+
console.log(`Broadcasting ${phase} transaction...`)
|
|
451
|
+
// Your broadcast logic here
|
|
452
|
+
return broadcastTransaction(transaction)
|
|
453
|
+
}
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
if (result.success) {
|
|
457
|
+
console.log('✅ Covenant deployed successfully')
|
|
458
|
+
console.log('Creation TX:', result.creationTx.id)
|
|
459
|
+
console.log('Test spending TX:', result.spendingTx.id)
|
|
460
|
+
} else {
|
|
461
|
+
console.log('❌ Deployment failed:', result.errors)
|
|
462
|
+
}
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
---
|
|
466
|
+
|
|
467
|
+
## Best Practices
|
|
468
|
+
|
|
469
|
+
### 1. Always Add Comments
|
|
470
|
+
|
|
471
|
+
```javascript
|
|
472
|
+
const covenant = builder
|
|
473
|
+
.comment('=== Value Validation Section ===')
|
|
474
|
+
.extractField('value')
|
|
475
|
+
.comment('Minimum required: 1M satoshis')
|
|
476
|
+
.push('1000000')
|
|
477
|
+
.greaterThanOrEqual()
|
|
478
|
+
.comment('Assert minimum value requirement')
|
|
479
|
+
.verify()
|
|
480
|
+
.build()
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### 2. Test Before Deployment
|
|
484
|
+
|
|
485
|
+
```javascript
|
|
486
|
+
// Always simulate before deploying
|
|
487
|
+
const simulation = SmartContract.simulateScript(covenant.operations)
|
|
488
|
+
if (!simulation.success) {
|
|
489
|
+
throw new Error(`Script validation failed: ${simulation.error}`)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Test with real preimage data
|
|
493
|
+
const testResult = SmartContract.testCovenant(preimageHex, constraints)
|
|
494
|
+
if (!testResult.success) {
|
|
495
|
+
throw new Error(`Covenant validation failed`)
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### 3. Use Template Patterns
|
|
500
|
+
|
|
501
|
+
```javascript
|
|
502
|
+
// Prefer templates for common patterns
|
|
503
|
+
const valueLock = SmartContract.createValueLockCovenant('1000000')
|
|
504
|
+
|
|
505
|
+
// Rather than building from scratch every time
|
|
506
|
+
const manual = SmartContract.createCovenantBuilder()
|
|
507
|
+
.extractField('value')
|
|
508
|
+
.push('1000000')
|
|
509
|
+
.greaterThanOrEqual()
|
|
510
|
+
.verify()
|
|
511
|
+
.build()
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### 4. Handle Errors Gracefully
|
|
515
|
+
|
|
516
|
+
```javascript
|
|
517
|
+
try {
|
|
518
|
+
const covenant = builder.build()
|
|
519
|
+
const simulation = SmartContract.simulateScript(covenant.operations)
|
|
520
|
+
|
|
521
|
+
if (!simulation.success) {
|
|
522
|
+
console.log('Simulation failed:', simulation.error)
|
|
523
|
+
console.log('Stack at failure:', simulation.stackAtFailure)
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
} catch (error) {
|
|
527
|
+
console.log('Build error:', error.message)
|
|
528
|
+
// Handle specific error types
|
|
529
|
+
}
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
---
|
|
533
|
+
|
|
534
|
+
## Next Steps
|
|
535
|
+
|
|
536
|
+
1. **Explore the Full API**: See [API_REFERENCE.md](./API_REFERENCE.md) for complete function documentation
|
|
537
|
+
2. **Study Real Examples**: Check `/examples` directory for production covenant patterns
|
|
538
|
+
3. **Join the Community**: Connect with other BSV covenant developers
|
|
539
|
+
4. **Build Advanced Covenants**: Experiment with complex multi-condition logic
|
|
540
|
+
|
|
541
|
+
The JavaScript-to-Bitcoin Script framework makes covenant development accessible while maintaining the full power of Bitcoin Script. Start building your first covenant today!
|
|
542
|
+
|
|
543
|
+
---
|
|
544
|
+
|
|
545
|
+
**Need Help?**
|
|
546
|
+
- API Reference: [API_REFERENCE.md](./API_REFERENCE.md)
|
|
547
|
+
- Opcode Documentation: [opcodes.md](./opcodes.md)
|
|
548
|
+
- Example Code: `/examples` directory
|
|
549
|
+
- Community: BSV Developer Resources
|