@degenerate/sharedata 0.4.0 → 0.7.0

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 (48) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -0
  4. package/dist/library/apply-library-function-transaction.d.ts +60 -0
  5. package/dist/library/apply-library-function-transaction.d.ts.map +1 -0
  6. package/dist/library/apply-library-function-transaction.js +73 -0
  7. package/dist/manager/share-data-manager.d.ts +6 -2
  8. package/dist/manager/share-data-manager.d.ts.map +1 -1
  9. package/dist/manager/share-data-manager.js +7 -3
  10. package/dist/persistence/deserialize-share-data.d.ts +8 -1
  11. package/dist/persistence/deserialize-share-data.d.ts.map +1 -1
  12. package/dist/persistence/deserialize-share-data.js +14 -2
  13. package/dist/persistence/file-share-data-repository.d.ts +8 -2
  14. package/dist/persistence/file-share-data-repository.d.ts.map +1 -1
  15. package/dist/persistence/file-share-data-repository.js +9 -3
  16. package/dist/persistence/firestore-share-data-repository.d.ts +3 -2
  17. package/dist/persistence/firestore-share-data-repository.d.ts.map +1 -1
  18. package/dist/persistence/firestore-share-data-repository.js +4 -3
  19. package/dist/persistence/in-memory-share-data-repository.d.ts +8 -1
  20. package/dist/persistence/in-memory-share-data-repository.d.ts.map +1 -1
  21. package/dist/persistence/in-memory-share-data-repository.js +9 -3
  22. package/dist/share-data/creation/create-share-data-initial-crypto.d.ts +4 -5
  23. package/dist/share-data/creation/create-share-data-initial-crypto.d.ts.map +1 -1
  24. package/dist/share-data/creation/create-share-data-initial-crypto.js +6 -7
  25. package/dist/share-data/helpers/config/default-share-data-string.config.d.ts.map +1 -1
  26. package/dist/share-data/helpers/config/default-share-data-string.config.js +5 -0
  27. package/dist/share-data/proofs/creation/build-transaction-proposal-signer.d.ts +5 -10
  28. package/dist/share-data/proofs/creation/build-transaction-proposal-signer.d.ts.map +1 -1
  29. package/dist/share-data/proofs/creation/build-transaction-proposal-signer.js +1 -3
  30. package/dist/share-data/proofs/creation/create-share-data-proposal-proof.d.ts +3 -4
  31. package/dist/share-data/proofs/creation/create-share-data-proposal-proof.d.ts.map +1 -1
  32. package/dist/share-data/proofs/creation/create-share-data-proposal-proof.js +5 -6
  33. package/dist/share-data/proofs/creation/create-transaction-proposal-proof.d.ts +3 -4
  34. package/dist/share-data/proofs/creation/create-transaction-proposal-proof.d.ts.map +1 -1
  35. package/dist/share-data/proofs/creation/create-transaction-proposal-proof.js +5 -6
  36. package/dist/share-data/proofs/testing/test-proof-signer.d.ts +8 -8
  37. package/dist/share-data/proofs/testing/test-proof-signer.d.ts.map +1 -1
  38. package/dist/share-data/proofs/testing/test-proof-signer.js +17 -19
  39. package/dist/share-data/proofs/verification/verify-share-data-proposal-proof.d.ts +4 -6
  40. package/dist/share-data/proofs/verification/verify-share-data-proposal-proof.d.ts.map +1 -1
  41. package/dist/share-data/proofs/verification/verify-share-data-proposal-proof.js +8 -57
  42. package/dist/share-data/proofs/verification/verify-transaction-proposal-proof.d.ts +4 -4
  43. package/dist/share-data/proofs/verification/verify-transaction-proposal-proof.d.ts.map +1 -1
  44. package/dist/share-data/proofs/verification/verify-transaction-proposal-proof.js +8 -54
  45. package/dist/share-data/share-data.d.ts +41 -7
  46. package/dist/share-data/share-data.d.ts.map +1 -1
  47. package/dist/share-data/share-data.js +93 -11
  48. package/package.json +4 -4
@@ -31,6 +31,7 @@ class ShareData {
31
31
  this._signingCurve = signingCurve;
32
32
  this._structuralProofs = [];
33
33
  this._authorizedSigners = [];
34
+ this._ruleReferences = [];
34
35
  }
35
36
  /**
36
37
  * This initializes the cryptographic properties of the share data
@@ -150,6 +151,14 @@ class ShareData {
150
151
  get authorizedSigners() {
151
152
  return this._authorizedSigners;
152
153
  }
154
+ /**
155
+ * The library rule references governing this block. Each binding pairs a
156
+ * library reference (id + pinned `currentHash`) with the root property it
157
+ * governs. Frozen after `initialize()` like rules/functions.
158
+ */
159
+ get ruleReferences() {
160
+ return this._ruleReferences;
161
+ }
153
162
  /**
154
163
  * This gets the transaction history for the share block
155
164
  */
@@ -200,6 +209,64 @@ class ShareData {
200
209
  this._structuralProofs.push(cryptoProof);
201
210
  this._rules.push(rule);
202
211
  }
212
+ /**
213
+ * Registers a library rule reference governing this block. The reference
214
+ * (id + pinned `currentHash`) plus the governed `targettedProperty` are
215
+ * folded into the signed structure, so a tampered snapshot cannot add or
216
+ * retarget a rule reference without breaking the hash chain. Creation-time
217
+ * only (rejected once locked); verified against the pre-append state,
218
+ * mirroring `addRule` / `addFunction`.
219
+ */
220
+ addRuleReference(binding, cryptoProof) {
221
+ if (this._isLocked) {
222
+ throw new generic_1.ShareDataIsLockedError(this._id, generic_1.ShareDataTypes.RULE);
223
+ }
224
+ // Pre-append verification: the signer signed over the state before this
225
+ // reference was added.
226
+ this.verifyStructuralProof(cryptoProof);
227
+ this._structuralProofs.push(cryptoProof);
228
+ this._ruleReferences.push(binding);
229
+ }
230
+ /**
231
+ * Injects the library store used to resolve this block's rule references at
232
+ * change time. This is runtime infrastructure — it is NOT folded into the
233
+ * canonical hash or the signed structure, so setting it does not affect any
234
+ * proof. Replacing the store clears the resolved-rule cache.
235
+ */
236
+ setLibraryStore(store) {
237
+ this._libraryStore = store;
238
+ this._hydratedReferenceRules = undefined;
239
+ }
240
+ /**
241
+ * Resolves `_ruleReferences` into executable applied rules via the library
242
+ * store, materializing the lean "reference" block into the rules needed to
243
+ * evaluate a change. The result is cached (references are pinned to
244
+ * content-addressed versions, so the resolution is stable). Fail-closed: if
245
+ * references exist but no store was injected, evaluation refuses to proceed
246
+ * rather than silently dropping the governing rules.
247
+ */
248
+ async resolveReferenceRules() {
249
+ if (this._ruleReferences.length === 0) {
250
+ return [];
251
+ }
252
+ if (this._hydratedReferenceRules) {
253
+ return this._hydratedReferenceRules;
254
+ }
255
+ if (!this._libraryStore) {
256
+ throw new generic_1.ShareDataLibraryStoreNotSetError(this._id);
257
+ }
258
+ const resolved = [];
259
+ for (const binding of this._ruleReferences) {
260
+ const { version } = await (0, generic_1.resolveLibraryReference)(binding.reference, this._libraryStore);
261
+ // A rule entity's source is the canonical applied-rule DEFINITION;
262
+ // the binding carries the routed `targettedProperty` as a backstop
263
+ // for sources that predate embedding it.
264
+ const rule = JSON.parse(version.source);
265
+ resolved.push({ ...rule, targettedProperty: rule.targettedProperty ?? binding.targettedProperty });
266
+ }
267
+ this._hydratedReferenceRules = resolved;
268
+ return resolved;
269
+ }
203
270
  /**
204
271
  * Delegates `signerPublicKey` the right to propose runtime transactions for
205
272
  * the given `functionIds` (and ONLY those — least privilege). Creation-time
@@ -320,7 +387,7 @@ class ShareData {
320
387
  * @param proposedData the proposed data
321
388
  * @param transactionFunction the function that was attempted
322
389
  */
323
- addTransactionNotValidated(proposedData, transactionFunction) {
390
+ async addTransactionNotValidated(proposedData, transactionFunction) {
324
391
  return this.evaluateAndApply(proposedData, transactionFunction);
325
392
  }
326
393
  /**
@@ -356,7 +423,7 @@ class ShareData {
356
423
  * @param transactionFunction the function producing `proposedData`
357
424
  * @param proof the transaction proposal proof authorizing the change
358
425
  */
359
- addTransaction(proposedData, transactionFunction, proof) {
426
+ async addTransaction(proposedData, transactionFunction, proof) {
360
427
  // Gate 1: function registered on this block.
361
428
  if (!this._functions.includes(transactionFunction)) {
362
429
  throw new Error('The function is not known');
@@ -392,13 +459,28 @@ class ShareData {
392
459
  * (proof already verified by the caller). When `proof` is supplied it is
393
460
  * recorded on the history entry so the audit log carries the signature.
394
461
  */
395
- evaluateAndApply(proposedData, transactionFunction, proof) {
462
+ async evaluateAndApply(proposedData, transactionFunction, proof) {
396
463
  // make sure the function is known.
397
464
  if (!this._functions.includes(transactionFunction)) {
398
465
  // throw ShareDataFunctionBeingAppliedDoesNotExistOnShareDataError
399
466
  throw new Error('The function is not known');
400
467
  }
401
- const rulesOutcome = (0, rules_1.startRuleEvaluation)(this._id, transactionFunction, this.functions, this._rules, this._currentState, proposedData);
468
+ // Rule gate. The data must actually change (the engine's guard, inlined
469
+ // so the no-op path stays decoupled from the rules module), then the
470
+ // block's rules are evaluated in the SANDBOX (untrusted) under the
471
+ // engine's trusted per-property routing. `evaluateRulesViaSandbox` is
472
+ // faithfulness-proven to match `startRuleEvaluation`'s verdict.
473
+ if (JSON.stringify(this._currentState) === JSON.stringify(proposedData)) {
474
+ throw new generic_1.ShareDataCurrentAndProposalDataMatchesError(this._id);
475
+ }
476
+ // Hydrate any library rule references into executable rules and evaluate
477
+ // them alongside the inline `_rules`. When the block carries no
478
+ // references (every block today), this resolves to `_rules` unchanged.
479
+ const referenceRules = await this.resolveReferenceRules();
480
+ const effectiveRules = referenceRules.length > 0
481
+ ? [...this._rules, ...referenceRules]
482
+ : this._rules;
483
+ const rulesOutcome = await (0, rules_1.evaluateRulesViaSandbox)(effectiveRules, this._currentState, proposedData);
402
484
  const transactionTime = Date.now();
403
485
  const transactionId = (this._transactionHistory.length + 1).toString();
404
486
  const transaction = {
@@ -426,6 +508,9 @@ class ShareData {
426
508
  // return share data with a new ID
427
509
  const newShareData = new ShareData(`${this._id}-${transactionNumber}`, publicKey);
428
510
  newShareData._rules = [...this._rules];
511
+ newShareData._ruleReferences = [...this._ruleReferences];
512
+ // Carry the resolver so a copied block can still hydrate its references.
513
+ newShareData._libraryStore = this._libraryStore;
429
514
  newShareData._functions = [...this._functions];
430
515
  newShareData._transactionHistory = [...this._transactionHistory];
431
516
  newShareData._events = [...this._events];
@@ -448,8 +533,8 @@ class ShareData {
448
533
  * success; throws `ShareDataStructuralProofFailedError` on any mismatch
449
534
  * (hash, previousHash, signature, or purpose).
450
535
  */
451
- verifyStructuralProof(proof, curve = this._signingCurve) {
452
- return (0, verify_share_data_proposal_proof_1.verifyShareDataProposalProof)(proof, this, curve);
536
+ verifyStructuralProof(proof) {
537
+ return (0, verify_share_data_proposal_proof_1.verifyShareDataProposalProof)(proof, this);
453
538
  }
454
539
  /**
455
540
  * Verifies that `proof` authorizes applying a transaction that moves this
@@ -459,8 +544,8 @@ class ShareData {
459
544
  * verify against any other. Returns `true`; throws
460
545
  * `ShareDataTransactionProofFailedError` on any mismatch.
461
546
  */
462
- verifyTransactionProof(proof, proposedData, curve = this._signingCurve) {
463
- return (0, verify_transaction_proposal_proof_1.verifyTransactionProposalProof)(proof, this, proposedData, curve);
547
+ verifyTransactionProof(proof, proposedData) {
548
+ return (0, verify_transaction_proposal_proof_1.verifyTransactionProposalProof)(proof, this, proposedData);
464
549
  }
465
550
  //#endregion Crypto Proofs
466
551
  //#region Structure Proofs
@@ -485,6 +570,3 @@ class ShareData {
485
570
  }
486
571
  }
487
572
  exports.ShareData = ShareData;
488
- const itiem2 = new ShareData('1', '1234');
489
- const item = new ShareData('1', '1234');
490
- item.initialize();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@degenerate/sharedata",
3
- "version": "0.4.0",
3
+ "version": "0.7.0",
4
4
  "description": "This contains the ShareData Adaptive Framework Ecosystem (SAFE) network core code",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -49,10 +49,10 @@
49
49
  "url": "https://github.com/syncrude/safe/issues"
50
50
  },
51
51
  "homepage": "https://github.com/syncrude/safe#readme",
52
- "gitHead": "9453d9ab785e03fd01eb5b408ed0e3ca22baefd5",
52
+ "gitHead": "1f747e160c9022863c7b2d7b8a6a157f204be95b",
53
53
  "dependencies": {
54
- "@degenerate/generic": "^0.4.0",
55
- "@degenerate/rules": "^0.4.0",
54
+ "@degenerate/generic": "^0.7.0",
55
+ "@degenerate/rules": "^0.7.0",
56
56
  "zod": "^4.4.3"
57
57
  },
58
58
  "engines": {