@originals/sdk 1.4.3 → 1.4.5

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 (222) hide show
  1. package/dist/adapters/FeeOracleMock.d.ts +6 -0
  2. package/dist/adapters/FeeOracleMock.js +8 -0
  3. package/dist/adapters/index.d.ts +4 -0
  4. package/dist/adapters/index.js +4 -0
  5. package/dist/adapters/providers/OrdHttpProvider.d.ts +56 -0
  6. package/dist/adapters/providers/OrdHttpProvider.js +110 -0
  7. package/dist/adapters/providers/OrdMockProvider.d.ts +70 -0
  8. package/dist/adapters/providers/OrdMockProvider.js +75 -0
  9. package/dist/adapters/types.d.ts +71 -0
  10. package/dist/adapters/types.js +1 -0
  11. package/dist/bitcoin/BitcoinManager.d.ts +15 -0
  12. package/dist/bitcoin/BitcoinManager.js +262 -0
  13. package/dist/bitcoin/BroadcastClient.d.ts +30 -0
  14. package/dist/bitcoin/BroadcastClient.js +35 -0
  15. package/dist/bitcoin/OrdinalsClient.d.ts +21 -0
  16. package/dist/bitcoin/OrdinalsClient.js +105 -0
  17. package/dist/bitcoin/PSBTBuilder.d.ts +24 -0
  18. package/dist/bitcoin/PSBTBuilder.js +80 -0
  19. package/dist/bitcoin/fee-calculation.d.ts +14 -0
  20. package/dist/bitcoin/fee-calculation.js +31 -0
  21. package/dist/bitcoin/providers/OrdNodeProvider.d.ts +38 -0
  22. package/dist/bitcoin/providers/OrdNodeProvider.js +67 -0
  23. package/dist/bitcoin/providers/OrdinalsProvider.d.ts +33 -0
  24. package/dist/bitcoin/providers/OrdinalsProvider.js +50 -0
  25. package/dist/bitcoin/providers/types.d.ts +63 -0
  26. package/dist/bitcoin/providers/types.js +1 -0
  27. package/dist/bitcoin/transactions/commit.d.ts +89 -0
  28. package/dist/bitcoin/transactions/commit.js +311 -0
  29. package/dist/bitcoin/transactions/index.d.ts +7 -0
  30. package/dist/bitcoin/transactions/index.js +8 -0
  31. package/dist/bitcoin/transfer.d.ts +9 -0
  32. package/dist/bitcoin/transfer.js +26 -0
  33. package/dist/bitcoin/utxo-selection.d.ts +78 -0
  34. package/dist/bitcoin/utxo-selection.js +237 -0
  35. package/dist/bitcoin/utxo.d.ts +26 -0
  36. package/dist/bitcoin/utxo.js +78 -0
  37. package/dist/contexts/credentials-v1.json +195 -0
  38. package/dist/contexts/credentials-v2-examples.json +5 -0
  39. package/dist/contexts/credentials-v2.json +301 -0
  40. package/dist/contexts/credentials.json +195 -0
  41. package/dist/contexts/data-integrity-v2.json +81 -0
  42. package/dist/contexts/dids.json +57 -0
  43. package/dist/contexts/ed255192020.json +93 -0
  44. package/dist/contexts/ordinals-plus.json +23 -0
  45. package/dist/contexts/originals.json +22 -0
  46. package/dist/core/OriginalsSDK.d.ts +158 -0
  47. package/dist/core/OriginalsSDK.js +274 -0
  48. package/dist/crypto/Multikey.d.ts +30 -0
  49. package/dist/crypto/Multikey.js +149 -0
  50. package/dist/crypto/Signer.d.ts +21 -0
  51. package/dist/crypto/Signer.js +196 -0
  52. package/dist/crypto/noble-init.d.ts +18 -0
  53. package/dist/crypto/noble-init.js +106 -0
  54. package/dist/did/BtcoDidResolver.d.ts +57 -0
  55. package/dist/did/BtcoDidResolver.js +166 -0
  56. package/dist/did/DIDManager.d.ts +101 -0
  57. package/dist/did/DIDManager.js +493 -0
  58. package/dist/did/Ed25519Verifier.d.ts +30 -0
  59. package/dist/did/Ed25519Verifier.js +59 -0
  60. package/dist/did/KeyManager.d.ts +17 -0
  61. package/dist/did/KeyManager.js +207 -0
  62. package/dist/did/WebVHManager.d.ts +100 -0
  63. package/dist/did/WebVHManager.js +312 -0
  64. package/dist/did/createBtcoDidDocument.d.ts +10 -0
  65. package/dist/did/createBtcoDidDocument.js +42 -0
  66. package/dist/did/providers/OrdinalsClientProviderAdapter.d.ts +23 -0
  67. package/dist/did/providers/OrdinalsClientProviderAdapter.js +51 -0
  68. package/dist/events/EventEmitter.d.ts +115 -0
  69. package/dist/events/EventEmitter.js +198 -0
  70. package/dist/events/index.d.ts +7 -0
  71. package/dist/events/index.js +6 -0
  72. package/dist/events/types.d.ts +286 -0
  73. package/dist/events/types.js +9 -0
  74. package/dist/examples/basic-usage.d.ts +3 -0
  75. package/dist/examples/basic-usage.js +62 -0
  76. package/dist/examples/create-module-original.d.ts +32 -0
  77. package/dist/examples/create-module-original.js +376 -0
  78. package/dist/examples/full-lifecycle-flow.d.ts +56 -0
  79. package/dist/examples/full-lifecycle-flow.js +419 -0
  80. package/dist/examples/run.d.ts +12 -0
  81. package/dist/examples/run.js +51 -0
  82. package/dist/index.d.ts +43 -0
  83. package/dist/index.js +52 -0
  84. package/dist/kinds/KindRegistry.d.ts +76 -0
  85. package/dist/kinds/KindRegistry.js +216 -0
  86. package/dist/kinds/index.d.ts +33 -0
  87. package/dist/kinds/index.js +36 -0
  88. package/dist/kinds/types.d.ts +363 -0
  89. package/dist/kinds/types.js +25 -0
  90. package/dist/kinds/validators/AgentValidator.d.ts +14 -0
  91. package/dist/kinds/validators/AgentValidator.js +155 -0
  92. package/dist/kinds/validators/AppValidator.d.ts +14 -0
  93. package/dist/kinds/validators/AppValidator.js +135 -0
  94. package/dist/kinds/validators/DatasetValidator.d.ts +14 -0
  95. package/dist/kinds/validators/DatasetValidator.js +148 -0
  96. package/dist/kinds/validators/DocumentValidator.d.ts +14 -0
  97. package/dist/kinds/validators/DocumentValidator.js +180 -0
  98. package/dist/kinds/validators/MediaValidator.d.ts +14 -0
  99. package/dist/kinds/validators/MediaValidator.js +172 -0
  100. package/dist/kinds/validators/ModuleValidator.d.ts +14 -0
  101. package/dist/kinds/validators/ModuleValidator.js +140 -0
  102. package/dist/kinds/validators/base.d.ts +96 -0
  103. package/dist/kinds/validators/base.js +218 -0
  104. package/dist/kinds/validators/index.d.ts +10 -0
  105. package/dist/kinds/validators/index.js +10 -0
  106. package/dist/lifecycle/BatchOperations.d.ts +147 -0
  107. package/dist/lifecycle/BatchOperations.js +251 -0
  108. package/dist/lifecycle/LifecycleManager.d.ts +362 -0
  109. package/dist/lifecycle/LifecycleManager.js +1692 -0
  110. package/dist/lifecycle/OriginalsAsset.d.ts +164 -0
  111. package/dist/lifecycle/OriginalsAsset.js +380 -0
  112. package/dist/lifecycle/ProvenanceQuery.d.ts +126 -0
  113. package/dist/lifecycle/ProvenanceQuery.js +220 -0
  114. package/dist/lifecycle/ResourceVersioning.d.ts +73 -0
  115. package/dist/lifecycle/ResourceVersioning.js +127 -0
  116. package/dist/migration/MigrationManager.d.ts +86 -0
  117. package/dist/migration/MigrationManager.js +412 -0
  118. package/dist/migration/audit/AuditLogger.d.ts +51 -0
  119. package/dist/migration/audit/AuditLogger.js +156 -0
  120. package/dist/migration/checkpoint/CheckpointManager.d.ts +31 -0
  121. package/dist/migration/checkpoint/CheckpointManager.js +96 -0
  122. package/dist/migration/checkpoint/CheckpointStorage.d.ts +26 -0
  123. package/dist/migration/checkpoint/CheckpointStorage.js +89 -0
  124. package/dist/migration/index.d.ts +22 -0
  125. package/dist/migration/index.js +27 -0
  126. package/dist/migration/operations/BaseMigration.d.ts +48 -0
  127. package/dist/migration/operations/BaseMigration.js +83 -0
  128. package/dist/migration/operations/PeerToBtcoMigration.d.ts +25 -0
  129. package/dist/migration/operations/PeerToBtcoMigration.js +67 -0
  130. package/dist/migration/operations/PeerToWebvhMigration.d.ts +19 -0
  131. package/dist/migration/operations/PeerToWebvhMigration.js +46 -0
  132. package/dist/migration/operations/WebvhToBtcoMigration.d.ts +25 -0
  133. package/dist/migration/operations/WebvhToBtcoMigration.js +67 -0
  134. package/dist/migration/rollback/RollbackManager.d.ts +29 -0
  135. package/dist/migration/rollback/RollbackManager.js +146 -0
  136. package/dist/migration/state/StateMachine.d.ts +25 -0
  137. package/dist/migration/state/StateMachine.js +76 -0
  138. package/dist/migration/state/StateTracker.d.ts +36 -0
  139. package/dist/migration/state/StateTracker.js +123 -0
  140. package/dist/migration/types.d.ts +306 -0
  141. package/dist/migration/types.js +33 -0
  142. package/dist/migration/validation/BitcoinValidator.d.ts +13 -0
  143. package/dist/migration/validation/BitcoinValidator.js +83 -0
  144. package/dist/migration/validation/CredentialValidator.d.ts +13 -0
  145. package/dist/migration/validation/CredentialValidator.js +46 -0
  146. package/dist/migration/validation/DIDCompatibilityValidator.d.ts +16 -0
  147. package/dist/migration/validation/DIDCompatibilityValidator.js +127 -0
  148. package/dist/migration/validation/LifecycleValidator.d.ts +10 -0
  149. package/dist/migration/validation/LifecycleValidator.js +52 -0
  150. package/dist/migration/validation/StorageValidator.d.ts +10 -0
  151. package/dist/migration/validation/StorageValidator.js +65 -0
  152. package/dist/migration/validation/ValidationPipeline.d.ts +29 -0
  153. package/dist/migration/validation/ValidationPipeline.js +180 -0
  154. package/dist/resources/ResourceManager.d.ts +231 -0
  155. package/dist/resources/ResourceManager.js +573 -0
  156. package/dist/resources/index.d.ts +11 -0
  157. package/dist/resources/index.js +10 -0
  158. package/dist/resources/types.d.ts +93 -0
  159. package/dist/resources/types.js +80 -0
  160. package/dist/storage/LocalStorageAdapter.d.ts +11 -0
  161. package/dist/storage/LocalStorageAdapter.js +53 -0
  162. package/dist/storage/MemoryStorageAdapter.d.ts +6 -0
  163. package/dist/storage/MemoryStorageAdapter.js +21 -0
  164. package/dist/storage/StorageAdapter.d.ts +16 -0
  165. package/dist/storage/StorageAdapter.js +1 -0
  166. package/dist/storage/index.d.ts +2 -0
  167. package/dist/storage/index.js +2 -0
  168. package/dist/types/bitcoin.d.ts +84 -0
  169. package/dist/types/bitcoin.js +1 -0
  170. package/dist/types/common.d.ts +82 -0
  171. package/dist/types/common.js +1 -0
  172. package/dist/types/credentials.d.ts +75 -0
  173. package/dist/types/credentials.js +1 -0
  174. package/dist/types/did.d.ts +26 -0
  175. package/dist/types/did.js +1 -0
  176. package/dist/types/index.d.ts +5 -0
  177. package/dist/types/index.js +5 -0
  178. package/dist/types/network.d.ts +78 -0
  179. package/dist/types/network.js +145 -0
  180. package/dist/utils/EventLogger.d.ts +71 -0
  181. package/dist/utils/EventLogger.js +232 -0
  182. package/dist/utils/Logger.d.ts +106 -0
  183. package/dist/utils/Logger.js +257 -0
  184. package/dist/utils/MetricsCollector.d.ts +110 -0
  185. package/dist/utils/MetricsCollector.js +264 -0
  186. package/dist/utils/bitcoin-address.d.ts +38 -0
  187. package/dist/utils/bitcoin-address.js +113 -0
  188. package/dist/utils/cbor.d.ts +2 -0
  189. package/dist/utils/cbor.js +9 -0
  190. package/dist/utils/encoding.d.ts +37 -0
  191. package/dist/utils/encoding.js +120 -0
  192. package/dist/utils/hash.d.ts +1 -0
  193. package/dist/utils/hash.js +5 -0
  194. package/dist/utils/retry.d.ts +10 -0
  195. package/dist/utils/retry.js +35 -0
  196. package/dist/utils/satoshi-validation.d.ts +60 -0
  197. package/dist/utils/satoshi-validation.js +156 -0
  198. package/dist/utils/serialization.d.ts +14 -0
  199. package/dist/utils/serialization.js +76 -0
  200. package/dist/utils/telemetry.d.ts +17 -0
  201. package/dist/utils/telemetry.js +24 -0
  202. package/dist/utils/validation.d.ts +5 -0
  203. package/dist/utils/validation.js +98 -0
  204. package/dist/vc/CredentialManager.d.ts +329 -0
  205. package/dist/vc/CredentialManager.js +615 -0
  206. package/dist/vc/Issuer.d.ts +27 -0
  207. package/dist/vc/Issuer.js +70 -0
  208. package/dist/vc/Verifier.d.ts +16 -0
  209. package/dist/vc/Verifier.js +50 -0
  210. package/dist/vc/cryptosuites/bbs.d.ts +44 -0
  211. package/dist/vc/cryptosuites/bbs.js +213 -0
  212. package/dist/vc/cryptosuites/bbsSimple.d.ts +9 -0
  213. package/dist/vc/cryptosuites/bbsSimple.js +12 -0
  214. package/dist/vc/cryptosuites/eddsa.d.ts +30 -0
  215. package/dist/vc/cryptosuites/eddsa.js +81 -0
  216. package/dist/vc/documentLoader.d.ts +16 -0
  217. package/dist/vc/documentLoader.js +59 -0
  218. package/dist/vc/proofs/data-integrity.d.ts +21 -0
  219. package/dist/vc/proofs/data-integrity.js +15 -0
  220. package/dist/vc/utils/jsonld.d.ts +2 -0
  221. package/dist/vc/utils/jsonld.js +15 -0
  222. package/package.json +2 -1
@@ -0,0 +1,573 @@
1
+ /**
2
+ * ResourceManager - CRUD operations for immutable, versioned resources.
3
+ *
4
+ * Resources in the Originals SDK are content-addressed and immutable. Each "update"
5
+ * creates a new version with a new content hash, linked to the previous version
6
+ * via previousVersionHash. This creates a verifiable provenance chain.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const manager = new ResourceManager();
11
+ *
12
+ * // Create a new resource
13
+ * const resource = manager.createResource('Hello, World!', {
14
+ * type: 'text',
15
+ * contentType: 'text/plain'
16
+ * });
17
+ *
18
+ * // Update creates a new version
19
+ * const updatedResource = manager.updateResource(resource, 'Hello, Updated World!', {
20
+ * changes: 'Updated greeting'
21
+ * });
22
+ *
23
+ * // Get version history
24
+ * const history = manager.getResourceHistory(resource.id);
25
+ * ```
26
+ */
27
+ import { sha256 } from '@noble/hashes/sha2.js';
28
+ import { bytesToHex } from '@noble/hashes/utils.js';
29
+ import { v4 as uuidv4 } from 'uuid';
30
+ import { MIME_TYPE_MAP, DEFAULT_RESOURCE_CONFIG } from './types.js';
31
+ /**
32
+ * Regular expression for validating MIME types according to RFC 6838.
33
+ * Format: type/subtype where type and subtype are restricted character sets.
34
+ */
35
+ const MIME_TYPE_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9!#$&^_.+-]{0,126}\/[a-zA-Z0-9][a-zA-Z0-9!#$&^_.+-]{0,126}$/;
36
+ /**
37
+ * ResourceManager provides CRUD operations for immutable, content-addressed resources
38
+ * with versioning support and validation.
39
+ */
40
+ export class ResourceManager {
41
+ /**
42
+ * Create a new ResourceManager instance.
43
+ *
44
+ * @param config - Optional configuration for the manager
45
+ */
46
+ constructor(config) {
47
+ this.resources = new Map();
48
+ this.config = { ...DEFAULT_RESOURCE_CONFIG, ...config };
49
+ }
50
+ /**
51
+ * Create a new resource from content.
52
+ *
53
+ * @param content - The resource content (string for text, Buffer for binary)
54
+ * @param options - Creation options including type and contentType
55
+ * @returns The created Resource
56
+ * @throws Error if content or options are invalid
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Create a text resource
61
+ * const textResource = manager.createResource('# README\nHello', {
62
+ * type: 'text',
63
+ * contentType: 'text/markdown'
64
+ * });
65
+ *
66
+ * // Create a binary resource (image)
67
+ * const imageBuffer = fs.readFileSync('image.png');
68
+ * const imageResource = manager.createResource(imageBuffer, {
69
+ * type: 'image',
70
+ * contentType: 'image/png'
71
+ * });
72
+ * ```
73
+ */
74
+ createResource(content, options) {
75
+ // Validate inputs
76
+ if (content === null || content === undefined) {
77
+ throw new Error('Content is required');
78
+ }
79
+ if (!options) {
80
+ throw new Error('Options are required');
81
+ }
82
+ if (!options.type) {
83
+ throw new Error('Resource type is required');
84
+ }
85
+ if (!options.contentType) {
86
+ throw new Error('Content type is required');
87
+ }
88
+ // Validate MIME type format
89
+ if (!this.isValidMimeType(options.contentType)) {
90
+ throw new Error(`Invalid MIME type format: ${options.contentType}`);
91
+ }
92
+ // Check allowed content types
93
+ if (this.config.allowedContentTypes.length > 0 &&
94
+ !this.config.allowedContentTypes.includes(options.contentType)) {
95
+ throw new Error(`Content type not allowed: ${options.contentType}. Allowed types: ${this.config.allowedContentTypes.join(', ')}`);
96
+ }
97
+ // Convert content to buffer for consistent handling
98
+ const contentBuffer = this.toBuffer(content);
99
+ // Validate size
100
+ const maxSize = options.maxSize || this.config.defaultMaxSize;
101
+ if (contentBuffer.length > maxSize) {
102
+ throw new Error(`Resource size (${contentBuffer.length} bytes) exceeds maximum allowed size (${maxSize} bytes)`);
103
+ }
104
+ // Generate hash
105
+ const hash = this.hashContent(contentBuffer);
106
+ // Generate or use provided ID
107
+ const id = options.id || uuidv4();
108
+ // Create resource object
109
+ const resource = {
110
+ id,
111
+ type: options.type,
112
+ contentType: options.contentType,
113
+ hash,
114
+ size: contentBuffer.length,
115
+ version: 1,
116
+ createdAt: new Date().toISOString(),
117
+ url: options.url,
118
+ description: options.description,
119
+ };
120
+ // Store content if configured to do so
121
+ if (this.config.storeContent) {
122
+ if (this.isBinaryContent(content)) {
123
+ resource.contentBase64 = contentBuffer.toString('base64');
124
+ }
125
+ else {
126
+ resource.content = typeof content === 'string' ? content : contentBuffer.toString('utf-8');
127
+ }
128
+ }
129
+ // Store in version history
130
+ this.resources.set(id, [resource]);
131
+ return resource;
132
+ }
133
+ /**
134
+ * Update a resource by creating a new version.
135
+ * The original resource remains unchanged (immutable versioning).
136
+ *
137
+ * @param resource - The resource to update (or its ID)
138
+ * @param newContent - The new content
139
+ * @param options - Optional update options including change description
140
+ * @returns The new version of the resource
141
+ * @throws Error if resource not found or content unchanged
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * const v2 = manager.updateResource(originalResource, 'Updated content', {
146
+ * changes: 'Fixed typo in documentation'
147
+ * });
148
+ *
149
+ * console.log(v2.version); // 2
150
+ * console.log(v2.previousVersionHash); // hash of v1
151
+ * ```
152
+ */
153
+ updateResource(resource, newContent, options) {
154
+ const resourceId = typeof resource === 'string' ? resource : resource.id;
155
+ // Get version history
156
+ const versions = this.resources.get(resourceId);
157
+ if (!versions || versions.length === 0) {
158
+ throw new Error(`Resource not found: ${resourceId}`);
159
+ }
160
+ // Get current (latest) version
161
+ const currentVersion = versions[versions.length - 1];
162
+ // Convert content to buffer
163
+ const contentBuffer = this.toBuffer(newContent);
164
+ // Generate hash for new content
165
+ const newHash = this.hashContent(contentBuffer);
166
+ // Check if content has actually changed
167
+ if (newHash === currentVersion.hash) {
168
+ throw new Error('Content unchanged - new version would be identical to current version');
169
+ }
170
+ // Validate size
171
+ if (contentBuffer.length > this.config.defaultMaxSize) {
172
+ throw new Error(`Resource size (${contentBuffer.length} bytes) exceeds maximum allowed size (${this.config.defaultMaxSize} bytes)`);
173
+ }
174
+ // Determine content type (use provided or inherit from previous)
175
+ const contentType = options?.contentType || currentVersion.contentType;
176
+ // Validate new content type if changed
177
+ if (options?.contentType && !this.isValidMimeType(options.contentType)) {
178
+ throw new Error(`Invalid MIME type format: ${options.contentType}`);
179
+ }
180
+ // Create new version
181
+ const newVersion = {
182
+ id: resourceId,
183
+ type: currentVersion.type,
184
+ contentType,
185
+ hash: newHash,
186
+ size: contentBuffer.length,
187
+ version: (currentVersion.version || 1) + 1,
188
+ previousVersionHash: currentVersion.hash,
189
+ createdAt: new Date().toISOString(),
190
+ url: currentVersion.url,
191
+ description: currentVersion.description,
192
+ };
193
+ // Store content if configured
194
+ if (this.config.storeContent) {
195
+ if (this.isBinaryContent(newContent)) {
196
+ newVersion.contentBase64 = contentBuffer.toString('base64');
197
+ }
198
+ else {
199
+ newVersion.content = typeof newContent === 'string' ? newContent : contentBuffer.toString('utf-8');
200
+ }
201
+ }
202
+ // Add to version history
203
+ versions.push(newVersion);
204
+ return newVersion;
205
+ }
206
+ /**
207
+ * Get the complete version history for a resource.
208
+ *
209
+ * @param resourceId - The logical resource ID
210
+ * @returns Array of all versions (oldest to newest), or empty array if not found
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * const history = manager.getResourceHistory('my-resource-id');
215
+ * console.log(`Found ${history.length} versions`);
216
+ * history.forEach((v, i) => console.log(`v${i + 1}: ${v.hash}`));
217
+ * ```
218
+ */
219
+ getResourceHistory(resourceId) {
220
+ const versions = this.resources.get(resourceId);
221
+ if (!versions) {
222
+ return [];
223
+ }
224
+ return [...versions]; // Return copy to prevent external mutation
225
+ }
226
+ /**
227
+ * Get detailed version history with metadata.
228
+ *
229
+ * @param resourceId - The logical resource ID
230
+ * @returns ResourceVersionHistory object or null if not found
231
+ */
232
+ getResourceVersionHistory(resourceId) {
233
+ const versions = this.resources.get(resourceId);
234
+ if (!versions || versions.length === 0) {
235
+ return null;
236
+ }
237
+ return {
238
+ resourceId,
239
+ versions: [...versions],
240
+ currentVersion: versions[versions.length - 1],
241
+ versionCount: versions.length,
242
+ };
243
+ }
244
+ /**
245
+ * Get a specific version of a resource.
246
+ *
247
+ * @param resourceId - The logical resource ID
248
+ * @param version - Version number (1-indexed)
249
+ * @returns The resource at that version, or null if not found
250
+ */
251
+ getResourceVersion(resourceId, version) {
252
+ const versions = this.resources.get(resourceId);
253
+ if (!versions || version < 1 || version > versions.length) {
254
+ return null;
255
+ }
256
+ return versions[version - 1];
257
+ }
258
+ /**
259
+ * Get the current (latest) version of a resource.
260
+ *
261
+ * @param resourceId - The logical resource ID
262
+ * @returns The current version, or null if not found
263
+ */
264
+ getCurrentVersion(resourceId) {
265
+ const versions = this.resources.get(resourceId);
266
+ if (!versions || versions.length === 0) {
267
+ return null;
268
+ }
269
+ return versions[versions.length - 1];
270
+ }
271
+ /**
272
+ * Get a resource by its content hash.
273
+ *
274
+ * @param hash - The content hash to search for
275
+ * @returns The resource with that hash, or null if not found
276
+ */
277
+ getResourceByHash(hash) {
278
+ for (const versions of this.resources.values()) {
279
+ const found = versions.find(r => r.hash === hash);
280
+ if (found) {
281
+ return found;
282
+ }
283
+ }
284
+ return null;
285
+ }
286
+ /**
287
+ * Validate a resource object.
288
+ *
289
+ * @param resource - The resource to validate
290
+ * @returns ValidationResult with valid flag and any errors/warnings
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * const result = manager.validateResource(resource);
295
+ * if (!result.valid) {
296
+ * console.error('Validation errors:', result.errors);
297
+ * }
298
+ * if (result.warnings.length > 0) {
299
+ * console.warn('Warnings:', result.warnings);
300
+ * }
301
+ * ```
302
+ */
303
+ validateResource(resource) {
304
+ const errors = [];
305
+ const warnings = [];
306
+ // Required field validation
307
+ if (!resource) {
308
+ return { valid: false, errors: ['Resource is null or undefined'], warnings: [] };
309
+ }
310
+ if (!resource.id || typeof resource.id !== 'string') {
311
+ errors.push('Missing or invalid resource ID');
312
+ }
313
+ if (!resource.type || typeof resource.type !== 'string') {
314
+ errors.push('Missing or invalid resource type');
315
+ }
316
+ if (!resource.contentType || typeof resource.contentType !== 'string') {
317
+ errors.push('Missing or invalid content type');
318
+ }
319
+ else if (this.config.strictMimeValidation && !this.isValidMimeType(resource.contentType)) {
320
+ errors.push(`Invalid MIME type format: ${resource.contentType}`);
321
+ }
322
+ if (!resource.hash || typeof resource.hash !== 'string') {
323
+ errors.push('Missing or invalid hash');
324
+ }
325
+ else if (!/^[0-9a-fA-F]{64}$/.test(resource.hash)) {
326
+ errors.push('Invalid hash format (must be 64 character hex string)');
327
+ }
328
+ // Version chain validation
329
+ if (resource.version !== undefined) {
330
+ if (typeof resource.version !== 'number' || resource.version < 1) {
331
+ errors.push('Invalid version number (must be positive integer)');
332
+ }
333
+ // v1 should not have previousVersionHash
334
+ if (resource.version === 1 && resource.previousVersionHash) {
335
+ warnings.push('First version should not have previousVersionHash');
336
+ }
337
+ // v2+ should have previousVersionHash
338
+ if (resource.version > 1 && !resource.previousVersionHash) {
339
+ errors.push('Versions greater than 1 must have previousVersionHash');
340
+ }
341
+ }
342
+ // Size validation
343
+ if (resource.size !== undefined) {
344
+ if (typeof resource.size !== 'number' || resource.size < 0) {
345
+ errors.push('Invalid size (must be non-negative number)');
346
+ }
347
+ if (resource.size > this.config.defaultMaxSize) {
348
+ warnings.push(`Resource size (${resource.size} bytes) exceeds default maximum (${this.config.defaultMaxSize} bytes)`);
349
+ }
350
+ }
351
+ // Timestamp validation
352
+ if (resource.createdAt) {
353
+ const date = new Date(resource.createdAt);
354
+ if (isNaN(date.getTime())) {
355
+ errors.push('Invalid createdAt timestamp');
356
+ }
357
+ }
358
+ // Content hash verification (if content is present)
359
+ if (resource.content || resource.contentBase64) {
360
+ const content = resource.content
361
+ ? Buffer.from(resource.content, 'utf-8')
362
+ : Buffer.from(resource.contentBase64 || '', 'base64');
363
+ const computedHash = this.hashContent(content);
364
+ if (computedHash !== resource.hash) {
365
+ errors.push(`Content hash mismatch: expected ${resource.hash}, computed ${computedHash}`);
366
+ }
367
+ }
368
+ // Check allowed content types if configured
369
+ if (this.config.allowedContentTypes.length > 0 &&
370
+ resource.contentType &&
371
+ !this.config.allowedContentTypes.includes(resource.contentType)) {
372
+ errors.push(`Content type not allowed: ${resource.contentType}`);
373
+ }
374
+ return {
375
+ valid: errors.length === 0,
376
+ errors,
377
+ warnings,
378
+ };
379
+ }
380
+ /**
381
+ * Verify the integrity of a resource's version chain.
382
+ * Ensures that previousVersionHash references form a valid chain.
383
+ *
384
+ * @param resourceId - The logical resource ID to verify
385
+ * @returns ResourceValidationResult indicating chain integrity
386
+ */
387
+ verifyVersionChain(resourceId) {
388
+ const errors = [];
389
+ const warnings = [];
390
+ const versions = this.resources.get(resourceId);
391
+ if (!versions || versions.length === 0) {
392
+ return { valid: false, errors: ['Resource not found'], warnings: [] };
393
+ }
394
+ for (let i = 0; i < versions.length; i++) {
395
+ const version = versions[i];
396
+ // Check version numbers are sequential
397
+ if ((version.version || i + 1) !== i + 1) {
398
+ errors.push(`Version number mismatch at index ${i}: expected ${i + 1}, got ${version.version}`);
399
+ }
400
+ // First version should not have previousVersionHash
401
+ if (i === 0) {
402
+ if (version.previousVersionHash) {
403
+ warnings.push('First version has previousVersionHash (should not exist for v1)');
404
+ }
405
+ }
406
+ else {
407
+ // Subsequent versions must link to previous
408
+ const prevVersion = versions[i - 1];
409
+ if (!version.previousVersionHash) {
410
+ errors.push(`Version ${i + 1} missing previousVersionHash`);
411
+ }
412
+ else if (version.previousVersionHash !== prevVersion.hash) {
413
+ errors.push(`Version ${i + 1} previousVersionHash mismatch: expected ${prevVersion.hash}, got ${version.previousVersionHash}`);
414
+ }
415
+ }
416
+ }
417
+ return {
418
+ valid: errors.length === 0,
419
+ errors,
420
+ warnings,
421
+ };
422
+ }
423
+ /**
424
+ * Hash content using SHA-256.
425
+ *
426
+ * @param content - Content to hash (string or Buffer)
427
+ * @returns Hex-encoded SHA-256 hash
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * const hash = manager.hashContent('Hello, World!');
432
+ * console.log(hash); // 64-character hex string
433
+ * ```
434
+ */
435
+ hashContent(content) {
436
+ const buffer = this.toBuffer(content);
437
+ const hash = sha256(buffer);
438
+ return bytesToHex(hash);
439
+ }
440
+ /**
441
+ * Delete a resource and all its versions.
442
+ *
443
+ * @param resourceId - The resource ID to delete
444
+ * @returns true if deleted, false if not found
445
+ */
446
+ deleteResource(resourceId) {
447
+ return this.resources.delete(resourceId);
448
+ }
449
+ /**
450
+ * List all resource IDs managed by this instance.
451
+ *
452
+ * @returns Array of resource IDs
453
+ */
454
+ listResourceIds() {
455
+ return Array.from(this.resources.keys());
456
+ }
457
+ /**
458
+ * Get the total number of resources (unique IDs) managed.
459
+ *
460
+ * @returns Number of resources
461
+ */
462
+ getResourceCount() {
463
+ return this.resources.size;
464
+ }
465
+ /**
466
+ * Get the total number of versions across all resources.
467
+ *
468
+ * @returns Total version count
469
+ */
470
+ getTotalVersionCount() {
471
+ let count = 0;
472
+ for (const versions of this.resources.values()) {
473
+ count += versions.length;
474
+ }
475
+ return count;
476
+ }
477
+ /**
478
+ * Import a resource from an existing AssetResource.
479
+ * Useful for loading resources from storage or external sources.
480
+ *
481
+ * @param assetResource - The AssetResource to import
482
+ * @returns The imported Resource
483
+ */
484
+ importResource(assetResource) {
485
+ const resourceId = assetResource.id;
486
+ // Get or create version array
487
+ let versions = this.resources.get(resourceId);
488
+ if (!versions) {
489
+ versions = [];
490
+ this.resources.set(resourceId, versions);
491
+ }
492
+ // Check if this version already exists (by hash)
493
+ const existingVersion = versions.find(v => v.hash === assetResource.hash);
494
+ if (existingVersion) {
495
+ return existingVersion;
496
+ }
497
+ // Add to version array (maintain order by version number)
498
+ const version = assetResource.version || 1;
499
+ const insertIndex = versions.findIndex(v => (v.version || 1) > version);
500
+ if (insertIndex === -1) {
501
+ versions.push(assetResource);
502
+ }
503
+ else {
504
+ versions.splice(insertIndex, 0, assetResource);
505
+ }
506
+ return assetResource;
507
+ }
508
+ /**
509
+ * Export all resources as an array (for serialization).
510
+ *
511
+ * @returns Array of all resources (all versions)
512
+ */
513
+ exportResources() {
514
+ const allResources = [];
515
+ for (const versions of this.resources.values()) {
516
+ allResources.push(...versions);
517
+ }
518
+ return allResources;
519
+ }
520
+ /**
521
+ * Clear all resources from this manager.
522
+ */
523
+ clear() {
524
+ this.resources.clear();
525
+ }
526
+ /**
527
+ * Infer resource type from MIME type.
528
+ *
529
+ * @param contentType - The MIME content type
530
+ * @returns Inferred ResourceType
531
+ */
532
+ static inferResourceType(contentType) {
533
+ // Check exact match first
534
+ if (contentType in MIME_TYPE_MAP) {
535
+ return MIME_TYPE_MAP[contentType];
536
+ }
537
+ // Check by prefix
538
+ const prefix = contentType.split('/')[0];
539
+ switch (prefix) {
540
+ case 'image':
541
+ return 'image';
542
+ case 'audio':
543
+ return 'audio';
544
+ case 'video':
545
+ return 'video';
546
+ case 'text':
547
+ return 'text';
548
+ default:
549
+ return 'other';
550
+ }
551
+ }
552
+ /**
553
+ * Check if a string is a valid MIME type format.
554
+ */
555
+ isValidMimeType(mimeType) {
556
+ return MIME_TYPE_REGEX.test(mimeType);
557
+ }
558
+ /**
559
+ * Convert content to Buffer.
560
+ */
561
+ toBuffer(content) {
562
+ if (Buffer.isBuffer(content)) {
563
+ return content;
564
+ }
565
+ return Buffer.from(content, 'utf-8');
566
+ }
567
+ /**
568
+ * Check if content is binary (Buffer) rather than text.
569
+ */
570
+ isBinaryContent(content) {
571
+ return Buffer.isBuffer(content);
572
+ }
573
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Resources module for the Originals SDK.
3
+ *
4
+ * This module provides resource management with immutable versioning,
5
+ * content hashing, and validation.
6
+ *
7
+ * @module resources
8
+ */
9
+ export { ResourceManager } from './ResourceManager.js';
10
+ export type { Resource, ResourceOptions, ResourceUpdateOptions, ResourceVersionHistory, ResourceManagerConfig, ResourceValidationResult, ResourceType, } from './types.js';
11
+ export { MIME_TYPE_MAP, DEFAULT_RESOURCE_CONFIG } from './types.js';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Resources module for the Originals SDK.
3
+ *
4
+ * This module provides resource management with immutable versioning,
5
+ * content hashing, and validation.
6
+ *
7
+ * @module resources
8
+ */
9
+ export { ResourceManager } from './ResourceManager.js';
10
+ export { MIME_TYPE_MAP, DEFAULT_RESOURCE_CONFIG } from './types.js';
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Resource types for the Originals SDK ResourceManager.
3
+ *
4
+ * Resources in the Originals protocol are immutable, content-addressed entities
5
+ * that can be versioned through a linked hash chain.
6
+ */
7
+ import { AssetResource } from '../types/common';
8
+ /**
9
+ * Supported resource types for categorization
10
+ */
11
+ export type ResourceType = 'image' | 'text' | 'code' | 'data' | 'audio' | 'video' | 'document' | 'binary' | 'other';
12
+ /**
13
+ * Options for creating a new resource
14
+ */
15
+ export interface ResourceOptions {
16
+ /** Logical resource ID (optional - will be generated if not provided) */
17
+ id?: string;
18
+ /** Resource type category */
19
+ type: ResourceType;
20
+ /** MIME content type (e.g., 'text/plain', 'image/png') */
21
+ contentType: string;
22
+ /** Optional URL if resource is externally hosted */
23
+ url?: string;
24
+ /** Optional description of the resource */
25
+ description?: string;
26
+ /** Maximum allowed size in bytes (optional, uses default if not specified) */
27
+ maxSize?: number;
28
+ }
29
+ /**
30
+ * Options for updating an existing resource
31
+ */
32
+ export interface ResourceUpdateOptions {
33
+ /** New content type (optional - inherits from previous version if not specified) */
34
+ contentType?: string;
35
+ /** Description of changes made in this version */
36
+ changes?: string;
37
+ }
38
+ /**
39
+ * Result of resource validation
40
+ */
41
+ export interface ResourceValidationResult {
42
+ /** Whether the resource is valid */
43
+ valid: boolean;
44
+ /** Array of error messages if validation failed */
45
+ errors: string[];
46
+ /** Array of warning messages (non-fatal issues) */
47
+ warnings: string[];
48
+ }
49
+ /**
50
+ * A resource with its content and metadata
51
+ */
52
+ export interface Resource extends AssetResource {
53
+ /** The actual content (for in-memory resources) */
54
+ content?: string;
55
+ /** Binary content as base64-encoded string */
56
+ contentBase64?: string;
57
+ /** Description of the resource */
58
+ description?: string;
59
+ }
60
+ /**
61
+ * Complete history of a resource including all versions
62
+ */
63
+ export interface ResourceVersionHistory {
64
+ /** Logical resource ID (stable across all versions) */
65
+ resourceId: string;
66
+ /** All versions in chronological order (oldest first) */
67
+ versions: Resource[];
68
+ /** The current (latest) version */
69
+ currentVersion: Resource;
70
+ /** Total number of versions */
71
+ versionCount: number;
72
+ }
73
+ /**
74
+ * Configuration for the ResourceManager
75
+ */
76
+ export interface ResourceManagerConfig {
77
+ /** Default maximum resource size in bytes (default: 10MB) */
78
+ defaultMaxSize?: number;
79
+ /** Whether to store content in memory (default: true) */
80
+ storeContent?: boolean;
81
+ /** Allowed MIME types (if empty, all types allowed) */
82
+ allowedContentTypes?: string[];
83
+ /** Whether to enable strict MIME type validation (default: true) */
84
+ strictMimeValidation?: boolean;
85
+ }
86
+ /**
87
+ * Common MIME types and their resource type mappings
88
+ */
89
+ export declare const MIME_TYPE_MAP: Record<string, ResourceType>;
90
+ /**
91
+ * Default configuration values
92
+ */
93
+ export declare const DEFAULT_RESOURCE_CONFIG: Required<ResourceManagerConfig>;