@fangorn-network/sdk 2026.3.29 → 2026.4.0-8.dev
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 +24 -31
- package/lib/cli/cli.js +18 -2
- package/lib/config.d.ts +241 -238
- package/lib/config.js +4 -2
- package/lib/fangorn.d.ts +0 -3
- package/lib/fangorn.js +12 -24
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/lib/modules/encryption/lit.d.ts +0 -1
- package/lib/modules/encryption/lit.js +1 -7
- package/lib/modules/gadgets/settledGadget.d.ts +8 -1
- package/lib/modules/gadgets/settledGadget.js +13 -1
- package/lib/providers/storage/index.d.ts +12 -12
- package/lib/providers/storage/index.js +47 -2
- package/lib/providers/storage/pinata.d.ts +11 -0
- package/lib/providers/storage/pinata.js +24 -0
- package/lib/registries/datasource-registry/index.d.ts +4 -3
- package/lib/registries/datasource-registry/index.js +17 -2
- package/lib/registries/settlement-registry/index.js +11 -13
- package/lib/registries/settlement-registry/types.d.ts +6 -3
- package/lib/roles/consumer/index.d.ts +2 -24
- package/lib/roles/consumer/index.js +5 -26
- package/lib/roles/publisher/index.d.ts +19 -17
- package/lib/roles/publisher/index.js +45 -38
- package/lib/roles/publisher/types.d.ts +5 -7
- package/lib/roles/schema/index.d.ts +9 -2
- package/lib/roles/schema/index.js +35 -2
- package/lib/test/testbed.d.ts +3 -2
- package/lib/test/testbed.js +18 -9
- package/lib/types/index.d.ts +10 -20
- package/package.json +1 -1
- package/lib/providers/storage/pinata/index.d.ts +0 -13
- package/lib/providers/storage/pinata/index.js +0 -41
- package/lib/providers/storage/storacha/index.d.ts +0 -13
- package/lib/providers/storage/storacha/index.js +0 -49
- package/lib/providers/storage/storacha/readOnly.d.ts +0 -8
- package/lib/providers/storage/storacha/readOnly.js +0 -16
package/lib/config.js
CHANGED
|
@@ -32,7 +32,8 @@ const FangornConfig = {
|
|
|
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
|
@@ -17,17 +17,14 @@ declare class Fangorn {
|
|
|
17
17
|
private constructor();
|
|
18
18
|
/**
|
|
19
19
|
* Schema owner: register agents, register schemas, validate definitions.
|
|
20
|
-
* Requires writable storage.
|
|
21
20
|
*/
|
|
22
21
|
get schema(): SchemaRole;
|
|
23
22
|
/**
|
|
24
23
|
* Publisher: encrypt, stage, and commit data under a schema.
|
|
25
|
-
* Requires writable storage.
|
|
26
24
|
*/
|
|
27
25
|
get publisher(): PublisherRole;
|
|
28
26
|
/**
|
|
29
27
|
* Consumer: purchase, claim, and decrypt data.
|
|
30
|
-
* Works with both readable and writable storage.
|
|
31
28
|
*/
|
|
32
29
|
get consumer(): ConsumerRole;
|
|
33
30
|
/**
|
package/lib/fangorn.js
CHANGED
|
@@ -1,40 +1,29 @@
|
|
|
1
1
|
import { FangornConfig } from "./config.js";
|
|
2
2
|
import { LitEncryptionService } from "./modules/encryption/lit.js";
|
|
3
3
|
import "./modules/encryption/index.js";
|
|
4
|
+
import { PinataStorage } from "./providers/storage/pinata.js";
|
|
5
|
+
import "./providers/storage/index.js";
|
|
4
6
|
import { SchemaRole } from "./roles/schema/index.js";
|
|
5
7
|
import { SettlementRegistry } from "./registries/settlement-registry/index.js";
|
|
6
8
|
import { PublisherRole } from "./roles/publisher/index.js";
|
|
7
9
|
import { ConsumerRole } from "./roles/consumer/index.js";
|
|
8
10
|
import { SchemaRegistry } from "./registries/schema-registry/index.js";
|
|
9
11
|
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";
|
|
14
12
|
import { createPublicClient, createWalletClient, http } from "viem";
|
|
15
13
|
import { privateKeyToAccount } from "viem/accounts";
|
|
16
14
|
|
|
17
15
|
//#region src/fangorn.ts
|
|
16
|
+
const DEFAULT_IPFS_GATEWAY = "https://ipfs.io";
|
|
18
17
|
function isEncryptionService(e) {
|
|
19
18
|
return typeof e.encrypt === "function";
|
|
20
19
|
}
|
|
21
20
|
function isPinataConfig(s) {
|
|
22
21
|
return "pinata" in s;
|
|
23
22
|
}
|
|
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
23
|
async function resolveStorage(storage) {
|
|
24
|
+
if (!storage) return void 0;
|
|
34
25
|
if (isPinataConfig(storage)) return new PinataStorage(storage.pinata.jwt, storage.pinata.gateway);
|
|
35
|
-
|
|
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.");
|
|
26
|
+
throw new Error(`Invalid storage config: must be { pinata: { jwt, gateway } }, but was ${JSON.stringify(storage)}`);
|
|
38
27
|
}
|
|
39
28
|
var Fangorn = class Fangorn {
|
|
40
29
|
ctx;
|
|
@@ -46,26 +35,23 @@ var Fangorn = class Fangorn {
|
|
|
46
35
|
}
|
|
47
36
|
/**
|
|
48
37
|
* Schema owner: register agents, register schemas, validate definitions.
|
|
49
|
-
* Requires writable storage.
|
|
50
38
|
*/
|
|
51
39
|
get schema() {
|
|
52
|
-
if (!
|
|
53
|
-
return this._schema ??= new SchemaRole(this.ctx.schemaRegistry, this.ctx.storage, this.ctx.walletClient);
|
|
40
|
+
if (!this.ctx.storage) throw new Error("fangorn.schema requires storage. Pass { pinata: { ... } } to Fangorn.create()");
|
|
41
|
+
return this._schema ??= new SchemaRole(this.ctx.schemaRegistry, this.ctx.storage, this.ctx.walletClient, this.ctx.ipfsGateway, this.ctx.schemaRoleConfig);
|
|
54
42
|
}
|
|
55
43
|
/**
|
|
56
44
|
* Publisher: encrypt, stage, and commit data under a schema.
|
|
57
|
-
* Requires writable storage.
|
|
58
45
|
*/
|
|
59
46
|
get publisher() {
|
|
60
|
-
if (!
|
|
61
|
-
return this._publisher ??= new PublisherRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry, this.ctx.storage, this.ctx.encryption, this.ctx.walletClient, this.ctx.config);
|
|
47
|
+
if (!this.ctx.storage) throw new Error("fangorn.publisher requires storage. Pass { pinata: { ... } } to Fangorn.create()");
|
|
48
|
+
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);
|
|
62
49
|
}
|
|
63
50
|
/**
|
|
64
51
|
* Consumer: purchase, claim, and decrypt data.
|
|
65
|
-
* Works with both readable and writable storage.
|
|
66
52
|
*/
|
|
67
53
|
get consumer() {
|
|
68
|
-
return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry, this.ctx.
|
|
54
|
+
return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry, this.ctx.encryption, this.ctx.domain, this.ctx.ipfsGateway);
|
|
69
55
|
}
|
|
70
56
|
/**
|
|
71
57
|
* Create a new Fangorn instance
|
|
@@ -83,12 +69,14 @@ var Fangorn = class Fangorn {
|
|
|
83
69
|
const storage = await resolveStorage(options.storage);
|
|
84
70
|
const encryption = isEncryptionService(options.encryption) ? options.encryption : await LitEncryptionService.init(resolvedConfig.chainName);
|
|
85
71
|
const domain = options.domain ?? new URL(resolvedConfig.rpcUrl).hostname;
|
|
72
|
+
const ipfsGateway = options.ipfsGateway ?? DEFAULT_IPFS_GATEWAY;
|
|
86
73
|
const publicClient = createPublicClient({ transport: http(resolvedConfig.rpcUrl) });
|
|
87
74
|
return new Fangorn({
|
|
88
75
|
walletClient,
|
|
89
76
|
storage,
|
|
90
77
|
encryption,
|
|
91
78
|
domain,
|
|
79
|
+
ipfsGateway,
|
|
92
80
|
schemaRegistry: new SchemaRegistry(resolvedConfig.schemaRegistryContractAddress, publicClient, walletClient),
|
|
93
81
|
dataSourceRegistry: new DataSourceRegistry(resolvedConfig.dataSourceRegistryContractAddress, publicClient, walletClient),
|
|
94
82
|
settlementRegistry: new SettlementRegistry(resolvedConfig.settlementRegistryContractAddress, publicClient, walletClient),
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AppConfig, FangornConfig, SupportedNetworks } from "./config.js";
|
|
2
|
-
import { PinataStorage } from "./providers/storage/pinata
|
|
3
|
-
import {
|
|
2
|
+
import { PinataStorage } from "./providers/storage/pinata.js";
|
|
3
|
+
import { PinningService, deserialize, retrieveByCid, serialize } from "./providers/storage/index.js";
|
|
4
4
|
import { Gadget } from "./modules/gadgets/types.js";
|
|
5
5
|
import "./modules/gadgets/index.js";
|
|
6
6
|
import { LitEncryptionService } from "./modules/encryption/lit.js";
|
|
@@ -9,4 +9,4 @@ import { Fangorn } from "./fangorn.js";
|
|
|
9
9
|
import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
|
|
10
10
|
import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
|
|
11
11
|
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,
|
|
12
|
+
export { AppConfig, AuthContext, AuthSig, DS_REGISTRY_ABI, EncryptionService, Fangorn, FangornConfig, Gadget, LitEncryptionService, PinataStorage, PinningService, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
|
package/lib/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FangornConfig, SupportedNetworks } from "./config.js";
|
|
2
2
|
import { LitEncryptionService } from "./modules/encryption/lit.js";
|
|
3
3
|
import "./modules/encryption/index.js";
|
|
4
|
+
import { PinataStorage } from "./providers/storage/pinata.js";
|
|
5
|
+
import { deserialize, retrieveByCid, serialize } from "./providers/storage/index.js";
|
|
4
6
|
import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
|
|
5
7
|
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";
|
|
8
8
|
import { Fangorn } from "./fangorn.js";
|
|
9
9
|
import "./registries/index.js";
|
|
10
10
|
import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
|
|
11
11
|
|
|
12
|
-
export { DS_REGISTRY_ABI, Fangorn, FangornConfig, LitEncryptionService, PinataStorage, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array };
|
|
12
|
+
export { DS_REGISTRY_ABI, Fangorn, FangornConfig, LitEncryptionService, PinataStorage, SETTLEMENT_REGISTRY_ABI, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
|
|
@@ -15,7 +15,6 @@ declare class LitEncryptionService implements EncryptionService {
|
|
|
15
15
|
decrypt(payload: EncryptedPayload, authContext: AuthContextWrapper): Promise<DecryptedPayload>;
|
|
16
16
|
createAuthContext(walletClient: WalletClient, domain: string): Promise<AuthContextWrapper>;
|
|
17
17
|
private createAuthSig;
|
|
18
|
-
private parseKeyResponse;
|
|
19
18
|
}
|
|
20
19
|
//#endregion
|
|
21
20
|
export { LitEncryptionService };
|
|
@@ -47,7 +47,7 @@ var LitEncryptionService = class LitEncryptionService {
|
|
|
47
47
|
}
|
|
48
48
|
async createAuthContext(walletClient, domain) {
|
|
49
49
|
const isNode = typeof window === "undefined";
|
|
50
|
-
const account =
|
|
50
|
+
const account = walletClient.account;
|
|
51
51
|
if (!account) throw new Error("No account found in wallet client");
|
|
52
52
|
if (!walletClient.chain) throw new Error("No chain found in wallet client");
|
|
53
53
|
const sessionContext = await (isNode ? createAuthManager({ storage: storagePlugins.localStorageNode({
|
|
@@ -115,12 +115,6 @@ var LitEncryptionService = class LitEncryptionService {
|
|
|
115
115
|
address: account.address
|
|
116
116
|
};
|
|
117
117
|
}
|
|
118
|
-
parseKeyResponse(response) {
|
|
119
|
-
return Uint8Array.from(response.replace(/^[^\d]+/, "").split(","), (entry) => {
|
|
120
|
-
const val = parseInt(entry.trim(), 10);
|
|
121
|
-
return isNaN(val) ? 0 : val;
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
118
|
};
|
|
125
119
|
|
|
126
120
|
//#endregion
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AppConfig } from "../../config.js";
|
|
1
2
|
import { Gadget, GadgetDescriptor } from "./types.js";
|
|
2
3
|
import { Address, Hex } from "viem";
|
|
3
4
|
import { UnifiedAccessControlCondition } from "@lit-protocol/access-control-conditions";
|
|
@@ -11,6 +12,12 @@ interface SettledGadgetParams {
|
|
|
11
12
|
/** "arbitrumSepolia" | "baseSepolia" */
|
|
12
13
|
chainName: string;
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Build a new default gadget from the given config
|
|
17
|
+
* @param config : AppConfig
|
|
18
|
+
* @returns the configured gadget
|
|
19
|
+
*/
|
|
20
|
+
declare function makeSettledGadgetFactory(config: AppConfig): (resourceId: Hex) => SettledGadget;
|
|
14
21
|
declare class SettledGadget implements Gadget {
|
|
15
22
|
private readonly params;
|
|
16
23
|
readonly type = "settled";
|
|
@@ -21,4 +28,4 @@ declare class SettledGadget implements Gadget {
|
|
|
21
28
|
toDescriptor(): GadgetDescriptor;
|
|
22
29
|
}
|
|
23
30
|
//#endregion
|
|
24
|
-
export { SettledGadget, SettledGadgetParams };
|
|
31
|
+
export { SettledGadget, SettledGadgetParams, makeSettledGadgetFactory };
|
|
@@ -2,6 +2,18 @@ import { zeroAddress } from "viem";
|
|
|
2
2
|
import { createAccBuilder } from "@lit-protocol/access-control-conditions";
|
|
3
3
|
|
|
4
4
|
//#region src/modules/gadgets/settledGadget.ts
|
|
5
|
+
/**
|
|
6
|
+
* Build a new default gadget from the given config
|
|
7
|
+
* @param config : AppConfig
|
|
8
|
+
* @returns the configured gadget
|
|
9
|
+
*/
|
|
10
|
+
function makeSettledGadgetFactory(config) {
|
|
11
|
+
return (resourceId) => new SettledGadget({
|
|
12
|
+
resourceId,
|
|
13
|
+
settlementRegistryAddress: config.settlementRegistryContractAddress,
|
|
14
|
+
chainName: config.chainName
|
|
15
|
+
});
|
|
16
|
+
}
|
|
5
17
|
var SettledGadget = class {
|
|
6
18
|
type = "settled";
|
|
7
19
|
constructor(params) {
|
|
@@ -60,4 +72,4 @@ var SettledGadget = class {
|
|
|
60
72
|
};
|
|
61
73
|
|
|
62
74
|
//#endregion
|
|
63
|
-
export { SettledGadget };
|
|
75
|
+
export { SettledGadget, makeSettledGadgetFactory };
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { PinataStorage } from "./pinata
|
|
1
|
+
import { PinataStorage } from "./pinata.js";
|
|
2
2
|
|
|
3
3
|
//#region src/providers/storage/index.d.ts
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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
8
|
*/
|
|
9
|
-
interface
|
|
10
|
-
|
|
9
|
+
interface PinningService {
|
|
10
|
+
store(data: unknown, metadata?: Record<string, unknown>): Promise<string>;
|
|
11
|
+
delete(cid: string): Promise<void>;
|
|
11
12
|
}
|
|
13
|
+
declare function serialize(data: unknown): string;
|
|
14
|
+
declare function deserialize<T>(text: string): T;
|
|
12
15
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
16
|
+
* Fetch any content by CID from a public IPFS gateway.
|
|
17
|
+
* No auth required — all Fangorn content is public; privacy is cryptographic.
|
|
15
18
|
*/
|
|
16
|
-
|
|
17
|
-
store(data: T, metadata?: Record<string, unknown>): Promise<string>;
|
|
18
|
-
delete(cid: string): Promise<void>;
|
|
19
|
-
}
|
|
19
|
+
declare function retrieveByCid<T>(cid: string, gateway?: string): Promise<T>;
|
|
20
20
|
//#endregion
|
|
21
|
-
export { PinataStorage,
|
|
21
|
+
export { PinataStorage, PinningService, deserialize, retrieveByCid, serialize };
|
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
import { PinataStorage } from "./pinata
|
|
1
|
+
import { PinataStorage } from "./pinata.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
//#region src/providers/storage/index.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") {
|
|
32
|
+
const url = `${gateway.replace(/\/$/, "")}/ipfs/${cid}`;
|
|
33
|
+
const controller = new AbortController();
|
|
34
|
+
const timeout = setTimeout(() => controller.abort(), 1e4);
|
|
35
|
+
try {
|
|
36
|
+
const res = await fetch(url, { signal: controller.signal });
|
|
37
|
+
if (!res.ok) throw new Error(`Failed to retrieve ${cid}: ${res.statusText}`);
|
|
38
|
+
return deserialize(await res.text());
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err.name === "AbortError") throw new Error(`Timed out retrieving ${cid} from ${url}`);
|
|
41
|
+
throw err;
|
|
42
|
+
} finally {
|
|
43
|
+
clearTimeout(timeout);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { PinataStorage, deserialize, retrieveByCid, serialize };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PinningService } from "./index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/storage/pinata.d.ts
|
|
4
|
+
declare class PinataStorage implements PinningService {
|
|
5
|
+
private pinata;
|
|
6
|
+
constructor(pinataJwt: string, pinataGateway: string);
|
|
7
|
+
store(data: unknown, metadata?: Record<string, unknown>): Promise<string>;
|
|
8
|
+
delete(cid: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { PinataStorage };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { serialize } from "./index.js";
|
|
2
|
+
import { PinataSDK } from "pinata";
|
|
3
|
+
|
|
4
|
+
//#region src/providers/storage/pinata.ts
|
|
5
|
+
var PinataStorage = class {
|
|
6
|
+
pinata;
|
|
7
|
+
constructor(pinataJwt, pinataGateway) {
|
|
8
|
+
this.pinata = new PinataSDK({
|
|
9
|
+
pinataJwt,
|
|
10
|
+
pinataGateway
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async store(data, metadata) {
|
|
14
|
+
const content = serialize(data);
|
|
15
|
+
const file = new File([content], metadata?.name ?? "file", { type: "text/plain" });
|
|
16
|
+
return (await this.pinata.upload.public.file(file, { metadata })).cid;
|
|
17
|
+
}
|
|
18
|
+
async delete(cid) {
|
|
19
|
+
await this.pinata.files.public.delete([cid]);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { PinataStorage };
|
|
@@ -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/datasource-registry/index.d.ts
|
|
@@ -15,10 +15,11 @@ declare class DataSourceRegistry {
|
|
|
15
15
|
private getWriteConfig;
|
|
16
16
|
getContractAddress(): `0x${string}`;
|
|
17
17
|
initialize(schemaRegistryAddress: Address): Promise<Hash>;
|
|
18
|
-
publishManifest(manifestCid: string, schemaId: Hex): Promise<Hash>;
|
|
18
|
+
publishManifest(manifestCid: string, schemaId: Hex, gas?: bigint): Promise<Hash>;
|
|
19
19
|
getManifest(owner: Address, schemaId: Hex): Promise<DataSource>;
|
|
20
20
|
getVersion(owner: Address, schemaId: Hex): Promise<bigint>;
|
|
21
|
-
waitForTransaction(hash: Hash): Promise<
|
|
21
|
+
waitForTransaction(hash: Hash): Promise<viem171.TransactionReceipt>;
|
|
22
|
+
private estimateGas;
|
|
22
23
|
}
|
|
23
24
|
//#endregion
|
|
24
25
|
export { DataSource, DataSourceRegistry };
|
|
@@ -34,15 +34,26 @@ var DataSourceRegistry = class {
|
|
|
34
34
|
await this.waitForTransaction(hash);
|
|
35
35
|
return hash;
|
|
36
36
|
}
|
|
37
|
-
async publishManifest(manifestCid, schemaId) {
|
|
37
|
+
async publishManifest(manifestCid, schemaId, gas) {
|
|
38
38
|
const { chain, account } = this.getWriteConfig();
|
|
39
|
+
const gasLimit = await this.estimateGas(() => this.publicClient.estimateContractGas({
|
|
40
|
+
address: this.contractAddress,
|
|
41
|
+
abi: DS_REGISTRY_ABI,
|
|
42
|
+
functionName: "publishManifest",
|
|
43
|
+
args: [manifestCid, schemaId],
|
|
44
|
+
account
|
|
45
|
+
}), gas);
|
|
46
|
+
const fees = await this.publicClient.estimateFeesPerGas();
|
|
39
47
|
const hash = await this.walletClient.writeContract({
|
|
40
48
|
address: this.contractAddress,
|
|
41
49
|
abi: DS_REGISTRY_ABI,
|
|
42
50
|
functionName: "publishManifest",
|
|
43
51
|
args: [manifestCid, schemaId],
|
|
44
52
|
chain,
|
|
45
|
-
account
|
|
53
|
+
account,
|
|
54
|
+
gas: gasLimit,
|
|
55
|
+
maxFeePerGas: fees.maxFeePerGas * 3n,
|
|
56
|
+
maxPriorityFeePerGas: fees.maxPriorityFeePerGas
|
|
46
57
|
});
|
|
47
58
|
await this.waitForTransaction(hash);
|
|
48
59
|
return hash;
|
|
@@ -76,6 +87,10 @@ var DataSourceRegistry = class {
|
|
|
76
87
|
async waitForTransaction(hash) {
|
|
77
88
|
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
78
89
|
}
|
|
90
|
+
async estimateGas(fn, override) {
|
|
91
|
+
if (override !== void 0) return override;
|
|
92
|
+
return await fn() * 130n / 100n;
|
|
93
|
+
}
|
|
79
94
|
};
|
|
80
95
|
|
|
81
96
|
//#endregion
|
|
@@ -50,20 +50,18 @@ var SettlementRegistry = class {
|
|
|
50
50
|
* @returns The payload containing the signed call data
|
|
51
51
|
*/
|
|
52
52
|
async prepareTransferWithAuth(params) {
|
|
53
|
-
const {
|
|
54
|
-
const
|
|
53
|
+
const { paymentRecipient, amount, usdcAddress, usdcDomainName, usdcDomainVersion } = params;
|
|
54
|
+
const walletClient = params.walletClient ?? this.walletClient;
|
|
55
|
+
const chain = walletClient.chain;
|
|
55
56
|
if (!chain) throw new Error("Wallet client must have a chain configured.");
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
account: burner,
|
|
59
|
-
chain,
|
|
60
|
-
transport: http(chain.rpcUrls.default.http[0])
|
|
61
|
-
});
|
|
57
|
+
const account = walletClient.account;
|
|
58
|
+
if (!account) throw new Error("Wallet clietn must have an account configured.");
|
|
62
59
|
const validAfter = 0n;
|
|
63
60
|
const validBefore = BigInt(Math.floor(Date.now() / 1e3) + 3600);
|
|
64
61
|
const nonceBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
65
62
|
const nonce = `0x${Array.from(nonceBytes).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
66
|
-
const { v, r, s } = parseSignature(await
|
|
63
|
+
const { v, r, s } = parseSignature(await walletClient.signTypedData({
|
|
64
|
+
account,
|
|
67
65
|
domain: {
|
|
68
66
|
name: usdcDomainName,
|
|
69
67
|
version: usdcDomainVersion,
|
|
@@ -98,7 +96,7 @@ var SettlementRegistry = class {
|
|
|
98
96
|
] },
|
|
99
97
|
primaryType: "TransferWithAuthorization",
|
|
100
98
|
message: {
|
|
101
|
-
from:
|
|
99
|
+
from: account.address,
|
|
102
100
|
to: paymentRecipient,
|
|
103
101
|
value: amount,
|
|
104
102
|
validAfter,
|
|
@@ -107,7 +105,7 @@ var SettlementRegistry = class {
|
|
|
107
105
|
}
|
|
108
106
|
}));
|
|
109
107
|
return {
|
|
110
|
-
|
|
108
|
+
sender: account.address,
|
|
111
109
|
paymentRecipient,
|
|
112
110
|
amount,
|
|
113
111
|
validAfter,
|
|
@@ -125,7 +123,7 @@ var SettlementRegistry = class {
|
|
|
125
123
|
*/
|
|
126
124
|
async register(params) {
|
|
127
125
|
const { resourceId, identityCommitment, relayerPrivateKey, preparedRegister } = params;
|
|
128
|
-
const {
|
|
126
|
+
const { sender, paymentRecipient, amount, validAfter, validBefore, nonce, v, r, s } = preparedRegister;
|
|
129
127
|
const chain = this.walletClient.chain;
|
|
130
128
|
if (!chain) throw new Error("Wallet client must have a chain configured.");
|
|
131
129
|
const hash = await createWalletClient({
|
|
@@ -139,7 +137,7 @@ var SettlementRegistry = class {
|
|
|
139
137
|
args: [
|
|
140
138
|
resourceId,
|
|
141
139
|
identityCommitment,
|
|
142
|
-
|
|
140
|
+
sender,
|
|
143
141
|
paymentRecipient,
|
|
144
142
|
amount,
|
|
145
143
|
validAfter,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Address, Hex } from "viem";
|
|
1
|
+
import { Address, Hex, WalletClient } from "viem";
|
|
2
2
|
import { Identity } from "@semaphore-protocol/identity";
|
|
3
3
|
|
|
4
4
|
//#region src/registries/settlement-registry/types.d.ts
|
|
5
5
|
interface TransferWithAuthParams {
|
|
6
|
-
|
|
6
|
+
walletClient?: WalletClient;
|
|
7
7
|
paymentRecipient: Address;
|
|
8
8
|
amount: bigint;
|
|
9
9
|
usdcAddress: Address;
|
|
@@ -11,7 +11,7 @@ interface TransferWithAuthParams {
|
|
|
11
11
|
usdcDomainVersion: string;
|
|
12
12
|
}
|
|
13
13
|
interface TransferWithAuthPayload {
|
|
14
|
-
|
|
14
|
+
sender: Address;
|
|
15
15
|
paymentRecipient: Address;
|
|
16
16
|
amount: bigint;
|
|
17
17
|
validAfter: bigint;
|
|
@@ -21,6 +21,9 @@ interface TransferWithAuthPayload {
|
|
|
21
21
|
r: Hex;
|
|
22
22
|
s: Hex;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The params for registering with a semaphore group
|
|
26
|
+
*/
|
|
24
27
|
interface RegisterParams {
|
|
25
28
|
resourceId: Hex;
|
|
26
29
|
identityCommitment: bigint;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ReadableStorage } from "../../providers/storage/index.js";
|
|
2
1
|
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
3
2
|
import { PrepareSettleParams, PrepareSettleResult, TransferWithAuthParams, TransferWithAuthPayload } from "../../registries/settlement-registry/types.js";
|
|
4
3
|
import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
|
|
@@ -16,34 +15,13 @@ import { Identity } from "@semaphore-protocol/identity";
|
|
|
16
15
|
declare class ConsumerRole {
|
|
17
16
|
private readonly dataSourceRegistry;
|
|
18
17
|
private readonly settlementRegistry;
|
|
19
|
-
private readonly storage;
|
|
20
18
|
private readonly encryptionService;
|
|
21
19
|
private readonly domain;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
20
|
+
private readonly ipfsGateway;
|
|
21
|
+
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry, encryptionService: EncryptionService, domain: string, ipfsGateway: string);
|
|
28
22
|
prepareRegister(params: TransferWithAuthParams): Promise<TransferWithAuthPayload>;
|
|
29
|
-
/**
|
|
30
|
-
* Builds the zkp to prove you're in a semaphore group
|
|
31
|
-
* @param params
|
|
32
|
-
* @returns
|
|
33
|
-
*/
|
|
34
23
|
prepareSettle(params: PrepareSettleParams): Promise<PrepareSettleResult>;
|
|
35
|
-
/**
|
|
36
|
-
* Purchase the resource and register in the semaphore group (on success)
|
|
37
|
-
* @param params
|
|
38
|
-
* @returns
|
|
39
|
-
*/
|
|
40
24
|
register(params: PurchaseParams): Promise<PurchaseResult>;
|
|
41
|
-
/**
|
|
42
|
-
* Claim access to a resource (prove settlement)
|
|
43
|
-
* Must register first.
|
|
44
|
-
* @param params
|
|
45
|
-
* @returns
|
|
46
|
-
*/
|
|
47
25
|
claim(params: ClaimParams): Promise<ClaimResult>;
|
|
48
26
|
/**
|
|
49
27
|
* Decrypt a specific encrypted field within a record.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { retrieveByCid } from "../../providers/storage/index.js";
|
|
1
2
|
import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
|
|
2
3
|
|
|
3
4
|
//#region src/roles/consumer/index.ts
|
|
@@ -5,34 +6,19 @@ import { SettlementRegistry } from "../../registries/settlement-registry/index.j
|
|
|
5
6
|
* The consumer namespace encapsulates all functionality relative to accessing and consuming data.
|
|
6
7
|
*/
|
|
7
8
|
var ConsumerRole = class {
|
|
8
|
-
constructor(dataSourceRegistry, settlementRegistry,
|
|
9
|
+
constructor(dataSourceRegistry, settlementRegistry, encryptionService, domain, ipfsGateway) {
|
|
9
10
|
this.dataSourceRegistry = dataSourceRegistry;
|
|
10
11
|
this.settlementRegistry = settlementRegistry;
|
|
11
|
-
this.storage = storage;
|
|
12
12
|
this.encryptionService = encryptionService;
|
|
13
13
|
this.domain = domain;
|
|
14
|
+
this.ipfsGateway = ipfsGateway;
|
|
14
15
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
17
|
-
* @param params
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
16
|
async prepareRegister(params) {
|
|
21
17
|
return this.settlementRegistry.prepareTransferWithAuth(params);
|
|
22
18
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds the zkp to prove you're in a semaphore group
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
19
|
async prepareSettle(params) {
|
|
29
20
|
return this.settlementRegistry.prepareSettle(params);
|
|
30
21
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Purchase the resource and register in the semaphore group (on success)
|
|
33
|
-
* @param params
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
22
|
async register(params) {
|
|
37
23
|
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.tag);
|
|
38
24
|
return {
|
|
@@ -45,12 +31,6 @@ var ConsumerRole = class {
|
|
|
45
31
|
resourceId
|
|
46
32
|
};
|
|
47
33
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Claim access to a resource (prove settlement)
|
|
50
|
-
* Must register first.
|
|
51
|
-
* @param params
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
34
|
async claim(params) {
|
|
55
35
|
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.tag);
|
|
56
36
|
const { hash, nullifier } = await this.settlementRegistry.settle({
|
|
@@ -80,8 +60,7 @@ var ConsumerRole = class {
|
|
|
80
60
|
}
|
|
81
61
|
const fieldValue = (await this.getEntry(params.owner, params.schemaId, params.tag)).fields[params.field];
|
|
82
62
|
if (!fieldValue || typeof fieldValue !== "object") throw new Error(`Field "${params.field}" is missing or invalid`);
|
|
83
|
-
const
|
|
84
|
-
const encrypted = await this.storage.retrieve(encryptedField.handle.cid);
|
|
63
|
+
const encrypted = await retrieveByCid(fieldValue.handle.cid, this.ipfsGateway);
|
|
85
64
|
const authContext = params.authContext ?? await this.encryptionService.createAuthContext(params.walletClient, this.domain, params.nullifierHash);
|
|
86
65
|
return (await this.encryptionService.decrypt(encrypted, authContext)).data;
|
|
87
66
|
}
|
|
@@ -89,7 +68,7 @@ var ConsumerRole = class {
|
|
|
89
68
|
try {
|
|
90
69
|
const ds = await this.dataSourceRegistry.getManifest(owner, schemaId);
|
|
91
70
|
if (!ds.manifestCid || ds.manifestCid === "") return void 0;
|
|
92
|
-
return
|
|
71
|
+
return retrieveByCid(ds.manifestCid, this.ipfsGateway);
|
|
93
72
|
} catch {
|
|
94
73
|
return;
|
|
95
74
|
}
|