@fangorn-network/sdk 2026.4.7 → 2026.4.15

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 (61) hide show
  1. package/README.md +137 -150
  2. package/lib/cli/cli.js +134 -226
  3. package/lib/cli/index.d.ts +170 -170
  4. package/lib/config.d.ts +260 -257
  5. package/lib/config.js +7 -5
  6. package/lib/fangorn.d.ts +3 -20
  7. package/lib/fangorn.js +15 -52
  8. package/lib/index.d.ts +3 -7
  9. package/lib/index.js +4 -7
  10. package/lib/modules/encryption/aes.d.ts +3 -3
  11. package/lib/modules/encryption/index.d.ts +1 -28
  12. package/lib/modules/encryption/index.js +1 -3
  13. package/lib/modules/encryption/types.d.ts +1 -48
  14. package/lib/providers/storage/index.d.ts +4 -21
  15. package/lib/providers/storage/index.js +3 -2
  16. package/lib/providers/storage/pinata.d.ts +14 -0
  17. package/lib/providers/storage/pinata.js +32 -0
  18. package/lib/providers/storage/types.d.ts +37 -0
  19. package/lib/providers/storage/utils.d.ts +12 -0
  20. package/lib/providers/storage/utils.js +50 -0
  21. package/lib/registries/datasource-registry/abi.d.ts +116 -43
  22. package/lib/registries/datasource-registry/abi.js +190 -12
  23. package/lib/registries/datasource-registry/index.d.ts +12 -6
  24. package/lib/registries/datasource-registry/index.js +96 -20
  25. package/lib/registries/index.js +1 -1
  26. package/lib/registries/schema-registry/abi.d.ts +157 -66
  27. package/lib/registries/schema-registry/abi.js +242 -15
  28. package/lib/registries/schema-registry/index.d.ts +8 -3
  29. package/lib/registries/schema-registry/index.js +82 -17
  30. package/lib/registries/settlement-registry/abi.d.ts +199 -145
  31. package/lib/registries/settlement-registry/abi.js +331 -25
  32. package/lib/registries/settlement-registry/index.d.ts +36 -39
  33. package/lib/registries/settlement-registry/index.js +125 -91
  34. package/lib/registries/settlement-registry/types.d.ts +6 -3
  35. package/lib/roles/consumer/index.d.ts +17 -42
  36. package/lib/roles/consumer/index.js +87 -58
  37. package/lib/roles/consumer/types.d.ts +15 -34
  38. package/lib/roles/publisher/index.d.ts +22 -40
  39. package/lib/roles/publisher/index.js +69 -113
  40. package/lib/roles/publisher/types.d.ts +40 -41
  41. package/lib/roles/schema/index.d.ts +4 -30
  42. package/lib/roles/schema/index.js +7 -60
  43. package/lib/roles/schema/types.d.ts +16 -2
  44. package/lib/test/testbed.d.ts +12 -36
  45. package/lib/test/testbed.js +35 -68
  46. package/lib/types/index.d.ts +14 -57
  47. package/package.json +1 -6
  48. package/lib/modules/encryption/lit.d.ts +0 -21
  49. package/lib/modules/encryption/lit.js +0 -127
  50. package/lib/modules/gadgets/index.d.ts +0 -2
  51. package/lib/modules/gadgets/index.js +0 -1
  52. package/lib/modules/gadgets/settledGadget.d.ts +0 -31
  53. package/lib/modules/gadgets/settledGadget.js +0 -75
  54. package/lib/modules/gadgets/types.d.ts +0 -27
  55. package/lib/providers/storage/pinata/index.d.ts +0 -13
  56. package/lib/providers/storage/pinata/index.js +0 -41
  57. package/lib/providers/storage/storacha/index.d.ts +0 -13
  58. package/lib/providers/storage/storacha/index.js +0 -49
  59. package/lib/providers/storage/storacha/readOnly.d.ts +0 -8
  60. package/lib/providers/storage/storacha/readOnly.js +0 -16
  61. /package/lib/{modules/gadgets → providers/storage}/types.js +0 -0
package/lib/config.js CHANGED
@@ -26,13 +26,14 @@ function getNetwork(name) {
26
26
  }
27
27
  const FangornConfig = {
28
28
  ArbitrumSepolia: {
29
- dataSourceRegistryContractAddress: "0xddd338e6a200012642a103c6631ea92eea94cabe",
30
- schemaRegistryContractAddress: "0xef6754c29cfd0c8937a080695899f2a9a23c7c70",
31
- settlementRegistryContractAddress: "0x5e918ba3fe33b0bdc68cd46eb6a77db754edef57",
29
+ dataSourceRegistryContractAddress: "0xdb82c131a9d51f6e7695e744bb2bd7774cbb224c",
30
+ schemaRegistryContractAddress: "0x35b67934f9c75bfef6ff3f4d61ff406d81420066",
31
+ settlementRegistryContractAddress: "0x6aff8212e126ed3232958fd228bc58a202b8f590",
32
32
  chainName: "arbitrumSepolia",
33
33
  chain: arbitrumSepolia,
34
34
  rpcUrl: "https://sepolia-rollup.arbitrum.io/rpc",
35
- caip2: 421614
35
+ caip2: 421614,
36
+ ipfsGateway: "https://ipfs.io"
36
37
  },
37
38
  BaseSepolia: {
38
39
  dataSourceRegistryContractAddress: "0x6fd0e50073dbd8169bcaf066bb4a4991bfa48eeb",
@@ -41,7 +42,8 @@ const FangornConfig = {
41
42
  chainName: "baseSepolia",
42
43
  chain: baseSepolia,
43
44
  rpcUrl: "https://sepolia.base.org",
44
- caip2: 84532
45
+ caip2: 84532,
46
+ ipfsGateway: "https://ipfs.io"
45
47
  }
46
48
  };
47
49
 
package/lib/fangorn.d.ts CHANGED
@@ -2,10 +2,10 @@ import { AppConfig } from "./config.js";
2
2
  import { SchemaRegistry } from "./registries/schema-registry/index.js";
3
3
  import { SchemaRole } from "./roles/schema/index.js";
4
4
  import { DataSourceRegistry } from "./registries/datasource-registry/index.js";
5
- import { SettlementRegistry } from "./registries/settlement-registry/index.js";
6
- import { FangornCreateOptions } from "./types/index.js";
7
5
  import { PublisherRole } from "./roles/publisher/index.js";
6
+ import { SettlementRegistry } from "./registries/settlement-registry/index.js";
8
7
  import { ConsumerRole } from "./roles/consumer/index.js";
8
+ import { FangornCreateOptions } from "./types/index.js";
9
9
  import { Hex, WalletClient } from "viem";
10
10
 
11
11
  //#region src/fangorn.d.ts
@@ -15,27 +15,10 @@ declare class Fangorn {
15
15
  private _publisher;
16
16
  private _consumer;
17
17
  private constructor();
18
- /**
19
- * Schema owner: register agents, register schemas, validate definitions.
20
- * Requires writable storage.
21
- */
22
18
  get schema(): SchemaRole;
23
- /**
24
- * Publisher: encrypt, stage, and commit data under a schema.
25
- * Requires writable storage.
26
- */
27
19
  get publisher(): PublisherRole;
28
- /**
29
- * Consumer: purchase, claim, and decrypt data.
30
- * Works with both readable and writable storage.
31
- */
32
20
  get consumer(): ConsumerRole;
33
- /**
34
- * Create a new Fangorn instance
35
- * @param options
36
- * @returns
37
- */
38
- static create(options: FangornCreateOptions): Promise<Fangorn>;
21
+ static create(options: FangornCreateOptions): Fangorn;
39
22
  getConfig(): AppConfig;
40
23
  getSchemaRegistry(): SchemaRegistry;
41
24
  getDatasourceRegistry(): DataSourceRegistry;
package/lib/fangorn.js CHANGED
@@ -1,40 +1,22 @@
1
1
  import { FangornConfig } from "./config.js";
2
- import { LitEncryptionService } from "./modules/encryption/lit.js";
3
- import "./modules/encryption/index.js";
4
2
  import { SchemaRole } from "./roles/schema/index.js";
5
- import { SettlementRegistry } from "./registries/settlement-registry/index.js";
6
3
  import { PublisherRole } from "./roles/publisher/index.js";
4
+ import { DataSourceRegistry } from "./registries/datasource-registry/index.js";
5
+ import { PinataBackend } from "./providers/storage/pinata.js";
7
6
  import { ConsumerRole } from "./roles/consumer/index.js";
8
7
  import { SchemaRegistry } from "./registries/schema-registry/index.js";
9
- import { DataSourceRegistry } from "./registries/datasource-registry/index.js";
10
- import { PinataStorage } from "./providers/storage/pinata/index.js";
11
- import "./providers/storage/index.js";
12
- import { StorachaStorage } from "./providers/storage/storacha/index.js";
13
- import { ReadOnlyStorachaStorage } from "./providers/storage/storacha/readOnly.js";
8
+ import { SettlementRegistry } from "./registries/settlement-registry/index.js";
14
9
  import { createPublicClient, createWalletClient, http } from "viem";
15
10
  import { privateKeyToAccount } from "viem/accounts";
16
11
 
17
12
  //#region src/fangorn.ts
18
- function isEncryptionService(e) {
19
- return typeof e.encrypt === "function";
20
- }
21
13
  function isPinataConfig(s) {
22
14
  return "pinata" in s;
23
15
  }
24
- function isStorachaReadOnly(s) {
25
- return "storacha" in s && "readOnly" in s.storacha;
26
- }
27
- function isStorachaConfig(s) {
28
- return "storacha" in s && "email" in s.storacha;
29
- }
30
- function isWritable(s) {
31
- return "store" in s && "delete" in s;
32
- }
33
- async function resolveStorage(storage) {
34
- if (isPinataConfig(storage)) return new PinataStorage(storage.pinata.jwt, storage.pinata.gateway);
35
- if (isStorachaReadOnly(storage)) return new ReadOnlyStorachaStorage();
36
- if (isStorachaConfig(storage)) return StorachaStorage.create(storage.storacha.email);
37
- throw new Error("Invalid storage config: must be a StorageProvider instance, a { pinata } object, or a { storacha } object.");
16
+ function resolveStorage(storage) {
17
+ if (!storage) return void 0;
18
+ if (isPinataConfig(storage)) return new PinataBackend(storage.pinata.jwt, storage.pinata.gateway);
19
+ throw new Error(`Invalid storage config: must be { pinata: { jwt, gateway } }, got ${JSON.stringify(storage)}`);
38
20
  }
39
21
  var Fangorn = class Fangorn {
40
22
  ctx;
@@ -44,35 +26,18 @@ var Fangorn = class Fangorn {
44
26
  constructor(ctx) {
45
27
  this.ctx = ctx;
46
28
  }
47
- /**
48
- * Schema owner: register agents, register schemas, validate definitions.
49
- * Requires writable storage.
50
- */
51
29
  get schema() {
52
- if (!isWritable(this.ctx.storage)) throw new Error("fangorn.schema requires writable storage. Use { storacha: { email } } or { pinata: { ... } }");
53
- return this._schema ??= new SchemaRole(this.ctx.schemaRegistry, this.ctx.storage, this.ctx.walletClient, this.ctx.schemaRoleConfig);
30
+ if (!this.ctx.metadataStorage) throw new Error("fangorn.schema requires storage. Pass { pinata: { ... } } to Fangorn.create()");
31
+ return this._schema ??= new SchemaRole(this.ctx.schemaRegistry, this.ctx.metadataStorage, this.ctx.walletClient);
54
32
  }
55
- /**
56
- * Publisher: encrypt, stage, and commit data under a schema.
57
- * Requires writable storage.
58
- */
59
33
  get publisher() {
60
- if (!isWritable(this.ctx.storage)) throw new Error("fangorn.publisher requires writable storage. Use { storacha: { email } } or { pinata: { ... } }");
61
- return this._publisher ??= new PublisherRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry, this.ctx.schemaRegistry, this.ctx.storage, this.ctx.encryption, this.ctx.walletClient, this.ctx.config);
34
+ if (!this.ctx.metadataStorage) throw new Error("fangorn.publisher requires storage. Pass { pinata: { ... } } to Fangorn.create()");
35
+ return this._publisher ??= new PublisherRole(this.ctx.dataSourceRegistry, this.ctx.schemaRegistry, this.ctx.metadataStorage, this.ctx.walletClient, this.ctx.config);
62
36
  }
63
- /**
64
- * Consumer: purchase, claim, and decrypt data.
65
- * Works with both readable and writable storage.
66
- */
67
37
  get consumer() {
68
- return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry, this.ctx.storage, this.ctx.encryption, this.ctx.domain);
38
+ return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry);
69
39
  }
70
- /**
71
- * Create a new Fangorn instance
72
- * @param options
73
- * @returns
74
- */
75
- static async create(options) {
40
+ static create(options) {
76
41
  if (!options.privateKey && !options.walletClient) throw new Error("Either privateKey or walletClient must be provided");
77
42
  const resolvedConfig = options.config ?? FangornConfig.ArbitrumSepolia;
78
43
  const walletClient = options.walletClient ?? createWalletClient({
@@ -80,14 +45,12 @@ var Fangorn = class Fangorn {
80
45
  chain: resolvedConfig.chain,
81
46
  transport: http(resolvedConfig.rpcUrl)
82
47
  });
83
- const storage = await resolveStorage(options.storage);
84
- const encryption = isEncryptionService(options.encryption) ? options.encryption : await LitEncryptionService.init(resolvedConfig.chainName);
48
+ const metadataStorage = resolveStorage(options.storage);
85
49
  const domain = options.domain ?? new URL(resolvedConfig.rpcUrl).hostname;
86
50
  const publicClient = createPublicClient({ transport: http(resolvedConfig.rpcUrl) });
87
51
  return new Fangorn({
88
52
  walletClient,
89
- storage,
90
- encryption,
53
+ metadataStorage,
91
54
  domain,
92
55
  schemaRegistry: new SchemaRegistry(resolvedConfig.schemaRegistryContractAddress, publicClient, walletClient),
93
56
  dataSourceRegistry: new DataSourceRegistry(resolvedConfig.dataSourceRegistryContractAddress, publicClient, walletClient),
package/lib/index.d.ts CHANGED
@@ -1,12 +1,8 @@
1
1
  import { AppConfig, FangornConfig, SupportedNetworks } from "./config.js";
2
- import { PinataStorage } from "./providers/storage/pinata/index.js";
3
- import { ReadableStorage, WritableStorage } from "./providers/storage/index.js";
4
- import { Gadget } from "./modules/gadgets/types.js";
5
- import "./modules/gadgets/index.js";
6
- import { LitEncryptionService } from "./modules/encryption/lit.js";
7
- import { AuthContext, AuthSig, EncryptionService } from "./modules/encryption/index.js";
8
2
  import { Fangorn } from "./fangorn.js";
3
+ import { PinataBackend } from "./providers/storage/pinata.js";
4
+ import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
9
5
  import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
10
6
  import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
11
7
  import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
12
- export { AppConfig, AuthContext, AuthSig, DS_REGISTRY_ABI, EncryptionService, Fangorn, FangornConfig, Gadget, LitEncryptionService, PinataStorage, ReadableStorage, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, WritableStorage, addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array };
8
+ export { AppConfig, DS_REGISTRY_ABI, Fangorn, FangornConfig, PinataBackend, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
package/lib/index.js CHANGED
@@ -1,12 +1,9 @@
1
1
  import { FangornConfig, SupportedNetworks } from "./config.js";
2
- import { LitEncryptionService } from "./modules/encryption/lit.js";
3
- import "./modules/encryption/index.js";
4
- import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
5
2
  import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
6
- import { PinataStorage } from "./providers/storage/pinata/index.js";
7
- import "./providers/storage/index.js";
3
+ import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
4
+ import { PinataBackend } from "./providers/storage/pinata.js";
5
+ import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
8
6
  import { Fangorn } from "./fangorn.js";
9
- import "./registries/index.js";
10
7
  import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
11
8
 
12
- export { DS_REGISTRY_ABI, Fangorn, FangornConfig, LitEncryptionService, PinataStorage, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array };
9
+ export { DS_REGISTRY_ABI, Fangorn, FangornConfig, PinataBackend, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
@@ -1,10 +1,10 @@
1
- import { EncryptedData } from "../../types/index.js";
1
+ import { AesEncryptedData } from "./types.js";
2
2
 
3
3
  //#region src/modules/encryption/aes.d.ts
4
4
  declare function encryptData(data: string | Uint8Array): Promise<{
5
- encryptedData: EncryptedData;
5
+ encryptedData: AesEncryptedData;
6
6
  keyMaterial: Uint8Array<ArrayBuffer>;
7
7
  }>;
8
- declare function decryptData(encryptedData: EncryptedData, keyMaterial: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
8
+ declare function decryptData(encryptedData: AesEncryptedData, keyMaterial: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
9
9
  //#endregion
10
10
  export { decryptData, encryptData };
@@ -1,28 +1 @@
1
- import { Gadget } from "../gadgets/types.js";
2
- import { Filedata } from "../../types/index.js";
3
- import "../gadgets/index.js";
4
- import { DecryptedPayload, EncryptedPayload } from "./types.js";
5
- import { LitEncryptionService } from "./lit.js";
6
- import { WalletClient } from "viem";
7
-
8
- //#region src/modules/encryption/index.d.ts
9
- interface EncryptionService {
10
- encrypt(file: Filedata, gadget: Gadget): Promise<EncryptedPayload>;
11
- decrypt(payload: EncryptedPayload, authContext: AuthContext): Promise<DecryptedPayload>;
12
- createAuthContext(walletClient: WalletClient, domain: string, nullifierHash: bigint): Promise<AuthContext>;
13
- }
14
- /**
15
- * Auth context needed for Lit decryption
16
- */
17
- interface AuthContext {
18
- authSig: AuthSig;
19
- sessionContext?: unknown;
20
- }
21
- interface AuthSig {
22
- sig: string;
23
- derivedVia: string;
24
- signedMessage: string;
25
- address: string;
26
- }
27
- //#endregion
28
- export { AuthContext, AuthSig, EncryptionService, LitEncryptionService };
1
+ export { };
@@ -1,3 +1 @@
1
- import { LitEncryptionService } from "./lit.js";
2
-
3
- export { LitEncryptionService };
1
+ export { };
@@ -1,60 +1,13 @@
1
- import { createAuthManager } from "@lit-protocol/auth";
2
- import { UnifiedAccessControlCondition } from "@lit-protocol/access-control-conditions";
3
-
4
1
  //#region src/modules/encryption/types.d.ts
5
- type EoaAuthContext = Awaited<ReturnType<ReturnType<typeof createAuthManager>["createEoaAuthContext"]>>;
6
2
  interface AesEncryptedData {
7
3
  ciphertext: Uint8Array<ArrayBuffer>;
8
4
  iv: Uint8Array<ArrayBuffer>;
9
5
  authTag: Uint8Array<ArrayBuffer>;
10
6
  salt: Uint8Array<ArrayBuffer>;
11
7
  }
12
- interface LitEncryptedData {
13
- ciphertext: string;
14
- dataToEncryptHash: string;
15
- }
16
- interface EncryptedPayload {
17
- /** Locally AES-encrypted file bytes */
18
- data: AesEncryptedData;
19
- /** Lit-threshold-encrypted AES key */
20
- key: {
21
- ciphertext: string;
22
- dataToEncryptHash: string;
23
- };
24
- /**
25
- * The ACC used to encrypt the key — stored here so decrypt() can
26
- * reconstruct the exact conditions without re-deriving them from
27
- * the gadget. The gadget is a publisher-time concept; the consumer
28
- * only has the payload.
29
- */
30
- acc: UnifiedAccessControlCondition[];
31
- }
32
8
  interface DecryptedPayload {
33
9
  data: Uint8Array;
34
10
  }
35
- /**
36
- * Lit's access control condition structure
37
- * Using a loose type since Lit's ACC shape is complex and varies by condition type
38
- */
39
- type AccessControlCondition = Record<string, unknown>;
40
- /**
41
- * Auth signature for Lit
42
- */
43
- interface AuthSig {
44
- sig: string;
45
- derivedVia: string;
46
- signedMessage: string;
47
- address: string;
48
- }
49
- /**
50
- * Context needed to authenticate with Lit for decryption
51
- */
52
- interface AuthContextWrapper {
53
- authSig: AuthSig;
54
- sessionContext: EoaAuthContext;
55
- chainName: string;
56
- nullifierHash?: string;
57
- }
58
11
  /**
59
12
  * Result of successful decryption
60
13
  */
@@ -67,4 +20,4 @@ interface DecryptedPayload {
67
20
  };
68
21
  }
69
22
  //#endregion
70
- export { AccessControlCondition, AesEncryptedData, AuthContextWrapper, AuthSig, DecryptedPayload, EncryptedPayload, EoaAuthContext, LitEncryptedData };
23
+ export { AesEncryptedData, DecryptedPayload };
@@ -1,21 +1,4 @@
1
- import { PinataStorage } from "./pinata/index.js";
2
-
3
- //#region src/providers/storage/index.d.ts
4
-
5
- /**
6
- * Read-only storage (e.g. for a consuming party)
7
- * Does not require any authentication
8
- */
9
- interface ReadableStorage<T> {
10
- retrieve(cid: string): Promise<T>;
11
- }
12
- /**
13
- * Read/Write storage
14
- * (Likely) Requires authentication.
15
- */
16
- interface WritableStorage<T> extends ReadableStorage<T> {
17
- store(data: T, metadata?: Record<string, unknown>): Promise<string>;
18
- delete(cid: string): Promise<void>;
19
- }
20
- //#endregion
21
- export { PinataStorage, ReadableStorage, WritableStorage };
1
+ import { MetadataStorage, StorageMeta } from "./types.js";
2
+ import { PinataBackend } from "./pinata.js";
3
+ import { deserialize, retrieveByCid, serialize } from "./utils.js";
4
+ export { type MetadataStorage, PinataBackend, type StorageMeta, deserialize, retrieveByCid, serialize };
@@ -1,3 +1,4 @@
1
- import { PinataStorage } from "./pinata/index.js";
1
+ import { deserialize, retrieveByCid, serialize } from "./utils.js";
2
+ import { PinataBackend } from "./pinata.js";
2
3
 
3
- export { PinataStorage };
4
+ export { PinataBackend, deserialize, retrieveByCid, serialize };
@@ -0,0 +1,14 @@
1
+ import { MetadataStorage, StorageMeta } from "./types.js";
2
+
3
+ //#region src/providers/storage/pinata.d.ts
4
+ declare class PinataBackend implements MetadataStorage {
5
+ private pinata;
6
+ private gateway;
7
+ constructor(pinataJwt: string, pinataGateway: string);
8
+ put(data: unknown, meta?: StorageMeta): Promise<string>;
9
+ get<T>(uri: string): Promise<T>;
10
+ static getStatic<T>(uri: string): Promise<T>;
11
+ delete(uri: string): Promise<void>;
12
+ }
13
+ //#endregion
14
+ export { PinataBackend };
@@ -0,0 +1,32 @@
1
+ import { retrieveByCid, serialize } from "./utils.js";
2
+ import { PinataSDK } from "pinata";
3
+
4
+ //#region src/providers/storage/pinata.ts
5
+ var PinataBackend = class {
6
+ pinata;
7
+ gateway;
8
+ constructor(pinataJwt, pinataGateway) {
9
+ this.pinata = new PinataSDK({
10
+ pinataJwt,
11
+ pinataGateway
12
+ });
13
+ this.gateway = pinataGateway;
14
+ }
15
+ async put(data, meta) {
16
+ const content = serialize(data);
17
+ const file = new File([content], meta?.name ?? "file", { type: "text/plain" });
18
+ return (await this.pinata.upload.public.file(file, { metadata: meta })).cid;
19
+ }
20
+ async get(uri) {
21
+ return retrieveByCid(uri);
22
+ }
23
+ static async getStatic(uri) {
24
+ return retrieveByCid(uri);
25
+ }
26
+ async delete(uri) {
27
+ await this.pinata.files.public.delete([uri]);
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ export { PinataBackend };
@@ -0,0 +1,37 @@
1
+ //#region src/providers/storage/types.d.ts
2
+ /**
3
+ * StorageBackend — backend-agnostic interface for storing and retrieving
4
+ * opaque content. The URI returned by put() is the only thing callers
5
+ * hold onto; get() knows how to resolve it.
6
+ */
7
+ interface StorageMeta {
8
+ name?: string;
9
+ [key: string]: unknown;
10
+ }
11
+ interface MetadataStorage {
12
+ /**
13
+ * Store content and return an opaque URI.
14
+ * Callers never construct or parse URIs — treat them as handles.
15
+ */
16
+ put(data: unknown, meta?: StorageMeta): Promise<string>;
17
+ /**
18
+ * Retrieve content by URI previously returned from put().
19
+ */
20
+ get<T>(uri: string): Promise<T>;
21
+ /**
22
+ * Delete content by URI. Best-effort — implementations may no-op.
23
+ */
24
+ delete(uri: string): Promise<void>;
25
+ }
26
+ /**
27
+ * Proof required by access-controlled backends (e.g. R2 via the Fangorn worker).
28
+ * Plain backends (e.g. IPFS) ignore this entirely.
29
+ */
30
+ interface AccessProof {
31
+ nullifier: string;
32
+ resourceId: string;
33
+ timestamp: number;
34
+ signature: `0x${string}`;
35
+ }
36
+ //#endregion
37
+ export { AccessProof, MetadataStorage, StorageMeta };
@@ -0,0 +1,12 @@
1
+ import { PinataBackend } from "./pinata.js";
2
+
3
+ //#region src/providers/storage/utils.d.ts
4
+ declare function serialize(data: unknown): string;
5
+ declare function deserialize(text: string): unknown;
6
+ /**
7
+ * Fetch any content by CID from a public IPFS gateway.
8
+ * No auth required — all Fangorn content is public; privacy is cryptographic.
9
+ */
10
+ declare function retrieveByCid<T>(cid: string, gateway?: string, timeoutSecond?: number): Promise<T>;
11
+ //#endregion
12
+ export { PinataBackend, deserialize, retrieveByCid, serialize };
@@ -0,0 +1,50 @@
1
+ import { PinataBackend } from "./pinata.js";
2
+
3
+ //#region src/providers/storage/utils.ts
4
+ const REVIVERS = [(_key, value) => {
5
+ if (value !== null && typeof value === "object" && "__type" in value && "data" in value && value.__type === "Uint8Array") return new Uint8Array(Buffer.from(value.data, "base64"));
6
+ return value;
7
+ }];
8
+ function serialize(data) {
9
+ if (typeof data === "string") return data;
10
+ return JSON.stringify(data, (_key, value) => {
11
+ if (value instanceof Uint8Array) return {
12
+ __type: "Uint8Array",
13
+ data: Buffer.from(value).toString("base64")
14
+ };
15
+ return value;
16
+ });
17
+ }
18
+ function deserialize(text) {
19
+ return JSON.parse(text, (key, value) => {
20
+ for (const reviver of REVIVERS) {
21
+ const result = reviver(key, value);
22
+ if (result !== value) return result;
23
+ }
24
+ return value;
25
+ });
26
+ }
27
+ /**
28
+ * Fetch any content by CID from a public IPFS gateway.
29
+ * No auth required — all Fangorn content is public; privacy is cryptographic.
30
+ */
31
+ async function retrieveByCid(cid, gateway = "https://ipfs.io", timeoutSecond = 16e3) {
32
+ const url = `${gateway.replace(/\/$/, "")}/ipfs/${cid}`;
33
+ const controller = new AbortController();
34
+ const timeout = setTimeout(() => {
35
+ controller.abort();
36
+ }, timeoutSecond);
37
+ try {
38
+ const res = await fetch(url, { signal: controller.signal });
39
+ if (!res.ok) throw new Error(`Failed to retrieve ${cid}: ${res.statusText}`);
40
+ return deserialize(await res.text());
41
+ } catch (err) {
42
+ if (err.name === "AbortError") throw new Error(`Timed out retrieving ${cid} from ${url}`);
43
+ throw err;
44
+ } finally {
45
+ clearTimeout(timeout);
46
+ }
47
+ }
48
+
49
+ //#endregion
50
+ export { PinataBackend, deserialize, retrieveByCid, serialize };