smartledger-bsv 3.2.2 → 3.3.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 +147 -0
- package/architecture_demo.js +247 -0
- package/bsv-gdaf.min.js +37 -0
- package/bsv-ltp.min.js +37 -0
- package/bsv-shamir.min.js +12 -0
- package/bsv.bundle.js +9 -9
- package/build/bsv-smartcontract.min.js +10 -8
- package/build/bsv.bundle.js +9 -9
- package/build/bsv.min.js +10 -8
- package/build/webpack.gdaf.config.js +54 -0
- package/build/webpack.ltp.config.js +17 -0
- package/bundle-entry.js +77 -1
- package/complete_ltp_demo.js +511 -0
- package/gdaf-entry.js +54 -0
- package/index.js +259 -0
- package/lib/crypto/shamir.js +360 -0
- package/lib/gdaf/attestation-signer.js +461 -0
- package/lib/gdaf/attestation-verifier.js +600 -0
- package/lib/gdaf/did-resolver.js +382 -0
- package/lib/gdaf/index.js +471 -0
- package/lib/gdaf/schema-validator.js +682 -0
- package/lib/gdaf/smartledger-anchor.js +486 -0
- package/lib/gdaf/zk-prover.js +507 -0
- package/lib/ltp/anchor.js +438 -0
- package/lib/ltp/claim.js +1026 -0
- package/lib/ltp/index.js +470 -0
- package/lib/ltp/obligation.js +945 -0
- package/lib/ltp/proof.js +828 -0
- package/lib/ltp/registry.js +702 -0
- package/lib/ltp/right.js +765 -0
- package/lib/smart_contract/API_REFERENCE.md +1 -1
- package/lib/smart_contract/EXAMPLES.md +2 -2
- package/lib/smart_contract/QUICK_START.md +2 -2
- package/lib/smart_contract/README.md +1 -1
- package/ltp-entry.js +92 -0
- package/package.json +44 -4
- package/shamir-entry.js +173 -0
- package/shamir_demo.js +121 -0
- package/simple_demo.js +204 -0
- package/test_shamir.js +221 -0
- package/test_standalone_shamir.html +83 -0
package/simple_demo.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SmartLedger-BSV Legal Token Protocol (LTP) - Primitives-Only Architecture Demo
|
|
3
|
+
*
|
|
4
|
+
* This demonstrates the architectural transformation from application framework
|
|
5
|
+
* to foundation library with primitives-only approach.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const bsv = require('./index.js')
|
|
9
|
+
|
|
10
|
+
console.log('🚀 SmartLedger-BSV LTP: Primitives-Only Architecture')
|
|
11
|
+
console.log('==================================================\n')
|
|
12
|
+
|
|
13
|
+
console.log('🔄 ARCHITECTURAL TRANSFORMATION COMPARISON')
|
|
14
|
+
console.log('------------------------------------------\n')
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* BEFORE vs AFTER: Key Differences
|
|
18
|
+
*/
|
|
19
|
+
console.log('📋 BEFORE (Application Framework Approach):')
|
|
20
|
+
console.log('--------------------------------------------')
|
|
21
|
+
console.log('❌ bsv.createRightToken() → Created AND published to blockchain')
|
|
22
|
+
console.log('❌ bsv.validateLegalClaim() → Validated AND stored in database')
|
|
23
|
+
console.log('❌ bsv.anchorTokenBatch() → Created batch AND sent transaction')
|
|
24
|
+
console.log('❌ bsv.createLegalRegistry() → Created registry AND managed storage')
|
|
25
|
+
console.log('❌ bsv.transferRight() → Prepared transfer AND published')
|
|
26
|
+
console.log('')
|
|
27
|
+
console.log(' Problems with this approach:')
|
|
28
|
+
console.log(' • Library had too many responsibilities')
|
|
29
|
+
console.log(' • Developers locked into specific platforms')
|
|
30
|
+
console.log(' • Hard to integrate with existing systems')
|
|
31
|
+
console.log(' • Mixed crypto logic with application logic')
|
|
32
|
+
console.log('')
|
|
33
|
+
|
|
34
|
+
console.log('📋 AFTER (Primitives-Only Approach):')
|
|
35
|
+
console.log('-------------------------------------')
|
|
36
|
+
console.log('✅ bsv.prepareRightToken() → Prepares token structure only')
|
|
37
|
+
console.log('✅ bsv.prepareClaimValidation() → Validates structure only')
|
|
38
|
+
console.log('✅ bsv.prepareBatchCommitment() → Prepares commitment only')
|
|
39
|
+
console.log('✅ bsv.prepareRegistry() → Prepares registry data only')
|
|
40
|
+
console.log('✅ bsv.prepareRightTokenTransfer() → Prepares transfer data only')
|
|
41
|
+
console.log('')
|
|
42
|
+
console.log(' Benefits of this approach:')
|
|
43
|
+
console.log(' • Clear separation of concerns')
|
|
44
|
+
console.log(' • Maximum developer flexibility')
|
|
45
|
+
console.log(' • Easy integration with any system')
|
|
46
|
+
console.log(' • Focus on cryptographic correctness')
|
|
47
|
+
console.log('')
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* DEMONSTRATE THE NEW INTERFACE
|
|
51
|
+
*/
|
|
52
|
+
console.log('🛠️ NEW PRIMITIVES INTERFACE AVAILABLE:')
|
|
53
|
+
console.log('======================================\n')
|
|
54
|
+
|
|
55
|
+
console.log('🏛️ RIGHT TOKEN PRIMITIVES:')
|
|
56
|
+
console.log(' • bsv.prepareRightToken()')
|
|
57
|
+
console.log(' • bsv.prepareRightTokenVerification()')
|
|
58
|
+
console.log(' • bsv.prepareRightTokenTransfer()')
|
|
59
|
+
console.log(' • bsv.prepareRightTypeValidation()')
|
|
60
|
+
console.log('')
|
|
61
|
+
|
|
62
|
+
console.log('⚖️ OBLIGATION TOKEN PRIMITIVES:')
|
|
63
|
+
console.log(' • bsv.prepareObligationToken()')
|
|
64
|
+
console.log(' • bsv.prepareObligationVerification()')
|
|
65
|
+
console.log(' • bsv.prepareObligationFulfillment()')
|
|
66
|
+
console.log(' • bsv.prepareObligationBreachAssessment()')
|
|
67
|
+
console.log(' • bsv.prepareObligationMonitoringReport()')
|
|
68
|
+
console.log('')
|
|
69
|
+
|
|
70
|
+
console.log('📝 CLAIM VALIDATION PRIMITIVES:')
|
|
71
|
+
console.log(' • bsv.prepareClaimValidation()')
|
|
72
|
+
console.log(' • bsv.prepareClaimAttestation()')
|
|
73
|
+
console.log(' • bsv.prepareClaimDispute()')
|
|
74
|
+
console.log(' • bsv.prepareBulkClaimValidation()')
|
|
75
|
+
console.log(' • bsv.prepareClaimTemplate()')
|
|
76
|
+
console.log('')
|
|
77
|
+
|
|
78
|
+
console.log('🔐 PROOF GENERATION PRIMITIVES:')
|
|
79
|
+
console.log(' • bsv.prepareSignatureProof()')
|
|
80
|
+
console.log(' • bsv.prepareSelectiveDisclosure()')
|
|
81
|
+
console.log(' • bsv.prepareLegalValidityProof()')
|
|
82
|
+
console.log(' • bsv.prepareZeroKnowledgeProof()')
|
|
83
|
+
console.log('')
|
|
84
|
+
|
|
85
|
+
console.log('📚 REGISTRY MANAGEMENT PRIMITIVES:')
|
|
86
|
+
console.log(' • bsv.prepareRegistry()')
|
|
87
|
+
console.log(' • bsv.prepareTokenRegistration()')
|
|
88
|
+
console.log(' • bsv.prepareTokenApproval()')
|
|
89
|
+
console.log(' • bsv.prepareTokenRevocation()')
|
|
90
|
+
console.log(' • bsv.prepareTokenStatusQuery()')
|
|
91
|
+
console.log(' • bsv.prepareTokenSearch()')
|
|
92
|
+
console.log('')
|
|
93
|
+
|
|
94
|
+
console.log('⛓️ BLOCKCHAIN ANCHORING PRIMITIVES:')
|
|
95
|
+
console.log(' • bsv.prepareTokenCommitment()')
|
|
96
|
+
console.log(' • bsv.prepareBatchCommitment()')
|
|
97
|
+
console.log(' • bsv.verifyTokenAnchor()')
|
|
98
|
+
console.log(' • bsv.formatRevocation()')
|
|
99
|
+
console.log('')
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* SHOW UTILITY FUNCTIONS (UNCHANGED)
|
|
103
|
+
*/
|
|
104
|
+
console.log('🔧 UTILITY FUNCTIONS (Unchanged):')
|
|
105
|
+
console.log(' • bsv.getRightTypes() → Static data access')
|
|
106
|
+
console.log(' • bsv.getObligationTypes() → Static data access')
|
|
107
|
+
console.log(' • bsv.getClaimSchemas() → Static data access')
|
|
108
|
+
console.log(' • bsv.canonicalizeClaim() → Data transformation')
|
|
109
|
+
console.log(' • bsv.hashClaim() → Hash generation')
|
|
110
|
+
console.log('')
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* EXAMPLE USAGE PATTERN
|
|
114
|
+
*/
|
|
115
|
+
console.log('💡 EXAMPLE: How Applications Use The New Primitives')
|
|
116
|
+
console.log('===================================================\n')
|
|
117
|
+
|
|
118
|
+
console.log('// STEP 1: Use SmartLedger-BSV to prepare legal structures')
|
|
119
|
+
console.log('const rightTokenPrep = bsv.prepareRightToken(')
|
|
120
|
+
console.log(' "PROPERTY_OWNERSHIP", issuerDID, ownerDID, claimData, privateKey')
|
|
121
|
+
console.log(')')
|
|
122
|
+
console.log('')
|
|
123
|
+
|
|
124
|
+
console.log('// STEP 2: Use external system to publish to blockchain')
|
|
125
|
+
console.log('const blockchainResult = await MyBlockchain.publish({')
|
|
126
|
+
console.log(' commitment: rightTokenPrep.commitment,')
|
|
127
|
+
console.log(' signature: rightTokenPrep.signature')
|
|
128
|
+
console.log('})')
|
|
129
|
+
console.log('')
|
|
130
|
+
|
|
131
|
+
console.log('// STEP 3: Use external system to store in registry')
|
|
132
|
+
console.log('const registryResult = await MyRegistry.store({')
|
|
133
|
+
console.log(' token: rightTokenPrep.token,')
|
|
134
|
+
console.log(' metadata: rightTokenPrep.metadata')
|
|
135
|
+
console.log('})')
|
|
136
|
+
console.log('')
|
|
137
|
+
|
|
138
|
+
console.log('// STEP 4: Use SmartLedger-BSV to verify the results')
|
|
139
|
+
console.log('const verification = bsv.verifyTokenAnchor(')
|
|
140
|
+
console.log(' rightTokenPrep.token, blockchainResult.txid')
|
|
141
|
+
console.log(')')
|
|
142
|
+
console.log('')
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* BENEFITS SUMMARY
|
|
146
|
+
*/
|
|
147
|
+
console.log('🎯 PRIMITIVES-ONLY ARCHITECTURE BENEFITS')
|
|
148
|
+
console.log('========================================\n')
|
|
149
|
+
|
|
150
|
+
console.log('👨💻 FOR DEVELOPERS:')
|
|
151
|
+
console.log(' ✅ Choose your own blockchain (BSV, Bitcoin, Ethereum, etc.)')
|
|
152
|
+
console.log(' ✅ Choose your own storage (SQL, NoSQL, IPFS, etc.)')
|
|
153
|
+
console.log(' ✅ Choose your own UI framework (React, Vue, Angular, etc.)')
|
|
154
|
+
console.log(' ✅ Integrate with existing business systems')
|
|
155
|
+
console.log(' ✅ Maintain full control over user experience')
|
|
156
|
+
console.log('')
|
|
157
|
+
|
|
158
|
+
console.log('🏢 FOR ENTERPRISES:')
|
|
159
|
+
console.log(' ✅ No vendor lock-in to specific platforms')
|
|
160
|
+
console.log(' ✅ Compliance with existing IT policies')
|
|
161
|
+
console.log(' ✅ Integration with legacy systems')
|
|
162
|
+
console.log(' ✅ Scalable architecture patterns')
|
|
163
|
+
console.log(' ✅ Audit-friendly separation of concerns')
|
|
164
|
+
console.log('')
|
|
165
|
+
|
|
166
|
+
console.log('🔒 FOR SECURITY:')
|
|
167
|
+
console.log(' ✅ Cryptographic operations isolated and testable')
|
|
168
|
+
console.log(' ✅ No network dependencies in core library')
|
|
169
|
+
console.log(' ✅ Predictable, deterministic behavior')
|
|
170
|
+
console.log(' ✅ Smaller attack surface')
|
|
171
|
+
console.log(' ✅ Clear boundaries for security reviews')
|
|
172
|
+
console.log('')
|
|
173
|
+
|
|
174
|
+
console.log('⚖️ FOR LEGAL COMPLIANCE:')
|
|
175
|
+
console.log(' ✅ Standardized legal token structures')
|
|
176
|
+
console.log(' ✅ Cryptographic proof generation')
|
|
177
|
+
console.log(' ✅ Audit trail preparation')
|
|
178
|
+
console.log(' ✅ Jurisdiction-specific adaptability')
|
|
179
|
+
console.log(' ✅ Regulatory compliance primitives')
|
|
180
|
+
console.log('')
|
|
181
|
+
|
|
182
|
+
console.log('🚀 CONCLUSION')
|
|
183
|
+
console.log('=============')
|
|
184
|
+
console.log('')
|
|
185
|
+
console.log('SmartLedger-BSV is now a pure foundation library that provides')
|
|
186
|
+
console.log('everything needed to build Legal Token Protocol applications')
|
|
187
|
+
console.log('while giving developers complete architectural freedom.')
|
|
188
|
+
console.log('')
|
|
189
|
+
console.log('The library focuses on what it does best:')
|
|
190
|
+
console.log('• Cryptographic correctness')
|
|
191
|
+
console.log('• Legal structure validation')
|
|
192
|
+
console.log('• Standardized data formats')
|
|
193
|
+
console.log('• Compliance primitives')
|
|
194
|
+
console.log('')
|
|
195
|
+
console.log('External systems handle:')
|
|
196
|
+
console.log('• Blockchain publishing')
|
|
197
|
+
console.log('• Data storage')
|
|
198
|
+
console.log('• User interfaces')
|
|
199
|
+
console.log('• Business workflows')
|
|
200
|
+
console.log('')
|
|
201
|
+
console.log('This creates the perfect foundation for any Legal Token')
|
|
202
|
+
console.log('Protocol application while maintaining maximum flexibility!')
|
|
203
|
+
console.log('')
|
|
204
|
+
console.log('🎉 Primitives-only transformation: COMPLETE! 🎉')
|
package/test_shamir.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
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')
|
|
@@ -0,0 +1,83 @@
|
|
|
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>
|