@smartledger/bsv 3.2.1 → 3.3.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +289 -55
  3. package/architecture_demo.js +247 -0
  4. package/bsv-covenant.min.js +10 -0
  5. package/bsv-gdaf.min.js +37 -0
  6. package/bsv-ltp.min.js +37 -0
  7. package/bsv-script-helper.min.js +10 -0
  8. package/bsv-security.min.js +31 -0
  9. package/bsv-shamir.min.js +12 -0
  10. package/bsv-smartcontract.min.js +37 -0
  11. package/bsv.bundle.js +9 -9
  12. package/bsv.min.js +3 -3
  13. package/build/bsv-covenant.min.js +10 -0
  14. package/build/bsv-script-helper.min.js +10 -0
  15. package/build/bsv-security.min.js +31 -0
  16. package/build/bsv-smartcontract.min.js +39 -0
  17. package/build/bsv.bundle.js +39 -0
  18. package/build/bsv.min.js +39 -0
  19. package/build/webpack.bundle.config.js +22 -0
  20. package/build/webpack.config.js +18 -0
  21. package/build/webpack.covenant.config.js +27 -0
  22. package/build/webpack.gdaf.config.js +54 -0
  23. package/build/webpack.ltp.config.js +17 -0
  24. package/build/webpack.script-helper.config.js +27 -0
  25. package/build/webpack.security.config.js +23 -0
  26. package/build/webpack.smartcontract.config.js +25 -0
  27. package/build/webpack.subproject.config.js +6 -0
  28. package/bundle-entry.js +341 -0
  29. package/complete_ltp_demo.js +511 -0
  30. package/covenant-entry.js +44 -0
  31. package/docs/pushtx-key-insights.md +106 -0
  32. package/gdaf-entry.js +54 -0
  33. package/index.js +272 -5
  34. package/lib/crypto/shamir.js +360 -0
  35. package/lib/gdaf/attestation-signer.js +461 -0
  36. package/lib/gdaf/attestation-verifier.js +600 -0
  37. package/lib/gdaf/did-resolver.js +382 -0
  38. package/lib/gdaf/index.js +471 -0
  39. package/lib/gdaf/schema-validator.js +682 -0
  40. package/lib/gdaf/smartledger-anchor.js +486 -0
  41. package/lib/gdaf/zk-prover.js +507 -0
  42. package/lib/ltp/anchor.js +438 -0
  43. package/lib/ltp/claim.js +1026 -0
  44. package/lib/ltp/index.js +470 -0
  45. package/lib/ltp/obligation.js +945 -0
  46. package/lib/ltp/proof.js +828 -0
  47. package/lib/ltp/registry.js +702 -0
  48. package/lib/ltp/right.js +765 -0
  49. package/lib/smart_contract/API_REFERENCE.md +1 -1
  50. package/lib/smart_contract/EXAMPLES.md +2 -2
  51. package/lib/smart_contract/QUICK_START.md +2 -2
  52. package/lib/smart_contract/README.md +1 -1
  53. package/lib/smart_contract/index.js +4 -0
  54. package/ltp-entry.js +92 -0
  55. package/package.json +91 -20
  56. package/script-helper-entry.js +49 -0
  57. package/security-entry.js +70 -0
  58. package/shamir-entry.js +173 -0
  59. package/shamir_demo.js +121 -0
  60. package/simple_demo.js +204 -0
  61. package/smartcontract-entry.js +133 -0
  62. package/test_shamir.js +221 -0
  63. package/test_standalone_shamir.html +83 -0
  64. package/tests/bundle-completeness-test.html +131 -0
  65. package/tests/bundle-demo.html +476 -0
  66. package/tests/smartcontract-test.html +239 -0
  67. package/tests/standalone-modules-test.html +260 -0
  68. package/tests/test.html +612 -0
  69. package/tests/unpkg-demo.html +194 -0
  70. package/docs/nchain.md +0 -958
@@ -0,0 +1,470 @@
1
+ 'use strict'
2
+
3
+ var LTPRight = require('./right')
4
+ var LTPClaim = require('./claim')
5
+ var LTPAnchor = require('./anchor')
6
+ var LTPProof = require('./proof')
7
+ var LTPRegistry = require('./registry')
8
+ var LTPObligation = require('./obligation')
9
+
10
+ var bsv = require('../../')
11
+ var PrivateKey = bsv.PrivateKey
12
+ var $ = bsv.util.preconditions
13
+
14
+ /**
15
+ * Legal Token Protocol (LTP) - Main Interface
16
+ *
17
+ * The Legal Token Protocol provides a framework for creating, managing,
18
+ * and validating legally interpretable digital tokens that represent
19
+ * rights, obligations, and attestations with cryptographic proof and
20
+ * blockchain anchoring.
21
+ *
22
+ * This interface combines all LTP components to provide a unified API
23
+ * for legal token operations with compliance and audit capabilities.
24
+ */
25
+
26
+ var LTP = function(config) {
27
+ if (!(this instanceof LTP)) {
28
+ return new LTP(config)
29
+ }
30
+
31
+ this.config = config || {}
32
+ this.registry = null
33
+
34
+ // Initialize registry if specified
35
+ if (this.config.registry) {
36
+ this.registry = LTPRegistry.createRegistry(this.config.registry)
37
+ }
38
+
39
+ return this
40
+ }
41
+
42
+ /**
43
+ * Create legal right token
44
+ * @param {Object} rightData - Right token data
45
+ * @param {PrivateKey} privateKey - Signing private key
46
+ * @param {Object} options - Creation options
47
+ * @returns {Object} Created right token
48
+ */
49
+ LTP.prototype.createRightToken = function(rightData, privateKey, options) {
50
+ $.checkArgument(rightData && typeof rightData === 'object', 'Invalid right data')
51
+ $.checkArgument(privateKey instanceof PrivateKey, 'Invalid private key')
52
+
53
+ options = options || {}
54
+
55
+ try {
56
+ // Create the right token
57
+ var token = LTPRight.create(rightData.type, rightData.owner, rightData.owner, rightData, privateKey, options)
58
+
59
+ if (!token) {
60
+ return {
61
+ success: false,
62
+ error: 'Failed to create token'
63
+ }
64
+ }
65
+
66
+ // Add cryptographic proof
67
+ if (options.addProof !== false) {
68
+ var proofResult = LTPProof.createSignature(token, privateKey, {
69
+ purpose: 'assertionMethod'
70
+ })
71
+
72
+ if (proofResult.success) {
73
+ token.proof = proofResult.proof
74
+ token.tokenHash = proofResult.tokenHash
75
+ }
76
+ }
77
+
78
+ // Anchor to blockchain if requested
79
+ if (options.anchor) {
80
+ var anchorResult = LTPAnchor.anchorToken(token, options.anchor)
81
+ if (anchorResult.success) {
82
+ token.anchorTx = anchorResult.txid
83
+ token.anchorBlock = anchorResult.blockHeight
84
+ }
85
+ }
86
+
87
+ // Register token if registry is available
88
+ if (this.registry && options.register !== false) {
89
+ var regResult = LTPRegistry.registerToken(this.registry, token, {
90
+ registeredBy: options.registeredBy || 'SYSTEM'
91
+ })
92
+
93
+ if (regResult.success) {
94
+ token.registrationId = regResult.registrationId
95
+ token.registrationStatus = regResult.status
96
+ }
97
+ }
98
+
99
+ return {
100
+ success: true,
101
+ token: token,
102
+ type: 'LegalRightToken'
103
+ }
104
+
105
+ } catch (error) {
106
+ return {
107
+ success: false,
108
+ error: error.message
109
+ }
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Validate legal claim
115
+ * @param {Object} claimData - Claim data to validate
116
+ * @param {String} schemaType - Schema type for validation
117
+ * @returns {Object} Validation result
118
+ */
119
+ LTP.prototype.validateClaim = function(claimData, schemaType) {
120
+ return LTPClaim.validate(claimData, schemaType)
121
+ }
122
+
123
+ /**
124
+ * Create selective disclosure proof
125
+ * @param {Object} token - Token for disclosure
126
+ * @param {Array} revealedFields - Fields to reveal
127
+ * @param {String} nonce - Proof nonce
128
+ * @returns {Object} Selective disclosure proof
129
+ */
130
+ LTP.prototype.createSelectiveDisclosure = function(token, revealedFields, nonce) {
131
+ return LTPProof.createSelectiveDisclosure(token, revealedFields, nonce)
132
+ }
133
+
134
+ /**
135
+ * Verify token signature
136
+ * @param {Object} token - Token to verify
137
+ * @param {String} publicKey - Signer's public key
138
+ * @returns {Object} Verification result
139
+ */
140
+ LTP.prototype.verifyToken = function(token, publicKey) {
141
+ if (!token || !token.proof) {
142
+ return {
143
+ valid: false,
144
+ error: 'No proof found in token'
145
+ }
146
+ }
147
+
148
+ return LTPProof.verifySignature(token, publicKey)
149
+ }
150
+
151
+ /**
152
+ * Transfer right token
153
+ * @param {Object} token - Token to transfer
154
+ * @param {String} newOwner - New owner identifier
155
+ * @param {PrivateKey} ownerKey - Current owner's private key
156
+ * @param {Object} options - Transfer options
157
+ * @returns {Object} Transfer result
158
+ */
159
+ LTP.prototype.transferRight = function(token, newOwner, ownerKey, options) {
160
+ $.checkArgument(token && typeof token === 'object', 'Invalid token')
161
+ $.checkArgument(typeof newOwner === 'string', 'New owner must be string')
162
+ $.checkArgument(ownerKey instanceof PrivateKey, 'Invalid owner key')
163
+
164
+ options = options || {}
165
+
166
+ try {
167
+ // Verify current ownership
168
+ var verification = this.verifyToken(token, ownerKey.toPublicKey().toString())
169
+ if (!verification.valid) {
170
+ return {
171
+ success: false,
172
+ error: 'Invalid token or ownership proof'
173
+ }
174
+ }
175
+
176
+ // Create transfer
177
+ var transferResult = LTPRight.transfer(token, newOwner, ownerKey, options)
178
+
179
+ var transferredToken = transferResult.newToken
180
+ var transferProof = transferResult.transferProof
181
+
182
+ // Add new proof with new owner
183
+ var proofResult = LTPProof.createSignature(transferredToken, ownerKey, {
184
+ purpose: 'authentication'
185
+ })
186
+
187
+ if (proofResult.success) {
188
+ transferredToken.proof = proofResult.proof
189
+ transferredToken.tokenHash = proofResult.tokenHash
190
+ }
191
+
192
+ // Anchor transfer if requested
193
+ if (options.anchor) {
194
+ var anchorResult = LTPAnchor.anchorToken(transferredToken, options.anchor)
195
+ if (anchorResult.success) {
196
+ transferredToken.transferAnchorTx = anchorResult.txid
197
+ }
198
+ }
199
+
200
+ // Update registry if available
201
+ if (this.registry) {
202
+ // Register the new token state
203
+ var regResult = LTPRegistry.registerToken(this.registry, transferredToken, {
204
+ registeredBy: options.registeredBy || newOwner,
205
+ metadata: {
206
+ transferFrom: token.owner,
207
+ transferTo: newOwner,
208
+ originalTokenId: token.id
209
+ }
210
+ })
211
+
212
+ if (regResult.success) {
213
+ transferredToken.registrationId = regResult.registrationId
214
+ }
215
+ }
216
+
217
+ return {
218
+ success: true,
219
+ token: transferredToken,
220
+ transferId: transferProof.id,
221
+ transferredAt: transferProof.issuanceDate,
222
+ transferProof: transferProof
223
+ }
224
+
225
+ } catch (error) {
226
+ return {
227
+ success: false,
228
+ error: error.message
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Create obligation from right
235
+ * @param {Object} rightToken - Right token
236
+ * @param {Object} obligationData - Obligation details
237
+ * @param {PrivateKey} privateKey - Signing key
238
+ * @returns {Object} Created obligation
239
+ */
240
+ LTP.prototype.createObligation = function(rightToken, obligationData, privateKey) {
241
+ $.checkArgument(rightToken && typeof rightToken === 'object', 'Invalid right token')
242
+ $.checkArgument(obligationData && typeof obligationData === 'object', 'Invalid obligation data')
243
+ $.checkArgument(privateKey instanceof PrivateKey, 'Invalid private key')
244
+
245
+ try {
246
+ // Create obligation token
247
+ var obligation = LTPRight.createObligation(rightToken, obligationData.obligor, obligationData, privateKey)
248
+
249
+ if (!obligation) {
250
+ return {
251
+ success: false,
252
+ error: 'Failed to create obligation'
253
+ }
254
+ }
255
+
256
+ // Add proof
257
+ var proofResult = LTPProof.createSignature(obligation, privateKey, {
258
+ purpose: 'assertionMethod'
259
+ })
260
+
261
+ if (proofResult.success) {
262
+ obligation.proof = proofResult.proof
263
+ obligation.tokenHash = proofResult.tokenHash
264
+ }
265
+
266
+ return {
267
+ success: true,
268
+ obligation: obligation,
269
+ rightTokenId: rightToken.id
270
+ }
271
+
272
+ } catch (error) {
273
+ return {
274
+ success: false,
275
+ error: error.message
276
+ }
277
+ }
278
+ }
279
+
280
+ /**
281
+ * Revoke token
282
+ * @param {String} tokenId - Token ID to revoke
283
+ * @param {Object} revocationData - Revocation details
284
+ * @param {String} authority - Revoking authority
285
+ * @returns {Object} Revocation result
286
+ */
287
+ LTP.prototype.revokeToken = function(tokenId, revocationData, authority) {
288
+ $.checkArgument(typeof tokenId === 'string', 'Token ID must be string')
289
+ $.checkArgument(revocationData && typeof revocationData === 'object', 'Invalid revocation data')
290
+ $.checkArgument(typeof authority === 'string', 'Authority must be string')
291
+
292
+ if (!this.registry) {
293
+ return {
294
+ success: false,
295
+ error: 'No registry available for revocation'
296
+ }
297
+ }
298
+
299
+ return LTPRegistry.revokeToken(this.registry, tokenId, {
300
+ reason: revocationData.reason,
301
+ revokedBy: authority,
302
+ legalBasis: revocationData.legalBasis,
303
+ evidence: revocationData.evidence
304
+ })
305
+ }
306
+
307
+ /**
308
+ * Check token status
309
+ * @param {String} tokenId - Token ID to check
310
+ * @returns {Object} Token status
311
+ */
312
+ LTP.prototype.checkTokenStatus = function(tokenId) {
313
+ $.checkArgument(typeof tokenId === 'string', 'Token ID must be string')
314
+
315
+ if (!this.registry) {
316
+ return {
317
+ found: false,
318
+ error: 'No registry available'
319
+ }
320
+ }
321
+
322
+ return LTPRegistry.checkTokenStatus(this.registry, tokenId)
323
+ }
324
+
325
+ /**
326
+ * Search tokens
327
+ * @param {Object} criteria - Search criteria
328
+ * @returns {Object} Search results
329
+ */
330
+ LTP.prototype.searchTokens = function(criteria) {
331
+ if (!this.registry) {
332
+ return {
333
+ success: false,
334
+ error: 'No registry available'
335
+ }
336
+ }
337
+
338
+ return LTPRegistry.searchTokens(this.registry, criteria)
339
+ }
340
+
341
+ /**
342
+ * Get registry statistics
343
+ * @returns {Object} Registry statistics
344
+ */
345
+ LTP.prototype.getRegistryStats = function() {
346
+ if (!this.registry) {
347
+ return {
348
+ error: 'No registry available'
349
+ }
350
+ }
351
+
352
+ return LTPRegistry.getStatistics(this.registry)
353
+ }
354
+
355
+ /**
356
+ * Create legal validity proof
357
+ * @param {Object} token - Token to prove
358
+ * @param {Object} jurisdiction - Jurisdiction rules
359
+ * @param {String} nonce - Proof nonce
360
+ * @returns {Object} Legal validity proof
361
+ */
362
+ LTP.prototype.createLegalValidityProof = function(token, jurisdiction, nonce) {
363
+ return LTPProof.createLegalValidityProof(token, jurisdiction, nonce)
364
+ }
365
+
366
+ /**
367
+ * Anchor multiple tokens in batch
368
+ * @param {Array} tokens - Tokens to anchor
369
+ * @param {Object} options - Anchoring options
370
+ * @returns {Object} Batch anchor result
371
+ */
372
+ LTP.prototype.anchorTokenBatch = function(tokens, options) {
373
+ return LTPAnchor.anchorBatch(tokens, options)
374
+ }
375
+
376
+ /**
377
+ * Verify blockchain anchor
378
+ * @param {Object} token - Token with anchor
379
+ * @param {String} txid - Transaction ID
380
+ * @returns {Object} Verification result
381
+ */
382
+ LTP.prototype.verifyAnchor = function(token, txid) {
383
+ return LTPAnchor.verifyAnchor(token, txid)
384
+ }
385
+
386
+ /**
387
+ * Get supported right types
388
+ * @returns {Object} Right types
389
+ */
390
+ LTP.prototype.getRightTypes = function() {
391
+ return LTPRight.getRightTypes()
392
+ }
393
+
394
+ /**
395
+ * Get supported claim schemas
396
+ * @returns {Object} Claim schemas
397
+ */
398
+ LTP.prototype.getClaimSchemas = function() {
399
+ return LTPClaim.getSchemas()
400
+ }
401
+
402
+ /**
403
+ * Create registry
404
+ * @param {Object} config - Registry configuration
405
+ * @returns {Object} Created registry
406
+ */
407
+ LTP.prototype.createRegistry = function(config) {
408
+ this.registry = LTPRegistry.createRegistry(config)
409
+ return this.registry
410
+ }
411
+
412
+ /**
413
+ * Set registry
414
+ * @param {Object} registry - Registry instance
415
+ */
416
+ LTP.prototype.setRegistry = function(registry) {
417
+ $.checkArgument(registry && typeof registry === 'object', 'Invalid registry')
418
+ this.registry = registry
419
+ }
420
+
421
+ // Static methods for direct access without instantiation
422
+
423
+ /**
424
+ * Create LTP instance
425
+ * @param {Object} config - Configuration
426
+ * @returns {LTP} LTP instance
427
+ */
428
+ LTP.create = function(config) {
429
+ return new LTP(config)
430
+ }
431
+
432
+ /**
433
+ * Create right token (static)
434
+ */
435
+ LTP.createRightToken = function(rightData, privateKey, options) {
436
+ var ltp = new LTP()
437
+ return ltp.createRightToken(rightData, privateKey, options)
438
+ }
439
+
440
+ /**
441
+ * Validate claim (static)
442
+ */
443
+ LTP.validateClaim = function(claimData, schemaType) {
444
+ return LTPClaim.validate(claimData, schemaType)
445
+ }
446
+
447
+ /**
448
+ * Verify token (static)
449
+ */
450
+ LTP.verifyToken = function(token, publicKey) {
451
+ var ltp = new LTP()
452
+ return ltp.verifyToken(token, publicKey)
453
+ }
454
+
455
+ /**
456
+ * Create registry (static)
457
+ */
458
+ LTP.createRegistry = function(config) {
459
+ return LTPRegistry.createRegistry(config)
460
+ }
461
+
462
+ // Export component modules for advanced usage
463
+ LTP.Right = LTPRight
464
+ LTP.Claim = LTPClaim
465
+ LTP.Anchor = LTPAnchor
466
+ LTP.Proof = LTPProof
467
+ LTP.Registry = LTPRegistry
468
+ LTP.Obligation = LTPObligation
469
+
470
+ module.exports = LTP