@fangorn-network/sdk 2026.5.11 → 2026.6.16
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 +87 -37
- package/lib/ crypto/constants.d.ts +4 -0
- package/lib/ crypto/constants.js +5 -0
- package/lib/ crypto/merkle.d.ts +10 -0
- package/lib/ crypto/merkle.js +34 -0
- package/lib/ crypto/utils.d.ts +1 -0
- package/lib/ crypto/utils.js +1 -0
- package/lib/cli/cli.js +61 -156
- package/lib/cli/index.d.ts +171 -170
- package/lib/config.d.ts +258 -257
- package/lib/config.js +4 -4
- package/lib/fangorn.js +2 -2
- package/lib/index.d.ts +1 -3
- package/lib/index.js +1 -3
- package/lib/modules/encryption/rand.d.ts +1 -1
- package/lib/providers/storage/pinata.d.ts +5 -1
- package/lib/providers/storage/pinata.js +24 -3
- package/lib/providers/storage/types.d.ts +8 -0
- package/lib/providers/storage/utils.js +2 -1
- package/lib/registries/datasource-registry/abi.d.ts +34 -4
- package/lib/registries/datasource-registry/abi.js +40 -5
- package/lib/registries/datasource-registry/index.d.ts +22 -13
- package/lib/registries/datasource-registry/index.js +76 -76
- package/lib/registries/index.d.ts +1 -1
- package/lib/registries/schema-registry/index.d.ts +3 -3
- package/lib/registries/schema-registry/index.js +1 -1
- package/lib/registries/settlement-registry/index.d.ts +2 -2
- package/lib/roles/consumer/index.d.ts +3 -3
- package/lib/roles/consumer/index.js +19 -12
- package/lib/roles/publisher/builders/bundle.d.ts +32 -0
- package/lib/roles/publisher/builders/bundle.js +137 -0
- package/lib/roles/publisher/builders/record-set.d.ts +17 -0
- package/lib/roles/publisher/builders/record-set.js +55 -0
- package/lib/roles/publisher/builders/types.d.ts +38 -0
- package/lib/roles/publisher/builders/types.js +1 -0
- package/lib/roles/publisher/builders/utils.d.ts +9 -0
- package/lib/roles/publisher/builders/utils.js +29 -0
- package/lib/roles/publisher/encryption.d.ts +18 -0
- package/lib/roles/publisher/encryption.js +38 -0
- package/lib/roles/publisher/index.d.ts +54 -36
- package/lib/roles/publisher/index.js +137 -182
- package/lib/roles/publisher/types.d.ts +61 -46
- package/lib/roles/schema/index.d.ts +30 -4
- package/lib/roles/schema/index.js +81 -47
- package/lib/roles/schema/types.d.ts +59 -37
- package/lib/roles/schema/validate.d.ts +9 -0
- package/lib/roles/schema/validate.js +128 -0
- package/lib/test/cleanup-embeddings-testdata.d.ts +1 -0
- package/lib/test/cleanup-embeddings-testdata.js +104 -0
- package/lib/test/publish_mb_bundle.d.ts +1 -0
- package/lib/test/publish_mb_bundle.js +296 -0
- package/lib/test/setup-embeddings-testdata.d.ts +13 -0
- package/lib/test/setup-embeddings-testdata.js +209 -0
- package/lib/test/testbed.d.ts +14 -8
- package/lib/test/testbed.js +28 -24
- package/package.json +16 -2
package/lib/config.js
CHANGED
|
@@ -26,12 +26,12 @@ function getNetwork(name) {
|
|
|
26
26
|
}
|
|
27
27
|
const FangornConfig = {
|
|
28
28
|
ArbitrumSepolia: {
|
|
29
|
-
dataSourceRegistryContractAddress: "
|
|
30
|
-
schemaRegistryContractAddress: "
|
|
31
|
-
settlementRegistryContractAddress: "
|
|
29
|
+
dataSourceRegistryContractAddress: "0x207ab1866704b2adc34e8ec1069fb8febafff2fd",
|
|
30
|
+
schemaRegistryContractAddress: "0xecafc21ca3ec41c020287fb8c2126b1a9af9d220",
|
|
31
|
+
settlementRegistryContractAddress: "0x93a5e93e76a3c150d35d4cd40029e4f45f3e650f",
|
|
32
32
|
chainName: "arbitrumSepolia",
|
|
33
33
|
chain: arbitrumSepolia,
|
|
34
|
-
rpcUrl: "https://
|
|
34
|
+
rpcUrl: "https://sepolia-rollup.arbitrum.io/rpc",
|
|
35
35
|
caip2: 421614,
|
|
36
36
|
ipfsGateway: "https://ipfs.io"
|
|
37
37
|
},
|
package/lib/fangorn.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FangornConfig } from "./config.js";
|
|
2
2
|
import { SchemaRole } from "./roles/schema/index.js";
|
|
3
|
-
import { PublisherRole } from "./roles/publisher/index.js";
|
|
4
3
|
import { DataSourceRegistry } from "./registries/datasource-registry/index.js";
|
|
4
|
+
import { PublisherRole } from "./roles/publisher/index.js";
|
|
5
5
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
6
6
|
import { ConsumerRole } from "./roles/consumer/index.js";
|
|
7
7
|
import { SchemaRegistry } from "./registries/schema-registry/index.js";
|
|
@@ -32,7 +32,7 @@ var Fangorn = class Fangorn {
|
|
|
32
32
|
}
|
|
33
33
|
get publisher() {
|
|
34
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
|
|
35
|
+
return this._publisher ??= new PublisherRole(this.ctx.dataSourceRegistry, this.ctx.schemaRegistry, this.ctx.metadataStorage, this.ctx.walletClient);
|
|
36
36
|
}
|
|
37
37
|
get consumer() {
|
|
38
38
|
return this._consumer ??= new ConsumerRole(this.ctx.dataSourceRegistry, this.ctx.settlementRegistry);
|
package/lib/index.d.ts
CHANGED
|
@@ -2,7 +2,5 @@ import { AppConfig, FangornConfig, SupportedNetworks } from "./config.js";
|
|
|
2
2
|
import { Fangorn } from "./fangorn.js";
|
|
3
3
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
4
4
|
import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
|
|
5
|
-
import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
|
|
6
|
-
import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
|
|
7
5
|
import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
|
|
8
|
-
export { AppConfig,
|
|
6
|
+
export { AppConfig, Fangorn, FangornConfig, PinataBackend, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { FangornConfig, SupportedNetworks } from "./config.js";
|
|
2
|
-
import { DS_REGISTRY_ABI } from "./registries/datasource-registry/abi.js";
|
|
3
2
|
import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
|
|
4
3
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
5
|
-
import { SETTLEMENT_REGISTRY_ABI } from "./registries/settlement-registry/abi.js";
|
|
6
4
|
import { Fangorn } from "./fangorn.js";
|
|
7
5
|
import { addressToBytes32Array, computeSchemaId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array } from "./utils/index.js";
|
|
8
6
|
|
|
9
|
-
export {
|
|
7
|
+
export { Fangorn, FangornConfig, PinataBackend, SupportedNetworks, addressToBytes32Array, computeSchemaId, deserialize, fieldToHex, hexToBytes32Array, hexToField, retrieveByCid, serialize, stringToBytes32Array };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//#region src/modules/encryption/rand.d.ts
|
|
2
2
|
declare function getSubtleCrypto(): SubtleCrypto;
|
|
3
|
-
declare function getRandomValues(array: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
3
|
+
declare function getRandomValues(array: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
4
4
|
//#endregion
|
|
5
5
|
export { getRandomValues, getSubtleCrypto };
|
|
@@ -6,8 +6,12 @@ declare class PinataBackend implements MetadataStorage {
|
|
|
6
6
|
private gateway;
|
|
7
7
|
constructor(pinataJwt: string, pinataGateway: string);
|
|
8
8
|
put(data: unknown, meta?: StorageMeta): Promise<string>;
|
|
9
|
+
putMany(items: {
|
|
10
|
+
data: unknown;
|
|
11
|
+
name: string;
|
|
12
|
+
}[]): Promise<Record<string, string>>;
|
|
9
13
|
get<T>(uri: string): Promise<T>;
|
|
10
|
-
static getStatic<T>(uri: string): Promise<T>;
|
|
14
|
+
static getStatic<T>(uri: string, gateway?: string): Promise<T>;
|
|
11
15
|
delete(uri: string): Promise<void>;
|
|
12
16
|
}
|
|
13
17
|
//#endregion
|
|
@@ -17,11 +17,32 @@ var PinataBackend = class {
|
|
|
17
17
|
const file = new File([content], meta?.name ?? "file", { type: "text/plain" });
|
|
18
18
|
return (await this.pinata.upload.public.file(file, { metadata: meta })).cid;
|
|
19
19
|
}
|
|
20
|
+
async putMany(items) {
|
|
21
|
+
if (items.length === 0) return {};
|
|
22
|
+
const uriMap = {};
|
|
23
|
+
const PINATA_MAX_FILES = 500;
|
|
24
|
+
for (let i = 0; i < items.length; i += PINATA_MAX_FILES) {
|
|
25
|
+
const subBatch = items.slice(i, i + PINATA_MAX_FILES);
|
|
26
|
+
const filesToUpload = subBatch.map(({ data, name }) => {
|
|
27
|
+
const content = typeof data === "string" ? data : JSON.stringify(data);
|
|
28
|
+
return new File([content], `manifests/${name}.json`, { type: "application/json" });
|
|
29
|
+
});
|
|
30
|
+
try {
|
|
31
|
+
const batchName = `batch-${Date.now().toString()}-${i.toString()}`;
|
|
32
|
+
const folderCid = (await this.pinata.upload.public.fileArray(filesToUpload).name(batchName)).cid;
|
|
33
|
+
for (const { name } of subBatch) uriMap[name] = `ipfs://${folderCid}/manifests/${name}.json`;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(`❌ Pinata sub-batch upload failure at offset ${i.toString()}:`, error);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return uriMap;
|
|
40
|
+
}
|
|
20
41
|
async get(uri) {
|
|
21
|
-
return retrieveByCid(uri);
|
|
42
|
+
return retrieveByCid(uri, this.gateway);
|
|
22
43
|
}
|
|
23
|
-
static async getStatic(uri) {
|
|
24
|
-
return retrieveByCid(uri);
|
|
44
|
+
static async getStatic(uri, gateway) {
|
|
45
|
+
return retrieveByCid(uri, gateway);
|
|
25
46
|
}
|
|
26
47
|
async delete(uri) {
|
|
27
48
|
await this.pinata.files.public.delete([uri]);
|
|
@@ -14,6 +14,14 @@ interface MetadataStorage {
|
|
|
14
14
|
* Callers never construct or parse URIs — treat them as handles.
|
|
15
15
|
*/
|
|
16
16
|
put(data: unknown, meta?: StorageMeta): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Pack multiple items into a single CAR and upload as one pin.
|
|
19
|
+
* Returns a map of { name -> CID } for each item.
|
|
20
|
+
*/
|
|
21
|
+
putMany(items: {
|
|
22
|
+
data: unknown;
|
|
23
|
+
name: string;
|
|
24
|
+
}[]): Promise<Record<string, string>>;
|
|
17
25
|
/**
|
|
18
26
|
* Retrieve content by URI previously returned from put().
|
|
19
27
|
*/
|
|
@@ -29,7 +29,8 @@ function deserialize(text) {
|
|
|
29
29
|
* No auth required — all Fangorn content is public; privacy is cryptographic.
|
|
30
30
|
*/
|
|
31
31
|
async function retrieveByCid(cid, gateway = "https://ipfs.io", timeoutSecond = 16e3) {
|
|
32
|
-
const
|
|
32
|
+
const base = (gateway || "https://ipfs.io").replace(/\/$/, "");
|
|
33
|
+
const url = `${/^https?:\/\//.test(base) ? base : `https://${base}`}/ipfs/${cid.replace(/^ipfs:\/\//, "")}`;
|
|
33
34
|
const controller = new AbortController();
|
|
34
35
|
const timeout = setTimeout(() => {
|
|
35
36
|
controller.abort();
|
|
@@ -38,6 +38,10 @@ declare const DS_REGISTRY_ABI: readonly [{
|
|
|
38
38
|
readonly internalType: "string";
|
|
39
39
|
readonly name: "";
|
|
40
40
|
readonly type: "string";
|
|
41
|
+
}, {
|
|
42
|
+
readonly internalType: "bytes32";
|
|
43
|
+
readonly name: "";
|
|
44
|
+
readonly type: "bytes32";
|
|
41
45
|
}];
|
|
42
46
|
readonly stateMutability: "view";
|
|
43
47
|
readonly type: "function";
|
|
@@ -60,6 +64,32 @@ declare const DS_REGISTRY_ABI: readonly [{
|
|
|
60
64
|
readonly internalType: "string";
|
|
61
65
|
readonly name: "";
|
|
62
66
|
readonly type: "string";
|
|
67
|
+
}, {
|
|
68
|
+
readonly internalType: "bytes32";
|
|
69
|
+
readonly name: "";
|
|
70
|
+
readonly type: "bytes32";
|
|
71
|
+
}];
|
|
72
|
+
readonly stateMutability: "view";
|
|
73
|
+
readonly type: "function";
|
|
74
|
+
}, {
|
|
75
|
+
readonly inputs: readonly [{
|
|
76
|
+
readonly internalType: "address";
|
|
77
|
+
readonly name: "owner";
|
|
78
|
+
readonly type: "address";
|
|
79
|
+
}, {
|
|
80
|
+
readonly internalType: "bytes32";
|
|
81
|
+
readonly name: "schema_id";
|
|
82
|
+
readonly type: "bytes32";
|
|
83
|
+
}, {
|
|
84
|
+
readonly internalType: "bytes32";
|
|
85
|
+
readonly name: "name_hash";
|
|
86
|
+
readonly type: "bytes32";
|
|
87
|
+
}];
|
|
88
|
+
readonly name: "getMerkleRoot";
|
|
89
|
+
readonly outputs: readonly [{
|
|
90
|
+
readonly internalType: "bytes32";
|
|
91
|
+
readonly name: "";
|
|
92
|
+
readonly type: "bytes32";
|
|
63
93
|
}];
|
|
64
94
|
readonly stateMutability: "view";
|
|
65
95
|
readonly type: "function";
|
|
@@ -112,6 +142,10 @@ declare const DS_REGISTRY_ABI: readonly [{
|
|
|
112
142
|
readonly internalType: "string";
|
|
113
143
|
readonly name: "manifest_cid";
|
|
114
144
|
readonly type: "string";
|
|
145
|
+
}, {
|
|
146
|
+
readonly internalType: "bytes32";
|
|
147
|
+
readonly name: "merkle_root";
|
|
148
|
+
readonly type: "bytes32";
|
|
115
149
|
}, {
|
|
116
150
|
readonly internalType: "bytes32";
|
|
117
151
|
readonly name: "schema_id";
|
|
@@ -120,10 +154,6 @@ declare const DS_REGISTRY_ABI: readonly [{
|
|
|
120
154
|
readonly internalType: "string";
|
|
121
155
|
readonly name: "name";
|
|
122
156
|
readonly type: "string";
|
|
123
|
-
}, {
|
|
124
|
-
readonly internalType: "uint256";
|
|
125
|
-
readonly name: "price";
|
|
126
|
-
readonly type: "uint256";
|
|
127
157
|
}];
|
|
128
158
|
readonly name: "publish";
|
|
129
159
|
readonly outputs: readonly [];
|
|
@@ -48,6 +48,10 @@ const DS_REGISTRY_ABI = [
|
|
|
48
48
|
"internalType": "string",
|
|
49
49
|
"name": "",
|
|
50
50
|
"type": "string"
|
|
51
|
+
}, {
|
|
52
|
+
"internalType": "bytes32",
|
|
53
|
+
"name": "",
|
|
54
|
+
"type": "bytes32"
|
|
51
55
|
}],
|
|
52
56
|
"stateMutability": "view",
|
|
53
57
|
"type": "function"
|
|
@@ -75,6 +79,37 @@ const DS_REGISTRY_ABI = [
|
|
|
75
79
|
"internalType": "string",
|
|
76
80
|
"name": "",
|
|
77
81
|
"type": "string"
|
|
82
|
+
}, {
|
|
83
|
+
"internalType": "bytes32",
|
|
84
|
+
"name": "",
|
|
85
|
+
"type": "bytes32"
|
|
86
|
+
}],
|
|
87
|
+
"stateMutability": "view",
|
|
88
|
+
"type": "function"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"inputs": [
|
|
92
|
+
{
|
|
93
|
+
"internalType": "address",
|
|
94
|
+
"name": "owner",
|
|
95
|
+
"type": "address"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"internalType": "bytes32",
|
|
99
|
+
"name": "schema_id",
|
|
100
|
+
"type": "bytes32"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"internalType": "bytes32",
|
|
104
|
+
"name": "name_hash",
|
|
105
|
+
"type": "bytes32"
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
"name": "getMerkleRoot",
|
|
109
|
+
"outputs": [{
|
|
110
|
+
"internalType": "bytes32",
|
|
111
|
+
"name": "",
|
|
112
|
+
"type": "bytes32"
|
|
78
113
|
}],
|
|
79
114
|
"stateMutability": "view",
|
|
80
115
|
"type": "function"
|
|
@@ -140,6 +175,11 @@ const DS_REGISTRY_ABI = [
|
|
|
140
175
|
"name": "manifest_cid",
|
|
141
176
|
"type": "string"
|
|
142
177
|
},
|
|
178
|
+
{
|
|
179
|
+
"internalType": "bytes32",
|
|
180
|
+
"name": "merkle_root",
|
|
181
|
+
"type": "bytes32"
|
|
182
|
+
},
|
|
143
183
|
{
|
|
144
184
|
"internalType": "bytes32",
|
|
145
185
|
"name": "schema_id",
|
|
@@ -149,11 +189,6 @@ const DS_REGISTRY_ABI = [
|
|
|
149
189
|
"internalType": "string",
|
|
150
190
|
"name": "name",
|
|
151
191
|
"type": "string"
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
"internalType": "uint256",
|
|
155
|
-
"name": "price",
|
|
156
|
-
"type": "uint256"
|
|
157
192
|
}
|
|
158
193
|
],
|
|
159
194
|
"name": "publish",
|
|
@@ -1,30 +1,39 @@
|
|
|
1
|
-
import * as viem430 from "viem";
|
|
2
1
|
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
3
2
|
|
|
4
3
|
//#region src/registries/datasource-registry/index.d.ts
|
|
4
|
+
declare function poseidonHash(inputs: bigint[]): bigint;
|
|
5
|
+
declare function hashString(value: string): bigint;
|
|
6
|
+
interface ManifestLeaf {
|
|
7
|
+
index: bigint;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
declare class MerkleTree {
|
|
11
|
+
protected constructor();
|
|
12
|
+
static leafHash(leaf: ManifestLeaf): bigint;
|
|
13
|
+
static buildTree(leaves: ManifestLeaf[]): {
|
|
14
|
+
root: bigint;
|
|
15
|
+
layers: bigint[][];
|
|
16
|
+
};
|
|
17
|
+
static buildProof(layers: bigint[][], index: number): bigint[];
|
|
18
|
+
static rootToHex(root: bigint): Hex;
|
|
19
|
+
}
|
|
5
20
|
interface DataSource {
|
|
6
21
|
manifestCid: string;
|
|
22
|
+
merkleRoot: Hex;
|
|
7
23
|
name: string;
|
|
8
24
|
schemaId: Hex;
|
|
9
25
|
version: bigint;
|
|
10
26
|
}
|
|
11
27
|
declare class DataSourceRegistry {
|
|
28
|
+
private contractAddress;
|
|
12
29
|
private publicClient;
|
|
13
30
|
private walletClient;
|
|
14
|
-
private contractAddress;
|
|
15
31
|
constructor(contractAddress: Address, publicClient: PublicClient, walletClient: WalletClient);
|
|
16
32
|
private getWriteConfig;
|
|
17
|
-
|
|
18
|
-
publish(manifestCid: string, schemaId: Hex, name: string, price: bigint, gas?: bigint): Promise<Hash>;
|
|
33
|
+
publish(manifestCid: string, merkleRoot: Hex, schemaId: Hex, name: string): Promise<Hash>;
|
|
19
34
|
get(owner: Address, schemaId: Hex, name: string): Promise<DataSource>;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
getVersion(owner: Address, schemaId: Hex, name: string): Promise<bigint>;
|
|
23
|
-
resourceId(owner: Address, schemaId: Hex, name: string): Promise<Hex>;
|
|
24
|
-
static resourceIdLocal(owner: Address, schemaId: Hex, name: string): Hex;
|
|
25
|
-
hashName(name: string): Hex;
|
|
26
|
-
waitForTransaction(hash: Hash): Promise<viem430.TransactionReceipt>;
|
|
27
|
-
private estimateGas;
|
|
35
|
+
static resourceId(owner: Address, schemaId: Hex, name: string): Hex;
|
|
36
|
+
static hashName(name: string): Hex;
|
|
28
37
|
}
|
|
29
38
|
//#endregion
|
|
30
|
-
export { DataSource, DataSourceRegistry };
|
|
39
|
+
export { DataSource, DataSourceRegistry, ManifestLeaf, MerkleTree, hashString, poseidonHash };
|
|
@@ -1,14 +1,70 @@
|
|
|
1
1
|
import { DS_REGISTRY_ABI } from "./abi.js";
|
|
2
2
|
import { encodePacked, keccak256 } from "viem";
|
|
3
|
+
import { poseidon2 } from "poseidon-lite";
|
|
3
4
|
|
|
4
5
|
//#region src/registries/datasource-registry/index.ts
|
|
6
|
+
const MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
7
|
+
function normalize(v) {
|
|
8
|
+
return (v % MODULUS + MODULUS) % MODULUS;
|
|
9
|
+
}
|
|
10
|
+
function poseidonHash(inputs) {
|
|
11
|
+
return poseidon2(inputs.map(normalize));
|
|
12
|
+
}
|
|
13
|
+
function bytesToField(chunk) {
|
|
14
|
+
let acc = 0n;
|
|
15
|
+
for (const b of chunk) acc = acc << 8n | BigInt(b);
|
|
16
|
+
return acc;
|
|
17
|
+
}
|
|
18
|
+
function hashString(value) {
|
|
19
|
+
const bytes = new TextEncoder().encode(value);
|
|
20
|
+
let h = 0n;
|
|
21
|
+
for (let i = 0; i < bytes.length; i += 31) h = poseidonHash([h, bytesToField(bytes.subarray(i, i + 31))]);
|
|
22
|
+
return h;
|
|
23
|
+
}
|
|
24
|
+
var MerkleTree = class MerkleTree {
|
|
25
|
+
constructor() {
|
|
26
|
+
throw new Error("MerkleTree is a static utility class and cannot be instantiated.");
|
|
27
|
+
}
|
|
28
|
+
static leafHash(leaf) {
|
|
29
|
+
return poseidonHash([leaf.index, hashString(leaf.name)]);
|
|
30
|
+
}
|
|
31
|
+
static buildTree(leaves) {
|
|
32
|
+
if (leaves.length === 0) throw new Error("Empty tree");
|
|
33
|
+
let current = [...leaves].sort((a, b) => Number(a.index - b.index)).map((leaf) => MerkleTree.leafHash(leaf));
|
|
34
|
+
const layers = [current];
|
|
35
|
+
while (current.length > 1) {
|
|
36
|
+
const next = [];
|
|
37
|
+
for (let i = 0; i < current.length; i += 2) {
|
|
38
|
+
const left = current[i];
|
|
39
|
+
const right = current[i + 1] ?? left;
|
|
40
|
+
next.push(poseidonHash([left, right]));
|
|
41
|
+
}
|
|
42
|
+
current = next;
|
|
43
|
+
layers.push(next);
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
root: current[0],
|
|
47
|
+
layers
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
static buildProof(layers, index) {
|
|
51
|
+
const proof = [];
|
|
52
|
+
for (let d = 0; d < layers.length - 1; d++) {
|
|
53
|
+
const layer = layers[d];
|
|
54
|
+
const sibling = index % 2 === 0 ? layer[index + 1] ?? layer[index] : layer[index - 1];
|
|
55
|
+
proof.push(sibling);
|
|
56
|
+
index = Math.floor(index / 2);
|
|
57
|
+
}
|
|
58
|
+
return proof;
|
|
59
|
+
}
|
|
60
|
+
static rootToHex(root) {
|
|
61
|
+
return `0x${root.toString(16).padStart(64, "0")}`;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
5
64
|
var DataSourceRegistry = class {
|
|
6
|
-
publicClient;
|
|
7
|
-
walletClient;
|
|
8
|
-
contractAddress;
|
|
9
65
|
constructor(contractAddress, publicClient, walletClient) {
|
|
10
|
-
this.publicClient = publicClient;
|
|
11
66
|
this.contractAddress = contractAddress;
|
|
67
|
+
this.publicClient = publicClient;
|
|
12
68
|
this.walletClient = walletClient;
|
|
13
69
|
}
|
|
14
70
|
getWriteConfig() {
|
|
@@ -19,45 +75,42 @@ var DataSourceRegistry = class {
|
|
|
19
75
|
account: this.walletClient.account
|
|
20
76
|
};
|
|
21
77
|
}
|
|
22
|
-
|
|
23
|
-
return this.contractAddress;
|
|
24
|
-
}
|
|
25
|
-
async publish(manifestCid, schemaId, name, price, gas) {
|
|
78
|
+
async publish(manifestCid, merkleRoot, schemaId, name) {
|
|
26
79
|
const { chain, account } = this.getWriteConfig();
|
|
27
|
-
const
|
|
80
|
+
const fees = await this.publicClient.estimateFeesPerGas();
|
|
81
|
+
const gas = await this.publicClient.estimateContractGas({
|
|
28
82
|
address: this.contractAddress,
|
|
29
83
|
abi: DS_REGISTRY_ABI,
|
|
30
84
|
functionName: "publish",
|
|
31
85
|
args: [
|
|
32
86
|
manifestCid,
|
|
87
|
+
merkleRoot,
|
|
33
88
|
schemaId,
|
|
34
|
-
name
|
|
35
|
-
price
|
|
89
|
+
name
|
|
36
90
|
],
|
|
37
91
|
account
|
|
38
|
-
})
|
|
39
|
-
const fees = await this.publicClient.estimateFeesPerGas();
|
|
92
|
+
});
|
|
40
93
|
const hash = await this.walletClient.writeContract({
|
|
41
94
|
address: this.contractAddress,
|
|
42
95
|
abi: DS_REGISTRY_ABI,
|
|
43
96
|
functionName: "publish",
|
|
44
97
|
args: [
|
|
45
98
|
manifestCid,
|
|
99
|
+
merkleRoot,
|
|
46
100
|
schemaId,
|
|
47
|
-
name
|
|
48
|
-
price
|
|
101
|
+
name
|
|
49
102
|
],
|
|
50
103
|
chain,
|
|
51
104
|
account,
|
|
52
|
-
gas:
|
|
105
|
+
gas: gas * 130n / 100n,
|
|
53
106
|
maxFeePerGas: fees.maxFeePerGas * 3n,
|
|
54
107
|
maxPriorityFeePerGas: fees.maxPriorityFeePerGas
|
|
55
108
|
});
|
|
56
|
-
await this.
|
|
109
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
57
110
|
return hash;
|
|
58
111
|
}
|
|
59
112
|
async get(owner, schemaId, name) {
|
|
60
|
-
const [
|
|
113
|
+
const [tuple, version] = await Promise.all([this.publicClient.readContract({
|
|
61
114
|
address: this.contractAddress,
|
|
62
115
|
abi: DS_REGISTRY_ABI,
|
|
63
116
|
functionName: "get",
|
|
@@ -76,62 +129,16 @@ var DataSourceRegistry = class {
|
|
|
76
129
|
name
|
|
77
130
|
]
|
|
78
131
|
})]);
|
|
132
|
+
const [manifestCid, merkleRoot] = tuple;
|
|
79
133
|
return {
|
|
80
134
|
manifestCid,
|
|
135
|
+
merkleRoot,
|
|
81
136
|
name,
|
|
82
137
|
schemaId,
|
|
83
138
|
version
|
|
84
139
|
};
|
|
85
140
|
}
|
|
86
|
-
|
|
87
|
-
return this.publicClient.readContract({
|
|
88
|
-
address: this.contractAddress,
|
|
89
|
-
abi: DS_REGISTRY_ABI,
|
|
90
|
-
functionName: "getByHash",
|
|
91
|
-
args: [
|
|
92
|
-
owner,
|
|
93
|
-
schemaId,
|
|
94
|
-
nameHash
|
|
95
|
-
]
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
async getName(owner, schemaId, nameHash) {
|
|
99
|
-
return this.publicClient.readContract({
|
|
100
|
-
address: this.contractAddress,
|
|
101
|
-
abi: DS_REGISTRY_ABI,
|
|
102
|
-
functionName: "getName",
|
|
103
|
-
args: [
|
|
104
|
-
owner,
|
|
105
|
-
schemaId,
|
|
106
|
-
nameHash
|
|
107
|
-
]
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
async getVersion(owner, schemaId, name) {
|
|
111
|
-
return this.publicClient.readContract({
|
|
112
|
-
address: this.contractAddress,
|
|
113
|
-
abi: DS_REGISTRY_ABI,
|
|
114
|
-
functionName: "getVersion",
|
|
115
|
-
args: [
|
|
116
|
-
owner,
|
|
117
|
-
schemaId,
|
|
118
|
-
name
|
|
119
|
-
]
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
async resourceId(owner, schemaId, name) {
|
|
123
|
-
return this.publicClient.readContract({
|
|
124
|
-
address: this.contractAddress,
|
|
125
|
-
abi: DS_REGISTRY_ABI,
|
|
126
|
-
functionName: "resourceId",
|
|
127
|
-
args: [
|
|
128
|
-
owner,
|
|
129
|
-
schemaId,
|
|
130
|
-
name
|
|
131
|
-
]
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
static resourceIdLocal(owner, schemaId, name) {
|
|
141
|
+
static resourceId(owner, schemaId, name) {
|
|
135
142
|
return keccak256(encodePacked([
|
|
136
143
|
"address",
|
|
137
144
|
"bytes32",
|
|
@@ -142,17 +149,10 @@ var DataSourceRegistry = class {
|
|
|
142
149
|
keccak256(new TextEncoder().encode(name))
|
|
143
150
|
]));
|
|
144
151
|
}
|
|
145
|
-
hashName(name) {
|
|
152
|
+
static hashName(name) {
|
|
146
153
|
return keccak256(new TextEncoder().encode(name));
|
|
147
154
|
}
|
|
148
|
-
async waitForTransaction(hash) {
|
|
149
|
-
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
150
|
-
}
|
|
151
|
-
async estimateGas(fn, override) {
|
|
152
|
-
if (override !== void 0) return override;
|
|
153
|
-
return await fn() * 130n / 100n;
|
|
154
|
-
}
|
|
155
155
|
};
|
|
156
156
|
|
|
157
157
|
//#endregion
|
|
158
|
-
export { DataSourceRegistry };
|
|
158
|
+
export { DataSourceRegistry, MerkleTree, hashString, poseidonHash };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as viem350 from "viem";
|
|
2
2
|
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
3
3
|
|
|
4
4
|
//#region src/registries/schema-registry/index.d.ts
|
|
@@ -16,7 +16,7 @@ declare class SchemaRegistry {
|
|
|
16
16
|
private getWriteConfig;
|
|
17
17
|
getContractAddress(): `0x${string}`;
|
|
18
18
|
schemaId(name: string): Promise<Hex>;
|
|
19
|
-
registerSchema(name: string, specCid: string, agentId
|
|
19
|
+
registerSchema(name: string, specCid: string, agentId?: string): Promise<{
|
|
20
20
|
hash: Hash;
|
|
21
21
|
schemaId: Hex;
|
|
22
22
|
}>;
|
|
@@ -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<viem350.TransactionReceipt>;
|
|
31
31
|
private resolveId;
|
|
32
32
|
}
|
|
33
33
|
//#endregion
|
|
@@ -29,7 +29,7 @@ var SchemaRegistry = class {
|
|
|
29
29
|
args: [name]
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
-
async registerSchema(name, specCid, agentId) {
|
|
32
|
+
async registerSchema(name, specCid, agentId = "") {
|
|
33
33
|
const { chain, account } = this.getWriteConfig();
|
|
34
34
|
const schemaId = await this.publicClient.simulateContract({
|
|
35
35
|
address: this.contractAddress,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PrepareSettleParams, PrepareSettleResult, RegisterParams, SettleParams, TransferWithAuthParams, TransferWithAuthPayload } from "./types.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as viem349 from "viem";
|
|
3
3
|
import { Address, Hex, PublicClient, WalletClient } from "viem";
|
|
4
4
|
|
|
5
5
|
//#region src/registries/settlement-registry/index.d.ts
|
|
@@ -53,7 +53,7 @@ declare class SettlementRegistry {
|
|
|
53
53
|
isRegistered(resourceId: Hex, identityCommitment: bigint): Promise<boolean>;
|
|
54
54
|
getGroupId(resourceId: Hex): Promise<bigint>;
|
|
55
55
|
getPrice(resourceId: Hex): Promise<bigint>;
|
|
56
|
-
waitForTransaction(hash: Hex): Promise<
|
|
56
|
+
waitForTransaction(hash: Hex): Promise<viem349.TransactionReceipt>;
|
|
57
57
|
/**
|
|
58
58
|
* Reconstruct the Semaphore group from MemberRegistered events.
|
|
59
59
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ManifestEntry } from "../publisher/types.js";
|
|
3
3
|
import { PrepareSettleParams, PrepareSettleResult, TransferWithAuthParams, TransferWithAuthPayload } from "../../registries/settlement-registry/types.js";
|
|
4
4
|
import { SettlementRegistry } from "../../registries/settlement-registry/index.js";
|
|
5
5
|
import { ClaimParams, ClaimResult, FetchParams, FetchResult, PurchaseParams, PurchaseResult } from "./types.js";
|
|
@@ -25,8 +25,8 @@ declare class ConsumerRole {
|
|
|
25
25
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
26
26
|
*/
|
|
27
27
|
fetch(params: FetchParams): Promise<FetchResult>;
|
|
28
|
-
|
|
29
|
-
getEntry(owner: Address, schemaId: Hex, name: string): Promise<ManifestEntry>;
|
|
28
|
+
checkManifestExists(who: Address, schemaId: Hex, name: string): Promise<boolean>;
|
|
29
|
+
getEntry(owner: Address, schemaId: Hex, name: string, gateway?: string): Promise<ManifestEntry | undefined>;
|
|
30
30
|
/**
|
|
31
31
|
* Resolves the handle URI for a specific field
|
|
32
32
|
* and fetches it via the worker in one call.
|