@degenerate/generic 0.6.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 (103) hide show
  1. package/dist/errors/index.d.ts +1 -0
  2. package/dist/errors/index.d.ts.map +1 -1
  3. package/dist/errors/index.js +1 -0
  4. package/dist/errors/library/library-errors.d.ts +101 -0
  5. package/dist/errors/library/library-errors.d.ts.map +1 -0
  6. package/dist/errors/library/library-errors.js +133 -0
  7. package/dist/errors/share-data/share-data-errors.d.ts +21 -0
  8. package/dist/errors/share-data/share-data-errors.d.ts.map +1 -1
  9. package/dist/errors/share-data/share-data-errors.js +30 -1
  10. package/dist/helpers/apply-share-function.d.ts +23 -0
  11. package/dist/helpers/apply-share-function.d.ts.map +1 -0
  12. package/dist/helpers/apply-share-function.js +32 -0
  13. package/dist/helpers/index.d.ts +1 -0
  14. package/dist/helpers/index.d.ts.map +1 -1
  15. package/dist/helpers/index.js +1 -0
  16. package/dist/index.d.ts +2 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +2 -0
  19. package/dist/library/authorize-library.d.ts +17 -0
  20. package/dist/library/authorize-library.d.ts.map +1 -0
  21. package/dist/library/authorize-library.js +39 -0
  22. package/dist/library/build-library.d.ts +49 -0
  23. package/dist/library/build-library.d.ts.map +1 -0
  24. package/dist/library/build-library.js +119 -0
  25. package/dist/library/caching-library-store.d.ts +19 -0
  26. package/dist/library/caching-library-store.d.ts.map +1 -0
  27. package/dist/library/caching-library-store.js +56 -0
  28. package/dist/library/canonical-json.d.ts +12 -0
  29. package/dist/library/canonical-json.d.ts.map +1 -0
  30. package/dist/library/canonical-json.js +30 -0
  31. package/dist/library/compile-rule-source.d.ts +21 -0
  32. package/dist/library/compile-rule-source.d.ts.map +1 -0
  33. package/dist/library/compile-rule-source.js +91 -0
  34. package/dist/library/evaluate-library-rules.d.ts +36 -0
  35. package/dist/library/evaluate-library-rules.d.ts.map +1 -0
  36. package/dist/library/evaluate-library-rules.js +47 -0
  37. package/dist/library/evaluate-library.d.ts +34 -0
  38. package/dist/library/evaluate-library.d.ts.map +1 -0
  39. package/dist/library/evaluate-library.js +66 -0
  40. package/dist/library/in-memory-library-store.d.ts +22 -0
  41. package/dist/library/in-memory-library-store.d.ts.map +1 -0
  42. package/dist/library/in-memory-library-store.js +92 -0
  43. package/dist/library/index.d.ts +18 -0
  44. package/dist/library/index.d.ts.map +1 -0
  45. package/dist/library/index.js +33 -0
  46. package/dist/library/library-hash.d.ts +9 -0
  47. package/dist/library/library-hash.d.ts.map +1 -0
  48. package/dist/library/library-hash.js +14 -0
  49. package/dist/library/library-models.d.ts +134 -0
  50. package/dist/library/library-models.d.ts.map +1 -0
  51. package/dist/library/library-models.js +84 -0
  52. package/dist/library/library-proof.d.ts +40 -0
  53. package/dist/library/library-proof.d.ts.map +1 -0
  54. package/dist/library/library-proof.js +44 -0
  55. package/dist/library/library-reference.d.ts +75 -0
  56. package/dist/library/library-reference.d.ts.map +1 -0
  57. package/dist/library/library-reference.js +98 -0
  58. package/dist/library/library-server.d.ts +17 -0
  59. package/dist/library/library-server.d.ts.map +1 -0
  60. package/dist/library/library-server.js +45 -0
  61. package/dist/library/library-store.d.ts +37 -0
  62. package/dist/library/library-store.d.ts.map +1 -0
  63. package/dist/library/library-store.js +2 -0
  64. package/dist/library/remote-library-models.d.ts +54 -0
  65. package/dist/library/remote-library-models.d.ts.map +1 -0
  66. package/dist/library/remote-library-models.js +2 -0
  67. package/dist/library/remote-library-store.d.ts +15 -0
  68. package/dist/library/remote-library-store.d.ts.map +1 -0
  69. package/dist/library/remote-library-store.js +59 -0
  70. package/dist/library/verify-library.d.ts +21 -0
  71. package/dist/library/verify-library.d.ts.map +1 -0
  72. package/dist/library/verify-library.js +86 -0
  73. package/dist/models/functions/share-data-function-models.d.ts +6 -74
  74. package/dist/models/functions/share-data-function-models.d.ts.map +1 -1
  75. package/dist/models/functions/share-data-function-models.js +7 -65
  76. package/dist/models/share-data/share-data-models.d.ts +12 -0
  77. package/dist/models/share-data/share-data-models.d.ts.map +1 -1
  78. package/dist/models/shareblock/shareblock-models.js +6 -6
  79. package/dist/sandbox/in-process-executor.d.ts +13 -0
  80. package/dist/sandbox/in-process-executor.d.ts.map +1 -0
  81. package/dist/sandbox/in-process-executor.js +31 -0
  82. package/dist/sandbox/index.d.ts +8 -0
  83. package/dist/sandbox/index.d.ts.map +1 -0
  84. package/dist/sandbox/index.js +23 -0
  85. package/dist/sandbox/remote-sandbox-executor.d.ts +12 -0
  86. package/dist/sandbox/remote-sandbox-executor.d.ts.map +1 -0
  87. package/dist/sandbox/remote-sandbox-executor.js +49 -0
  88. package/dist/sandbox/remote-sandbox-models.d.ts +37 -0
  89. package/dist/sandbox/remote-sandbox-models.d.ts.map +1 -0
  90. package/dist/sandbox/remote-sandbox-models.js +2 -0
  91. package/dist/sandbox/rule-evaluator.d.ts +32 -0
  92. package/dist/sandbox/rule-evaluator.d.ts.map +1 -0
  93. package/dist/sandbox/rule-evaluator.js +23 -0
  94. package/dist/sandbox/run-sandboxed.d.ts +24 -0
  95. package/dist/sandbox/run-sandboxed.d.ts.map +1 -0
  96. package/dist/sandbox/run-sandboxed.js +88 -0
  97. package/dist/sandbox/sandbox-models.d.ts +40 -0
  98. package/dist/sandbox/sandbox-models.d.ts.map +1 -0
  99. package/dist/sandbox/sandbox-models.js +2 -0
  100. package/dist/sandbox/sandbox-server.d.ts +22 -0
  101. package/dist/sandbox/sandbox-server.d.ts.map +1 -0
  102. package/dist/sandbox/sandbox-server.js +62 -0
  103. package/package.json +2 -2
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryLibraryStore = void 0;
4
+ const library_errors_1 = require("../errors/library/library-errors");
5
+ const verify_library_1 = require("./verify-library");
6
+ const clone = (value) => JSON.parse(JSON.stringify(value));
7
+ /**
8
+ * Asserts that `next` extends `existing` append-only: it must keep every
9
+ * existing version (by `currentHash`, in order) and may only grow. Truncation
10
+ * or divergence is a {@link LibraryStoreConflictError}.
11
+ */
12
+ const assertAppendOnly = (existing, next) => {
13
+ if (next.versions.length < existing.versions.length) {
14
+ throw new library_errors_1.LibraryStoreConflictError(existing.id, 'history truncated');
15
+ }
16
+ for (let i = 0; i < existing.versions.length; i += 1) {
17
+ if (existing.versions[i].currentHash !== next.versions[i].currentHash) {
18
+ throw new library_errors_1.LibraryStoreConflictError(existing.id, `version ${i} diverges from stored history`);
19
+ }
20
+ }
21
+ };
22
+ /**
23
+ * In-memory {@link LibraryStore}. Deep-copies on the way in and out so callers
24
+ * cannot mutate stored state by reference, and re-verifies on every read.
25
+ *
26
+ * The optional `seed` models data already present in a backend: it is stored
27
+ * WITHOUT verification, precisely so that the re-verify-on-read contract is what
28
+ * catches a tampered backing record.
29
+ */
30
+ class InMemoryLibraryStore {
31
+ constructor(seed = []) {
32
+ this.entities = new Map();
33
+ for (const entity of seed) {
34
+ this.entities.set(entity.id, clone(entity));
35
+ }
36
+ }
37
+ async get(id) {
38
+ const stored = this.entities.get(id);
39
+ if (!stored) {
40
+ return undefined;
41
+ }
42
+ (0, verify_library_1.verifyLibraryEntity)(stored); // never trust the backing data
43
+ return clone(stored);
44
+ }
45
+ async getVersion(id, currentHash) {
46
+ const entity = await this.get(id);
47
+ return entity?.versions.find((version) => version.currentHash === currentHash);
48
+ }
49
+ async has(id) {
50
+ return this.entities.has(id);
51
+ }
52
+ async list(filter) {
53
+ return this.search({ kind: filter?.kind });
54
+ }
55
+ async search(query) {
56
+ const text = query.text?.trim().toLowerCase();
57
+ const result = [];
58
+ for (const stored of this.entities.values()) {
59
+ (0, verify_library_1.verifyLibraryEntity)(stored); // re-verify before exposing
60
+ if (query.kind && stored.kind !== query.kind) {
61
+ continue;
62
+ }
63
+ if (query.ownerPublicKey && stored.owner.publicKey !== query.ownerPublicKey) {
64
+ continue;
65
+ }
66
+ if (text && !`${stored.name} ${stored.description}`.toLowerCase().includes(text)) {
67
+ continue;
68
+ }
69
+ result.push(clone(stored));
70
+ }
71
+ return result;
72
+ }
73
+ async save(entity) {
74
+ (0, verify_library_1.verifyLibraryEntity)(entity); // verify on write
75
+ const existing = this.entities.get(entity.id);
76
+ if (existing) {
77
+ // Ids are content-addressed (id = hash of the first version's
78
+ // source), so two owners who publish identical source collide on id.
79
+ // Reject the second — an existing id may not be re-registered under a
80
+ // different owner (no id hijack); the entity already exists to reuse.
81
+ if (existing.owner.publicKey !== entity.owner.publicKey) {
82
+ throw new library_errors_1.LibraryStoreConflictError(entity.id, 'id already registered by another owner');
83
+ }
84
+ assertAppendOnly(existing, entity);
85
+ }
86
+ this.entities.set(entity.id, clone(entity));
87
+ }
88
+ async delete(id) {
89
+ this.entities.delete(id);
90
+ }
91
+ }
92
+ exports.InMemoryLibraryStore = InMemoryLibraryStore;
@@ -0,0 +1,18 @@
1
+ export * from './library-models';
2
+ export * from './canonical-json';
3
+ export * from './library-hash';
4
+ export * from './library-proof';
5
+ export * from './authorize-library';
6
+ export * from './build-library';
7
+ export * from './verify-library';
8
+ export * from './library-store';
9
+ export * from './in-memory-library-store';
10
+ export * from './caching-library-store';
11
+ export * from './remote-library-models';
12
+ export * from './remote-library-store';
13
+ export * from './library-server';
14
+ export * from './evaluate-library';
15
+ export * from './library-reference';
16
+ export * from './evaluate-library-rules';
17
+ export * from './compile-rule-source';
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/library/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./library-models"), exports);
18
+ __exportStar(require("./canonical-json"), exports);
19
+ __exportStar(require("./library-hash"), exports);
20
+ __exportStar(require("./library-proof"), exports);
21
+ __exportStar(require("./authorize-library"), exports);
22
+ __exportStar(require("./build-library"), exports);
23
+ __exportStar(require("./verify-library"), exports);
24
+ __exportStar(require("./library-store"), exports);
25
+ __exportStar(require("./in-memory-library-store"), exports);
26
+ __exportStar(require("./caching-library-store"), exports);
27
+ __exportStar(require("./remote-library-models"), exports);
28
+ __exportStar(require("./remote-library-store"), exports);
29
+ __exportStar(require("./library-server"), exports);
30
+ __exportStar(require("./evaluate-library"), exports);
31
+ __exportStar(require("./library-reference"), exports);
32
+ __exportStar(require("./evaluate-library-rules"), exports);
33
+ __exportStar(require("./compile-rule-source"), exports);
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Content hash of a library version: SHA-256 over the canonical JSON of
3
+ * `{ source, previousHash }`. Binds BEHAVIOUR ONLY — the source and its chain
4
+ * position — and deliberately excludes `createdAt`, `name`, and `description`
5
+ * so identity stays purely content-addressed. Those mutable fields are made
6
+ * tamper-evident by the version's signature instead, not by this hash.
7
+ */
8
+ export declare const computeVersionHash: (source: string, previousHash: string) => string;
9
+ //# sourceMappingURL=library-hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"library-hash.d.ts","sourceRoot":"","sources":["../../src/library/library-hash.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAAI,QAAQ,MAAM,EAAE,cAAc,MAAM,KAAG,MACP,CAAC"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeVersionHash = void 0;
4
+ const calculate_hash_1 = require("../crypto/calculate-hash");
5
+ const canonical_json_1 = require("./canonical-json");
6
+ /**
7
+ * Content hash of a library version: SHA-256 over the canonical JSON of
8
+ * `{ source, previousHash }`. Binds BEHAVIOUR ONLY — the source and its chain
9
+ * position — and deliberately excludes `createdAt`, `name`, and `description`
10
+ * so identity stays purely content-addressed. Those mutable fields are made
11
+ * tamper-evident by the version's signature instead, not by this hash.
12
+ */
13
+ const computeVersionHash = (source, previousHash) => (0, calculate_hash_1.calculateHash)((0, canonical_json_1.canonicalJsonStringify)({ source, previousHash }));
14
+ exports.computeVersionHash = computeVersionHash;
@@ -0,0 +1,134 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Library ledger models — the storage-agnostic, portable-JSON shape of a
4
+ * reusable function or rule in the SAFE network logic library. An entity is a
5
+ * signed, content-addressed, append-only ledger of versions. See
6
+ * `documentation/functions/functions.md` and `plans/FUNCTION_LEDGER_PLAN.html`.
7
+ */
8
+ /** One gas unit is 100ms of sandbox processing time. */
9
+ export declare const GAS_UNIT_MS = 100;
10
+ /** Maximum gas a function may declare: 30 minutes / 100ms = 18,000 units. */
11
+ export declare const MAX_GAS_UNITS = 18000;
12
+ /**
13
+ * The two kinds of reusable library entity. Both share the same versioned,
14
+ * signed ledger primitive; the kind selects the proof `purpose` namespace.
15
+ *
16
+ * Modelled as a Zod-inferred union + a companion value object (rather than a TS
17
+ * `enum`) so the schema's inferred field type and the value used in code are the
18
+ * same string-literal type — a nominal `enum` would not be assignable to the
19
+ * `z.enum` inference.
20
+ */
21
+ export declare const LibraryEntityKindSchema: z.ZodEnum<{
22
+ function: "function";
23
+ rule: "rule";
24
+ }>;
25
+ export type LibraryEntityKind = z.infer<typeof LibraryEntityKindSchema>;
26
+ export declare const LibraryEntityKind: {
27
+ readonly FUNCTION: "function";
28
+ readonly RULE: "rule";
29
+ };
30
+ /** The proof scheme used to sign versions. Only EdDSA signatures today. */
31
+ export declare const LibraryProofTypeSchema: z.ZodEnum<{
32
+ CRYPTOGRAPHIC_SIGNATURE: "CRYPTOGRAPHIC_SIGNATURE";
33
+ }>;
34
+ export type LibraryProofType = z.infer<typeof LibraryProofTypeSchema>;
35
+ export declare const LibraryProofType: {
36
+ readonly CRYPTOGRAPHIC_SIGNATURE: "CRYPTOGRAPHIC_SIGNATURE";
37
+ };
38
+ /** An Ed25519 signature over a canonical, purpose-bound payload. */
39
+ export declare const ILibraryProofSchema: z.ZodObject<{
40
+ publicKey: z.ZodString;
41
+ signature: z.ZodString;
42
+ }, z.core.$strip>;
43
+ export type ILibraryProof = z.infer<typeof ILibraryProofSchema>;
44
+ /**
45
+ * One version in a library entity's ledger. `currentHash` binds behaviour only
46
+ * (`source` + `previousHash`); `createdAt` is NOT in the content hash but IS in
47
+ * the signed proof payload, so it is tamper-evident via the signature.
48
+ */
49
+ export declare const ILibraryVersionSchema: z.ZodObject<{
50
+ createdAt: z.ZodNumber;
51
+ previousHash: z.ZodString;
52
+ source: z.ZodString;
53
+ currentHash: z.ZodString;
54
+ proof: z.ZodObject<{
55
+ publicKey: z.ZodString;
56
+ signature: z.ZodString;
57
+ }, z.core.$strip>;
58
+ proofType: z.ZodEnum<{
59
+ CRYPTOGRAPHIC_SIGNATURE: "CRYPTOGRAPHIC_SIGNATURE";
60
+ }>;
61
+ }, z.core.$strip>;
62
+ export type ILibraryVersion = z.infer<typeof ILibraryVersionSchema>;
63
+ /** The ledger tip: the latest version's hash plus a proof over it. */
64
+ export declare const ILibraryHeadSchema: z.ZodObject<{
65
+ currentHash: z.ZodString;
66
+ proof: z.ZodObject<{
67
+ publicKey: z.ZodString;
68
+ signature: z.ZodString;
69
+ }, z.core.$strip>;
70
+ }, z.core.$strip>;
71
+ export type ILibraryHead = z.infer<typeof ILibraryHeadSchema>;
72
+ /**
73
+ * A delegation granting `signerPublicKey` the right to append versions. The
74
+ * `proof` is the OWNER's signature over a canonical payload binding the entity
75
+ * id and the granted key — so a delegation cannot be forged (no valid owner
76
+ * signature) and cannot be replayed onto another entity (id is bound).
77
+ */
78
+ export declare const ILibraryDelegationSchema: z.ZodObject<{
79
+ signerPublicKey: z.ZodString;
80
+ createdAt: z.ZodNumber;
81
+ proof: z.ZodObject<{
82
+ publicKey: z.ZodString;
83
+ signature: z.ZodString;
84
+ }, z.core.$strip>;
85
+ }, z.core.$strip>;
86
+ export type ILibraryDelegation = z.infer<typeof ILibraryDelegationSchema>;
87
+ /**
88
+ * A reusable library entity: content-addressed `id` (= the first version's
89
+ * `currentHash`), mutable metadata, a declared `gasLimit`, an owner key, and an
90
+ * append-only `versions` ledger with a signed `head`.
91
+ */
92
+ export declare const ILibraryEntitySchema: z.ZodObject<{
93
+ kind: z.ZodEnum<{
94
+ function: "function";
95
+ rule: "rule";
96
+ }>;
97
+ id: z.ZodString;
98
+ name: z.ZodString;
99
+ description: z.ZodString;
100
+ gasLimit: z.ZodNumber;
101
+ owner: z.ZodObject<{
102
+ publicKey: z.ZodString;
103
+ }, z.core.$strip>;
104
+ delegations: z.ZodArray<z.ZodObject<{
105
+ signerPublicKey: z.ZodString;
106
+ createdAt: z.ZodNumber;
107
+ proof: z.ZodObject<{
108
+ publicKey: z.ZodString;
109
+ signature: z.ZodString;
110
+ }, z.core.$strip>;
111
+ }, z.core.$strip>>;
112
+ versions: z.ZodArray<z.ZodObject<{
113
+ createdAt: z.ZodNumber;
114
+ previousHash: z.ZodString;
115
+ source: z.ZodString;
116
+ currentHash: z.ZodString;
117
+ proof: z.ZodObject<{
118
+ publicKey: z.ZodString;
119
+ signature: z.ZodString;
120
+ }, z.core.$strip>;
121
+ proofType: z.ZodEnum<{
122
+ CRYPTOGRAPHIC_SIGNATURE: "CRYPTOGRAPHIC_SIGNATURE";
123
+ }>;
124
+ }, z.core.$strip>>;
125
+ head: z.ZodObject<{
126
+ currentHash: z.ZodString;
127
+ proof: z.ZodObject<{
128
+ publicKey: z.ZodString;
129
+ signature: z.ZodString;
130
+ }, z.core.$strip>;
131
+ }, z.core.$strip>;
132
+ }, z.core.$strip>;
133
+ export type ILibraryEntity = z.infer<typeof ILibraryEntitySchema>;
134
+ //# sourceMappingURL=library-models.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"library-models.d.ts","sourceRoot":"","sources":["../../src/library/library-models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;GAKG;AAIH,wDAAwD;AACxD,eAAO,MAAM,WAAW,MAAM,CAAC;AAE/B,6EAA6E;AAC7E,eAAO,MAAM,aAAa,QAAS,CAAC;AAMpC;;;;;;;;GAQG;AACH,eAAO,MAAM,uBAAuB;;;EAA+B,CAAC;AACpE,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AACxE,eAAO,MAAM,iBAAiB;;;CAAkD,CAAC;AAEjF,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB;;EAAsC,CAAC;AAC1E,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,eAAO,MAAM,gBAAgB;;CAAkE,CAAC;AAMhG,oEAAoE;AACpE,eAAO,MAAM,mBAAmB;;;iBAG9B,CAAC;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,eAAO,MAAM,qBAAqB;;;;;;;;;;;;iBAOhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEpE,sEAAsE;AACtE,eAAO,MAAM,kBAAkB;;;;;;iBAG7B,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAE9D;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB;;;;;;;iBAInC,CAAC;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAU/B,CAAC;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC"}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ILibraryEntitySchema = exports.ILibraryDelegationSchema = exports.ILibraryHeadSchema = exports.ILibraryVersionSchema = exports.ILibraryProofSchema = exports.LibraryProofType = exports.LibraryProofTypeSchema = exports.LibraryEntityKind = exports.LibraryEntityKindSchema = exports.MAX_GAS_UNITS = exports.GAS_UNIT_MS = void 0;
4
+ const zod_1 = require("zod");
5
+ /**
6
+ * Library ledger models — the storage-agnostic, portable-JSON shape of a
7
+ * reusable function or rule in the SAFE network logic library. An entity is a
8
+ * signed, content-addressed, append-only ledger of versions. See
9
+ * `documentation/functions/functions.md` and `plans/FUNCTION_LEDGER_PLAN.html`.
10
+ */
11
+ //#region constants
12
+ /** One gas unit is 100ms of sandbox processing time. */
13
+ exports.GAS_UNIT_MS = 100;
14
+ /** Maximum gas a function may declare: 30 minutes / 100ms = 18,000 units. */
15
+ exports.MAX_GAS_UNITS = 18000;
16
+ //#endregion constants
17
+ //#region enums
18
+ /**
19
+ * The two kinds of reusable library entity. Both share the same versioned,
20
+ * signed ledger primitive; the kind selects the proof `purpose` namespace.
21
+ *
22
+ * Modelled as a Zod-inferred union + a companion value object (rather than a TS
23
+ * `enum`) so the schema's inferred field type and the value used in code are the
24
+ * same string-literal type — a nominal `enum` would not be assignable to the
25
+ * `z.enum` inference.
26
+ */
27
+ exports.LibraryEntityKindSchema = zod_1.z.enum(['function', 'rule']);
28
+ exports.LibraryEntityKind = { FUNCTION: 'function', RULE: 'rule' };
29
+ /** The proof scheme used to sign versions. Only EdDSA signatures today. */
30
+ exports.LibraryProofTypeSchema = zod_1.z.enum(['CRYPTOGRAPHIC_SIGNATURE']);
31
+ exports.LibraryProofType = { CRYPTOGRAPHIC_SIGNATURE: 'CRYPTOGRAPHIC_SIGNATURE' };
32
+ //#endregion enums
33
+ //#region models
34
+ /** An Ed25519 signature over a canonical, purpose-bound payload. */
35
+ exports.ILibraryProofSchema = zod_1.z.object({
36
+ publicKey: zod_1.z.string(), // hex public key of the signer
37
+ signature: zod_1.z.string(), // hex Ed25519 signature
38
+ });
39
+ /**
40
+ * One version in a library entity's ledger. `currentHash` binds behaviour only
41
+ * (`source` + `previousHash`); `createdAt` is NOT in the content hash but IS in
42
+ * the signed proof payload, so it is tamper-evident via the signature.
43
+ */
44
+ exports.ILibraryVersionSchema = zod_1.z.object({
45
+ createdAt: zod_1.z.number(), // milliseconds; signed, not hashed
46
+ previousHash: zod_1.z.string(), // prior version's currentHash ('' for the first)
47
+ source: zod_1.z.string(), // the stringified function/rule source
48
+ currentHash: zod_1.z.string(), // SHA-256(canonicalJSON({ source, previousHash }))
49
+ proof: exports.ILibraryProofSchema, // signs the canonical, purpose-bound payload
50
+ proofType: exports.LibraryProofTypeSchema,
51
+ });
52
+ /** The ledger tip: the latest version's hash plus a proof over it. */
53
+ exports.ILibraryHeadSchema = zod_1.z.object({
54
+ currentHash: zod_1.z.string(), // == the latest version's currentHash
55
+ proof: exports.ILibraryProofSchema,
56
+ });
57
+ /**
58
+ * A delegation granting `signerPublicKey` the right to append versions. The
59
+ * `proof` is the OWNER's signature over a canonical payload binding the entity
60
+ * id and the granted key — so a delegation cannot be forged (no valid owner
61
+ * signature) and cannot be replayed onto another entity (id is bound).
62
+ */
63
+ exports.ILibraryDelegationSchema = zod_1.z.object({
64
+ signerPublicKey: zod_1.z.string(), // the delegated key being granted append rights
65
+ createdAt: zod_1.z.number(),
66
+ proof: exports.ILibraryProofSchema, // signed by the entity owner
67
+ });
68
+ /**
69
+ * A reusable library entity: content-addressed `id` (= the first version's
70
+ * `currentHash`), mutable metadata, a declared `gasLimit`, an owner key, and an
71
+ * append-only `versions` ledger with a signed `head`.
72
+ */
73
+ exports.ILibraryEntitySchema = zod_1.z.object({
74
+ kind: exports.LibraryEntityKindSchema,
75
+ id: zod_1.z.string(), // = versions[0].currentHash
76
+ name: zod_1.z.string(),
77
+ description: zod_1.z.string(),
78
+ gasLimit: zod_1.z.number(), // gas units (100ms each), 1..MAX_GAS_UNITS
79
+ owner: zod_1.z.object({ publicKey: zod_1.z.string() }),
80
+ delegations: zod_1.z.array(exports.ILibraryDelegationSchema), // owner-signed append grants
81
+ versions: zod_1.z.array(exports.ILibraryVersionSchema),
82
+ head: exports.ILibraryHeadSchema,
83
+ });
84
+ //#endregion models
@@ -0,0 +1,40 @@
1
+ import { LibraryEntityKind } from './library-models';
2
+ /**
3
+ * Canonical, purpose-bound payloads that library proofs sign over. Reuses the
4
+ * trust-chain discipline (Ed25519 over a canonical string with a `purpose`
5
+ * tag): the payload binds the entity `id` so a version's signature cannot be
6
+ * replayed onto another entity, folds in `createdAt`/`publicKey` so they are
7
+ * tamper-evident, and carries a `purpose` so a version proof can never satisfy
8
+ * a head check (or a function proof a rule check) and vice versa.
9
+ */
10
+ /** Purpose tag for a per-version proof, namespaced by entity kind. */
11
+ export declare const versionPurpose: (kind: LibraryEntityKind) => string;
12
+ /** Purpose tag for the head (ledger tip) proof, namespaced by entity kind. */
13
+ export declare const headPurpose: (kind: LibraryEntityKind) => string;
14
+ /** Purpose tag for a delegation proof, namespaced by entity kind. */
15
+ export declare const delegatePurpose: (kind: LibraryEntityKind) => string;
16
+ export interface VersionProofPayloadArgs {
17
+ id: string;
18
+ currentHash: string;
19
+ previousHash: string;
20
+ createdAt: number;
21
+ purpose: string;
22
+ publicKey: string;
23
+ }
24
+ export declare const buildVersionProofPayload: (args: VersionProofPayloadArgs) => string;
25
+ export interface HeadProofPayloadArgs {
26
+ id: string;
27
+ head: string;
28
+ purpose: string;
29
+ publicKey: string;
30
+ }
31
+ export declare const buildHeadProofPayload: (args: HeadProofPayloadArgs) => string;
32
+ export interface DelegationProofPayloadArgs {
33
+ id: string;
34
+ signerPublicKey: string;
35
+ purpose: string;
36
+ /** The owner's public key (the signer of the delegation). */
37
+ publicKey: string;
38
+ }
39
+ export declare const buildDelegationProofPayload: (args: DelegationProofPayloadArgs) => string;
40
+ //# sourceMappingURL=library-proof.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"library-proof.d.ts","sourceRoot":"","sources":["../../src/library/library-proof.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;;;;GAOG;AAEH,sEAAsE;AACtE,eAAO,MAAM,cAAc,GAAI,MAAM,iBAAiB,KAAG,MAA2B,CAAC;AAErF,8EAA8E;AAC9E,eAAO,MAAM,WAAW,GAAI,MAAM,iBAAiB,KAAG,MAAwB,CAAC;AAE/E,qEAAqE;AACrE,eAAO,MAAM,eAAe,GAAI,MAAM,iBAAiB,KAAG,MAA4B,CAAC;AAEvF,MAAM,WAAW,uBAAuB;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,wBAAwB,GAAI,MAAM,uBAAuB,KAAG,MAQnE,CAAC;AAEP,MAAM,WAAW,oBAAoB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,qBAAqB,GAAI,MAAM,oBAAoB,KAAG,MAM7D,CAAC;AAEP,MAAM,WAAW,0BAA0B;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,eAAO,MAAM,2BAA2B,GAAI,MAAM,0BAA0B,KAAG,MAMzE,CAAC"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildDelegationProofPayload = exports.buildHeadProofPayload = exports.buildVersionProofPayload = exports.delegatePurpose = exports.headPurpose = exports.versionPurpose = void 0;
4
+ const canonical_json_1 = require("./canonical-json");
5
+ /**
6
+ * Canonical, purpose-bound payloads that library proofs sign over. Reuses the
7
+ * trust-chain discipline (Ed25519 over a canonical string with a `purpose`
8
+ * tag): the payload binds the entity `id` so a version's signature cannot be
9
+ * replayed onto another entity, folds in `createdAt`/`publicKey` so they are
10
+ * tamper-evident, and carries a `purpose` so a version proof can never satisfy
11
+ * a head check (or a function proof a rule check) and vice versa.
12
+ */
13
+ /** Purpose tag for a per-version proof, namespaced by entity kind. */
14
+ const versionPurpose = (kind) => `${kind}-version`;
15
+ exports.versionPurpose = versionPurpose;
16
+ /** Purpose tag for the head (ledger tip) proof, namespaced by entity kind. */
17
+ const headPurpose = (kind) => `${kind}-head`;
18
+ exports.headPurpose = headPurpose;
19
+ /** Purpose tag for a delegation proof, namespaced by entity kind. */
20
+ const delegatePurpose = (kind) => `${kind}-delegate`;
21
+ exports.delegatePurpose = delegatePurpose;
22
+ const buildVersionProofPayload = (args) => (0, canonical_json_1.canonicalJsonStringify)({
23
+ id: args.id,
24
+ currentHash: args.currentHash,
25
+ previousHash: args.previousHash,
26
+ createdAt: args.createdAt,
27
+ purpose: args.purpose,
28
+ publicKey: args.publicKey,
29
+ });
30
+ exports.buildVersionProofPayload = buildVersionProofPayload;
31
+ const buildHeadProofPayload = (args) => (0, canonical_json_1.canonicalJsonStringify)({
32
+ id: args.id,
33
+ head: args.head,
34
+ purpose: args.purpose,
35
+ publicKey: args.publicKey,
36
+ });
37
+ exports.buildHeadProofPayload = buildHeadProofPayload;
38
+ const buildDelegationProofPayload = (args) => (0, canonical_json_1.canonicalJsonStringify)({
39
+ id: args.id,
40
+ signerPublicKey: args.signerPublicKey,
41
+ purpose: args.purpose,
42
+ publicKey: args.publicKey,
43
+ });
44
+ exports.buildDelegationProofPayload = buildDelegationProofPayload;
@@ -0,0 +1,75 @@
1
+ import { FunctionExecutionResult, FunctionExecutor } from '../sandbox/sandbox-models';
2
+ import { RuleEvaluationResult, RuleEvaluator } from '../sandbox/rule-evaluator';
3
+ import { IShareData } from '../models/share-data/share-data-models';
4
+ import { IShareRuleApplied } from '../models/rules/share-rule-models';
5
+ import { ILibraryEntity, ILibraryVersion, LibraryEntityKind } from './library-models';
6
+ import { LibraryStore } from './library-store';
7
+ /**
8
+ * A reference from a consumer (e.g. a ShareData) to a specific version of a
9
+ * library entity, pinned by `currentHash`. Pinning a version is what preserves
10
+ * the "frozen at lock" guarantee: the consumer keeps running its exact version
11
+ * even after the library entity gains new versions.
12
+ */
13
+ export interface LibraryReference {
14
+ kind: LibraryEntityKind;
15
+ id: string;
16
+ currentHash: string;
17
+ }
18
+ /**
19
+ * A rule reference as carried by a ShareData: the library reference plus the
20
+ * root property the rule governs (`targettedProperty`), which the per-property
21
+ * routing needs to group rules during evaluation.
22
+ */
23
+ export interface ILibraryRuleReferenceBinding {
24
+ reference: LibraryReference;
25
+ targettedProperty: string;
26
+ }
27
+ /** Builds a reference pinning a specific version of `entity` (defaults to the head). */
28
+ export declare const referenceLibraryEntity: (entity: ILibraryEntity, versionHash?: string) => LibraryReference;
29
+ /**
30
+ * The canonical source string for a rule library entity: the stringified rule
31
+ * definition. A rule entity published with this source resolves to a reference
32
+ * built by {@link referenceForRule} (both hash the same bytes).
33
+ */
34
+ export declare const ruleLibrarySource: (rule: IShareRuleApplied) => string;
35
+ /**
36
+ * Computes the content-addressed reference for a rule WITHOUT publishing it: the
37
+ * id is the hash of the rule's first (and, for a standard rule, only) version,
38
+ * exactly as `createLibraryEntity` would compute it. This lets a block reference
39
+ * a rule by hash and resolve it on demand from any store that has been seeded
40
+ * with the same rule definition (see `seedRuleLibrary` in `@degenerate/shareblock`).
41
+ */
42
+ export declare const referenceForRule: (rule: IShareRuleApplied) => LibraryReference;
43
+ /**
44
+ * Locates the pinned version within an already-in-hand entity (the
45
+ * self-contained path). Checks the reference's `id` and `kind` match the entity
46
+ * and that the pinned `currentHash` exists.
47
+ */
48
+ export declare const findPinnedVersion: (entity: ILibraryEntity, reference: LibraryReference) => ILibraryVersion;
49
+ export interface ResolvedLibraryReference {
50
+ entity: ILibraryEntity;
51
+ version: ILibraryVersion;
52
+ }
53
+ /**
54
+ * Resolves a reference against the store (the limited-self-containment path):
55
+ * loads the entity by id (the store re-verifies it), then locates the pinned
56
+ * version. Throws {@link LibraryEntityNotFoundError} if the store lacks the id.
57
+ */
58
+ export declare const resolveLibraryReference: (reference: LibraryReference, store: LibraryStore) => Promise<ResolvedLibraryReference>;
59
+ export interface ExecuteFunctionReferenceOptions {
60
+ current: IShareData;
61
+ input: unknown;
62
+ gasLimit?: number;
63
+ executor?: FunctionExecutor;
64
+ }
65
+ /** Resolves a FUNCTION reference from the store and runs its pinned version. */
66
+ export declare const executeFunctionReference: (reference: LibraryReference, store: LibraryStore, options: ExecuteFunctionReferenceOptions) => Promise<FunctionExecutionResult>;
67
+ export interface EvaluateRuleReferenceOptions {
68
+ current: IShareData;
69
+ proposed: IShareData;
70
+ gasLimit?: number;
71
+ evaluator?: RuleEvaluator;
72
+ }
73
+ /** Resolves a RULE reference from the store and evaluates its pinned version. */
74
+ export declare const evaluateRuleReference: (reference: LibraryReference, store: LibraryStore, options: EvaluateRuleReferenceOptions) => Promise<RuleEvaluationResult>;
75
+ //# sourceMappingURL=library-reference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"library-reference.d.ts","sourceRoot":"","sources":["../../src/library/library-reference.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AACtF,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAGtE,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC7B,IAAI,EAAE,iBAAiB,CAAC;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,MAAM,WAAW,4BAA4B;IACzC,SAAS,EAAE,gBAAgB,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;CAC7B;AAaD,wFAAwF;AACxF,eAAO,MAAM,sBAAsB,GAAI,QAAQ,cAAc,EAAE,cAAc,MAAM,KAAG,gBAGrF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,MAAM,iBAAiB,KAAG,MAA8B,CAAC;AAE3F;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,MAAM,iBAAiB,KAAG,gBAG1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,cAAc,EAAE,WAAW,gBAAgB,KAAG,eAYvF,CAAC;AAEF,MAAM,WAAW,wBAAwB;IACrC,MAAM,EAAE,cAAc,CAAC;IACvB,OAAO,EAAE,eAAe,CAAC;CAC5B;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,GAChC,WAAW,gBAAgB,EAC3B,OAAO,YAAY,KACpB,OAAO,CAAC,wBAAwB,CAMlC,CAAC;AAEF,MAAM,WAAW,+BAA+B;IAC5C,OAAO,EAAE,UAAU,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC/B;AAED,gFAAgF;AAChF,eAAO,MAAM,wBAAwB,GACjC,WAAW,gBAAgB,EAC3B,OAAO,YAAY,EACnB,SAAS,+BAA+B,KACzC,OAAO,CAAC,uBAAuB,CASjC,CAAC;AAEF,MAAM,WAAW,4BAA4B;IACzC,OAAO,EAAE,UAAU,CAAC;IACpB,QAAQ,EAAE,UAAU,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,aAAa,CAAC;CAC7B;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,GAC9B,WAAW,gBAAgB,EAC3B,OAAO,YAAY,EACnB,SAAS,4BAA4B,KACtC,OAAO,CAAC,oBAAoB,CAS9B,CAAC"}