smartledger-bsv 3.4.0 → 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.
Files changed (117) hide show
  1. package/CHANGELOG.md +400 -0
  2. package/README.md +112 -84
  3. package/SECURITY.md +88 -0
  4. package/bin/cli.js +13 -8
  5. package/bsv-anchor.min.js +12 -0
  6. package/bsv-covenant.min.js +8 -8
  7. package/bsv-didweb.min.js +12 -0
  8. package/bsv-gdaf.min.js +9 -9
  9. package/bsv-ltp.min.js +9 -9
  10. package/bsv-mnemonic.min.js +2 -2
  11. package/bsv-shamir.min.js +3 -3
  12. package/bsv-smartcontract.min.js +5 -5
  13. package/bsv-statuslist.min.js +18 -0
  14. package/bsv-vcjwt.min.js +12 -0
  15. package/bsv.bundle.js +9 -9
  16. package/bsv.d.ts +486 -9
  17. package/bsv.min.js +5 -5
  18. package/build/webpack.anchor.config.js +9 -13
  19. package/build/webpack.didweb.config.js +10 -14
  20. package/build/webpack.statuslist.config.js +9 -14
  21. package/build/webpack.vcjwt.config.js +9 -13
  22. package/docs/COVENANT_DEVELOPMENT_RESOLVED.md +2 -2
  23. package/docs/MODULE_REFERENCE_COMPLETE.md +61 -58
  24. package/docs/advanced/LEGAL_TOKEN_PROTOCOL.md +3 -3
  25. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  26. package/docs/getting-started/INSTALLATION.md +30 -30
  27. package/docs/getting-started/QUICK_START.md +18 -18
  28. package/docs/migration/FROM_BSV_1_5_6.md +16 -10
  29. package/gdaf-entry.js +1 -2
  30. package/index.js +44 -13
  31. package/lib/browser-utxo-manager-es5.js +11 -4
  32. package/lib/browser-utxo-manager.js +15 -8
  33. package/lib/ltp/claim.js +1 -0
  34. package/lib/ltp/obligation.js +1 -0
  35. package/lib/ltp/registry.js +2 -0
  36. package/lib/ltp/right.js +1 -0
  37. package/lib/smart_contract/covenant.js +10 -1
  38. package/lib/smartutxo.js +20 -12
  39. package/lib/transaction/transaction.js +8 -1
  40. package/lib/util/_.js +7 -1
  41. package/ltp-entry.js +1 -2
  42. package/package.json +11 -13
  43. package/utilities/blockchain-state.js +32 -23
  44. package/demos/README.md +0 -188
  45. package/demos/architecture_demo.js +0 -247
  46. package/demos/browser-test.html +0 -1208
  47. package/demos/bsv_wallet_demo.js +0 -242
  48. package/demos/complete_ltp_demo.js +0 -511
  49. package/demos/debug_tools_demo.js +0 -87
  50. package/demos/demo_features.js +0 -123
  51. package/demos/easy_interface_demo.js +0 -109
  52. package/demos/ecies_demo.js +0 -182
  53. package/demos/gdaf_core_test.js +0 -131
  54. package/demos/gdaf_demo.js +0 -237
  55. package/demos/ltp_demo.js +0 -361
  56. package/demos/ltp_primitives_demo.js +0 -403
  57. package/demos/message_demo.js +0 -209
  58. package/demos/preimage_separation_demo.js +0 -383
  59. package/demos/script_helper_demo.js +0 -289
  60. package/demos/security_demo.js +0 -287
  61. package/demos/shamir_demo.js +0 -121
  62. package/demos/simple_demo.js +0 -204
  63. package/demos/simple_p2pkh_demo.js +0 -169
  64. package/demos/simple_utxo_preimage_demo.js +0 -196
  65. package/demos/smart_contract_demo.html +0 -1347
  66. package/demos/smart_contract_demo.js +0 -910
  67. package/demos/utxo_generator_demo.js +0 -244
  68. package/demos/validation_pipeline_demo.js +0 -155
  69. package/demos/web3keys.html +0 -740
  70. package/examples/README.md +0 -200
  71. package/examples/basic/transaction-creation.js +0 -534
  72. package/examples/basic/transaction_signature_api_gap.js +0 -178
  73. package/examples/complete_workflow_demo.js +0 -783
  74. package/examples/covenants/advanced_covenant_demo.js +0 -219
  75. package/examples/covenants/covenant_interface_demo.js +0 -270
  76. package/examples/covenants/covenant_manual_signature_resolved.js +0 -212
  77. package/examples/covenants/covenant_signature_template.js +0 -117
  78. package/examples/covenants2/covenant_bidirectional_example.js +0 -262
  79. package/examples/covenants2/covenant_utils_demo.js +0 -120
  80. package/examples/covenants2/preimage_covenant_utils.js +0 -287
  81. package/examples/covenants2/production_integration.js +0 -256
  82. package/examples/data/covenant_utxos.json +0 -28
  83. package/examples/data/utxos.json +0 -26
  84. package/examples/definitive_working_demo.js +0 -261
  85. package/examples/final_working_contracts.js +0 -338
  86. package/examples/preimage/README.md +0 -178
  87. package/examples/preimage/extract_preimage_bidirectional.js +0 -421
  88. package/examples/preimage/generate_sample_preimage.js +0 -208
  89. package/examples/preimage/generate_sighash_examples.js +0 -152
  90. package/examples/preimage/parse_preimage.js +0 -117
  91. package/examples/preimage/test_preimage_extractor.js +0 -53
  92. package/examples/preimage/test_varint_extraction.js +0 -95
  93. package/examples/scripts/custom_script_helper_example.js +0 -273
  94. package/examples/scripts/custom_script_signature_test.js +0 -344
  95. package/examples/scripts/script_interpreter.js +0 -193
  96. package/examples/smart_contract/complete_workflow_demo.js +0 -343
  97. package/examples/smart_contract/covenant_builder_demo.js +0 -176
  98. package/examples/smart_contract/script_testing_integration.js +0 -198
  99. package/examples/smart_contract_templates.js +0 -718
  100. package/examples/working_smart_contracts.js +0 -348
  101. package/lib/smart_contract/test_integration.js +0 -269
  102. package/tests/browser-compatibility/README.md +0 -35
  103. package/tests/browser-compatibility/test-cdn-vs-local.html +0 -186
  104. package/tests/browser-compatibility/test-pbkdf2.html +0 -51
  105. package/tests/bundle-completeness-test.html +0 -131
  106. package/tests/bundle-demo.html +0 -476
  107. package/tests/smartcontract-test.html +0 -239
  108. package/tests/standalone-modules-test.html +0 -260
  109. package/tests/test.html +0 -612
  110. package/tests/test_builtin_verify.js +0 -117
  111. package/tests/test_debug_integration.js +0 -71
  112. package/tests/test_ecdsa_little.js +0 -70
  113. package/tests/test_shamir.js +0 -221
  114. package/tests/test_smartverify_der.js +0 -110
  115. package/tests/test_standalone_shamir.html +0 -83
  116. package/tests/unpkg-demo.html +0 -194
  117. package/utilities/blockchain-state.json +0 -118565
@@ -1,117 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test Transaction VerifySignature Method
5
- *
6
- * Tests the transaction's built-in verifySignature method
7
- */
8
-
9
- const bsv = require('./index.js');
10
-
11
- console.log('🔍 Test Transaction VerifySignature Method');
12
- console.log('==========================================\n');
13
-
14
- const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
15
- const address = privateKey.toAddress().toString();
16
-
17
- // Create and sign transaction
18
- const mockUTXO = {
19
- txid: '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
20
- vout: 0,
21
- address: address,
22
- satoshis: 50000,
23
- script: bsv.Script.buildPublicKeyHashOut(address).toHex()
24
- };
25
-
26
- const transaction = new bsv.Transaction()
27
- .from(mockUTXO)
28
- .to(address, 100)
29
- .change(address)
30
- .feePerKb(10)
31
- .sign(privateKey);
32
-
33
- console.log('✅ Transaction created and signed');
34
- console.log(`Transaction ID: ${transaction.id}`);
35
- console.log(`Basic verify(): ${transaction.verify()}`);
36
-
37
- // Extract signature details
38
- const input = transaction.inputs[0];
39
- const signature = input.script.chunks[0]?.buf;
40
- const publicKey = input.script.chunks[1]?.buf;
41
-
42
- if (signature && publicKey) {
43
- console.log('\n🧪 Transaction VerifySignature Method Test:');
44
- console.log('==========================================');
45
-
46
- const sigWithoutHashtype = signature.slice(0, -1);
47
- const pubkeyObj = new bsv.PublicKey(publicKey);
48
- const subscript = input.output.script;
49
- const satoshisBN = new bsv.crypto.BN(input.output.satoshis);
50
-
51
- try {
52
- // Parse the signature first
53
- const parsedSig = bsv.crypto.Signature.fromDER(sigWithoutHashtype);
54
- parsedSig.nhashtype = signature[signature.length - 1]; // Set the hashtype
55
-
56
- console.log(`Parsed signature r: ${parsedSig.r.toString('hex')}`);
57
- console.log(`Parsed signature s: ${parsedSig.s.toString('hex')}`);
58
- console.log(`Signature hashtype: 0x${parsedSig.nhashtype.toString(16)}`);
59
-
60
- // Test transaction's built-in verifySignature method
61
- const builtinVerify = transaction.verifySignature(parsedSig, pubkeyObj, 0, subscript, satoshisBN);
62
- console.log(`transaction.verifySignature(): ${builtinVerify ? '✅ VALID' : '❌ INVALID'}`);
63
-
64
- // Test with default flags
65
- const builtinVerifyWithFlags = transaction.verifySignature(parsedSig, pubkeyObj, 0, subscript, satoshisBN,
66
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID);
67
- console.log(`transaction.verifySignature() with flags: ${builtinVerifyWithFlags ? '✅ VALID' : '❌ INVALID'}`);
68
-
69
- console.log('\n🔍 Manual Sighash.verify Test:');
70
- console.log('==============================');
71
-
72
- // Test the Sighash.verify method directly
73
- const sighashVerify = bsv.Transaction.Sighash.verify(transaction, parsedSig, pubkeyObj, 0, subscript, satoshisBN);
74
- console.log(`Sighash.verify(): ${sighashVerify ? '✅ VALID' : '❌ INVALID'}`);
75
-
76
- const sighashVerifyWithFlags = bsv.Transaction.Sighash.verify(transaction, parsedSig, pubkeyObj, 0, subscript, satoshisBN,
77
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID);
78
- console.log(`Sighash.verify() with flags: ${sighashVerifyWithFlags ? '✅ VALID' : '❌ INVALID'}`);
79
-
80
- console.log('\n🔧 Compare Sighash Calculation:');
81
- console.log('===============================');
82
-
83
- // Get the sighash that Sighash.verify would calculate
84
- const officialSighash = bsv.Transaction.Sighash.sighash(
85
- transaction,
86
- parsedSig.nhashtype,
87
- 0,
88
- subscript,
89
- satoshisBN,
90
- bsv.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID
91
- );
92
-
93
- console.log(`Official Sighash.sighash(): ${officialSighash.toString('hex')}`);
94
-
95
- // Compare with our transaction.sighash method
96
- const ourSighash = transaction.sighash(0, parsedSig.nhashtype, subscript, satoshisBN);
97
- console.log(`Our transaction.sighash(): ${ourSighash.toString('hex')}`);
98
- console.log(`Sighashes match: ${officialSighash.equals(ourSighash) ? '✅ YES' : '❌ NO'}`);
99
-
100
- // Test verification with the official sighash
101
- console.log('\n🎯 Verification with Official Sighash:');
102
- console.log('=====================================');
103
-
104
- const ecdsaWithOfficial = bsv.crypto.ECDSA.verify(officialSighash, parsedSig, pubkeyObj);
105
- console.log(`ECDSA.verify(officialSighash): ${ecdsaWithOfficial ? '✅ VALID' : '❌ INVALID'}`);
106
-
107
- const smartVerifyWithOfficial = bsv.SmartVerify.smartVerify(officialSighash, parsedSig, pubkeyObj);
108
- console.log(`SmartVerify(officialSighash): ${smartVerifyWithOfficial ? '✅ VALID' : '❌ INVALID'}`);
109
-
110
- } catch (error) {
111
- console.log('❌ Test failed:', error.message);
112
- console.log('Stack:', error.stack);
113
- }
114
-
115
- } else {
116
- console.log('❌ Could not extract signature or public key');
117
- }
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Test Script for Debug Tools Integration
4
- * =======================================
5
- * Tests the newly integrated StackExaminer and ScriptInterpreter tools
6
- * in the SmartContract interface
7
- */
8
-
9
- const SmartContract = require('./lib/smart_contract/index.js');
10
-
11
- console.log("🧪 Testing SmartContract Debug Tools Integration");
12
- console.log("=================================================\n");
13
-
14
- // Test 1: Check if modules are loaded
15
- console.log("1. Module Loading Test:");
16
- console.log(" StackExaminer loaded:", !!SmartContract.StackExaminer);
17
- console.log(" ScriptInterpreter loaded:", !!SmartContract.ScriptInterpreter);
18
- console.log(" examineStack method:", typeof SmartContract.examineStack);
19
- console.log(" debugScriptExecution method:", typeof SmartContract.debugScriptExecution);
20
- console.log(" parseScript method:", typeof SmartContract.parseScript);
21
-
22
- // Test 2: Feature flags
23
- console.log("\n2. Feature Flags Test:");
24
- console.log(" STACK_EXAMINATION:", SmartContract.features.STACK_EXAMINATION);
25
- console.log(" SCRIPT_DEBUGGING:", SmartContract.features.SCRIPT_DEBUGGING);
26
- console.log(" STEP_BY_STEP_EXECUTION:", SmartContract.features.STEP_BY_STEP_EXECUTION);
27
- console.log(" INTERACTIVE_DEBUGGING:", SmartContract.features.INTERACTIVE_DEBUGGING);
28
-
29
- // Test 3: Simple script parsing
30
- console.log("\n3. Script Parsing Test:");
31
- try {
32
- const parsedASM = SmartContract.parseScript("OP_DUP OP_HASH160");
33
- console.log(" ✅ ASM parsing successful");
34
- console.log(" Script length:", parsedASM.chunks.length);
35
- } catch (err) {
36
- console.log(" ❌ ASM parsing failed:", err.message);
37
- }
38
-
39
- try {
40
- const parsedHex = SmartContract.parseScript("76a914");
41
- console.log(" ✅ HEX parsing successful");
42
- console.log(" Script length:", parsedHex.chunks.length);
43
- } catch (err) {
44
- console.log(" ❌ HEX parsing failed:", err.message);
45
- }
46
-
47
- // Test 4: Stack examination (non-interactive)
48
- console.log("\n4. Stack Examination Test:");
49
- try {
50
- // Use simple valid scripts for testing
51
- const result = SmartContract.examineStack("51", "51"); // OP_1, OP_1
52
- console.log(" ✅ Stack examination completed, result:", result);
53
- } catch (err) {
54
- console.log(" ⚠️ Stack examination had issues:", err.message);
55
- }
56
-
57
- console.log("\n=================================================");
58
- console.log("🎉 Debug Tools Integration Test Complete!");
59
- console.log("=================================================");
60
-
61
- // Count total methods in SmartContract
62
- const methodCount = Object.keys(SmartContract).filter(key =>
63
- typeof SmartContract[key] === 'function'
64
- ).length;
65
-
66
- console.log(`\n📊 SmartContract Interface Summary:`);
67
- console.log(` Total Methods: ${methodCount}`);
68
- console.log(` Total Modules: ${Object.keys(SmartContract).filter(key =>
69
- typeof SmartContract[key] === 'object' && SmartContract[key].constructor.name !== 'Object'
70
- ).length}`);
71
- console.log(` Feature Flags: ${Object.keys(SmartContract.features).length}`);
@@ -1,70 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test ECDSA.verify with 'little' endianness
5
- *
6
- * Tests if adding 'little' parameter fixes ECDSA.verify too
7
- */
8
-
9
- const bsv = require('./index.js');
10
-
11
- console.log('🔍 Test ECDSA.verify with little endianness');
12
- console.log('============================================\n');
13
-
14
- // Simple signature test
15
- const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
16
- const publicKey = privateKey.publicKey;
17
- const message = Buffer.from('hello world', 'utf8');
18
- const hash = bsv.crypto.Hash.sha256(message);
19
-
20
- console.log('Creating signature...');
21
- const signature = bsv.crypto.ECDSA.sign(hash, privateKey);
22
- const derSig = signature.toDER();
23
-
24
- console.log('\n🧪 ECDSA.verify Test Matrix:');
25
- console.log('============================');
26
-
27
- // Test without 'little'
28
- const ecdsaWithoutLittle = bsv.crypto.ECDSA.verify(hash, signature, publicKey);
29
- console.log(`ECDSA.verify(hash, sig, pubkey): ${ecdsaWithoutLittle ? '✅ VALID' : '❌ INVALID'}`);
30
-
31
- // Test with 'little'
32
- const ecdsaWithLittle = bsv.crypto.ECDSA.verify(hash, signature, publicKey, 'little');
33
- console.log(`ECDSA.verify(hash, sig, pubkey, 'little'): ${ecdsaWithLittle ? '✅ VALID' : '❌ INVALID'}`);
34
-
35
- // Test with DER buffer without 'little'
36
- const ecdsaDerWithoutLittle = bsv.crypto.ECDSA.verify(hash, derSig, publicKey);
37
- console.log(`ECDSA.verify(hash, derSig, pubkey): ${ecdsaDerWithoutLittle ? '✅ VALID' : '❌ INVALID'}`);
38
-
39
- // Test with DER buffer with 'little'
40
- const ecdsaDerWithLittle = bsv.crypto.ECDSA.verify(hash, derSig, publicKey, 'little');
41
- console.log(`ECDSA.verify(hash, derSig, pubkey, 'little'): ${ecdsaDerWithLittle ? '✅ VALID' : '❌ INVALID'}`);
42
-
43
- console.log('\n🔍 Compare with SmartVerify:');
44
- console.log('============================');
45
-
46
- // Test SmartVerify
47
- const smartVerifyObj = bsv.SmartVerify.smartVerify(hash, signature, publicKey);
48
- console.log(`SmartVerify(hash, sig, pubkey): ${smartVerifyObj ? '✅ VALID' : '❌ INVALID'}`);
49
-
50
- const smartVerifyDer = bsv.SmartVerify.smartVerify(hash, derSig, publicKey);
51
- console.log(`SmartVerify(hash, derSig, pubkey): ${smartVerifyDer ? '✅ VALID' : '❌ INVALID'}`);
52
-
53
- console.log('\n🔧 Signature Details:');
54
- console.log('=====================');
55
- console.log(`Signature r: ${signature.r.toString('hex')}`);
56
- console.log(`Signature s: ${signature.s.toString('hex')}`);
57
- console.log(`Is canonical: ${signature.isCanonical()}`);
58
-
59
- // Check if this is a canonicalization issue
60
- const canonicalSig = signature.toCanonical();
61
- console.log(`Canonical r: ${canonicalSig.r.toString('hex')}`);
62
- console.log(`Canonical s: ${canonicalSig.s.toString('hex')}`);
63
- console.log(`Same as original: ${signature.r.eq(canonicalSig.r) && signature.s.eq(canonicalSig.s)}`);
64
-
65
- // Test with canonical signature
66
- const ecdsaCanonical = bsv.crypto.ECDSA.verify(hash, canonicalSig, publicKey, 'little');
67
- console.log(`ECDSA.verify(hash, canonicalSig, pubkey, 'little'): ${ecdsaCanonical ? '✅ VALID' : '❌ INVALID'}`);
68
-
69
- const smartVerifyCanonical = bsv.SmartVerify.smartVerify(hash, canonicalSig, publicKey);
70
- console.log(`SmartVerify(hash, canonicalSig, pubkey): ${smartVerifyCanonical ? '✅ VALID' : '❌ INVALID'}`);
@@ -1,221 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * BSV Shamir Secret Sharing Test
5
- * Comprehensive test of the Shamir Secret Sharing implementation
6
- */
7
-
8
- 'use strict'
9
-
10
- console.log('=== BSV Shamir Secret Sharing Test ===\n')
11
-
12
- // Load the BSV library with Shamir support
13
- var bsv
14
- try {
15
- bsv = require('./index.js')
16
- console.log('✓ Loaded BSV library with Shamir support')
17
- } catch (e) {
18
- console.error('✗ Failed to load BSV library:', e.message)
19
- process.exit(1)
20
- }
21
-
22
- // Test 1: Basic functionality test
23
- console.log('\n--- Test 1: Basic Secret Sharing ---')
24
- try {
25
- var secret = 'Hello, Bitcoin SV!'
26
- var threshold = 3
27
- var shares = 5
28
-
29
- console.log('Original secret:', secret)
30
- console.log('Threshold:', threshold, '/ Total shares:', shares)
31
-
32
- // Split the secret
33
- var splitShares = bsv.Shamir.split(secret, threshold, shares)
34
- console.log('✓ Successfully split secret into', splitShares.length, 'shares')
35
-
36
- // Reconstruct with minimum shares
37
- var reconstructed = bsv.Shamir.combine(splitShares.slice(0, threshold))
38
- var reconstructedSecret = reconstructed.toString('utf8')
39
-
40
- console.log('Reconstructed:', reconstructedSecret)
41
-
42
- if (reconstructedSecret === secret) {
43
- console.log('✓ Test 1 PASSED: Secret correctly reconstructed')
44
- } else {
45
- console.log('✗ Test 1 FAILED: Secret reconstruction failed')
46
- }
47
- } catch (e) {
48
- console.log('✗ Test 1 ERROR:', e.message)
49
- }
50
-
51
- // Test 2: Larger secret test
52
- console.log('\n--- Test 2: Large Secret Test ---')
53
- try {
54
- var largeSecret = 'This is a much longer secret that will test the chunking functionality of the Shamir Secret Sharing implementation. It should handle secrets of arbitrary length by breaking them into manageable chunks and processing each chunk separately.'
55
- var threshold2 = 4
56
- var shares2 = 7
57
-
58
- console.log('Large secret length:', largeSecret.length, 'characters')
59
-
60
- var splitShares2 = bsv.Shamir.split(largeSecret, threshold2, shares2)
61
- console.log('✓ Successfully split large secret')
62
-
63
- var reconstructed2 = bsv.Shamir.combine(splitShares2.slice(0, threshold2))
64
- var reconstructedSecret2 = reconstructed2.toString('utf8')
65
-
66
- if (reconstructedSecret2 === largeSecret) {
67
- console.log('✓ Test 2 PASSED: Large secret correctly reconstructed')
68
- } else {
69
- console.log('✗ Test 2 FAILED: Large secret reconstruction failed')
70
- console.log('Expected length:', largeSecret.length)
71
- console.log('Actual length:', reconstructedSecret2.length)
72
- console.log('First 50 chars expected:', largeSecret.substring(0, 50))
73
- console.log('First 50 chars actual :', reconstructedSecret2.substring(0, 50))
74
- console.log('Match:', largeSecret === reconstructedSecret2)
75
- }
76
- } catch (e) {
77
- console.log('✗ Test 2 ERROR:', e.message)
78
- }
79
-
80
- // Test 3: Different share combinations
81
- console.log('\n--- Test 3: Share Combination Test ---')
82
- try {
83
- var secret3 = 'Testing different combinations'
84
- var splitShares3 = bsv.Shamir.split(secret3, 3, 6)
85
-
86
- // Test different combinations of 3 shares
87
- var combinations = [
88
- [0, 1, 2], // First three
89
- [1, 3, 5], // Every other
90
- [2, 4, 5] // Last three + one
91
- ]
92
-
93
- var allPassed = true
94
- for (var i = 0; i < combinations.length; i++) {
95
- var combo = combinations[i]
96
- var testShares = combo.map(function(idx) { return splitShares3[idx] })
97
- var reconstructed3 = bsv.Shamir.combine(testShares)
98
- var result = reconstructed3.toString('utf8')
99
-
100
- if (result === secret3) {
101
- console.log('✓ Combination', combo, 'successful')
102
- } else {
103
- console.log('✗ Combination', combo, 'failed')
104
- allPassed = false
105
- }
106
- }
107
-
108
- if (allPassed) {
109
- console.log('✓ Test 3 PASSED: All share combinations work')
110
- } else {
111
- console.log('✗ Test 3 FAILED: Some combinations failed')
112
- }
113
- } catch (e) {
114
- console.log('✗ Test 3 ERROR:', e.message)
115
- }
116
-
117
- // Test 4: Share verification
118
- console.log('\n--- Test 4: Share Verification Test ---')
119
- try {
120
- var secret4 = 'Verification test'
121
- var splitShares4 = bsv.Shamir.split(secret4, 2, 4)
122
-
123
- var validCount = 0
124
- for (var j = 0; j < splitShares4.length; j++) {
125
- if (bsv.Shamir.verifyShare(splitShares4[j])) {
126
- validCount++
127
- }
128
- }
129
-
130
- if (validCount === splitShares4.length) {
131
- console.log('✓ Test 4 PASSED: All shares verified as valid')
132
- } else {
133
- console.log('✗ Test 4 FAILED: Only', validCount, 'of', splitShares4.length, 'shares valid')
134
- }
135
-
136
- // Test invalid share
137
- var invalidShare = { invalid: 'data' }
138
- if (!bsv.Shamir.verifyShare(invalidShare)) {
139
- console.log('✓ Invalid share correctly rejected')
140
- } else {
141
- console.log('✗ Invalid share incorrectly accepted')
142
- }
143
- } catch (e) {
144
- console.log('✗ Test 4 ERROR:', e.message)
145
- }
146
-
147
- // Test 5: Error handling
148
- console.log('\n--- Test 5: Error Handling Test ---')
149
- try {
150
- var errorTests = [
151
- function() { bsv.Shamir.split('', 2, 3) }, // Empty secret
152
- function() { bsv.Shamir.split('secret', 1, 3) }, // Threshold too low
153
- function() { bsv.Shamir.split('secret', 3, 2) }, // More threshold than shares
154
- function() { bsv.Shamir.combine([]) }, // Empty shares array
155
- function() { bsv.Shamir.combine([{id: 1, threshold: 2, shares: 3, length: 1, bytes: []}]) } // Insufficient shares
156
- ]
157
-
158
- var errorsPassed = 0
159
- for (var k = 0; k < errorTests.length; k++) {
160
- try {
161
- errorTests[k]()
162
- console.log('✗ Error test', k + 1, 'should have thrown an error')
163
- } catch (e) {
164
- console.log('✓ Error test', k + 1, 'correctly threw:', e.message)
165
- errorsPassed++
166
- }
167
- }
168
-
169
- if (errorsPassed === errorTests.length) {
170
- console.log('✓ Test 5 PASSED: All error conditions handled correctly')
171
- } else {
172
- console.log('✗ Test 5 FAILED: Some error conditions not handled')
173
- }
174
- } catch (e) {
175
- console.log('✗ Test 5 ERROR:', e.message)
176
- }
177
-
178
- // Test 6: Binary data test
179
- console.log('\n--- Test 6: Binary Data Test ---')
180
- try {
181
- var binaryData = Buffer.from([0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x80, 0x7F])
182
- var splitBinary = bsv.Shamir.split(binaryData, 2, 3)
183
- var reconstructedBinary = bsv.Shamir.combine(splitBinary.slice(0, 2))
184
-
185
- if (Buffer.compare(binaryData, reconstructedBinary) === 0) {
186
- console.log('✓ Test 6 PASSED: Binary data correctly handled')
187
- } else {
188
- console.log('✗ Test 6 FAILED: Binary data reconstruction failed')
189
- console.log('Original:', Array.from(binaryData))
190
- console.log('Reconstructed:', Array.from(reconstructedBinary))
191
- }
192
- } catch (e) {
193
- console.log('✗ Test 6 ERROR:', e.message)
194
- }
195
-
196
- // Test 7: Generate test vectors
197
- console.log('\n--- Test 7: Test Vectors Generation ---')
198
- try {
199
- var testVectors = bsv.Shamir.generateTestVectors()
200
-
201
- console.log('Test vectors generated:')
202
- console.log('- Secret:', testVectors.secret)
203
- console.log('- Threshold:', testVectors.threshold)
204
- console.log('- Total shares:', testVectors.totalShares)
205
- console.log('- Shares generated:', testVectors.shares.length)
206
- console.log('- Reconstruction successful:', testVectors.valid)
207
-
208
- if (testVectors.valid) {
209
- console.log('✓ Test 7 PASSED: Test vectors generated and validated')
210
- } else {
211
- console.log('✗ Test 7 FAILED: Test vectors validation failed')
212
- }
213
- } catch (e) {
214
- console.log('✗ Test 7 ERROR:', e.message)
215
- }
216
-
217
- console.log('\n=== Shamir Secret Sharing Tests Complete ===')
218
- console.log('💡 Integration successful! Shamir Secret Sharing is now available in:')
219
- console.log(' • Main library: bsv.Shamir or bsv.crypto.Shamir')
220
- console.log(' • Standalone: bsv-shamir.min.js (after build)')
221
- console.log(' • CDN ready: Use npm run build-shamir to generate minified version')
@@ -1,110 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test SmartVerify DER Buffer Handling
5
- *
6
- * Tests SmartVerify with different input types to isolate the DER parsing issue
7
- */
8
-
9
- const bsv = require('./index.js');
10
-
11
- console.log('🔍 Test SmartVerify DER Buffer Handling');
12
- console.log('=======================================\n');
13
-
14
- // Simple signature test like minimal reproduction
15
- const privateKey = new bsv.PrivateKey('L1aW4aubDFB7yfras2S1mN3bqg9nwySY8nkoLmJebSLD5BWv3ENZ');
16
- const publicKey = privateKey.publicKey;
17
- const message = Buffer.from('hello world', 'utf8');
18
- const hash = bsv.crypto.Hash.sha256(message);
19
-
20
- console.log('Creating signature...');
21
- const signature = bsv.crypto.ECDSA.sign(hash, privateKey);
22
- const derSig = signature.toDER();
23
-
24
- console.log(`Original signature r: ${signature.r.toString('hex')}`);
25
- console.log(`Original signature s: ${signature.s.toString('hex')}`);
26
- console.log(`DER buffer: ${derSig.toString('hex')}`);
27
- console.log(`DER length: ${derSig.length}`);
28
-
29
- console.log('\n🧪 SmartVerify Test Matrix:');
30
- console.log('===========================');
31
-
32
- // Test 1: Signature object
33
- const smartVerifyObj = bsv.SmartVerify.smartVerify(hash, signature, publicKey);
34
- console.log(`SmartVerify(hash, sigObject, pubkey): ${smartVerifyObj ? '✅ VALID' : '❌ INVALID'}`);
35
-
36
- // Test 2: DER buffer
37
- const smartVerifyBuffer = bsv.SmartVerify.smartVerify(hash, derSig, publicKey);
38
- console.log(`SmartVerify(hash, derBuffer, pubkey): ${smartVerifyBuffer ? '✅ VALID' : '❌ INVALID'}`);
39
-
40
- // Test 3: Manually parsed DER
41
- try {
42
- const parsedFromDER = bsv.crypto.Signature.fromDER(derSig);
43
- console.log(`Parsed from DER r: ${parsedFromDER.r.toString('hex')}`);
44
- console.log(`Parsed from DER s: ${parsedFromDER.s.toString('hex')}`);
45
-
46
- const smartVerifyParsed = bsv.SmartVerify.smartVerify(hash, parsedFromDER, publicKey);
47
- console.log(`SmartVerify(hash, parsedSig, pubkey): ${smartVerifyParsed ? '✅ VALID' : '❌ INVALID'}`);
48
-
49
- console.log('\n🔍 Compare r,s values:');
50
- console.log('======================');
51
- console.log(`Original r: ${signature.r.toString('hex')}`);
52
- console.log(`Parsed r : ${parsedFromDER.r.toString('hex')}`);
53
- console.log(`r matches : ${signature.r.eq(parsedFromDER.r) ? '✅ YES' : '❌ NO'}`);
54
-
55
- console.log(`Original s: ${signature.s.toString('hex')}`);
56
- console.log(`Parsed s : ${parsedFromDER.s.toString('hex')}`);
57
- console.log(`s matches : ${signature.s.eq(parsedFromDER.s) ? '✅ YES' : '❌ NO'}`);
58
-
59
- } catch (error) {
60
- console.log(`❌ DER parsing failed: ${error.message}`);
61
- }
62
-
63
- console.log('\n🔧 Debug SmartVerify DER Parsing:');
64
- console.log('==================================');
65
-
66
- // Let's manually step through what SmartVerify does with DER buffers
67
- try {
68
- console.log('Step 1: Input validation...');
69
- console.log(` Hash is Buffer: ${Buffer.isBuffer(hash)}`);
70
- console.log(` Hash length: ${hash.length}`);
71
- console.log(` DER is Buffer: ${Buffer.isBuffer(derSig)}`);
72
-
73
- console.log('Step 2: DER parsing in SmartVerify...');
74
-
75
- // This replicates what SmartVerify does internally
76
- const Signature = bsv.crypto.Signature;
77
- const testParsed = Signature.fromDER(derSig);
78
- console.log(` Parsed successfully: ✅`);
79
- console.log(` Parsed r: ${testParsed.r.toString('hex')}`);
80
- console.log(` Parsed s: ${testParsed.s.toString('hex')}`);
81
-
82
- const BN = bsv.crypto.BN;
83
- const r = BN.isBN(testParsed.r) ? testParsed.r : new BN(testParsed.r);
84
- const s = BN.isBN(testParsed.s) ? testParsed.s : new BN(testParsed.s);
85
- console.log(` r as BN: ${r.toString('hex')}`);
86
- console.log(` s as BN: ${s.toString('hex')}`);
87
-
88
- // Check canonicalization
89
- const Point = bsv.crypto.Point;
90
- const n = Point.getN();
91
- const nh = n.shrn(1);
92
- console.log(` s > n/2: ${s.gt(nh)}`);
93
-
94
- const canonicalS = s.gt(nh) ? n.sub(s) : s;
95
- console.log(` canonical s: ${canonicalS.toString('hex')}`);
96
-
97
- // Test final ECDSA call
98
- const canonicalSig = new Signature({
99
- r: r,
100
- s: canonicalS
101
- });
102
-
103
- const ECDSA = bsv.crypto.ECDSA;
104
- const finalResult = ECDSA.verify(hash, canonicalSig, publicKey, 'little');
105
- console.log(` Final ECDSA result: ${finalResult ? '✅ VALID' : '❌ INVALID'}`);
106
-
107
- } catch (error) {
108
- console.log(`❌ Debug failed: ${error.message}`);
109
- console.log(`Stack: ${error.stack}`);
110
- }
@@ -1,83 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>BSV Shamir Secret Sharing - Standalone Test</title>
5
- <meta charset="utf-8">
6
- </head>
7
- <body>
8
- <h1>BSV Shamir Secret Sharing - Standalone Module Test</h1>
9
- <div id="output"></div>
10
-
11
- <script src="bsv-shamir.min.js"></script>
12
- <script>
13
- function log(message) {
14
- document.getElementById('output').innerHTML += message + '<br>';
15
- console.log(message);
16
- }
17
-
18
- log('=== BSV Shamir Standalone Module Test ===');
19
- log('');
20
-
21
- try {
22
- if (typeof bsvShamir === 'undefined') {
23
- log('❌ bsvShamir not available');
24
- } else {
25
- log('✅ bsvShamir loaded successfully');
26
- log('Version: ' + bsvShamir.version);
27
- log('Algorithm: ' + bsvShamir.algorithm);
28
- log('');
29
-
30
- // Test basic functionality
31
- var secret = 'Hello from Browser!';
32
- var threshold = 2;
33
- var shares = 4;
34
-
35
- log('🔐 Testing secret splitting...');
36
- log('Secret: ' + secret);
37
- log('Threshold: ' + threshold + ', Total shares: ' + shares);
38
-
39
- var splitShares = bsvShamir.split(secret, threshold, shares);
40
- log('✅ Split into ' + splitShares.length + ' shares');
41
-
42
- // Test reconstruction
43
- log('');
44
- log('🔓 Testing secret reconstruction...');
45
- var reconstructed = bsvShamir.combine(splitShares.slice(0, threshold));
46
- var reconstructedSecret = reconstructed.toString();
47
-
48
- log('Reconstructed: ' + reconstructedSecret);
49
-
50
- if (reconstructedSecret === secret) {
51
- log('✅ Reconstruction successful!');
52
- } else {
53
- log('❌ Reconstruction failed');
54
- }
55
-
56
- // Test share verification
57
- log('');
58
- log('🔍 Testing share verification...');
59
- var validShares = 0;
60
- for (var i = 0; i < splitShares.length; i++) {
61
- if (bsvShamir.verifyShare(splitShares[i])) {
62
- validShares++;
63
- }
64
- }
65
- log('Valid shares: ' + validShares + '/' + splitShares.length);
66
-
67
- // Run demo
68
- log('');
69
- log('🚀 Running demo...');
70
- var demoResult = bsvShamir.demo();
71
- log('Demo completed: ' + (demoResult.success ? 'SUCCESS' : 'FAILED'));
72
-
73
- log('');
74
- log('🎉 All tests completed successfully!');
75
- log('📦 BSV Shamir Secret Sharing standalone module is working correctly.');
76
- }
77
- } catch (error) {
78
- log('❌ Error: ' + error.message);
79
- console.error('Full error:', error);
80
- }
81
- </script>
82
- </body>
83
- </html>