forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.ts
More file actions
37 lines (33 loc) · 1.52 KB
/
Copy pathreference.ts
File metadata and controls
37 lines (33 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Helpers for referencing IPFS-hosted ciphertext.
*
* Large encrypted prompt payloads are uploaded to IPFS and only a compact
* `ipfs://<cid>` reference is stored on-chain — comfortably under the 4KB
* contract field limit. These helpers let both the browser (upload side) and
* the unlock service (fetch side) recognise and parse those references.
*
* This module is intentionally free of any browser- or Node-specific APIs so it
* can be imported safely from both environments.
*/
export const IPFS_URI_PREFIX = "ipfs://";
/** Returns true when a stored payload is an IPFS reference rather than inline ciphertext. */
export function isIpfsReference(value: string | null | undefined): value is string {
return typeof value === "string" && value.trim().startsWith(IPFS_URI_PREFIX);
}
/**
* Extracts the bare CID from an `ipfs://` reference, tolerating extra leading
* slashes (e.g. `ipfs:///cid`). Returns null when the value is not a reference.
*/
export function parseIpfsCid(value: string | null | undefined): string | null {
if (!isIpfsReference(value)) return null;
const cid = value.trim().slice(IPFS_URI_PREFIX.length).replace(/^\/+/, "");
return cid.length > 0 ? cid : null;
}
/** Builds a canonical `ipfs://<cid>` reference for storage on-chain. */
export function toIpfsUri(cid: string): string {
const trimmed = cid.trim().replace(/^\/+/, "");
if (!trimmed) {
throw new Error("Cannot build an IPFS URI from an empty CID.");
}
return isIpfsReference(trimmed) ? trimmed : `${IPFS_URI_PREFIX}${trimmed}`;
}