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.
Files changed (41) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/architecture_demo.js +247 -0
  3. package/bsv-gdaf.min.js +37 -0
  4. package/bsv-ltp.min.js +37 -0
  5. package/bsv-shamir.min.js +12 -0
  6. package/bsv.bundle.js +9 -9
  7. package/build/bsv-smartcontract.min.js +10 -8
  8. package/build/bsv.bundle.js +9 -9
  9. package/build/bsv.min.js +10 -8
  10. package/build/webpack.gdaf.config.js +54 -0
  11. package/build/webpack.ltp.config.js +17 -0
  12. package/bundle-entry.js +77 -1
  13. package/complete_ltp_demo.js +511 -0
  14. package/gdaf-entry.js +54 -0
  15. package/index.js +259 -0
  16. package/lib/crypto/shamir.js +360 -0
  17. package/lib/gdaf/attestation-signer.js +461 -0
  18. package/lib/gdaf/attestation-verifier.js +600 -0
  19. package/lib/gdaf/did-resolver.js +382 -0
  20. package/lib/gdaf/index.js +471 -0
  21. package/lib/gdaf/schema-validator.js +682 -0
  22. package/lib/gdaf/smartledger-anchor.js +486 -0
  23. package/lib/gdaf/zk-prover.js +507 -0
  24. package/lib/ltp/anchor.js +438 -0
  25. package/lib/ltp/claim.js +1026 -0
  26. package/lib/ltp/index.js +470 -0
  27. package/lib/ltp/obligation.js +945 -0
  28. package/lib/ltp/proof.js +828 -0
  29. package/lib/ltp/registry.js +702 -0
  30. package/lib/ltp/right.js +765 -0
  31. package/lib/smart_contract/API_REFERENCE.md +1 -1
  32. package/lib/smart_contract/EXAMPLES.md +2 -2
  33. package/lib/smart_contract/QUICK_START.md +2 -2
  34. package/lib/smart_contract/README.md +1 -1
  35. package/ltp-entry.js +92 -0
  36. package/package.json +44 -4
  37. package/shamir-entry.js +173 -0
  38. package/shamir_demo.js +121 -0
  39. package/simple_demo.js +204 -0
  40. package/test_shamir.js +221 -0
  41. package/test_standalone_shamir.html +83 -0
@@ -19,7 +19,7 @@
19
19
  ## Quick Start
20
20
 
21
21
  ```javascript
22
- const SmartContract = require('bsv-elliptic-fix').SmartContract
22
+ const SmartContract = require('smartledger-bsv').SmartContract
23
23
 
24
24
  // šŸš€ Write covenant logic in JavaScript
25
25
  const builder = SmartContract.createCovenantBuilder()
@@ -17,7 +17,7 @@
17
17
  ### Example 1: Simple Value Check
18
18
 
19
19
  ```javascript
20
- const SmartContract = require('bsv-elliptic-fix').SmartContract
20
+ const SmartContract = require('smartledger-bsv').SmartContract
21
21
 
22
22
  // Ensure transaction maintains minimum value
23
23
  function createValueGuard(minimumSatoshis) {
@@ -419,7 +419,7 @@ function createSubscriptionCovenant(serviceProvider, subscriptionFee, renewalPer
419
419
  ### Example 12: Complete Covenant Deployment
420
420
 
421
421
  ```javascript
422
- const bsv = require('bsv-elliptic-fix')
422
+ const bsv = require('smartledger-bsv')
423
423
  const SmartContract = bsv.SmartContract
424
424
 
425
425
  async function deployValueLockCovenant(privateKey, minimumValue, utxo) {
@@ -9,7 +9,7 @@ The JavaScript-to-Bitcoin Script framework allows you to write complex covenant
9
9
  ## šŸš€ 30-Second Quick Start
10
10
 
11
11
  ```javascript
12
- const SmartContract = require('bsv-elliptic-fix').SmartContract
12
+ const SmartContract = require('smartledger-bsv').SmartContract
13
13
 
14
14
  // Write covenant logic in JavaScript
15
15
  const builder = SmartContract.createCovenantBuilder()
@@ -415,7 +415,7 @@ Object.keys(opcodes)
415
415
  ### With BSV Library
416
416
 
417
417
  ```javascript
418
- const bsv = require('bsv-elliptic-fix')
418
+ const bsv = require('smartledger-bsv')
419
419
  const SmartContract = bsv.SmartContract
420
420
 
421
421
  // Create covenant from private key
@@ -7,7 +7,7 @@ The SmartContract module provides enterprise-grade covenant functionality for Bi
7
7
  ## Installation & Usage
8
8
 
9
9
  ```javascript
10
- const bsv = require('bsv-elliptic-fix')
10
+ const bsv = require('smartledger-bsv')
11
11
 
12
12
  // Access SmartContract module
13
13
  const SmartContract = bsv.SmartContract
package/ltp-entry.js ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * SmartLedger-BSV Legal Token Protocol (LTP) - Standalone Entry Point
3
+ *
4
+ * This entry point provides the complete Legal Token Protocol framework
5
+ * as a standalone module for browser and Node.js environments.
6
+ *
7
+ * Features:
8
+ * - Complete LTP primitives-only architecture
9
+ * - Legal claim validation and attestation
10
+ * - Right and obligation token management
11
+ * - Cryptographic proof generation
12
+ * - Registry and blockchain anchoring preparation
13
+ * - Full W3C compatibility and legal compliance
14
+ */
15
+
16
+ const bsv = require('./index.js')
17
+
18
+ // Export LTP functionality as standalone module
19
+ module.exports = {
20
+ // Core BSV functionality needed for LTP
21
+ PrivateKey: bsv.PrivateKey,
22
+ PublicKey: bsv.PublicKey,
23
+ Address: bsv.Address,
24
+ Transaction: bsv.Transaction,
25
+ Script: bsv.Script,
26
+ crypto: bsv.crypto,
27
+
28
+ // Complete LTP framework
29
+ LTP: bsv.LTP,
30
+
31
+ // Right Token Primitives
32
+ prepareRightToken: bsv.prepareRightToken,
33
+ prepareRightTokenVerification: bsv.prepareRightTokenVerification,
34
+ prepareRightTokenTransfer: bsv.prepareRightTokenTransfer,
35
+ prepareRightTypeValidation: bsv.prepareRightTypeValidation,
36
+
37
+ // Obligation Token Primitives
38
+ prepareObligationToken: bsv.prepareObligationToken,
39
+ prepareObligationVerification: bsv.prepareObligationVerification,
40
+ prepareObligationFulfillment: bsv.prepareObligationFulfillment,
41
+ prepareObligationBreachAssessment: bsv.prepareObligationBreachAssessment,
42
+ prepareObligationMonitoringReport: bsv.prepareObligationMonitoringReport,
43
+
44
+ // Claim Validation Primitives
45
+ prepareClaimValidation: bsv.prepareClaimValidation,
46
+ prepareClaimAttestation: bsv.prepareClaimAttestation,
47
+ prepareClaimDispute: bsv.prepareClaimDispute,
48
+ prepareBulkClaimValidation: bsv.prepareBulkClaimValidation,
49
+ prepareClaimTemplate: bsv.prepareClaimTemplate,
50
+
51
+ // Proof Generation Primitives
52
+ prepareSignatureProof: bsv.prepareSignatureProof,
53
+ prepareSignatureVerification: bsv.prepareSignatureVerification,
54
+ prepareSelectiveDisclosure: bsv.prepareSelectiveDisclosure,
55
+ prepareSelectiveDisclosureVerification: bsv.prepareSelectiveDisclosureVerification,
56
+ prepareLegalValidityProof: bsv.prepareLegalValidityProof,
57
+ prepareZeroKnowledgeProof: bsv.prepareZeroKnowledgeProof,
58
+
59
+ // Registry Management Primitives
60
+ prepareRegistry: bsv.prepareRegistry,
61
+ prepareTokenRegistration: bsv.prepareTokenRegistration,
62
+ prepareTokenApproval: bsv.prepareTokenApproval,
63
+ prepareTokenRevocation: bsv.prepareTokenRevocation,
64
+ prepareTokenStatusQuery: bsv.prepareTokenStatusQuery,
65
+ prepareTokenSearch: bsv.prepareTokenSearch,
66
+ prepareStatisticsQuery: bsv.prepareStatisticsQuery,
67
+ prepareAuditLogQuery: bsv.prepareAuditLogQuery,
68
+
69
+ // Blockchain Anchoring Primitives
70
+ prepareTokenCommitment: bsv.prepareTokenCommitment,
71
+ prepareBatchCommitment: bsv.prepareBatchCommitment,
72
+ verifyTokenAnchor: bsv.verifyTokenAnchor,
73
+ formatRevocation: bsv.formatRevocation,
74
+
75
+ // Utility Functions
76
+ getRightTypes: bsv.getRightTypes,
77
+ getObligationTypes: bsv.getObligationTypes,
78
+ getObligationPriority: bsv.getObligationPriority,
79
+ getObligationStatus: bsv.getObligationStatus,
80
+ getClaimSchemas: bsv.getClaimSchemas,
81
+ getClaimSchemaNames: bsv.getClaimSchemaNames,
82
+ getClaimSchema: bsv.getClaimSchema,
83
+ createClaimTemplate: bsv.createClaimTemplate,
84
+ canonicalizeClaim: bsv.canonicalizeClaim,
85
+ hashClaim: bsv.hashClaim,
86
+ addCustomClaimSchema: bsv.addCustomClaimSchema,
87
+
88
+ // Version and metadata
89
+ version: '3.3.0',
90
+ framework: 'Legal Token Protocol',
91
+ architecture: 'primitives-only'
92
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smartledger-bsv",
3
- "version": "3.2.2",
4
- "description": "šŸš€ Complete Bitcoin SV development framework with 9 flexible loading options: standalone modules (covenant, script-helper, security, debug tools), complete bundle, or modular approach. Includes SmartContract framework, covenant builder, script debugger, elliptic curve security fixes, and comprehensive Bitcoin SV API. Perfect for DeFi, smart contracts, custom scripts, and secure Bitcoin applications.",
3
+ "version": "3.3.1",
4
+ "description": "šŸš€ Complete Bitcoin SV development framework with Legal Token Protocol (LTP), Global Digital Attestation Framework (GDAF), Shamir Secret Sharing, and 9 flexible loading options. Includes primitives-only architecture for maximum integration flexibility, SmartContract framework, covenant builder, and comprehensive Bitcoin SV API. Perfect for legal tokens, DeFi, smart contracts, and secure Bitcoin applications.",
5
5
  "author": "SmartLedger Technology <hello@smartledger.technology> (https://smartledger.technology)",
6
6
  "homepage": "https://github.com/codenlighten/smartledger-bsv#readme",
7
7
  "bugs": {
@@ -11,7 +11,9 @@
11
11
  "scripts": {
12
12
  "lint": "standard",
13
13
  "test": "standard && mocha",
14
- "test:signatures": "node validation_test.js",
14
+ "test:ltp": "node complete_ltp_demo.js",
15
+ "test:ltp-primitives": "node simple_demo.js",
16
+ "test:architecture": "node architecture_demo.js",
15
17
  "test:js2script": "node lib/smart_contract/opcode_map.js && node lib/smart_contract/covenant_builder.js",
16
18
  "test:opcodes": "node lib/smart_contract/opcode_list.js",
17
19
  "test:covenants": "node examples/covenants/advanced_covenant_demo.js",
@@ -23,12 +25,14 @@
23
25
  "build-ecies": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack ecies/index.js --config build/webpack.subproject.config.js --output-library bsvEcies -o bsv-ecies.min.js",
24
26
  "build-message": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack message/index.js --config build/webpack.subproject.config.js --output-library bsvMessage -o bsv-message.min.js",
25
27
  "build-mnemonic": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack mnemonic/index.js --config build/webpack.subproject.config.js --output-library bsvMnemonic -o bsv-mnemonic.min.js",
28
+ "build-shamir": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack shamir-entry.js --config build/webpack.subproject.config.js --output-library bsvShamir -o bsv-shamir.min.js",
29
+ "build-ltp": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack ltp-entry.js --config build/webpack.ltp.config.js",
26
30
  "build-smartcontract": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack smartcontract-entry.js --config build/webpack.smartcontract.config.js",
27
31
  "build-covenant": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack covenant-entry.js --config build/webpack.covenant.config.js",
28
32
  "build-script-helper": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack script-helper-entry.js --config build/webpack.script-helper.config.js",
29
33
  "build-security": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack security-entry.js --config build/webpack.security.config.js",
30
34
  "build-bundle": "NODE_OPTIONS=\"--openssl-legacy-provider\" webpack bundle-entry.js --config build/webpack.bundle.config.js",
31
- "build": "npm run build-bsv && npm run build-ecies && npm run build-message && npm run build-mnemonic && npm run build-smartcontract",
35
+ "build": "npm run build-bsv && npm run build-ecies && npm run build-message && npm run build-mnemonic && npm run build-shamir && npm run build-smartcontract",
32
36
  "build-specialized": "npm run build-covenant && npm run build-script-helper && npm run build-security",
33
37
  "build-all": "npm run build && npm run build-bundle && npm run build-specialized",
34
38
  "test:browser": "echo 'Open tests/standalone-modules-test.html in browser for comprehensive testing'",
@@ -58,12 +62,21 @@
58
62
  "bsv-ecies.min.js",
59
63
  "bsv-message.min.js",
60
64
  "bsv-mnemonic.min.js",
65
+ "bsv-shamir.min.js",
66
+ "bsv-gdaf.min.js",
67
+ "bsv-ltp.min.js",
61
68
  "bsv-smartcontract.min.js",
62
69
  "bsv-covenant.min.js",
63
70
  "bsv-script-helper.min.js",
64
71
  "bsv-security.min.js",
65
72
  "bsv.d.ts",
66
73
  "validation_test.js",
74
+ "test_shamir.js",
75
+ "shamir_demo.js",
76
+ "complete_ltp_demo.js",
77
+ "simple_demo.js",
78
+ "architecture_demo.js",
79
+ "test_standalone_shamir.html",
67
80
  "docs/",
68
81
  "examples/",
69
82
  "LICENSE",
@@ -75,6 +88,21 @@
75
88
  "bitcoin",
76
89
  "bitcoin-sv",
77
90
  "bsv",
91
+ "legal-token-protocol",
92
+ "ltp",
93
+ "legal-tokens",
94
+ "primitives-only",
95
+ "legal-compliance",
96
+ "property-rights",
97
+ "obligations",
98
+ "attestations",
99
+ "gdaf",
100
+ "global-digital-attestation",
101
+ "w3c-credentials",
102
+ "verifiable-credentials",
103
+ "decentralized-identity",
104
+ "shamir-secret-sharing",
105
+ "threshold-cryptography",
78
106
  "cryptocurrency",
79
107
  "blockchain",
80
108
  "smart-contracts",
@@ -101,6 +129,18 @@
101
129
  "pels",
102
130
  "bip143",
103
131
  "preimage",
132
+ "shamir-secret-sharing",
133
+ "threshold-cryptography",
134
+ "secret-splitting",
135
+ "global-digital-attestation",
136
+ "gdaf",
137
+ "verifiable-credentials",
138
+ "w3c-credentials",
139
+ "decentralized-identity",
140
+ "did-resolution",
141
+ "zero-knowledge-proofs",
142
+ "blockchain-anchoring",
143
+ "attestation-framework",
104
144
  "nchain",
105
145
  "custom-scripts",
106
146
  "multisig",
@@ -0,0 +1,173 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * BSV Shamir Secret Sharing - Standalone Module
5
+ * Secure secret distribution using Shamir's Secret Sharing algorithm
6
+ *
7
+ * Usage:
8
+ * // Split a secret into shares
9
+ * var shares = bsvShamir.split('my secret', 3, 5) // 3-of-5 threshold
10
+ *
11
+ * // Reconstruct secret from shares
12
+ * var secret = bsvShamir.combine(shares.slice(0, 3))
13
+ *
14
+ * Features:
15
+ * - Cryptographically secure threshold secret sharing
16
+ * - Support for any threshold (k) and total shares (n) where k <= n
17
+ * - Handles arbitrary secret sizes through chunking
18
+ * - Share verification and integrity checking
19
+ * - Compatible with BSV cryptographic ecosystem
20
+ */
21
+
22
+ // Initialize dependencies for browser compatibility
23
+ var deps = {}
24
+ try {
25
+ deps.bnjs = require('bn.js')
26
+ deps.Buffer = (typeof Buffer !== 'undefined') ? Buffer : null
27
+ } catch (e) {
28
+ // Browser environment - dependencies should be available globally
29
+ if (typeof window !== 'undefined') {
30
+ deps.bnjs = window.BN || (window.bsv && window.bsv.deps && window.bsv.deps.bnjs)
31
+ deps.Buffer = window.Buffer || (window.bsv && window.bsv.deps && window.bsv.deps.Buffer)
32
+ }
33
+ }
34
+
35
+ // Ensure we have required dependencies
36
+ if (!deps.bnjs) {
37
+ throw new Error('BN.js dependency not found. Please include bn.js or bsv.min.js')
38
+ }
39
+
40
+ if (!deps.Buffer) {
41
+ // Provide minimal Buffer polyfill for basic operations
42
+ deps.Buffer = {
43
+ isBuffer: function(obj) { return obj && obj.constructor && obj.constructor.name === 'Buffer' },
44
+ from: function(data, encoding) {
45
+ if (typeof data === 'string') {
46
+ if (encoding === 'hex') {
47
+ return new Uint8Array(data.match(/.{2}/g).map(byte => parseInt(byte, 16)))
48
+ }
49
+ return new Uint8Array(Array.from(data).map(c => c.charCodeAt(0)))
50
+ }
51
+ return new Uint8Array(data)
52
+ },
53
+ concat: function(arrays) {
54
+ var totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0)
55
+ var result = new Uint8Array(totalLength)
56
+ var offset = 0
57
+ for (var i = 0; i < arrays.length; i++) {
58
+ result.set(arrays[i], offset)
59
+ offset += arrays[i].length
60
+ }
61
+ return result
62
+ }
63
+ }
64
+ }
65
+
66
+ // Import the core Shamir implementation
67
+ var Shamir = require('./lib/crypto/shamir')
68
+
69
+ // Create standalone module interface
70
+ var bsvShamir = {
71
+ /**
72
+ * Split a secret into threshold shares
73
+ * @param {String|Buffer} secret - Secret to split
74
+ * @param {Number} threshold - Minimum shares needed to reconstruct
75
+ * @param {Number} shares - Total shares to generate
76
+ * @returns {Array} Array of share objects
77
+ */
78
+ split: function(secret, threshold, shares) {
79
+ return Shamir.split(secret, threshold, shares)
80
+ },
81
+
82
+ /**
83
+ * Combine shares to reconstruct secret
84
+ * @param {Array} shares - Array of share objects
85
+ * @returns {Buffer} Reconstructed secret
86
+ */
87
+ combine: function(shares) {
88
+ return Shamir.combine(shares)
89
+ },
90
+
91
+ /**
92
+ * Verify if a share is valid
93
+ * @param {Object} share - Share to verify
94
+ * @returns {Boolean} True if valid
95
+ */
96
+ verifyShare: function(share) {
97
+ return Shamir.verifyShare(share)
98
+ },
99
+
100
+ /**
101
+ * Generate test vectors for validation
102
+ * @returns {Object} Test data with secret, shares, and reconstruction
103
+ */
104
+ generateTestVectors: function() {
105
+ return Shamir.generateTestVectors()
106
+ },
107
+
108
+ /**
109
+ * Create a simple demo showing basic usage
110
+ * @returns {Object} Demo results
111
+ */
112
+ demo: function() {
113
+ console.log('=== BSV Shamir Secret Sharing Demo ===')
114
+
115
+ var originalSecret = 'Bitcoin SV is the original Bitcoin!'
116
+ var threshold = 3
117
+ var totalShares = 5
118
+
119
+ console.log('Original secret:', originalSecret)
120
+ console.log('Creating', totalShares, 'shares with threshold of', threshold)
121
+
122
+ // Split the secret
123
+ var shares = bsvShamir.split(originalSecret, threshold, totalShares)
124
+ console.log('Generated', shares.length, 'shares')
125
+
126
+ // Show first share structure (truncated for display)
127
+ var displayShare = JSON.parse(JSON.stringify(shares[0]))
128
+ if (displayShare.chunks && displayShare.chunks.length > 0) {
129
+ displayShare.chunks = displayShare.chunks.slice(0, 1) // Show only first chunk
130
+ displayShare.chunks[0].y = displayShare.chunks[0].y.substring(0, 20) + '...'
131
+ }
132
+ console.log('Sample share structure:', displayShare)
133
+
134
+ // Reconstruct with minimum shares
135
+ var minShares = shares.slice(0, threshold)
136
+ var reconstructed = bsvShamir.combine(minShares)
137
+ var reconstructedSecret = reconstructed.toString('utf8')
138
+
139
+ console.log('Reconstructed secret:', reconstructedSecret)
140
+ console.log('Reconstruction successful:', originalSecret === reconstructedSecret)
141
+
142
+ return {
143
+ original: originalSecret,
144
+ threshold: threshold,
145
+ totalShares: totalShares,
146
+ shares: shares,
147
+ reconstructed: reconstructedSecret,
148
+ success: originalSecret === reconstructedSecret
149
+ }
150
+ },
151
+
152
+ // Expose version and metadata
153
+ version: '3.2.2',
154
+ algorithm: 'Shamir Secret Sharing',
155
+ description: 'Threshold cryptography for secure secret distribution'
156
+ }
157
+
158
+ // Browser compatibility
159
+ if (typeof window !== 'undefined') {
160
+ window.bsvShamir = bsvShamir
161
+ }
162
+
163
+ // Node.js compatibility
164
+ if (typeof module !== 'undefined' && module.exports) {
165
+ module.exports = bsvShamir
166
+ }
167
+
168
+ // AMD compatibility
169
+ if (typeof define === 'function' && define.amd) {
170
+ define(function() {
171
+ return bsvShamir
172
+ })
173
+ }
package/shamir_demo.js ADDED
@@ -0,0 +1,121 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * BSV Shamir Secret Sharing Demo
5
+ *
6
+ * This demonstrates how to use Shamir Secret Sharing for secure secret distribution
7
+ * Perfect for backup keys, passwords, or any sensitive data that needs to be distributed
8
+ * across multiple parties with threshold security.
9
+ */
10
+
11
+ 'use strict'
12
+
13
+ var bsv = require('./index.js')
14
+
15
+ console.log('šŸ” BSV Shamir Secret Sharing Demo')
16
+ console.log('=====================================\n')
17
+
18
+ // Example 1: Basic secret sharing
19
+ console.log('šŸ“ Example 1: Basic Secret Sharing')
20
+ console.log('----------------------------------')
21
+
22
+ var secret = 'my-super-secret-bitcoin-private-key'
23
+ var threshold = 3 // Need at least 3 shares to reconstruct
24
+ var totalShares = 5 // Create 5 total shares
25
+
26
+ console.log('Secret to protect:', secret)
27
+ console.log('Security policy: ' + threshold + ' of ' + totalShares + ' shares required\n')
28
+
29
+ // Split the secret
30
+ var shares = bsv.Shamir.split(secret, threshold, totalShares)
31
+ console.log('āœ… Secret split into', shares.length, 'shares')
32
+
33
+ // Display share information (truncated for security)
34
+ shares.forEach(function(share, index) {
35
+ console.log(' Share ' + (index + 1) + ': ID=' + share.id + ', bytes=' + share.length)
36
+ })
37
+
38
+ // Reconstruct with minimum shares (3 of 5)
39
+ console.log('\nšŸ”“ Reconstructing with shares 1, 3, and 5...')
40
+ var selectedShares = [shares[0], shares[2], shares[4]] // shares 1, 3, 5
41
+ var reconstructed = bsv.Shamir.combine(selectedShares)
42
+ var reconstructedSecret = reconstructed.toString('utf8')
43
+
44
+ console.log('Reconstructed secret:', reconstructedSecret)
45
+ console.log('Match original:', reconstructedSecret === secret ? 'āœ… YES' : 'āŒ NO')
46
+
47
+ // Example 2: Bitcoin wallet backup scenario
48
+ console.log('\n\nšŸ’° Example 2: Bitcoin Wallet Backup Scenario')
49
+ console.log('---------------------------------------------')
50
+
51
+ var walletMnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
52
+ var backupPolicy = { threshold: 2, shares: 3 } // 2-of-3 backup
53
+
54
+ console.log('Wallet mnemonic (12 words):', walletMnemonic.split(' ').slice(0, 3).join(' ') + '...')
55
+ console.log('Backup policy: ' + backupPolicy.threshold + ' of ' + backupPolicy.shares + ' shares needed\n')
56
+
57
+ var walletShares = bsv.Shamir.split(walletMnemonic, backupPolicy.threshold, backupPolicy.shares)
58
+
59
+ console.log('šŸ“‹ Backup shares created:')
60
+ walletShares.forEach(function(share, index) {
61
+ var label = ['šŸ‘¤ Family Member', 'šŸ¦ Bank Safe', 'ā˜ļø Cloud Storage'][index]
62
+ console.log(' Share ' + (index + 1) + ': ' + label + ' (ID: ' + share.id + ')')
63
+ })
64
+
65
+ // Simulate recovery with 2 shares
66
+ console.log('\nšŸ”§ Simulating wallet recovery with shares from Family Member + Bank Safe...')
67
+ var recoveryShares = [walletShares[0], walletShares[1]] // First 2 shares
68
+ var recoveredMnemonic = bsv.Shamir.combine(recoveryShares).toString('utf8')
69
+
70
+ console.log('Recovered mnemonic:', recoveredMnemonic === walletMnemonic ? 'āœ… SUCCESS' : 'āŒ FAILED')
71
+
72
+ // Example 3: Binary data (keys, certificates, etc.)
73
+ console.log('\n\nšŸ”‘ Example 3: Binary Data Protection')
74
+ console.log('-----------------------------------')
75
+
76
+ var binarySecret = Buffer.from([0x04, 0x8f, 0xab, 0x23, 0xc1, 0x9e, 0x77, 0x44]) // Example key bytes
77
+ console.log('Binary secret (hex):', binarySecret.toString('hex'))
78
+ console.log('Binary secret length:', binarySecret.length, 'bytes\n')
79
+
80
+ var binaryShares = bsv.Shamir.split(binarySecret, 2, 4)
81
+ console.log('āœ… Binary data split into', binaryShares.length, 'shares')
82
+
83
+ var recoveredBinary = bsv.Shamir.combine(binaryShares.slice(0, 2))
84
+ console.log('Recovered binary (hex):', recoveredBinary.toString('hex'))
85
+ console.log('Binary match:', Buffer.compare(binarySecret, recoveredBinary) === 0 ? 'āœ… YES' : 'āŒ NO')
86
+
87
+ // Example 4: Share verification
88
+ console.log('\n\nšŸ” Example 4: Share Verification')
89
+ console.log('--------------------------------')
90
+
91
+ var testShares = bsv.Shamir.split('verification-test', 2, 3)
92
+ console.log('Testing share integrity...')
93
+
94
+ testShares.forEach(function(share, index) {
95
+ var isValid = bsv.Shamir.verifyShare(share)
96
+ console.log(' Share ' + (index + 1) + ':', isValid ? 'āœ… Valid' : 'āŒ Invalid')
97
+ })
98
+
99
+ // Test with corrupted share
100
+ var corruptedShare = JSON.parse(JSON.stringify(testShares[0]))
101
+ corruptedShare.bytes[0].y = 'invalid-hex' // Corrupt the data
102
+ console.log(' Corrupted share:', bsv.Shamir.verifyShare(corruptedShare) ? 'āŒ Invalid test failed' : 'āœ… Correctly rejected')
103
+
104
+ console.log('\nšŸŽÆ Use Cases for Shamir Secret Sharing:')
105
+ console.log('--------------------------------------')
106
+ console.log('• šŸ” Bitcoin wallet backup (split mnemonic across family/friends)')
107
+ console.log('• šŸ¢ Corporate key management (distribute signing keys)')
108
+ console.log('• šŸ›”ļø Multi-party authentication (API keys, passwords)')
109
+ console.log('• šŸ’¾ Secure data backup (encrypt once, distribute shares)')
110
+ console.log('• šŸ¤ Trustless escrow (require multiple parties to unlock)')
111
+ console.log('• šŸ¦ Bank vault security (multiple keyholders required)')
112
+
113
+ console.log('\nšŸ“¦ Integration Options:')
114
+ console.log('----------------------')
115
+ console.log('• Main library: bsv.Shamir or bsv.crypto.Shamir')
116
+ console.log('• Standalone: bsv-shamir.min.js (433 KB)')
117
+ console.log('• CDN ready: Use in browser with <script> tag')
118
+ console.log('• Node.js: require("smartledger-bsv").Shamir')
119
+
120
+ console.log('\n✨ Demo completed successfully!')
121
+ console.log('Visit: https://github.com/codenlighten/smartledger-bsv for more examples')