smartledger-bsv 3.0.1 → 3.0.2

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.
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * šŸ”§ BSV Working Script Validation Example
5
+ *
6
+ * Creates a transaction with properly signed inputs that will pass
7
+ * the BSV script interpreter validation.
8
+ */
9
+
10
+ const bsv = require('../index.js');
11
+ const { acceptTransaction } = require('./miner-simulator');
12
+ const { loadConfig } = require('./utxo-manager');
13
+
14
+ /**
15
+ * Create a properly signed transaction that should pass script validation
16
+ */
17
+ function createValidTransaction() {
18
+ console.log('šŸ”§ Creating Valid BSV Transaction');
19
+ console.log('═'.repeat(80));
20
+
21
+ try {
22
+ // Load wallet config
23
+ const config = loadConfig();
24
+ const wallet = config.wallet;
25
+ const utxo = config.utxo;
26
+
27
+ // Create recipient
28
+ const recipientKey = new bsv.PrivateKey();
29
+ const recipientAddress = recipientKey.toAddress();
30
+
31
+ console.log('šŸ“‹ Transaction Details:');
32
+ console.log(`šŸ‘› From: ${wallet.address}`);
33
+ console.log(`šŸŽÆ To: ${recipientAddress}`);
34
+ console.log(`šŸ’° Amount: 20,000 satoshis`);
35
+ console.log(`šŸ’³ UTXO: ${utxo.txid}:${utxo.vout} (${utxo.satoshis} sats)`);
36
+
37
+ // Create the transaction step by step
38
+ const tx = new bsv.Transaction();
39
+
40
+ // Add input
41
+ tx.from({
42
+ txid: utxo.txid,
43
+ vout: utxo.vout,
44
+ scriptPubKey: utxo.script,
45
+ satoshis: utxo.satoshis
46
+ });
47
+
48
+ // Add outputs
49
+ tx.to(recipientAddress, 20000);
50
+ tx.change(wallet.address);
51
+ tx.fee(1000);
52
+
53
+ console.log('\nšŸ” Signing transaction...');
54
+ console.log(`Private Key: ${wallet.privateKeyWIF}`);
55
+ console.log(`Input Script (before): ${tx.inputs[0].script ? tx.inputs[0].script.toHex() : 'empty'}`);
56
+
57
+ // Sign with the correct private key and signature type
58
+ const privateKey = bsv.PrivateKey.fromWIF(wallet.privateKeyWIF);
59
+
60
+ // Sign with SIGHASH_ALL | SIGHASH_FORKID
61
+ const sigType = bsv.crypto.Signature.SIGHASH_ALL | bsv.crypto.Signature.SIGHASH_FORKID;
62
+ tx.sign(privateKey, sigType);
63
+
64
+ console.log(`Input Script (after): ${tx.inputs[0].script.toHex()}`);
65
+ console.log(`Script ASM: ${tx.inputs[0].script.toASM()}`);
66
+
67
+ console.log('\nāœ… Transaction signed successfully');
68
+ console.log(`šŸ†” Transaction ID: ${tx.id}`);
69
+ console.log(`šŸ“¦ Raw Hex: ${tx.toString()}`);
70
+
71
+ // Verify the signature locally first
72
+ console.log('\nšŸ” Local signature verification:');
73
+ try {
74
+ const verified = tx.verify();
75
+ console.log(`Local verification: ${verified ? 'āœ… VALID' : 'āŒ INVALID'}`);
76
+ } catch (error) {
77
+ console.log(`Local verification error: ${error.message}`);
78
+ }
79
+
80
+ return tx;
81
+
82
+ } catch (error) {
83
+ console.error('āŒ Error creating transaction:', error.message);
84
+ return null;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Test the transaction with our miner
90
+ */
91
+ function testWithMiner() {
92
+ console.log('\n' + '═'.repeat(80));
93
+ console.log('šŸŽÆ Testing with BSV Script Interpreter Miner');
94
+ console.log('═'.repeat(80));
95
+
96
+ const tx = createValidTransaction();
97
+
98
+ if (!tx) {
99
+ console.log('āŒ Failed to create transaction');
100
+ return;
101
+ }
102
+
103
+ // Test with full script validation
104
+ console.log('\nšŸ“” Sending to miner with full BSV script validation...');
105
+ const result = acceptTransaction(tx);
106
+
107
+ if (result.accepted) {
108
+ console.log('\nšŸŽ‰ SUCCESS! Transaction accepted by BSV script interpreter!');
109
+ console.log(`āœ… TXID: ${result.txid}`);
110
+ } else {
111
+ console.log('\nāŒ Transaction rejected');
112
+ console.log('Errors:', result.errors);
113
+ }
114
+
115
+ return result;
116
+ }
117
+
118
+ /**
119
+ * Debug signature creation process
120
+ */
121
+ function debugSignatureCreation() {
122
+ console.log('\n' + '═'.repeat(80));
123
+ console.log('šŸ” Debugging Signature Creation');
124
+ console.log('═'.repeat(80));
125
+
126
+ try {
127
+ const config = loadConfig();
128
+ const wallet = config.wallet;
129
+ const utxo = config.utxo;
130
+
131
+ console.log('šŸ”‘ Wallet Info:');
132
+ console.log(`Address: ${wallet.address}`);
133
+ console.log(`Private Key: ${wallet.privateKeyWIF}`);
134
+ console.log(`Public Key: ${wallet.publicKey}`);
135
+
136
+ console.log('\nšŸ’° UTXO Info:');
137
+ console.log(`TXID: ${utxo.txid}`);
138
+ console.log(`Vout: ${utxo.vout}`);
139
+ console.log(`Value: ${utxo.satoshis} satoshis`);
140
+ console.log(`Script: ${utxo.script}`);
141
+
142
+ // Parse the script
143
+ const script = bsv.Script.fromHex(utxo.script);
144
+ console.log(`Script ASM: ${script.toASM()}`);
145
+
146
+ // Verify the address matches
147
+ const scriptAddress = script.toAddress();
148
+ console.log(`Script Address: ${scriptAddress}`);
149
+ console.log(`Wallet Address: ${wallet.address}`);
150
+ console.log(`Addresses match: ${scriptAddress.toString() === wallet.address ? 'āœ…' : 'āŒ'}`);
151
+
152
+ } catch (error) {
153
+ console.error('āŒ Debug error:', error.message);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Run all tests
159
+ */
160
+ function runTests() {
161
+ debugSignatureCreation();
162
+ const result = testWithMiner();
163
+
164
+ if (result && result.accepted) {
165
+ console.log('\nšŸŽÆ Perfect! The BSV script interpreter accepted our transaction!');
166
+ } else {
167
+ console.log('\nšŸ”§ Need to fix signature creation for script interpreter...');
168
+ }
169
+ }
170
+
171
+ // Run tests if called directly
172
+ if (require.main === module) {
173
+ runTests();
174
+ }
175
+
176
+ module.exports = {
177
+ createValidTransaction,
178
+ testWithMiner,
179
+ debugSignatureCreation,
180
+ runTests
181
+ };
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * SmartLedger-BSV Signature Verification Fix Validation
5
+ *
6
+ * This script validates that the signature verification bug has been fixed
7
+ */
8
+
9
+ const bsv = require('./index.js');
10
+
11
+ console.log('šŸŽÆ SmartLedger-BSV Signature Verification Fix Validation');
12
+ console.log('=======================================================\n');
13
+
14
+ // Test with multiple different signatures
15
+ const testCases = [
16
+ {
17
+ name: 'Test Case 1: hello world',
18
+ privateKey: 'L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ',
19
+ message: 'hello world'
20
+ },
21
+ {
22
+ name: 'Test Case 2: BSV transaction',
23
+ privateKey: 'KzsjKq2FVqVuQv2ueHVFuB65A9uEZ6S1E6hrhGCi5LAFS9kBf7Nm',
24
+ message: 'BSV blockchain transaction data'
25
+ },
26
+ {
27
+ name: 'Test Case 3: empty message',
28
+ privateKey: 'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
29
+ message: ''
30
+ }
31
+ ];
32
+
33
+ let passedTests = 0;
34
+ let totalTests = 0;
35
+
36
+ for (const testCase of testCases) {
37
+ console.log(`\nšŸ“ ${testCase.name}:`);
38
+ console.log('='.repeat(testCase.name.length + 4));
39
+
40
+ try {
41
+ const privateKey = new bsv.PrivateKey(testCase.privateKey);
42
+ const publicKey = privateKey.publicKey;
43
+ const message = Buffer.from(testCase.message, 'utf8');
44
+ const hash = bsv.crypto.Hash.sha256(message);
45
+
46
+ // Create signature
47
+ const signature = bsv.crypto.ECDSA.sign(hash, privateKey);
48
+ const derSig = signature.toDER();
49
+ const canonicalSig = signature.toCanonical();
50
+ const canonicalDer = canonicalSig.toDER();
51
+
52
+ console.log(`Private Key: ${testCase.privateKey}`);
53
+ console.log(`Message: "${testCase.message}"`);
54
+ console.log(`Signature canonical: ${signature.isCanonical()}`);
55
+
56
+ // Test all verification methods
57
+ const tests = [
58
+ { name: 'ECDSA.verify(hash, derSig, publicKey)', result: bsv.crypto.ECDSA.verify(hash, derSig, publicKey) },
59
+ { name: 'ECDSA.verify(hash, canonicalDer, publicKey)', result: bsv.crypto.ECDSA.verify(hash, canonicalDer, publicKey) },
60
+ { name: 'ECDSA.verify(hash, signature, publicKey)', result: bsv.crypto.ECDSA.verify(hash, signature, publicKey) },
61
+ { name: 'SmartVerify.smartVerify(hash, derSig, publicKey)', result: bsv.SmartVerify.smartVerify(hash, derSig, publicKey) },
62
+ { name: 'SmartVerify.smartVerify(hash, canonicalDer, publicKey)', result: bsv.SmartVerify.smartVerify(hash, canonicalDer, publicKey) },
63
+ { name: 'SmartVerify.isCanonical(derSig)', result: bsv.SmartVerify.isCanonical(derSig) },
64
+ { name: 'SmartVerify.isCanonical(canonicalDer)', result: bsv.SmartVerify.isCanonical(canonicalDer) }
65
+ ];
66
+
67
+ for (const test of tests) {
68
+ totalTests++;
69
+ const status = test.result ? 'āœ… PASS' : 'āŒ FAIL';
70
+ console.log(`${status} ${test.name}: ${test.result}`);
71
+ if (test.result) passedTests++;
72
+ }
73
+
74
+ } catch (error) {
75
+ console.log(`āŒ Test failed with error: ${error.message}`);
76
+ }
77
+ }
78
+
79
+ console.log('\nšŸ† Summary:');
80
+ console.log('===========');
81
+ console.log(`Passed: ${passedTests}/${totalTests} tests`);
82
+ console.log(`Success rate: ${((passedTests / totalTests) * 100).toFixed(1)}%`);
83
+
84
+ if (passedTests === totalTests) {
85
+ console.log('\nšŸŽ‰ ALL TESTS PASSED! Signature verification is now working correctly.');
86
+ console.log('āœ… The SmartLedger-BSV v3.0.1 signature verification bug has been FIXED!');
87
+ } else {
88
+ console.log('\nāš ļø Some tests failed. The fix may not be complete.');
89
+ }
90
+
91
+ console.log('\nšŸ“‹ Fix Summary:');
92
+ console.log('===============');
93
+ console.log('1. Fixed ECDSA.set() to auto-parse DER buffers to Signature objects');
94
+ console.log('2. Fixed ECDSA.sigError() to handle canonical signature verification properly');
95
+ console.log('3. Fixed SmartVerify.smartVerify() to accept both DER buffers and Signature objects');
96
+ console.log('4. Fixed SmartVerify.isCanonical() to parse DER buffers correctly');
97
+ console.log('5. Enhanced compatibility with both canonical and non-canonical signature inputs');