@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.
- package/CHANGELOG.md +336 -0
- package/README.md +72 -72
- package/SECURITY.md +88 -0
- package/bin/cli.js +13 -8
- package/bsv-covenant.min.js +4 -4
- package/bsv-gdaf.min.js +5 -5
- package/bsv-ltp.min.js +7 -7
- package/bsv-smartcontract.min.js +5 -5
- package/bsv.bundle.js +5 -5
- package/bsv.d.ts +486 -9
- package/bsv.min.js +5 -5
- package/docs/COVENANT_DEVELOPMENT_RESOLVED.md +2 -2
- package/docs/MODULE_REFERENCE_COMPLETE.md +61 -58
- package/docs/advanced/LEGAL_TOKEN_PROTOCOL.md +3 -3
- package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
- package/docs/getting-started/INSTALLATION.md +30 -30
- package/docs/getting-started/QUICK_START.md +18 -18
- package/docs/migration/FROM_BSV_1_5_6.md +16 -10
- package/gdaf-entry.js +1 -2
- package/index.js +20 -7
- package/lib/smart_contract/covenant.js +10 -1
- package/lib/smartutxo.js +20 -12
- package/lib/transaction/transaction.js +7 -0
- package/ltp-entry.js +1 -2
- package/package.json +3 -3
- package/utilities/blockchain-state.js +32 -23
- package/demos/README.md +0 -188
- package/demos/architecture_demo.js +0 -247
- package/demos/browser-test.html +0 -1208
- package/demos/bsv_wallet_demo.js +0 -242
- package/demos/complete_ltp_demo.js +0 -511
- package/demos/debug_tools_demo.js +0 -87
- package/demos/demo_features.js +0 -123
- package/demos/easy_interface_demo.js +0 -109
- package/demos/ecies_demo.js +0 -182
- package/demos/gdaf_demo.js +0 -237
- package/demos/ltp_demo.js +0 -361
- package/demos/ltp_primitives_demo.js +0 -403
- package/demos/message_demo.js +0 -209
- package/demos/preimage_separation_demo.js +0 -383
- package/demos/script_helper_demo.js +0 -289
- package/demos/security_demo.js +0 -287
- package/demos/shamir_demo.js +0 -121
- package/demos/simple_demo.js +0 -204
- package/demos/simple_p2pkh_demo.js +0 -169
- package/demos/simple_utxo_preimage_demo.js +0 -196
- package/demos/smart_contract_demo.html +0 -1347
- package/demos/smart_contract_demo.js +0 -910
- package/demos/utxo_generator_demo.js +0 -244
- package/demos/validation_pipeline_demo.js +0 -155
- package/demos/web3keys.html +0 -740
- package/examples/README.md +0 -200
- package/examples/basic/transaction-creation.js +0 -534
- package/examples/basic/transaction_signature_api_gap.js +0 -178
- package/examples/complete_workflow_demo.js +0 -783
- package/examples/covenants/advanced_covenant_demo.js +0 -219
- package/examples/covenants/covenant_interface_demo.js +0 -270
- package/examples/covenants/covenant_manual_signature_resolved.js +0 -212
- package/examples/covenants/covenant_signature_template.js +0 -117
- package/examples/covenants2/covenant_bidirectional_example.js +0 -262
- package/examples/covenants2/covenant_utils_demo.js +0 -120
- package/examples/covenants2/preimage_covenant_utils.js +0 -287
- package/examples/covenants2/production_integration.js +0 -256
- package/examples/data/covenant_utxos.json +0 -28
- package/examples/data/utxos.json +0 -26
- package/examples/definitive_working_demo.js +0 -261
- package/examples/final_working_contracts.js +0 -338
- package/examples/legacy/README.md +0 -11
- package/examples/legacy/smart_contract_test_integration.js +0 -269
- package/examples/legacy/test_builtin_verify.js +0 -117
- package/examples/legacy/test_debug_integration.js +0 -71
- package/examples/legacy/test_ecdsa_little.js +0 -70
- package/examples/legacy/test_shamir.js +0 -221
- package/examples/legacy/test_smartverify_der.js +0 -110
- package/examples/preimage/README.md +0 -178
- package/examples/preimage/extract_preimage_bidirectional.js +0 -421
- package/examples/preimage/generate_sample_preimage.js +0 -208
- package/examples/preimage/generate_sighash_examples.js +0 -152
- package/examples/preimage/parse_preimage.js +0 -117
- package/examples/preimage/test_preimage_extractor.js +0 -53
- package/examples/preimage/test_varint_extraction.js +0 -95
- package/examples/scripts/custom_script_helper_example.js +0 -273
- package/examples/scripts/script_interpreter.js +0 -193
- package/examples/smart_contract/complete_workflow_demo.js +0 -343
- package/examples/smart_contract/covenant_builder_demo.js +0 -176
- package/examples/smart_contract/script_testing_integration.js +0 -198
- package/examples/smart_contract_templates.js +0 -718
- package/examples/working_smart_contracts.js +0 -348
package/examples/data/utxos.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"txid": "98697480789ca50f967b9b324b44838d6b256e0db22206bf5c58f02fa652c864",
|
|
4
|
-
"vout": 1,
|
|
5
|
-
"satoshis": 917852,
|
|
6
|
-
"script": "76a914c0a3c16fca8d5c0b6a5e7c09e2c12c5a7d8b6e9f88ac",
|
|
7
|
-
"status": "confirmed",
|
|
8
|
-
"height": 850234
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"txid": "76543210abcdef0123456789abcdef0123456789abcdef0123456789abcdef01",
|
|
12
|
-
"vout": 0,
|
|
13
|
-
"satoshis": 250000,
|
|
14
|
-
"script": "76a914f0e9c5a4d8b6e7c9a8b7d6e5f4c3a2b1d9e8f7c688ac",
|
|
15
|
-
"status": "confirmed",
|
|
16
|
-
"height": 850235
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"txid": "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
|
|
20
|
-
"vout": 2,
|
|
21
|
-
"satoshis": 500000,
|
|
22
|
-
"script": "76a914a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b088ac",
|
|
23
|
-
"status": "confirmed",
|
|
24
|
-
"height": 850236
|
|
25
|
-
}
|
|
26
|
-
]
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* š DEFINITIVE PROOF: SMART CONTRACTS ARE WORKING! š
|
|
5
|
-
*
|
|
6
|
-
* This file demonstrates that ALL core smart contract functionality
|
|
7
|
-
* is working correctly in smartledger-bsv.
|
|
8
|
-
*
|
|
9
|
-
* ā
PROVEN WORKING:
|
|
10
|
-
* - Contract creation with createQuickCovenant()
|
|
11
|
-
* - Address generation for P2SH deployment
|
|
12
|
-
* - Bitcoin Script generation (ASM and hex)
|
|
13
|
-
* - Field extraction from preimages (value, locktime, etc.)
|
|
14
|
-
* - Multiple contract types (payment, escrow, multi-condition)
|
|
15
|
-
*
|
|
16
|
-
* Run with: node examples/definitive_working_demo.js
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const bsv = require('../index.js')
|
|
20
|
-
const chalk = require('chalk') || { green: (s) => s, red: (s) => s, blue: (s) => s, yellow: (s) => s, cyan: (s) => s }
|
|
21
|
-
|
|
22
|
-
console.log(chalk.blue('šÆ DEFINITIVE SMART CONTRACT WORKING DEMONSTRATION'))
|
|
23
|
-
console.log(chalk.blue('==================================================='))
|
|
24
|
-
|
|
25
|
-
class WorkingSmartContractSuite {
|
|
26
|
-
|
|
27
|
-
// ā
DEMO 1: Basic Payment Validation Contract
|
|
28
|
-
static createPaymentContract(amount) {
|
|
29
|
-
console.log(chalk.yellow(`\nš Creating Payment Contract (${amount} satoshis)`))
|
|
30
|
-
|
|
31
|
-
const covenant = bsv.SmartContract.createQuickCovenant('value_lock', {
|
|
32
|
-
value: amount
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
const script = bsv.Script.fromASM(covenant.asm)
|
|
36
|
-
const address = bsv.SmartContract.utils.createCovenantAddress(script)
|
|
37
|
-
|
|
38
|
-
console.log(chalk.green('ā
Payment contract created successfully!'))
|
|
39
|
-
console.log(chalk.cyan('š Address:'), address.toString())
|
|
40
|
-
console.log(chalk.cyan('š Script:'), covenant.asm)
|
|
41
|
-
console.log(chalk.cyan('š° Validates payments >= '), amount, 'satoshis')
|
|
42
|
-
|
|
43
|
-
return { covenant, script, address, amount }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// ā
DEMO 2: Field Extraction Verification
|
|
47
|
-
static testFieldExtraction(amount) {
|
|
48
|
-
console.log(chalk.yellow(`\nš Testing Field Extraction for ${amount} satoshis`))
|
|
49
|
-
|
|
50
|
-
// Create proper preimage with the amount
|
|
51
|
-
const version = Buffer.from('01000000', 'hex')
|
|
52
|
-
const hashPrevouts = Buffer.alloc(32, 0)
|
|
53
|
-
const hashSequence = Buffer.alloc(32, 0)
|
|
54
|
-
const outpoint = Buffer.alloc(36, 0)
|
|
55
|
-
const scriptLen = Buffer.from('00', 'hex')
|
|
56
|
-
|
|
57
|
-
const value = Buffer.alloc(8)
|
|
58
|
-
value.writeUInt32LE(amount, 0)
|
|
59
|
-
|
|
60
|
-
const sequence = Buffer.from('ffffffff', 'hex')
|
|
61
|
-
const hashOutputs = Buffer.alloc(32, 0)
|
|
62
|
-
const locktime = Buffer.alloc(4, 0)
|
|
63
|
-
const sighash = Buffer.from('41000000', 'hex')
|
|
64
|
-
|
|
65
|
-
const preimage = Buffer.concat([
|
|
66
|
-
version, hashPrevouts, hashSequence, outpoint,
|
|
67
|
-
scriptLen, value, sequence, hashOutputs, locktime, sighash
|
|
68
|
-
]).toString('hex')
|
|
69
|
-
|
|
70
|
-
// Test field extraction
|
|
71
|
-
const result = bsv.SmartContract.testFieldExtraction(preimage, 'value')
|
|
72
|
-
|
|
73
|
-
if (result.fieldExtraction && result.fieldExtraction.interpretation) {
|
|
74
|
-
const extracted = result.fieldExtraction.interpretation.satoshis
|
|
75
|
-
console.log(chalk.green('ā
Field extraction SUCCESS!'))
|
|
76
|
-
console.log(chalk.cyan('š° Expected:'), amount)
|
|
77
|
-
console.log(chalk.cyan('š° Extracted:'), extracted)
|
|
78
|
-
|
|
79
|
-
if (parseInt(extracted) === amount) {
|
|
80
|
-
console.log(chalk.green('ā
Values match perfectly!'))
|
|
81
|
-
return true
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
console.log(chalk.red('ā Field extraction failed'))
|
|
86
|
-
return false
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// ā
DEMO 3: Multiple Contract Types
|
|
90
|
-
static createContractPortfolio() {
|
|
91
|
-
console.log(chalk.yellow('\nšļø Creating Contract Portfolio'))
|
|
92
|
-
|
|
93
|
-
const contracts = []
|
|
94
|
-
|
|
95
|
-
// Different contract types
|
|
96
|
-
const contractTypes = [
|
|
97
|
-
{ name: 'Micro Payment', amount: 1000 }, // 0.00001 BSV
|
|
98
|
-
{ name: 'Small Payment', amount: 10000 }, // 0.0001 BSV
|
|
99
|
-
{ name: 'Medium Payment', amount: 100000 }, // 0.001 BSV
|
|
100
|
-
{ name: 'Large Payment', amount: 1000000 }, // 0.01 BSV
|
|
101
|
-
{ name: 'Enterprise', amount: 10000000 } // 0.1 BSV
|
|
102
|
-
]
|
|
103
|
-
|
|
104
|
-
contractTypes.forEach((type, i) => {
|
|
105
|
-
const contract = this.createPaymentContract(type.amount)
|
|
106
|
-
contracts.push({
|
|
107
|
-
name: type.name,
|
|
108
|
-
...contract
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
console.log(chalk.cyan(`${i + 1}. ${type.name}: ${contract.address.toString()}`))
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
console.log(chalk.green(`\nā
Created ${contracts.length} different smart contracts!`))
|
|
115
|
-
return contracts
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// ā
DEMO 4: Advanced Contract Features
|
|
119
|
-
static demonstrateAdvancedFeatures() {
|
|
120
|
-
console.log(chalk.yellow('\nš Advanced Smart Contract Features'))
|
|
121
|
-
|
|
122
|
-
console.log(chalk.cyan('\nš Available Contract Templates:'))
|
|
123
|
-
console.log('1. value_lock - Payment amount validation')
|
|
124
|
-
console.log('2. time_lock - Time-based spending restrictions')
|
|
125
|
-
console.log('3. multi_sig - Multiple signature requirements')
|
|
126
|
-
console.log('4. hash_lock - Secret reveal requirements')
|
|
127
|
-
|
|
128
|
-
// Demonstrate each template type
|
|
129
|
-
const templates = [
|
|
130
|
-
{
|
|
131
|
-
name: 'Value Lock',
|
|
132
|
-
type: 'value_lock',
|
|
133
|
-
params: { value: 250000 }
|
|
134
|
-
}
|
|
135
|
-
]
|
|
136
|
-
|
|
137
|
-
templates.forEach(template => {
|
|
138
|
-
try {
|
|
139
|
-
const covenant = bsv.SmartContract.createQuickCovenant(template.type, template.params)
|
|
140
|
-
const script = bsv.Script.fromASM(covenant.asm)
|
|
141
|
-
const address = bsv.SmartContract.utils.createCovenantAddress(script)
|
|
142
|
-
|
|
143
|
-
console.log(chalk.green(`ā
${template.name} contract created`))
|
|
144
|
-
console.log(chalk.cyan(' Address:'), address.toString())
|
|
145
|
-
console.log(chalk.cyan(' Script size:'), script.toBuffer().length, 'bytes')
|
|
146
|
-
} catch (error) {
|
|
147
|
-
console.log(chalk.yellow(`ā ļø ${template.name}: Template requires additional parameters`))
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// ā
DEMO 5: Production Deployment Information
|
|
153
|
-
static showProductionInfo(contracts) {
|
|
154
|
-
console.log(chalk.yellow('\nš Production Deployment Information'))
|
|
155
|
-
console.log(chalk.yellow('===================================='))
|
|
156
|
-
|
|
157
|
-
console.log(chalk.cyan('\nšļø Contract Deployment Process:'))
|
|
158
|
-
console.log('1. Fund contract addresses with Bitcoin')
|
|
159
|
-
console.log('2. Create spending transactions with proper amounts')
|
|
160
|
-
console.log('3. Generate BIP-143 preimages for validation')
|
|
161
|
-
console.log('4. Include preimage in unlocking scripts')
|
|
162
|
-
console.log('5. Broadcast to BSV network')
|
|
163
|
-
|
|
164
|
-
console.log(chalk.cyan('\nš° Contract Portfolio Summary:'))
|
|
165
|
-
contracts.forEach((contract, i) => {
|
|
166
|
-
console.log(`${i + 1}. ${contract.name}`)
|
|
167
|
-
console.log(` Address: ${contract.address.toString()}`)
|
|
168
|
-
console.log(` Min Amount: ${contract.amount} satoshis`)
|
|
169
|
-
console.log(` Script Size: ${contract.script.toBuffer().length} bytes`)
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
const totalValue = contracts.reduce((sum, c) => sum + c.amount, 0)
|
|
173
|
-
console.log(chalk.green(`\nš Total minimum validation amount: ${totalValue} satoshis`))
|
|
174
|
-
console.log(chalk.green(`š Average contract size: ${Math.round(contracts.reduce((sum, c) => sum + c.script.toBuffer().length, 0) / contracts.length)} bytes`))
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// ā
DEMO 6: Security and Best Practices
|
|
178
|
-
static showSecurityInfo() {
|
|
179
|
-
console.log(chalk.yellow('\nš Security & Best Practices'))
|
|
180
|
-
console.log(chalk.yellow('============================='))
|
|
181
|
-
|
|
182
|
-
console.log(chalk.cyan('\nā
Security Checklist:'))
|
|
183
|
-
console.log('ā
Use createQuickCovenant() for reliable script generation')
|
|
184
|
-
console.log('ā
Validate all contract parameters before deployment')
|
|
185
|
-
console.log('ā
Test extensively on testnet before mainnet')
|
|
186
|
-
console.log('ā
Monitor contract addresses for funding and spending')
|
|
187
|
-
console.log('ā
Implement proper error handling and recovery')
|
|
188
|
-
console.log('ā
Validate transaction structure before broadcast')
|
|
189
|
-
console.log('ā
Use proper BIP-143 preimage generation')
|
|
190
|
-
console.log('ā
Implement replay protection mechanisms')
|
|
191
|
-
|
|
192
|
-
console.log(chalk.cyan('\nš Production Readiness:'))
|
|
193
|
-
console.log('šÆ All core functionality is working correctly')
|
|
194
|
-
console.log('šÆ Contract creation is production-ready')
|
|
195
|
-
console.log('šÆ Address generation is functional')
|
|
196
|
-
console.log('šÆ Field extraction is operational')
|
|
197
|
-
console.log('šÆ Multiple contract types are supported')
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// š RUN COMPLETE DEMONSTRATION
|
|
202
|
-
console.log(chalk.blue('\nš Starting Complete Smart Contract Demonstration...'))
|
|
203
|
-
|
|
204
|
-
// Demo 1: Basic contracts
|
|
205
|
-
const contract1 = WorkingSmartContractSuite.createPaymentContract(100000)
|
|
206
|
-
const contract2 = WorkingSmartContractSuite.createPaymentContract(500000)
|
|
207
|
-
|
|
208
|
-
// Demo 2: Field extraction
|
|
209
|
-
const extraction1 = WorkingSmartContractSuite.testFieldExtraction(100000)
|
|
210
|
-
const extraction2 = WorkingSmartContractSuite.testFieldExtraction(500000)
|
|
211
|
-
|
|
212
|
-
// Demo 3: Contract portfolio
|
|
213
|
-
const portfolio = WorkingSmartContractSuite.createContractPortfolio()
|
|
214
|
-
|
|
215
|
-
// Demo 4: Advanced features
|
|
216
|
-
WorkingSmartContractSuite.demonstrateAdvancedFeatures()
|
|
217
|
-
|
|
218
|
-
// Demo 5: Production info
|
|
219
|
-
WorkingSmartContractSuite.showProductionInfo(portfolio)
|
|
220
|
-
|
|
221
|
-
// Demo 6: Security info
|
|
222
|
-
WorkingSmartContractSuite.showSecurityInfo()
|
|
223
|
-
|
|
224
|
-
// š FINAL SUCCESS MESSAGE
|
|
225
|
-
console.log(chalk.green('\nš š š SMART CONTRACTS ARE FULLY WORKING! š š š'))
|
|
226
|
-
console.log(chalk.green('========================================================='))
|
|
227
|
-
|
|
228
|
-
console.log(chalk.green('\nā
PROVEN WORKING FUNCTIONALITY:'))
|
|
229
|
-
console.log(chalk.green('⢠Contract Creation: WORKING'))
|
|
230
|
-
console.log(chalk.green('⢠Address Generation: WORKING'))
|
|
231
|
-
console.log(chalk.green('⢠Script Generation: WORKING'))
|
|
232
|
-
console.log(chalk.green('⢠Field Extraction: WORKING'))
|
|
233
|
-
console.log(chalk.green('⢠Multiple Contract Types: WORKING'))
|
|
234
|
-
console.log(chalk.green('⢠Production Deployment: READY'))
|
|
235
|
-
|
|
236
|
-
console.log(chalk.blue('\nš NEXT STEPS:'))
|
|
237
|
-
console.log('1. Fund contract addresses with Bitcoin')
|
|
238
|
-
console.log('2. Create spending transactions that meet contract conditions')
|
|
239
|
-
console.log('3. Generate proper preimages with transaction details')
|
|
240
|
-
console.log('4. Broadcast transactions to BSV network')
|
|
241
|
-
console.log('5. Monitor contract execution and results')
|
|
242
|
-
|
|
243
|
-
console.log(chalk.cyan('\nš” KEY INSIGHTS:'))
|
|
244
|
-
console.log('⢠Use bsv.SmartContract.createQuickCovenant() for reliable contracts')
|
|
245
|
-
console.log('⢠Convert ASM to Script with bsv.Script.fromASM()')
|
|
246
|
-
console.log('⢠Generate addresses with bsv.SmartContract.utils.createCovenantAddress()')
|
|
247
|
-
console.log('⢠Field extraction works perfectly for validating transaction data')
|
|
248
|
-
console.log('⢠All infrastructure is ready for Bitcoin smart contract deployment')
|
|
249
|
-
|
|
250
|
-
console.log(chalk.blue('\nš Documentation Available:'))
|
|
251
|
-
console.log('⢠Complete guide: docs/SMART_CONTRACT_DEVELOPMENT_GUIDE.md')
|
|
252
|
-
console.log('⢠Working templates: examples/smart_contract_templates.js')
|
|
253
|
-
console.log('⢠Full workflow: examples/complete_workflow_demo.js')
|
|
254
|
-
console.log('⢠This demo: examples/definitive_working_demo.js')
|
|
255
|
-
|
|
256
|
-
// Export functionality for use in other modules
|
|
257
|
-
module.exports = {
|
|
258
|
-
WorkingSmartContractSuite,
|
|
259
|
-
createContract: WorkingSmartContractSuite.createPaymentContract,
|
|
260
|
-
testExtraction: WorkingSmartContractSuite.testFieldExtraction
|
|
261
|
-
}
|
|
@@ -1,338 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Final Working Smart Contract Solution
|
|
3
|
-
* ====================================
|
|
4
|
-
*
|
|
5
|
-
* This demonstrates the correct way to create smart contracts with smartledger-bsv
|
|
6
|
-
* that actually work with proper preimage validation.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const bsv = require('../index.js')
|
|
10
|
-
|
|
11
|
-
// Enable colored output if available
|
|
12
|
-
let chalk
|
|
13
|
-
try {
|
|
14
|
-
chalk = require('chalk')
|
|
15
|
-
} catch (e) {
|
|
16
|
-
chalk = {
|
|
17
|
-
green: (text) => `ā
${text}`,
|
|
18
|
-
red: (text) => `ā ${text}`,
|
|
19
|
-
yellow: (text) => `ā ļø ${text}`,
|
|
20
|
-
blue: (text) => `ā¹ļø ${text}`,
|
|
21
|
-
magenta: (text) => `š® ${text}`,
|
|
22
|
-
cyan: (text) => `š ${text}`,
|
|
23
|
-
bold: (text) => `**${text}**`
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* SOLUTION: Working Smart Contract Class
|
|
29
|
-
* =====================================
|
|
30
|
-
*
|
|
31
|
-
* This is the CORRECT way to create smart contracts with smartledger-bsv
|
|
32
|
-
*/
|
|
33
|
-
class WorkingSmartContract {
|
|
34
|
-
constructor(expectedAmount) {
|
|
35
|
-
this.expectedAmount = expectedAmount
|
|
36
|
-
this.privateKey = new bsv.PrivateKey()
|
|
37
|
-
|
|
38
|
-
// CORRECT: Use createQuickCovenant for reliable script generation
|
|
39
|
-
this.covenantConfig = bsv.SmartContract.createQuickCovenant('value_lock', {
|
|
40
|
-
value: expectedAmount
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
// CORRECT: Convert ASM to proper BSV Script object
|
|
44
|
-
this.script = bsv.Script.fromASM(this.covenantConfig.asm)
|
|
45
|
-
this.address = bsv.SmartContract.utils.createCovenantAddress(this.script)
|
|
46
|
-
|
|
47
|
-
console.log(chalk.green(`ā
Smart Contract Created Successfully`))
|
|
48
|
-
console.log(chalk.blue(` Expected Amount: ${expectedAmount} satoshis`))
|
|
49
|
-
console.log(chalk.blue(` Contract Address: ${this.address.toString()}`))
|
|
50
|
-
console.log(chalk.blue(` Script ASM: ${this.covenantConfig.asm}`))
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* CORRECT: Create proper test preimage
|
|
55
|
-
*/
|
|
56
|
-
createTestPreimage(outputAmount) {
|
|
57
|
-
try {
|
|
58
|
-
// Create a proper transaction structure
|
|
59
|
-
const tx = new bsv.Transaction()
|
|
60
|
-
|
|
61
|
-
// Add input - spending from the contract
|
|
62
|
-
const input = new bsv.Transaction.Input({
|
|
63
|
-
prevTxId: 'a'.repeat(64), // Mock previous transaction ID
|
|
64
|
-
outputIndex: 0,
|
|
65
|
-
script: new bsv.Script() // Unlocking script (will contain preimage)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
// Set the input's output reference (what we're spending)
|
|
69
|
-
input.output = new bsv.Transaction.Output({
|
|
70
|
-
script: this.script,
|
|
71
|
-
satoshis: this.expectedAmount + 10000 // Contract has more than minimum
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
tx.inputs.push(input)
|
|
75
|
-
|
|
76
|
-
// Add output - where the money goes
|
|
77
|
-
const output = new bsv.Transaction.Output({
|
|
78
|
-
script: bsv.Script.buildPublicKeyHashOut(new bsv.Address('1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2')),
|
|
79
|
-
satoshis: outputAmount
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
tx.outputs.push(output)
|
|
83
|
-
|
|
84
|
-
// Generate preimage using correct parameters
|
|
85
|
-
const preimage = bsv.Transaction.sighash.sighashPreimage(
|
|
86
|
-
tx,
|
|
87
|
-
bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID,
|
|
88
|
-
0, // Input index
|
|
89
|
-
this.script, // Script being spent
|
|
90
|
-
new bsv.crypto.BN(this.expectedAmount + 10000) // Input amount
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
return preimage.toString('hex')
|
|
94
|
-
|
|
95
|
-
} catch (error) {
|
|
96
|
-
console.log(chalk.red('ā Preimage creation error:', error.message))
|
|
97
|
-
return null
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* CORRECT: Validate preimage against contract
|
|
103
|
-
*/
|
|
104
|
-
validatePayment(preimageHex) {
|
|
105
|
-
try {
|
|
106
|
-
console.log(chalk.yellow('š Validating payment with smart contract...'))
|
|
107
|
-
|
|
108
|
-
// Test the preimage against our contract script
|
|
109
|
-
const result = bsv.SmartContract.testScript(
|
|
110
|
-
preimageHex, // Unlocking script (just the preimage)
|
|
111
|
-
this.script.toHex(), // Locking script (our contract)
|
|
112
|
-
{ verbose: true }
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
if (result.valid) {
|
|
116
|
-
console.log(chalk.green('ā
VALIDATION PASSED - Contract accepts this payment!'))
|
|
117
|
-
} else {
|
|
118
|
-
console.log(chalk.red('ā VALIDATION FAILED - Contract rejects this payment'))
|
|
119
|
-
if (result.error) {
|
|
120
|
-
console.log(chalk.red(` Reason: ${result.error}`))
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
success: result.valid,
|
|
126
|
-
error: result.error,
|
|
127
|
-
details: result
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.log(chalk.red('ā Validation error:', error.message))
|
|
132
|
-
return {
|
|
133
|
-
success: false,
|
|
134
|
-
error: error.message
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* SOLUTION: Complete working test
|
|
141
|
-
*/
|
|
142
|
-
runWorkingDemo() {
|
|
143
|
-
console.log(chalk.bold.cyan('\nšÆ Complete Working Smart Contract Demo'))
|
|
144
|
-
console.log(chalk.cyan('======================================\n'))
|
|
145
|
-
|
|
146
|
-
// Test 1: Correct amount (should pass)
|
|
147
|
-
console.log(chalk.yellow('Test 1: Payment with CORRECT amount'))
|
|
148
|
-
const correctPreimage = this.createTestPreimage(this.expectedAmount)
|
|
149
|
-
if (correctPreimage) {
|
|
150
|
-
console.log(chalk.blue(` Generated preimage: ${correctPreimage.substring(0, 64)}...`))
|
|
151
|
-
const result1 = this.validatePayment(correctPreimage)
|
|
152
|
-
console.log(chalk.blue(` Result: ${result1.success ? 'PASS ā
' : 'FAIL ā'}\n`))
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Test 2: Incorrect amount (should fail)
|
|
156
|
-
console.log(chalk.yellow('Test 2: Payment with INCORRECT amount (too small)'))
|
|
157
|
-
const incorrectPreimage = this.createTestPreimage(this.expectedAmount - 10000)
|
|
158
|
-
if (incorrectPreimage) {
|
|
159
|
-
console.log(chalk.blue(` Generated preimage: ${incorrectPreimage.substring(0, 64)}...`))
|
|
160
|
-
const result2 = this.validatePayment(incorrectPreimage)
|
|
161
|
-
console.log(chalk.blue(` Result: ${result2.success ? 'UNEXPECTED PASS ā ļø' : 'EXPECTED FAIL ā
'}\n`))
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Show field extraction
|
|
165
|
-
console.log(chalk.yellow('Test 3: Field extraction verification'))
|
|
166
|
-
if (correctPreimage) {
|
|
167
|
-
const fieldResult = bsv.SmartContract.testFieldExtraction(correctPreimage, 'value')
|
|
168
|
-
if (fieldResult.success && fieldResult.fieldExtraction) {
|
|
169
|
-
console.log(chalk.green(` ā
Value field extracted: ${fieldResult.fieldExtraction.interpretation.satoshis} satoshis`))
|
|
170
|
-
console.log(chalk.blue(` Expected: ${this.expectedAmount} satoshis`))
|
|
171
|
-
console.log(chalk.blue(` Match: ${fieldResult.fieldExtraction.interpretation.satoshis == this.expectedAmount ? 'YES ā
' : 'NO ā'}`))
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return true
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* SOLUTION: Deploy contract for production use
|
|
180
|
-
*/
|
|
181
|
-
async deployContract(fundingUtxo, fundingPrivateKey) {
|
|
182
|
-
console.log(chalk.cyan('\nš¤ Deploying Smart Contract'))
|
|
183
|
-
console.log(chalk.cyan('==========================='))
|
|
184
|
-
|
|
185
|
-
try {
|
|
186
|
-
// Create funding transaction
|
|
187
|
-
const fundingTx = new bsv.Transaction()
|
|
188
|
-
.from(fundingUtxo)
|
|
189
|
-
.to(this.address, fundingUtxo.satoshis - 1000) // 1000 sat fee
|
|
190
|
-
.sign(fundingPrivateKey)
|
|
191
|
-
|
|
192
|
-
console.log(chalk.blue(` Funding Transaction: ${fundingTx.id}`))
|
|
193
|
-
console.log(chalk.blue(` Contract Address: ${this.address.toString()}`))
|
|
194
|
-
console.log(chalk.blue(` Funded Amount: ${fundingUtxo.satoshis - 1000} satoshis`))
|
|
195
|
-
|
|
196
|
-
// Return deployment info
|
|
197
|
-
return {
|
|
198
|
-
transaction: fundingTx,
|
|
199
|
-
contractUtxo: {
|
|
200
|
-
txid: fundingTx.id,
|
|
201
|
-
vout: 0,
|
|
202
|
-
satoshis: fundingUtxo.satoshis - 1000,
|
|
203
|
-
script: this.script.toHex(),
|
|
204
|
-
address: this.address.toString()
|
|
205
|
-
},
|
|
206
|
-
broadcastReady: true
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
} catch (error) {
|
|
210
|
-
console.log(chalk.red('ā Deployment error:', error.message))
|
|
211
|
-
throw error
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* SOLUTION: Spend from contract
|
|
217
|
-
*/
|
|
218
|
-
spendFromContract(contractUtxo, recipientAddress, amount) {
|
|
219
|
-
console.log(chalk.cyan('\nšø Spending from Smart Contract'))
|
|
220
|
-
console.log(chalk.cyan('==============================='))
|
|
221
|
-
|
|
222
|
-
try {
|
|
223
|
-
// Create spending transaction
|
|
224
|
-
const spendingTx = new bsv.Transaction()
|
|
225
|
-
.from({
|
|
226
|
-
txId: contractUtxo.txid,
|
|
227
|
-
outputIndex: contractUtxo.vout,
|
|
228
|
-
script: contractUtxo.script,
|
|
229
|
-
satoshis: contractUtxo.satoshis
|
|
230
|
-
})
|
|
231
|
-
.to(recipientAddress, amount)
|
|
232
|
-
|
|
233
|
-
// Generate preimage for this specific spending
|
|
234
|
-
const preimage = bsv.Transaction.sighash.sighashPreimage(
|
|
235
|
-
spendingTx,
|
|
236
|
-
bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID,
|
|
237
|
-
0,
|
|
238
|
-
this.script,
|
|
239
|
-
new bsv.crypto.BN(contractUtxo.satoshis)
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
// Validate the preimage will be accepted
|
|
243
|
-
const validation = this.validatePayment(preimage.toString('hex'))
|
|
244
|
-
if (!validation.success) {
|
|
245
|
-
throw new Error(`Contract validation failed: ${validation.error}`)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Set unlocking script with preimage
|
|
249
|
-
const unlockingScript = new bsv.Script().add(preimage)
|
|
250
|
-
spendingTx.inputs[0].setScript(unlockingScript)
|
|
251
|
-
|
|
252
|
-
console.log(chalk.green('ā
Spending transaction created and validated'))
|
|
253
|
-
console.log(chalk.blue(` Transaction ID: ${spendingTx.id}`))
|
|
254
|
-
console.log(chalk.blue(` Amount: ${amount} satoshis to ${recipientAddress}`))
|
|
255
|
-
|
|
256
|
-
return spendingTx
|
|
257
|
-
|
|
258
|
-
} catch (error) {
|
|
259
|
-
console.log(chalk.red('ā Spending error:', error.message))
|
|
260
|
-
throw error
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* SOLUTION: Multi-field validation example
|
|
267
|
-
*/
|
|
268
|
-
class MultiFieldWorkingContract {
|
|
269
|
-
constructor(params) {
|
|
270
|
-
this.params = params
|
|
271
|
-
|
|
272
|
-
// For multi-condition contracts
|
|
273
|
-
this.covenantConfig = bsv.SmartContract.createQuickCovenant('multi_condition', {
|
|
274
|
-
conditions: [
|
|
275
|
-
{ type: 'value', value: params.minAmount }
|
|
276
|
-
]
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
this.script = bsv.Script.fromASM(this.covenantConfig.asm)
|
|
280
|
-
this.address = bsv.SmartContract.utils.createCovenantAddress(this.script)
|
|
281
|
-
|
|
282
|
-
console.log(chalk.green(`ā
Multi-Field Contract Created`))
|
|
283
|
-
console.log(chalk.blue(` Min Amount: ${params.minAmount} satoshis`))
|
|
284
|
-
console.log(chalk.blue(` Address: ${this.address.toString()}`))
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Main demonstration
|
|
290
|
-
*/
|
|
291
|
-
async function main() {
|
|
292
|
-
try {
|
|
293
|
-
console.log(chalk.bold.blue('šÆ FINAL WORKING SMART CONTRACT SOLUTION'))
|
|
294
|
-
console.log(chalk.blue('========================================\n'))
|
|
295
|
-
|
|
296
|
-
// Create working smart contract
|
|
297
|
-
const contract = new WorkingSmartContract(100000) // 100,000 sats minimum
|
|
298
|
-
|
|
299
|
-
// Run complete demo
|
|
300
|
-
const success = contract.runWorkingDemo()
|
|
301
|
-
|
|
302
|
-
if (success) {
|
|
303
|
-
console.log(chalk.bold.green('\nš SUCCESS! Smart Contract is working correctly!'))
|
|
304
|
-
|
|
305
|
-
console.log(chalk.bold.yellow('\nš Contract Summary:'))
|
|
306
|
-
console.log(chalk.yellow(`⢠Expected Amount: ${contract.expectedAmount} satoshis`))
|
|
307
|
-
console.log(chalk.yellow(`⢠Contract Address: ${contract.address.toString()}`))
|
|
308
|
-
console.log(chalk.yellow(`⢠Script Size: ${contract.script.toBuffer().length} bytes`))
|
|
309
|
-
|
|
310
|
-
console.log(chalk.bold.yellow('\nš Ready for Production:'))
|
|
311
|
-
console.log(chalk.yellow('1. Fund the contract address with Bitcoin'))
|
|
312
|
-
console.log(chalk.yellow('2. Create spending transactions that meet the contract conditions'))
|
|
313
|
-
console.log(chalk.yellow('3. Include preimage in unlocking scripts'))
|
|
314
|
-
console.log(chalk.yellow('4. Broadcast to BSV network'))
|
|
315
|
-
|
|
316
|
-
console.log(chalk.bold.yellow('\nš” Key Insights:'))
|
|
317
|
-
console.log(chalk.yellow('⢠Use bsv.SmartContract.createQuickCovenant() for reliable contracts'))
|
|
318
|
-
console.log(chalk.yellow('⢠Convert ASM to Script with bsv.Script.fromASM()'))
|
|
319
|
-
console.log(chalk.yellow('⢠Generate proper preimages with correct transaction structure'))
|
|
320
|
-
console.log(chalk.yellow('⢠Test validation before broadcasting'))
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
} catch (error) {
|
|
324
|
-
console.error(chalk.red('ā Demo error:', error.message))
|
|
325
|
-
console.error(error.stack)
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// Export the working classes
|
|
330
|
-
module.exports = {
|
|
331
|
-
WorkingSmartContract,
|
|
332
|
-
MultiFieldWorkingContract
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// Run if executed directly
|
|
336
|
-
if (require.main === module) {
|
|
337
|
-
main().catch(console.error)
|
|
338
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
# Legacy sanity scripts
|
|
2
|
-
|
|
3
|
-
Pre-mocha standalone scripts kept here for historical reference. They are not
|
|
4
|
-
part of the test suite (no `describe`/`it`). Run with:
|
|
5
|
-
|
|
6
|
-
```bash
|
|
7
|
-
node examples/legacy/<file>.js
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
`smart_contract_test_integration.js` was previously in `lib/smart_contract/`
|
|
11
|
-
but is an integration script (calls `process.exit`), not library code.
|