@andamio/core 0.1.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.
@@ -0,0 +1,276 @@
1
+ /**
2
+ * SLT Hash Utility
3
+ *
4
+ * Computes the module token name (hash) from a list of Student Learning Targets (SLTs).
5
+ * This hash is used as the token name when minting module tokens on-chain.
6
+ *
7
+ * The algorithm matches the on-chain Plutus validator:
8
+ * ```haskell
9
+ * sltsToBbs MintModuleV2{slts} = blake2b_256 $ serialiseData $ toBuiltinData $ map stringToBuiltinByteString slts
10
+ * ```
11
+ *
12
+ * Serialization format:
13
+ * 1. Convert each SLT string to UTF-8 bytes
14
+ * 2. Encode as CBOR indefinite-length array of byte strings
15
+ * 3. Hash with Blake2b-256 (32 bytes / 256 bits)
16
+ *
17
+ * @module @andamio/core/hashing
18
+ */
19
+ /**
20
+ * Compute the module hash matching Plutus on-chain encoding.
21
+ *
22
+ * Plutus's `stringToBuiltinByteString` chunks byte strings at 64 bytes.
23
+ * This function replicates that behavior:
24
+ * - Strings <= 64 bytes: encoded as regular CBOR byte strings
25
+ * - Strings > 64 bytes: encoded as indefinite-length chunked byte strings
26
+ *
27
+ * @param slts - Array of Student Learning Target strings
28
+ * @returns 64-character hex string (256-bit Blake2b hash)
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { computeSltHash } from "@andamio/core/hashing";
33
+ *
34
+ * const slts = [
35
+ * "I can mint an access token.",
36
+ * "I can complete an assignment to earn a credential."
37
+ * ];
38
+ *
39
+ * const moduleHash = computeSltHash(slts);
40
+ * // Returns: "8dcbe1b925d87e6c547bbd8071c23a712db4c32751454b0948f8c846e9246b5c"
41
+ * ```
42
+ */
43
+ declare function computeSltHash(slts: string[]): string;
44
+ /**
45
+ * @deprecated Use `computeSltHash` instead. This alias is kept for backwards compatibility.
46
+ */
47
+ declare const computeSltHashDefinite: typeof computeSltHash;
48
+ /**
49
+ * Verify that a given hash matches the computed hash for SLTs.
50
+ *
51
+ * @param slts - Array of Student Learning Target strings
52
+ * @param expectedHash - The hash to verify (64-character hex string)
53
+ * @returns true if the computed hash matches the expected hash
54
+ */
55
+ declare function verifySltHash(slts: string[], expectedHash: string): boolean;
56
+ /**
57
+ * Validate that a string is a valid SLT hash format.
58
+ *
59
+ * SLT hashes are 64-character hexadecimal strings (256-bit Blake2b hash).
60
+ *
61
+ * @param hash - String to validate
62
+ * @returns true if the string is a valid SLT hash format
63
+ */
64
+ declare function isValidSltHash(hash: string): boolean;
65
+
66
+ /**
67
+ * Task Hash Utility
68
+ *
69
+ * Computes the task token name (hash) from task data.
70
+ * This hash is used as the task_hash on-chain (content-addressed identifier).
71
+ *
72
+ * The algorithm matches the on-chain Plutus validator serialization.
73
+ *
74
+ * @module @andamio/core/hashing
75
+ */
76
+ /**
77
+ * Native asset in ListValue format: [policyId.tokenName, quantity]
78
+ */
79
+ type NativeAsset = [string, number];
80
+ /**
81
+ * Task data structure matching the Atlas TX API ManageTasksTxRequest
82
+ *
83
+ * Fields must be arranged in this specific order for hashing:
84
+ * 1. project_content (string, max 140 chars)
85
+ * 2. expiration_time (number, Unix timestamp in milliseconds)
86
+ * 3. lovelace_amount (number)
87
+ * 4. native_assets (array of [asset_class, quantity] tuples)
88
+ */
89
+ interface TaskData {
90
+ project_content: string;
91
+ expiration_time: number;
92
+ lovelace_amount: number;
93
+ native_assets: NativeAsset[];
94
+ }
95
+ /**
96
+ * Compute the task hash (token name / task_hash) from task data.
97
+ *
98
+ * This produces the same hash as the on-chain Plutus validator, allowing
99
+ * clients to pre-compute or verify task hashes.
100
+ *
101
+ * @param task - Task data object
102
+ * @returns 64-character hex string (256-bit Blake2b hash)
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import { computeTaskHash } from "@andamio/core/hashing";
107
+ *
108
+ * const task = {
109
+ * project_content: "Open Task #1",
110
+ * expiration_time: 1769027280000,
111
+ * lovelace_amount: 15000000,
112
+ * native_assets: []
113
+ * };
114
+ *
115
+ * const taskHash = computeTaskHash(task);
116
+ * // Returns the on-chain task_hash
117
+ * ```
118
+ */
119
+ declare function computeTaskHash(task: TaskData): string;
120
+ /**
121
+ * Verify that a given hash matches the computed hash for a task.
122
+ *
123
+ * @param task - Task data object
124
+ * @param expectedHash - The hash to verify (64-character hex string)
125
+ * @returns true if the computed hash matches the expected hash
126
+ */
127
+ declare function verifyTaskHash(task: TaskData, expectedHash: string): boolean;
128
+ /**
129
+ * Validate that a string is a valid task hash format.
130
+ *
131
+ * Task hashes are 64-character hexadecimal strings (256-bit Blake2b hash).
132
+ */
133
+ declare function isValidTaskHash(hash: string): boolean;
134
+ /**
135
+ * Debug function to show the CBOR encoding of a task.
136
+ * Useful for comparing against on-chain data.
137
+ *
138
+ * @param task - Task data object
139
+ * @returns Hex string of the CBOR-encoded data (before hashing)
140
+ */
141
+ declare function debugTaskCBOR(task: TaskData): string;
142
+
143
+ /**
144
+ * Commitment Hash Utility
145
+ *
146
+ * Functions for computing and verifying hashes of commitment evidence.
147
+ * The hash is stored on-chain as `commitment_hash` in the course state datum,
148
+ * while the full evidence (Tiptap JSON document) is stored in the database.
149
+ *
150
+ * This provides:
151
+ * - Compact on-chain storage (64-char hex hash vs full JSON)
152
+ * - Tamper-evidence (can verify DB content matches on-chain commitment)
153
+ * - Privacy (evidence details not exposed on-chain)
154
+ *
155
+ * @module @andamio/core/hashing
156
+ */
157
+ /**
158
+ * Tiptap document structure (simplified)
159
+ * The actual structure can be more complex with nested content
160
+ */
161
+ type TiptapDoc = {
162
+ type: "doc";
163
+ content?: TiptapNode[];
164
+ [key: string]: unknown;
165
+ };
166
+ type TiptapNode = {
167
+ type: string;
168
+ content?: TiptapNode[];
169
+ text?: string;
170
+ marks?: TiptapMark[];
171
+ attrs?: Record<string, unknown>;
172
+ [key: string]: unknown;
173
+ };
174
+ type TiptapMark = {
175
+ type: string;
176
+ attrs?: Record<string, unknown>;
177
+ [key: string]: unknown;
178
+ };
179
+ /**
180
+ * Normalizes a value for consistent hashing.
181
+ *
182
+ * Normalization rules:
183
+ * - Objects: Sort keys alphabetically, recursively normalize values
184
+ * - Arrays: Preserve order, recursively normalize items
185
+ * - Strings: Trim whitespace
186
+ * - Numbers/Booleans/null: Keep as-is
187
+ * - undefined: Convert to null
188
+ *
189
+ * @param value - Any JSON-serializable value
190
+ * @returns Normalized value
191
+ */
192
+ declare function normalizeForHashing(value: unknown): unknown;
193
+ /**
194
+ * Computes the commitment hash from evidence content.
195
+ *
196
+ * The hash is computed as:
197
+ * 1. Normalize the evidence (sort keys, trim strings, etc.)
198
+ * 2. Serialize to JSON string (deterministic due to normalization)
199
+ * 3. Apply Blake2b-256 hash
200
+ *
201
+ * @param evidence - The evidence content (Tiptap JSON document or any JSON-serializable data)
202
+ * @returns 64-character lowercase hex string (Blake2b-256 hash)
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * import { computeCommitmentHash } from "@andamio/core/hashing";
207
+ *
208
+ * const evidence = {
209
+ * type: "doc",
210
+ * content: [
211
+ * { type: "paragraph", content: [{ type: "text", text: "My submission" }] }
212
+ * ]
213
+ * };
214
+ *
215
+ * const hash = computeCommitmentHash(evidence);
216
+ * // Use this hash as commitment_hash in the transaction
217
+ * ```
218
+ */
219
+ declare function computeCommitmentHash(evidence: unknown): string;
220
+ /**
221
+ * Verifies that evidence content matches an expected hash.
222
+ *
223
+ * Use this to verify that database evidence matches the on-chain commitment.
224
+ *
225
+ * @param evidence - The evidence content to verify
226
+ * @param expectedHash - The expected hash (from on-chain data)
227
+ * @returns True if the evidence produces the expected hash
228
+ */
229
+ declare function verifyCommitmentHash(evidence: unknown, expectedHash: string): boolean;
230
+ /**
231
+ * Validates that a string is a valid commitment hash format.
232
+ *
233
+ * A valid hash is a 64-character hexadecimal string (Blake2b-256 output).
234
+ *
235
+ * @param hash - The string to validate
236
+ * @returns True if the string is a valid hash format
237
+ */
238
+ declare function isValidCommitmentHash(hash: string): boolean;
239
+ /**
240
+ * Result of comparing evidence with an on-chain hash
241
+ */
242
+ type EvidenceVerificationResult = {
243
+ /** Whether the evidence matches the on-chain hash */
244
+ isValid: boolean;
245
+ /** The hash computed from the evidence */
246
+ computedHash: string;
247
+ /** The expected hash (from on-chain) */
248
+ expectedHash: string;
249
+ /** Human-readable status message */
250
+ message: string;
251
+ };
252
+ /**
253
+ * Performs a detailed verification of evidence against an on-chain hash.
254
+ *
255
+ * Returns a detailed result object with both hashes and a status message,
256
+ * useful for debugging and user feedback.
257
+ *
258
+ * @param evidence - The evidence content to verify
259
+ * @param onChainHash - The hash from on-chain data
260
+ * @returns Detailed verification result
261
+ */
262
+ declare function verifyEvidenceDetailed(evidence: unknown, onChainHash: string): EvidenceVerificationResult;
263
+ /**
264
+ * @deprecated Use `computeCommitmentHash` instead.
265
+ */
266
+ declare const computeAssignmentInfoHash: typeof computeCommitmentHash;
267
+ /**
268
+ * @deprecated Use `verifyCommitmentHash` instead.
269
+ */
270
+ declare const verifyAssignmentInfoHash: typeof verifyCommitmentHash;
271
+ /**
272
+ * @deprecated Use `isValidCommitmentHash` instead.
273
+ */
274
+ declare const isValidAssignmentInfoHash: typeof isValidCommitmentHash;
275
+
276
+ export { type EvidenceVerificationResult, type TaskData, type TiptapDoc, type TiptapMark, type TiptapNode, computeAssignmentInfoHash, computeCommitmentHash, computeSltHash, computeSltHashDefinite, computeTaskHash, debugTaskCBOR, isValidAssignmentInfoHash, isValidCommitmentHash, isValidSltHash, isValidTaskHash, normalizeForHashing, verifyAssignmentInfoHash, verifyCommitmentHash, verifyEvidenceDetailed, verifySltHash, verifyTaskHash };
@@ -0,0 +1,276 @@
1
+ /**
2
+ * SLT Hash Utility
3
+ *
4
+ * Computes the module token name (hash) from a list of Student Learning Targets (SLTs).
5
+ * This hash is used as the token name when minting module tokens on-chain.
6
+ *
7
+ * The algorithm matches the on-chain Plutus validator:
8
+ * ```haskell
9
+ * sltsToBbs MintModuleV2{slts} = blake2b_256 $ serialiseData $ toBuiltinData $ map stringToBuiltinByteString slts
10
+ * ```
11
+ *
12
+ * Serialization format:
13
+ * 1. Convert each SLT string to UTF-8 bytes
14
+ * 2. Encode as CBOR indefinite-length array of byte strings
15
+ * 3. Hash with Blake2b-256 (32 bytes / 256 bits)
16
+ *
17
+ * @module @andamio/core/hashing
18
+ */
19
+ /**
20
+ * Compute the module hash matching Plutus on-chain encoding.
21
+ *
22
+ * Plutus's `stringToBuiltinByteString` chunks byte strings at 64 bytes.
23
+ * This function replicates that behavior:
24
+ * - Strings <= 64 bytes: encoded as regular CBOR byte strings
25
+ * - Strings > 64 bytes: encoded as indefinite-length chunked byte strings
26
+ *
27
+ * @param slts - Array of Student Learning Target strings
28
+ * @returns 64-character hex string (256-bit Blake2b hash)
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { computeSltHash } from "@andamio/core/hashing";
33
+ *
34
+ * const slts = [
35
+ * "I can mint an access token.",
36
+ * "I can complete an assignment to earn a credential."
37
+ * ];
38
+ *
39
+ * const moduleHash = computeSltHash(slts);
40
+ * // Returns: "8dcbe1b925d87e6c547bbd8071c23a712db4c32751454b0948f8c846e9246b5c"
41
+ * ```
42
+ */
43
+ declare function computeSltHash(slts: string[]): string;
44
+ /**
45
+ * @deprecated Use `computeSltHash` instead. This alias is kept for backwards compatibility.
46
+ */
47
+ declare const computeSltHashDefinite: typeof computeSltHash;
48
+ /**
49
+ * Verify that a given hash matches the computed hash for SLTs.
50
+ *
51
+ * @param slts - Array of Student Learning Target strings
52
+ * @param expectedHash - The hash to verify (64-character hex string)
53
+ * @returns true if the computed hash matches the expected hash
54
+ */
55
+ declare function verifySltHash(slts: string[], expectedHash: string): boolean;
56
+ /**
57
+ * Validate that a string is a valid SLT hash format.
58
+ *
59
+ * SLT hashes are 64-character hexadecimal strings (256-bit Blake2b hash).
60
+ *
61
+ * @param hash - String to validate
62
+ * @returns true if the string is a valid SLT hash format
63
+ */
64
+ declare function isValidSltHash(hash: string): boolean;
65
+
66
+ /**
67
+ * Task Hash Utility
68
+ *
69
+ * Computes the task token name (hash) from task data.
70
+ * This hash is used as the task_hash on-chain (content-addressed identifier).
71
+ *
72
+ * The algorithm matches the on-chain Plutus validator serialization.
73
+ *
74
+ * @module @andamio/core/hashing
75
+ */
76
+ /**
77
+ * Native asset in ListValue format: [policyId.tokenName, quantity]
78
+ */
79
+ type NativeAsset = [string, number];
80
+ /**
81
+ * Task data structure matching the Atlas TX API ManageTasksTxRequest
82
+ *
83
+ * Fields must be arranged in this specific order for hashing:
84
+ * 1. project_content (string, max 140 chars)
85
+ * 2. expiration_time (number, Unix timestamp in milliseconds)
86
+ * 3. lovelace_amount (number)
87
+ * 4. native_assets (array of [asset_class, quantity] tuples)
88
+ */
89
+ interface TaskData {
90
+ project_content: string;
91
+ expiration_time: number;
92
+ lovelace_amount: number;
93
+ native_assets: NativeAsset[];
94
+ }
95
+ /**
96
+ * Compute the task hash (token name / task_hash) from task data.
97
+ *
98
+ * This produces the same hash as the on-chain Plutus validator, allowing
99
+ * clients to pre-compute or verify task hashes.
100
+ *
101
+ * @param task - Task data object
102
+ * @returns 64-character hex string (256-bit Blake2b hash)
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * import { computeTaskHash } from "@andamio/core/hashing";
107
+ *
108
+ * const task = {
109
+ * project_content: "Open Task #1",
110
+ * expiration_time: 1769027280000,
111
+ * lovelace_amount: 15000000,
112
+ * native_assets: []
113
+ * };
114
+ *
115
+ * const taskHash = computeTaskHash(task);
116
+ * // Returns the on-chain task_hash
117
+ * ```
118
+ */
119
+ declare function computeTaskHash(task: TaskData): string;
120
+ /**
121
+ * Verify that a given hash matches the computed hash for a task.
122
+ *
123
+ * @param task - Task data object
124
+ * @param expectedHash - The hash to verify (64-character hex string)
125
+ * @returns true if the computed hash matches the expected hash
126
+ */
127
+ declare function verifyTaskHash(task: TaskData, expectedHash: string): boolean;
128
+ /**
129
+ * Validate that a string is a valid task hash format.
130
+ *
131
+ * Task hashes are 64-character hexadecimal strings (256-bit Blake2b hash).
132
+ */
133
+ declare function isValidTaskHash(hash: string): boolean;
134
+ /**
135
+ * Debug function to show the CBOR encoding of a task.
136
+ * Useful for comparing against on-chain data.
137
+ *
138
+ * @param task - Task data object
139
+ * @returns Hex string of the CBOR-encoded data (before hashing)
140
+ */
141
+ declare function debugTaskCBOR(task: TaskData): string;
142
+
143
+ /**
144
+ * Commitment Hash Utility
145
+ *
146
+ * Functions for computing and verifying hashes of commitment evidence.
147
+ * The hash is stored on-chain as `commitment_hash` in the course state datum,
148
+ * while the full evidence (Tiptap JSON document) is stored in the database.
149
+ *
150
+ * This provides:
151
+ * - Compact on-chain storage (64-char hex hash vs full JSON)
152
+ * - Tamper-evidence (can verify DB content matches on-chain commitment)
153
+ * - Privacy (evidence details not exposed on-chain)
154
+ *
155
+ * @module @andamio/core/hashing
156
+ */
157
+ /**
158
+ * Tiptap document structure (simplified)
159
+ * The actual structure can be more complex with nested content
160
+ */
161
+ type TiptapDoc = {
162
+ type: "doc";
163
+ content?: TiptapNode[];
164
+ [key: string]: unknown;
165
+ };
166
+ type TiptapNode = {
167
+ type: string;
168
+ content?: TiptapNode[];
169
+ text?: string;
170
+ marks?: TiptapMark[];
171
+ attrs?: Record<string, unknown>;
172
+ [key: string]: unknown;
173
+ };
174
+ type TiptapMark = {
175
+ type: string;
176
+ attrs?: Record<string, unknown>;
177
+ [key: string]: unknown;
178
+ };
179
+ /**
180
+ * Normalizes a value for consistent hashing.
181
+ *
182
+ * Normalization rules:
183
+ * - Objects: Sort keys alphabetically, recursively normalize values
184
+ * - Arrays: Preserve order, recursively normalize items
185
+ * - Strings: Trim whitespace
186
+ * - Numbers/Booleans/null: Keep as-is
187
+ * - undefined: Convert to null
188
+ *
189
+ * @param value - Any JSON-serializable value
190
+ * @returns Normalized value
191
+ */
192
+ declare function normalizeForHashing(value: unknown): unknown;
193
+ /**
194
+ * Computes the commitment hash from evidence content.
195
+ *
196
+ * The hash is computed as:
197
+ * 1. Normalize the evidence (sort keys, trim strings, etc.)
198
+ * 2. Serialize to JSON string (deterministic due to normalization)
199
+ * 3. Apply Blake2b-256 hash
200
+ *
201
+ * @param evidence - The evidence content (Tiptap JSON document or any JSON-serializable data)
202
+ * @returns 64-character lowercase hex string (Blake2b-256 hash)
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * import { computeCommitmentHash } from "@andamio/core/hashing";
207
+ *
208
+ * const evidence = {
209
+ * type: "doc",
210
+ * content: [
211
+ * { type: "paragraph", content: [{ type: "text", text: "My submission" }] }
212
+ * ]
213
+ * };
214
+ *
215
+ * const hash = computeCommitmentHash(evidence);
216
+ * // Use this hash as commitment_hash in the transaction
217
+ * ```
218
+ */
219
+ declare function computeCommitmentHash(evidence: unknown): string;
220
+ /**
221
+ * Verifies that evidence content matches an expected hash.
222
+ *
223
+ * Use this to verify that database evidence matches the on-chain commitment.
224
+ *
225
+ * @param evidence - The evidence content to verify
226
+ * @param expectedHash - The expected hash (from on-chain data)
227
+ * @returns True if the evidence produces the expected hash
228
+ */
229
+ declare function verifyCommitmentHash(evidence: unknown, expectedHash: string): boolean;
230
+ /**
231
+ * Validates that a string is a valid commitment hash format.
232
+ *
233
+ * A valid hash is a 64-character hexadecimal string (Blake2b-256 output).
234
+ *
235
+ * @param hash - The string to validate
236
+ * @returns True if the string is a valid hash format
237
+ */
238
+ declare function isValidCommitmentHash(hash: string): boolean;
239
+ /**
240
+ * Result of comparing evidence with an on-chain hash
241
+ */
242
+ type EvidenceVerificationResult = {
243
+ /** Whether the evidence matches the on-chain hash */
244
+ isValid: boolean;
245
+ /** The hash computed from the evidence */
246
+ computedHash: string;
247
+ /** The expected hash (from on-chain) */
248
+ expectedHash: string;
249
+ /** Human-readable status message */
250
+ message: string;
251
+ };
252
+ /**
253
+ * Performs a detailed verification of evidence against an on-chain hash.
254
+ *
255
+ * Returns a detailed result object with both hashes and a status message,
256
+ * useful for debugging and user feedback.
257
+ *
258
+ * @param evidence - The evidence content to verify
259
+ * @param onChainHash - The hash from on-chain data
260
+ * @returns Detailed verification result
261
+ */
262
+ declare function verifyEvidenceDetailed(evidence: unknown, onChainHash: string): EvidenceVerificationResult;
263
+ /**
264
+ * @deprecated Use `computeCommitmentHash` instead.
265
+ */
266
+ declare const computeAssignmentInfoHash: typeof computeCommitmentHash;
267
+ /**
268
+ * @deprecated Use `verifyCommitmentHash` instead.
269
+ */
270
+ declare const verifyAssignmentInfoHash: typeof verifyCommitmentHash;
271
+ /**
272
+ * @deprecated Use `isValidCommitmentHash` instead.
273
+ */
274
+ declare const isValidAssignmentInfoHash: typeof isValidCommitmentHash;
275
+
276
+ export { type EvidenceVerificationResult, type TaskData, type TiptapDoc, type TiptapMark, type TiptapNode, computeAssignmentInfoHash, computeCommitmentHash, computeSltHash, computeSltHashDefinite, computeTaskHash, debugTaskCBOR, isValidAssignmentInfoHash, isValidCommitmentHash, isValidSltHash, isValidTaskHash, normalizeForHashing, verifyAssignmentInfoHash, verifyCommitmentHash, verifyEvidenceDetailed, verifySltHash, verifyTaskHash };