@fangorn-network/sdk 2026.4.10 → 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.
- package/README.md +118 -135
- package/lib/cli/cli.js +42 -96
- package/lib/cli/index.d.ts +159 -159
- package/lib/fangorn.d.ts +3 -17
- package/lib/fangorn.js +12 -37
- package/lib/index.d.ts +3 -7
- package/lib/index.js +3 -5
- package/lib/modules/encryption/aes.d.ts +3 -3
- package/lib/modules/encryption/index.d.ts +1 -28
- package/lib/modules/encryption/index.js +1 -3
- package/lib/modules/encryption/types.d.ts +1 -48
- package/lib/providers/storage/index.d.ts +4 -21
- package/lib/providers/storage/index.js +3 -49
- package/lib/providers/storage/pinata.d.ts +8 -5
- package/lib/providers/storage/pinata.js +16 -8
- package/lib/providers/storage/types.d.ts +37 -0
- package/lib/providers/storage/utils.d.ts +12 -0
- package/lib/providers/storage/utils.js +50 -0
- package/lib/registries/datasource-registry/index.d.ts +2 -2
- package/lib/registries/schema-registry/index.d.ts +2 -2
- package/lib/registries/settlement-registry/index.d.ts +2 -2
- package/lib/roles/consumer/index.d.ts +14 -18
- package/lib/roles/consumer/index.js +73 -24
- package/lib/roles/consumer/types.d.ts +13 -32
- package/lib/roles/publisher/index.d.ts +17 -32
- package/lib/roles/publisher/index.js +37 -74
- package/lib/roles/publisher/types.d.ts +36 -41
- package/lib/roles/schema/index.d.ts +4 -25
- package/lib/roles/schema/index.js +5 -30
- package/lib/roles/schema/types.d.ts +16 -2
- package/lib/test/testbed.d.ts +6 -5
- package/lib/test/testbed.js +16 -26
- package/lib/types/index.d.ts +12 -45
- package/package.json +1 -6
- package/lib/modules/encryption/lit.d.ts +0 -20
- package/lib/modules/encryption/lit.js +0 -121
- package/lib/modules/gadgets/index.d.ts +0 -2
- package/lib/modules/gadgets/index.js +0 -1
- package/lib/modules/gadgets/settledGadget.d.ts +0 -24
- package/lib/modules/gadgets/settledGadget.js +0 -63
- package/lib/modules/gadgets/types.d.ts +0 -27
- /package/lib/{modules/gadgets → providers/storage}/types.js +0 -0
package/lib/fangorn.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { FangornConfig } from "./config.js";
|
|
2
|
-
import { LitEncryptionService } from "./modules/encryption/lit.js";
|
|
3
|
-
import "./modules/encryption/index.js";
|
|
4
|
-
import { PinataStorage } from "./providers/storage/pinata.js";
|
|
5
|
-
import "./providers/storage/index.js";
|
|
6
2
|
import { SchemaRole } from "./roles/schema/index.js";
|
|
7
|
-
import { DataSourceRegistry } from "./registries/datasource-registry/index.js";
|
|
8
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";
|
|
9
6
|
import { ConsumerRole } from "./roles/consumer/index.js";
|
|
10
7
|
import { SchemaRegistry } from "./registries/schema-registry/index.js";
|
|
11
8
|
import { SettlementRegistry } from "./registries/settlement-registry/index.js";
|
|
@@ -13,17 +10,13 @@ import { createPublicClient, createWalletClient, http } from "viem";
|
|
|
13
10
|
import { privateKeyToAccount } from "viem/accounts";
|
|
14
11
|
|
|
15
12
|
//#region src/fangorn.ts
|
|
16
|
-
const DEFAULT_IPFS_GATEWAY = "https://ipfs.io";
|
|
17
|
-
function isEncryptionService(e) {
|
|
18
|
-
return typeof e.encrypt === "function";
|
|
19
|
-
}
|
|
20
13
|
function isPinataConfig(s) {
|
|
21
14
|
return "pinata" in s;
|
|
22
15
|
}
|
|
23
16
|
function resolveStorage(storage) {
|
|
24
17
|
if (!storage) return void 0;
|
|
25
|
-
if (isPinataConfig(storage)) return new
|
|
26
|
-
throw new Error(`Invalid storage config: must be { pinata: { jwt, gateway } },
|
|
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)}`);
|
|
27
20
|
}
|
|
28
21
|
var Fangorn = class Fangorn {
|
|
29
22
|
ctx;
|
|
@@ -33,32 +26,18 @@ var Fangorn = class Fangorn {
|
|
|
33
26
|
constructor(ctx) {
|
|
34
27
|
this.ctx = ctx;
|
|
35
28
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Schema owner: register agents, register schemas, validate definitions.
|
|
38
|
-
*/
|
|
39
29
|
get schema() {
|
|
40
|
-
if (!this.ctx.
|
|
41
|
-
return this._schema ??= new SchemaRole(this.ctx.schemaRegistry, this.ctx.
|
|
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);
|
|
42
32
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Publisher: encrypt, stage, and commit data under a schema.
|
|
45
|
-
*/
|
|
46
33
|
get publisher() {
|
|
47
|
-
if (!this.ctx.
|
|
48
|
-
return this._publisher ??= new PublisherRole(this.ctx.dataSourceRegistry, this.ctx.schemaRegistry, this.ctx.
|
|
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);
|
|
49
36
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Consumer: purchase, claim, and decrypt data.
|
|
52
|
-
*/
|
|
53
37
|
get consumer() {
|
|
54
|
-
return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry
|
|
38
|
+
return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry);
|
|
55
39
|
}
|
|
56
|
-
|
|
57
|
-
* Create a new Fangorn instance
|
|
58
|
-
* @param options
|
|
59
|
-
* @returns
|
|
60
|
-
*/
|
|
61
|
-
static async create(options) {
|
|
40
|
+
static create(options) {
|
|
62
41
|
if (!options.privateKey && !options.walletClient) throw new Error("Either privateKey or walletClient must be provided");
|
|
63
42
|
const resolvedConfig = options.config ?? FangornConfig.ArbitrumSepolia;
|
|
64
43
|
const walletClient = options.walletClient ?? createWalletClient({
|
|
@@ -66,17 +45,13 @@ var Fangorn = class Fangorn {
|
|
|
66
45
|
chain: resolvedConfig.chain,
|
|
67
46
|
transport: http(resolvedConfig.rpcUrl)
|
|
68
47
|
});
|
|
69
|
-
const
|
|
70
|
-
const encryption = isEncryptionService(options.encryption) ? options.encryption : await LitEncryptionService.init(resolvedConfig.chainName);
|
|
48
|
+
const metadataStorage = resolveStorage(options.storage);
|
|
71
49
|
const domain = options.domain ?? new URL(resolvedConfig.rpcUrl).hostname;
|
|
72
|
-
const ipfsGateway = options.ipfsGateway ?? DEFAULT_IPFS_GATEWAY;
|
|
73
50
|
const publicClient = createPublicClient({ transport: http(resolvedConfig.rpcUrl) });
|
|
74
51
|
return new Fangorn({
|
|
75
52
|
walletClient,
|
|
76
|
-
|
|
77
|
-
encryption,
|
|
53
|
+
metadataStorage,
|
|
78
54
|
domain,
|
|
79
|
-
ipfsGateway,
|
|
80
55
|
schemaRegistry: new SchemaRegistry(resolvedConfig.schemaRegistryContractAddress, publicClient, walletClient),
|
|
81
56
|
dataSourceRegistry: new DataSourceRegistry(resolvedConfig.dataSourceRegistryContractAddress, publicClient, walletClient),
|
|
82
57
|
settlementRegistry: new SettlementRegistry(resolvedConfig.settlementRegistryContractAddress, 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.js";
|
|
3
|
-
import { PinningService, deserialize, retrieveByCid, serialize } 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,
|
|
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,11 +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 { PinataStorage } from "./providers/storage/pinata.js";
|
|
5
|
-
import { deserialize, retrieveByCid, serialize } from "./providers/storage/index.js";
|
|
6
2
|
import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
|
|
3
|
+
import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
|
|
4
|
+
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
7
5
|
import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
|
|
8
6
|
import { Fangorn } from "./fangorn.js";
|
|
9
7
|
import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
|
|
10
8
|
|
|
11
|
-
export { DS_REGISTRY_ABI, Fangorn, FangornConfig,
|
|
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 {
|
|
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:
|
|
5
|
+
encryptedData: AesEncryptedData;
|
|
6
6
|
keyMaterial: Uint8Array<ArrayBuffer>;
|
|
7
7
|
}>;
|
|
8
|
-
declare function decryptData(encryptedData:
|
|
8
|
+
declare function decryptData(encryptedData: AesEncryptedData, keyMaterial: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
|
|
9
9
|
//#endregion
|
|
10
10
|
export { decryptData, encryptData };
|
|
@@ -1,28 +1 @@
|
|
|
1
|
-
|
|
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,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 {
|
|
23
|
+
export { AesEncryptedData, DecryptedPayload };
|
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A pinning service that stores content on IPFS and returns a CID.
|
|
7
|
-
* Retrieval is always done via a public gateway — see retrieveByCid.
|
|
8
|
-
*/
|
|
9
|
-
interface PinningService {
|
|
10
|
-
store(data: unknown, metadata?: Record<string, unknown>): Promise<string>;
|
|
11
|
-
delete(cid: string): Promise<void>;
|
|
12
|
-
}
|
|
13
|
-
declare function serialize(data: unknown): string;
|
|
14
|
-
declare function deserialize(text: string): unknown;
|
|
15
|
-
/**
|
|
16
|
-
* Fetch any content by CID from a public IPFS gateway.
|
|
17
|
-
* No auth required — all Fangorn content is public; privacy is cryptographic.
|
|
18
|
-
*/
|
|
19
|
-
declare function retrieveByCid<T>(cid: string, gateway?: string): Promise<T>;
|
|
20
|
-
//#endregion
|
|
21
|
-
export { PinataStorage, PinningService, deserialize, retrieveByCid, serialize };
|
|
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,50 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { deserialize, retrieveByCid, serialize } from "./utils.js";
|
|
2
|
+
import { PinataBackend } from "./pinata.js";
|
|
2
3
|
|
|
3
|
-
|
|
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") {
|
|
32
|
-
const url = `${gateway.replace(/\/$/, "")}/ipfs/${cid}`;
|
|
33
|
-
const controller = new AbortController();
|
|
34
|
-
const timeout = setTimeout(() => {
|
|
35
|
-
controller.abort();
|
|
36
|
-
}, 1e4);
|
|
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 { PinataStorage, deserialize, retrieveByCid, serialize };
|
|
4
|
+
export { PinataBackend, deserialize, retrieveByCid, serialize };
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MetadataStorage, StorageMeta } from "./types.js";
|
|
2
2
|
|
|
3
3
|
//#region src/providers/storage/pinata.d.ts
|
|
4
|
-
declare class
|
|
4
|
+
declare class PinataBackend implements MetadataStorage {
|
|
5
5
|
private pinata;
|
|
6
|
+
private gateway;
|
|
6
7
|
constructor(pinataJwt: string, pinataGateway: string);
|
|
7
|
-
|
|
8
|
-
|
|
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>;
|
|
9
12
|
}
|
|
10
13
|
//#endregion
|
|
11
|
-
export {
|
|
14
|
+
export { PinataBackend };
|
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
import { serialize } from "./
|
|
1
|
+
import { retrieveByCid, serialize } from "./utils.js";
|
|
2
2
|
import { PinataSDK } from "pinata";
|
|
3
3
|
|
|
4
4
|
//#region src/providers/storage/pinata.ts
|
|
5
|
-
var
|
|
5
|
+
var PinataBackend = class {
|
|
6
6
|
pinata;
|
|
7
|
+
gateway;
|
|
7
8
|
constructor(pinataJwt, pinataGateway) {
|
|
8
9
|
this.pinata = new PinataSDK({
|
|
9
10
|
pinataJwt,
|
|
10
11
|
pinataGateway
|
|
11
12
|
});
|
|
13
|
+
this.gateway = pinataGateway;
|
|
12
14
|
}
|
|
13
|
-
async
|
|
15
|
+
async put(data, meta) {
|
|
14
16
|
const content = serialize(data);
|
|
15
|
-
const file = new File([content],
|
|
16
|
-
return (await this.pinata.upload.public.file(file, { metadata })).cid;
|
|
17
|
+
const file = new File([content], meta?.name ?? "file", { type: "text/plain" });
|
|
18
|
+
return (await this.pinata.upload.public.file(file, { metadata: meta })).cid;
|
|
17
19
|
}
|
|
18
|
-
async
|
|
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]);
|
|
20
28
|
}
|
|
21
29
|
};
|
|
22
30
|
|
|
23
31
|
//#endregion
|
|
24
|
-
export {
|
|
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 };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as viem173 from "viem";
|
|
2
2
|
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
3
3
|
|
|
4
4
|
//#region src/registries/datasource-registry/index.d.ts
|
|
@@ -23,7 +23,7 @@ declare class DataSourceRegistry {
|
|
|
23
23
|
resourceId(owner: Address, schemaId: Hex, name: string): Promise<Hex>;
|
|
24
24
|
static resourceIdLocal(owner: Address, schemaId: Hex, name: string): Hex;
|
|
25
25
|
hashName(name: string): Hex;
|
|
26
|
-
waitForTransaction(hash: Hash): Promise<
|
|
26
|
+
waitForTransaction(hash: Hash): Promise<viem173.TransactionReceipt>;
|
|
27
27
|
private estimateGas;
|
|
28
28
|
}
|
|
29
29
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as viem171 from "viem";
|
|
2
2
|
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
3
3
|
|
|
4
4
|
//#region src/registries/schema-registry/index.d.ts
|
|
@@ -27,7 +27,7 @@ declare class SchemaRegistry {
|
|
|
27
27
|
hasPublishers(nameOrId: string): Promise<boolean>;
|
|
28
28
|
isPublisher(nameOrId: string, publisher: Address): Promise<boolean>;
|
|
29
29
|
getPublisherCount(nameOrId: string): Promise<bigint>;
|
|
30
|
-
waitForTransaction(hash: Hash): Promise<
|
|
30
|
+
waitForTransaction(hash: Hash): Promise<viem171.TransactionReceipt>;
|
|
31
31
|
private resolveId;
|
|
32
32
|
}
|
|
33
33
|
//#endregion
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PrepareSettleParams, PrepareSettleResult, RegisterParams, SettleParams, TransferWithAuthParams, TransferWithAuthPayload } from "./types.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as viem172 from "viem";
|
|
3
3
|
import { Address, Hex, PublicClient, WalletClient } from "viem";
|
|
4
4
|
|
|
5
5
|
//#region src/registries/settlement-registry/index.d.ts
|
|
@@ -59,7 +59,7 @@ declare class SettlementRegistry {
|
|
|
59
59
|
getGroupId(resourceId: Hex): Promise<bigint>;
|
|
60
60
|
getPrice(resourceId: Hex): Promise<bigint>;
|
|
61
61
|
getOwner(resourceId: Hex): Promise<Address>;
|
|
62
|
-
waitForTransaction(hash: Hex): Promise<
|
|
62
|
+
waitForTransaction(hash: Hex): Promise<viem172.TransactionReceipt>;
|
|
63
63
|
/**
|
|
64
64
|
* Reconstruct the Semaphore group from MemberRegistered events.
|
|
65
65
|
*/
|
|
@@ -1,41 +1,37 @@
|
|
|
1
1
|
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
2
|
+
import { Manifest, ManifestEntry } from "../publisher/types.js";
|
|
2
3
|
import { PrepareSettleParams, PrepareSettleResult, TransferWithAuthParams, TransferWithAuthPayload } from "../../registries/settlement-registry/types.js";
|
|
3
4
|
import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { ClaimParams, ClaimResult, DecryptParams, PurchaseParams, PurchaseResult } from "./types.js";
|
|
7
|
-
import { Address, Hex } from "viem";
|
|
5
|
+
import { ClaimParams, ClaimResult, FetchParams, FetchResult, PurchaseParams, PurchaseResult } from "./types.js";
|
|
6
|
+
import { Address, Hex, WalletClient } from "viem";
|
|
8
7
|
import { Identity } from "@semaphore-protocol/identity";
|
|
9
8
|
|
|
10
9
|
//#region src/roles/consumer/index.d.ts
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The consumer namespace encapsulates all functionality relative to
|
|
14
|
-
* accessing and consuming data.
|
|
15
|
-
*/
|
|
16
10
|
declare class ConsumerRole {
|
|
17
11
|
private readonly dataSourceRegistry;
|
|
18
12
|
private readonly settlementRegistry;
|
|
19
|
-
|
|
20
|
-
private readonly domain;
|
|
21
|
-
private readonly ipfsGateway;
|
|
22
|
-
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry, encryptionService: EncryptionService, domain: string, ipfsGateway: string);
|
|
13
|
+
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry);
|
|
23
14
|
prepareRegister(params: TransferWithAuthParams): Promise<TransferWithAuthPayload>;
|
|
24
15
|
prepareSettle(params: PrepareSettleParams): Promise<PrepareSettleResult>;
|
|
25
16
|
register(params: PurchaseParams): Promise<PurchaseResult>;
|
|
26
17
|
claim(params: ClaimParams): Promise<ClaimResult>;
|
|
27
18
|
/**
|
|
28
|
-
*
|
|
19
|
+
* Fetch a handle field's content from the Fangorn access worker.
|
|
29
20
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
21
|
+
* The consumer signs { nullifier, resourceId, objectKey, timestamp } with
|
|
22
|
+
* their stealth address private key. The worker recovers the address,
|
|
23
|
+
* verifies is_settled() on-chain, and proxies the R2 bytes.
|
|
33
24
|
*
|
|
34
25
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
35
26
|
*/
|
|
36
|
-
|
|
27
|
+
fetch(params: FetchParams): Promise<FetchResult>;
|
|
37
28
|
getManifest(owner: Address, schemaId: Hex, name: string): Promise<Manifest | undefined>;
|
|
38
29
|
getEntry(owner: Address, schemaId: Hex, name: string): Promise<ManifestEntry>;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves the handle URI for a specific field
|
|
32
|
+
* and fetches it via the worker in one call.
|
|
33
|
+
*/
|
|
34
|
+
fetchField(owner: Address, schemaId: Hex, name: string, field: string, nullifier: string, walletClient: WalletClient): Promise<FetchResult>;
|
|
39
35
|
isRegistered(owner: Address, schemaId: Hex, name: string, identity: Identity): Promise<boolean>;
|
|
40
36
|
private deriveResourceId;
|
|
41
37
|
}
|