@fangorn-network/sdk 2026.4.7 → 2026.4.10
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 +30 -26
- package/lib/cli/cli.js +115 -153
- package/lib/cli/index.d.ts +170 -170
- package/lib/config.d.ts +260 -257
- package/lib/config.js +7 -5
- package/lib/fangorn.d.ts +0 -3
- package/lib/fangorn.js +16 -28
- package/lib/index.d.ts +3 -3
- package/lib/index.js +4 -5
- package/lib/modules/encryption/lit.d.ts +0 -1
- package/lib/modules/encryption/lit.js +1 -7
- package/lib/modules/gadgets/settledGadget.d.ts +1 -8
- package/lib/modules/gadgets/settledGadget.js +1 -13
- package/lib/providers/storage/index.d.ts +12 -12
- package/lib/providers/storage/index.js +49 -2
- package/lib/providers/storage/pinata.d.ts +11 -0
- package/lib/providers/storage/pinata.js +24 -0
- package/lib/registries/datasource-registry/abi.d.ts +116 -43
- package/lib/registries/datasource-registry/abi.js +190 -12
- package/lib/registries/datasource-registry/index.d.ts +12 -6
- package/lib/registries/datasource-registry/index.js +96 -20
- package/lib/registries/index.js +1 -1
- package/lib/registries/schema-registry/abi.d.ts +157 -66
- package/lib/registries/schema-registry/abi.js +242 -15
- package/lib/registries/schema-registry/index.d.ts +8 -3
- package/lib/registries/schema-registry/index.js +82 -17
- package/lib/registries/settlement-registry/abi.d.ts +199 -145
- package/lib/registries/settlement-registry/abi.js +331 -25
- package/lib/registries/settlement-registry/index.d.ts +36 -39
- package/lib/registries/settlement-registry/index.js +125 -91
- package/lib/registries/settlement-registry/types.d.ts +6 -3
- package/lib/roles/consumer/index.d.ts +8 -29
- package/lib/roles/consumer/index.js +25 -45
- package/lib/roles/consumer/types.d.ts +4 -4
- package/lib/roles/publisher/index.d.ts +26 -29
- package/lib/roles/publisher/index.js +70 -77
- package/lib/roles/publisher/types.d.ts +7 -3
- package/lib/roles/schema/index.d.ts +3 -8
- package/lib/roles/schema/index.js +6 -34
- package/lib/test/testbed.d.ts +7 -32
- package/lib/test/testbed.js +22 -45
- 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
|
@@ -1,69 +1,120 @@
|
|
|
1
1
|
import { SETTLEMENT_REGISTRY_ABI } from "./abi.js";
|
|
2
|
-
import {
|
|
3
|
-
import { createWalletClient, encodePacked, http, keccak256, parseSignature } from "viem";
|
|
2
|
+
import { createWalletClient, http, parseSignature } from "viem";
|
|
4
3
|
import { privateKeyToAccount } from "viem/accounts";
|
|
5
4
|
import { Group } from "@semaphore-protocol/group";
|
|
6
5
|
import { generateProof } from "@semaphore-protocol/proof";
|
|
7
6
|
|
|
8
7
|
//#region src/registries/settlement-registry/index.ts
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* SDK wrapper for the on-chain SettlementRegistry Stylus contract.
|
|
12
|
-
*
|
|
13
|
-
* Responsible for:
|
|
14
|
-
* - createResource(): owner calls once per (schemaId, tag) asset
|
|
15
|
-
* - register(): buyer pays + joins Semaphore group
|
|
16
|
-
* - settle(): buyer proves membership + fires hook (NFT/timelock)
|
|
17
|
-
* - isSettled(): access check — used to gate decryptFile()
|
|
18
|
-
* - deriveResourceId(): deterministic (owner, schemaId, tag) → bytes32
|
|
19
|
-
*/
|
|
20
8
|
var SettlementRegistry = class {
|
|
21
9
|
constructor(contractAddress, publicClient, walletClient) {
|
|
22
10
|
this.contractAddress = contractAddress;
|
|
23
11
|
this.publicClient = publicClient;
|
|
24
12
|
this.walletClient = walletClient;
|
|
25
13
|
}
|
|
14
|
+
getAccount() {
|
|
15
|
+
const account = this.walletClient.account;
|
|
16
|
+
if (!account) throw new Error("Wallet client must have an account configured");
|
|
17
|
+
return account;
|
|
18
|
+
}
|
|
19
|
+
getChain() {
|
|
20
|
+
const chain = this.walletClient.chain;
|
|
21
|
+
if (!chain) throw new Error("Wallet client must have a chain configured");
|
|
22
|
+
return chain;
|
|
23
|
+
}
|
|
26
24
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @param resourceId: The hex encoded resource id
|
|
30
|
-
* @param price : The USDC price for the resource
|
|
31
|
-
* @returns The finalized transaction hash.
|
|
25
|
+
* Authorize or revoke a DataSourceRegistry contract.
|
|
26
|
+
* Only callable by the contract admin.
|
|
32
27
|
*/
|
|
33
|
-
async
|
|
34
|
-
const account = this.
|
|
35
|
-
|
|
28
|
+
async setRegistry(registry, authorized) {
|
|
29
|
+
const account = this.getAccount();
|
|
30
|
+
const hash = await this.walletClient.writeContract({
|
|
31
|
+
address: this.contractAddress,
|
|
32
|
+
abi: SETTLEMENT_REGISTRY_ABI,
|
|
33
|
+
functionName: "setRegistry",
|
|
34
|
+
args: [registry, authorized],
|
|
35
|
+
chain: this.getChain(),
|
|
36
|
+
account
|
|
37
|
+
});
|
|
38
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
39
|
+
return hash;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a Semaphore group for a resource.
|
|
43
|
+
* In production this is called by the DataSourceRegistry, which passes
|
|
44
|
+
* the publisher wallet as `owner`. Direct calls require the caller to
|
|
45
|
+
* be an authorized registry.
|
|
46
|
+
*/
|
|
47
|
+
async createResource(resourceId, price, owner) {
|
|
48
|
+
const account = this.getAccount();
|
|
36
49
|
const hash = await this.walletClient.writeContract({
|
|
37
50
|
address: this.contractAddress,
|
|
38
51
|
abi: SETTLEMENT_REGISTRY_ABI,
|
|
39
52
|
functionName: "createResource",
|
|
40
|
-
args: [
|
|
41
|
-
|
|
53
|
+
args: [
|
|
54
|
+
resourceId,
|
|
55
|
+
price,
|
|
56
|
+
owner
|
|
57
|
+
],
|
|
58
|
+
chain: this.getChain(),
|
|
59
|
+
account
|
|
60
|
+
});
|
|
61
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
62
|
+
return hash;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Update the price for a resource.
|
|
66
|
+
* In production this is called by the DataSourceRegistry.
|
|
67
|
+
*/
|
|
68
|
+
async updatePrice(resourceId, price, owner) {
|
|
69
|
+
const account = this.getAccount();
|
|
70
|
+
const hash = await this.walletClient.writeContract({
|
|
71
|
+
address: this.contractAddress,
|
|
72
|
+
abi: SETTLEMENT_REGISTRY_ABI,
|
|
73
|
+
functionName: "updatePrice",
|
|
74
|
+
args: [
|
|
75
|
+
resourceId,
|
|
76
|
+
price,
|
|
77
|
+
owner
|
|
78
|
+
],
|
|
79
|
+
chain: this.getChain(),
|
|
80
|
+
account
|
|
81
|
+
});
|
|
82
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
83
|
+
return hash;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Register a hook contract for a resource.
|
|
87
|
+
* Called directly by the publisher wallet (msg.sender must be resource owner).
|
|
88
|
+
*/
|
|
89
|
+
async registerHook(resourceId, hook) {
|
|
90
|
+
const account = this.getAccount();
|
|
91
|
+
const hash = await this.walletClient.writeContract({
|
|
92
|
+
address: this.contractAddress,
|
|
93
|
+
abi: SETTLEMENT_REGISTRY_ABI,
|
|
94
|
+
functionName: "registerHook",
|
|
95
|
+
args: [resourceId, hook],
|
|
96
|
+
chain: this.getChain(),
|
|
42
97
|
account
|
|
43
98
|
});
|
|
44
99
|
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
45
100
|
return hash;
|
|
46
101
|
}
|
|
47
102
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @param params The transferWithAuth params
|
|
50
|
-
* @returns The payload containing the signed call data
|
|
103
|
+
* Prepare the EIP-3009 transferWithAuthorization signature.
|
|
51
104
|
*/
|
|
52
105
|
async prepareTransferWithAuth(params) {
|
|
53
|
-
const {
|
|
54
|
-
const
|
|
106
|
+
const { paymentRecipient, amount, usdcAddress, usdcDomainName, usdcDomainVersion } = params;
|
|
107
|
+
const walletClient = params.walletClient ?? this.walletClient;
|
|
108
|
+
const chain = walletClient.chain;
|
|
55
109
|
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
|
-
});
|
|
110
|
+
const account = walletClient.account;
|
|
111
|
+
if (!account) throw new Error("Wallet client must have an account configured.");
|
|
62
112
|
const validAfter = 0n;
|
|
63
113
|
const validBefore = BigInt(Math.floor(Date.now() / 1e3) + 3600);
|
|
64
114
|
const nonceBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
65
115
|
const nonce = `0x${Array.from(nonceBytes).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
66
|
-
const { v, r, s } = parseSignature(await
|
|
116
|
+
const { v, r, s } = parseSignature(await walletClient.signTypedData({
|
|
117
|
+
account,
|
|
67
118
|
domain: {
|
|
68
119
|
name: usdcDomainName,
|
|
69
120
|
version: usdcDomainVersion,
|
|
@@ -98,7 +149,7 @@ var SettlementRegistry = class {
|
|
|
98
149
|
] },
|
|
99
150
|
primaryType: "TransferWithAuthorization",
|
|
100
151
|
message: {
|
|
101
|
-
from:
|
|
152
|
+
from: account.address,
|
|
102
153
|
to: paymentRecipient,
|
|
103
154
|
value: amount,
|
|
104
155
|
validAfter,
|
|
@@ -107,7 +158,7 @@ var SettlementRegistry = class {
|
|
|
107
158
|
}
|
|
108
159
|
}));
|
|
109
160
|
return {
|
|
110
|
-
|
|
161
|
+
sender: account.address,
|
|
111
162
|
paymentRecipient,
|
|
112
163
|
amount,
|
|
113
164
|
validAfter,
|
|
@@ -119,15 +170,14 @@ var SettlementRegistry = class {
|
|
|
119
170
|
};
|
|
120
171
|
}
|
|
121
172
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
173
|
+
* Phase 1 — submit payment and join the Semaphore group.
|
|
174
|
+
* Submitted via a relayer so the buyer's burner wallet is never linked
|
|
175
|
+
* to their identity commitment on-chain.
|
|
125
176
|
*/
|
|
126
177
|
async register(params) {
|
|
127
178
|
const { resourceId, identityCommitment, relayerPrivateKey, preparedRegister } = params;
|
|
128
|
-
const {
|
|
129
|
-
const chain = this.
|
|
130
|
-
if (!chain) throw new Error("Wallet client must have a chain configured.");
|
|
179
|
+
const { sender, paymentRecipient, amount, validAfter, validBefore, nonce, v, r, s } = preparedRegister;
|
|
180
|
+
const chain = this.getChain();
|
|
131
181
|
const hash = await createWalletClient({
|
|
132
182
|
account: privateKeyToAccount(relayerPrivateKey),
|
|
133
183
|
chain,
|
|
@@ -139,7 +189,7 @@ var SettlementRegistry = class {
|
|
|
139
189
|
args: [
|
|
140
190
|
resourceId,
|
|
141
191
|
identityCommitment,
|
|
142
|
-
|
|
192
|
+
sender,
|
|
143
193
|
paymentRecipient,
|
|
144
194
|
amount,
|
|
145
195
|
validAfter,
|
|
@@ -148,11 +198,16 @@ var SettlementRegistry = class {
|
|
|
148
198
|
v,
|
|
149
199
|
r,
|
|
150
200
|
s
|
|
151
|
-
]
|
|
201
|
+
],
|
|
202
|
+
chain,
|
|
203
|
+
account: privateKeyToAccount(relayerPrivateKey)
|
|
152
204
|
});
|
|
153
205
|
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
154
206
|
return hash;
|
|
155
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Build the Semaphore proof for Phase 2.
|
|
210
|
+
*/
|
|
156
211
|
async prepareSettle(params) {
|
|
157
212
|
const { resourceId, identity, stealthAddress } = params;
|
|
158
213
|
const groupId = await this.publicClient.readContract({
|
|
@@ -174,16 +229,21 @@ var SettlementRegistry = class {
|
|
|
174
229
|
hookData: params.hookData ?? "0x"
|
|
175
230
|
};
|
|
176
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Phase 2 — prove group membership and claim access.
|
|
234
|
+
* Submitted via a relayer for unlinkability.
|
|
235
|
+
*/
|
|
177
236
|
async settle(params) {
|
|
178
237
|
const { relayerPrivateKey, preparedSettle } = params;
|
|
179
|
-
const { resourceId, stealthAddress, merkleTreeDepth, merkleTreeRoot, nullifier, message, points } = preparedSettle;
|
|
180
|
-
const chain = this.
|
|
181
|
-
|
|
182
|
-
const hash = await createWalletClient({
|
|
238
|
+
const { resourceId, stealthAddress, merkleTreeDepth, merkleTreeRoot, nullifier, message, points, hookData } = preparedSettle;
|
|
239
|
+
const chain = this.getChain();
|
|
240
|
+
const submitter = createWalletClient({
|
|
183
241
|
account: privateKeyToAccount(relayerPrivateKey),
|
|
184
242
|
chain,
|
|
185
243
|
transport: http(chain.rpcUrls.default.http[0])
|
|
186
|
-
})
|
|
244
|
+
});
|
|
245
|
+
const hookBytes = hookData !== "0x" ? Array.from(Buffer.from(hookData.slice(2), "hex")) : [];
|
|
246
|
+
const hash = await submitter.writeContract({
|
|
187
247
|
address: this.contractAddress,
|
|
188
248
|
abi: SETTLEMENT_REGISTRY_ABI,
|
|
189
249
|
functionName: "settle",
|
|
@@ -196,9 +256,9 @@ var SettlementRegistry = class {
|
|
|
196
256
|
nullifier,
|
|
197
257
|
message,
|
|
198
258
|
points,
|
|
199
|
-
|
|
259
|
+
hookBytes
|
|
200
260
|
],
|
|
201
|
-
chain
|
|
261
|
+
chain,
|
|
202
262
|
account: privateKeyToAccount(relayerPrivateKey)
|
|
203
263
|
});
|
|
204
264
|
const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
|
|
@@ -208,10 +268,6 @@ var SettlementRegistry = class {
|
|
|
208
268
|
nullifier
|
|
209
269
|
};
|
|
210
270
|
}
|
|
211
|
-
/**
|
|
212
|
-
* Check if a nullifier has been settled (i.e. the user completed Phase 2).
|
|
213
|
-
* Used to gate decryptFile() in the Fangorn class.
|
|
214
|
-
*/
|
|
215
271
|
async isSettled(stealthAddress, resourceId) {
|
|
216
272
|
return this.publicClient.readContract({
|
|
217
273
|
address: this.contractAddress,
|
|
@@ -220,9 +276,6 @@ var SettlementRegistry = class {
|
|
|
220
276
|
args: [stealthAddress, resourceId]
|
|
221
277
|
});
|
|
222
278
|
}
|
|
223
|
-
/**
|
|
224
|
-
* Check if an identity commitment is registered for a resource (completed Phase 1).
|
|
225
|
-
*/
|
|
226
279
|
async isRegistered(resourceId, identityCommitment) {
|
|
227
280
|
return this.publicClient.readContract({
|
|
228
281
|
address: this.contractAddress,
|
|
@@ -231,11 +284,6 @@ var SettlementRegistry = class {
|
|
|
231
284
|
args: [resourceId, identityCommitment]
|
|
232
285
|
});
|
|
233
286
|
}
|
|
234
|
-
/**
|
|
235
|
-
* Get the semaphore group id for a resource
|
|
236
|
-
* @param resourceId The resource id
|
|
237
|
-
* @returns The group id (if it exists), else null
|
|
238
|
-
*/
|
|
239
287
|
async getGroupId(resourceId) {
|
|
240
288
|
return this.publicClient.readContract({
|
|
241
289
|
address: this.contractAddress,
|
|
@@ -244,11 +292,6 @@ var SettlementRegistry = class {
|
|
|
244
292
|
args: [resourceId]
|
|
245
293
|
});
|
|
246
294
|
}
|
|
247
|
-
/**
|
|
248
|
-
* Get the price associated with a resource
|
|
249
|
-
* @param resourceId The resource id
|
|
250
|
-
* @returns The price
|
|
251
|
-
*/
|
|
252
295
|
async getPrice(resourceId) {
|
|
253
296
|
return this.publicClient.readContract({
|
|
254
297
|
address: this.contractAddress,
|
|
@@ -257,28 +300,22 @@ var SettlementRegistry = class {
|
|
|
257
300
|
args: [resourceId]
|
|
258
301
|
});
|
|
259
302
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
owner,
|
|
271
|
-
schemaName,
|
|
272
|
-
tag
|
|
273
|
-
]));
|
|
303
|
+
async getOwner(resourceId) {
|
|
304
|
+
return this.publicClient.readContract({
|
|
305
|
+
address: this.contractAddress,
|
|
306
|
+
abi: SETTLEMENT_REGISTRY_ABI,
|
|
307
|
+
functionName: "getOwner",
|
|
308
|
+
args: [resourceId]
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
async waitForTransaction(hash) {
|
|
312
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
274
313
|
}
|
|
275
314
|
/**
|
|
276
315
|
* Reconstruct the Semaphore group from MemberRegistered events.
|
|
277
|
-
* Fetches all logs (no topic filter) and filters in JS to avoid
|
|
278
|
-
* viem topic-encoding edge cases with indexed params.
|
|
279
316
|
*/
|
|
280
317
|
async fetchGroup(resourceId) {
|
|
281
|
-
const
|
|
318
|
+
const logs = await this.publicClient.getLogs({
|
|
282
319
|
address: this.contractAddress,
|
|
283
320
|
event: {
|
|
284
321
|
name: "MemberRegistered",
|
|
@@ -302,14 +339,11 @@ var SettlementRegistry = class {
|
|
|
302
339
|
]
|
|
303
340
|
},
|
|
304
341
|
fromBlock: 0n
|
|
305
|
-
})
|
|
342
|
+
});
|
|
306
343
|
const group = new Group();
|
|
307
|
-
for (const log of
|
|
344
|
+
for (const log of logs.filter((l) => l.args.resourceId?.toLowerCase() === resourceId.toLowerCase())) group.addMember(String(log.args.identityCommitment));
|
|
308
345
|
return group;
|
|
309
346
|
}
|
|
310
|
-
async waitForTransaction(hash) {
|
|
311
|
-
return await this.publicClient.waitForTransactionReceipt({ hash });
|
|
312
|
-
}
|
|
313
347
|
};
|
|
314
348
|
|
|
315
349
|
//#endregion
|
|
@@ -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";
|
|
@@ -11,53 +10,33 @@ import { Identity } from "@semaphore-protocol/identity";
|
|
|
11
10
|
//#region src/roles/consumer/index.d.ts
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
|
-
* The consumer namespace encapsulates all functionality relative to
|
|
13
|
+
* The consumer namespace encapsulates all functionality relative to
|
|
14
|
+
* accessing and consuming data.
|
|
15
15
|
*/
|
|
16
16
|
declare class ConsumerRole {
|
|
17
17
|
private readonly dataSourceRegistry;
|
|
18
18
|
private readonly settlementRegistry;
|
|
19
|
-
private readonly storage;
|
|
20
19
|
private readonly encryptionService;
|
|
21
20
|
private readonly domain;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
21
|
+
private readonly ipfsGateway;
|
|
22
|
+
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry, encryptionService: EncryptionService, domain: string, ipfsGateway: string);
|
|
28
23
|
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
24
|
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
25
|
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
26
|
claim(params: ClaimParams): Promise<ClaimResult>;
|
|
48
27
|
/**
|
|
49
28
|
* Decrypt a specific encrypted field within a record.
|
|
50
29
|
*
|
|
51
30
|
* One purchase + claim unlocks ALL encrypted fields in the record —
|
|
52
|
-
* the resource is at the record (
|
|
31
|
+
* the resource is at the record (name) level, not the field level.
|
|
53
32
|
* This method can be called once per encrypted field without re-purchasing.
|
|
54
33
|
*
|
|
55
34
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
56
35
|
*/
|
|
57
36
|
decrypt(params: DecryptParams): Promise<Uint8Array>;
|
|
58
|
-
getManifest(owner: Address, schemaId: Hex): Promise<Manifest | undefined>;
|
|
59
|
-
getEntry(owner: Address, schemaId: Hex,
|
|
60
|
-
isRegistered(owner: Address, schemaId: Hex,
|
|
37
|
+
getManifest(owner: Address, schemaId: Hex, name: string): Promise<Manifest | undefined>;
|
|
38
|
+
getEntry(owner: Address, schemaId: Hex, name: string): Promise<ManifestEntry>;
|
|
39
|
+
isRegistered(owner: Address, schemaId: Hex, name: string, identity: Identity): Promise<boolean>;
|
|
61
40
|
private deriveResourceId;
|
|
62
41
|
}
|
|
63
42
|
//#endregion
|
|
@@ -1,40 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { retrieveByCid } from "../../providers/storage/index.js";
|
|
2
|
+
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
2
3
|
|
|
3
4
|
//#region src/roles/consumer/index.ts
|
|
4
5
|
/**
|
|
5
|
-
* The consumer namespace encapsulates all functionality relative to
|
|
6
|
+
* The consumer namespace encapsulates all functionality relative to
|
|
7
|
+
* accessing and consuming data.
|
|
6
8
|
*/
|
|
7
9
|
var ConsumerRole = class {
|
|
8
|
-
constructor(dataSourceRegistry, settlementRegistry,
|
|
10
|
+
constructor(dataSourceRegistry, settlementRegistry, encryptionService, domain, ipfsGateway) {
|
|
9
11
|
this.dataSourceRegistry = dataSourceRegistry;
|
|
10
12
|
this.settlementRegistry = settlementRegistry;
|
|
11
|
-
this.storage = storage;
|
|
12
13
|
this.encryptionService = encryptionService;
|
|
13
14
|
this.domain = domain;
|
|
15
|
+
this.ipfsGateway = ipfsGateway;
|
|
14
16
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
17
|
-
* @param params
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
17
|
async prepareRegister(params) {
|
|
21
18
|
return this.settlementRegistry.prepareTransferWithAuth(params);
|
|
22
19
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds the zkp to prove you're in a semaphore group
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
20
|
async prepareSettle(params) {
|
|
29
21
|
return this.settlementRegistry.prepareSettle(params);
|
|
30
22
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Purchase the resource and register in the semaphore group (on success)
|
|
33
|
-
* @param params
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
23
|
async register(params) {
|
|
37
|
-
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.
|
|
24
|
+
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.name);
|
|
38
25
|
return {
|
|
39
26
|
txHash: await this.settlementRegistry.register({
|
|
40
27
|
resourceId,
|
|
@@ -45,14 +32,8 @@ var ConsumerRole = class {
|
|
|
45
32
|
resourceId
|
|
46
33
|
};
|
|
47
34
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Claim access to a resource (prove settlement)
|
|
50
|
-
* Must register first.
|
|
51
|
-
* @param params
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
35
|
async claim(params) {
|
|
55
|
-
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.
|
|
36
|
+
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.name);
|
|
56
37
|
const { hash, nullifier } = await this.settlementRegistry.settle({
|
|
57
38
|
relayerPrivateKey: params.relayerPrivateKey,
|
|
58
39
|
preparedSettle: params.preparedSettle
|
|
@@ -67,46 +48,45 @@ var ConsumerRole = class {
|
|
|
67
48
|
* Decrypt a specific encrypted field within a record.
|
|
68
49
|
*
|
|
69
50
|
* One purchase + claim unlocks ALL encrypted fields in the record —
|
|
70
|
-
* the resource is at the record (
|
|
51
|
+
* the resource is at the record (name) level, not the field level.
|
|
71
52
|
* This method can be called once per encrypted field without re-purchasing.
|
|
72
53
|
*
|
|
73
54
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
74
55
|
*/
|
|
75
56
|
async decrypt(params) {
|
|
76
|
-
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.
|
|
57
|
+
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.name);
|
|
77
58
|
if (!params.skipSettlementCheck) {
|
|
78
59
|
if (!params.identity) throw new Error("identity is required for settlement verification. Pass skipSettlementCheck: true for owner / out-of-band flows.");
|
|
79
|
-
if (!await this.settlementRegistry.isRegistered(resourceId, params.identity.commitment)) throw new Error(`Access denied: identity not registered for resource ${resourceId}. Call
|
|
60
|
+
if (!await this.settlementRegistry.isRegistered(resourceId, params.identity.commitment)) throw new Error(`Access denied: identity not registered for resource ${resourceId}. Call register() and wait for confirmation before decrypting.`);
|
|
80
61
|
}
|
|
81
|
-
const fieldValue = (await this.getEntry(params.owner, params.schemaId, params.
|
|
62
|
+
const fieldValue = (await this.getEntry(params.owner, params.schemaId, params.name)).fields[params.field];
|
|
82
63
|
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);
|
|
64
|
+
const encrypted = await retrieveByCid(fieldValue.handle.cid, this.ipfsGateway);
|
|
85
65
|
const authContext = params.authContext ?? await this.encryptionService.createAuthContext(params.walletClient, this.domain, params.nullifierHash);
|
|
86
66
|
return (await this.encryptionService.decrypt(encrypted, authContext)).data;
|
|
87
67
|
}
|
|
88
|
-
async getManifest(owner, schemaId) {
|
|
68
|
+
async getManifest(owner, schemaId, name) {
|
|
89
69
|
try {
|
|
90
|
-
const ds = await this.dataSourceRegistry.
|
|
70
|
+
const ds = await this.dataSourceRegistry.get(owner, schemaId, name);
|
|
91
71
|
if (!ds.manifestCid || ds.manifestCid === "") return void 0;
|
|
92
|
-
return await
|
|
72
|
+
return await retrieveByCid(ds.manifestCid, this.ipfsGateway);
|
|
93
73
|
} catch {
|
|
94
74
|
return;
|
|
95
75
|
}
|
|
96
76
|
}
|
|
97
|
-
async getEntry(owner, schemaId,
|
|
98
|
-
const manifest = await this.getManifest(owner, schemaId);
|
|
99
|
-
if (!manifest) throw new Error(`No manifest found for owner ${owner} / schemaId ${schemaId}`);
|
|
100
|
-
const entry = manifest.entries.find((e) => e.
|
|
101
|
-
if (!entry) throw new Error(`Entry not found: "${
|
|
77
|
+
async getEntry(owner, schemaId, name) {
|
|
78
|
+
const manifest = await this.getManifest(owner, schemaId, name);
|
|
79
|
+
if (!manifest) throw new Error(`No manifest found for owner ${owner} / schemaId ${schemaId} / name ${name}`);
|
|
80
|
+
const entry = manifest.entries.find((e) => e.name === name);
|
|
81
|
+
if (!entry) throw new Error(`Entry not found: "${name}"`);
|
|
102
82
|
return entry;
|
|
103
83
|
}
|
|
104
|
-
async isRegistered(owner, schemaId,
|
|
105
|
-
const resourceId = this.deriveResourceId(owner, schemaId,
|
|
84
|
+
async isRegistered(owner, schemaId, name, identity) {
|
|
85
|
+
const resourceId = this.deriveResourceId(owner, schemaId, name);
|
|
106
86
|
return this.settlementRegistry.isRegistered(resourceId, identity.commitment);
|
|
107
87
|
}
|
|
108
|
-
deriveResourceId(owner, schemaId,
|
|
109
|
-
return
|
|
88
|
+
deriveResourceId(owner, schemaId, name) {
|
|
89
|
+
return DataSourceRegistry.resourceIdLocal(owner, schemaId, name);
|
|
110
90
|
}
|
|
111
91
|
};
|
|
112
92
|
|
|
@@ -8,7 +8,7 @@ import { Identity } from "@semaphore-protocol/identity";
|
|
|
8
8
|
interface PurchaseParams {
|
|
9
9
|
owner: Address;
|
|
10
10
|
schemaId: Hex;
|
|
11
|
-
|
|
11
|
+
name: string;
|
|
12
12
|
identityCommitment: bigint;
|
|
13
13
|
relayerPrivateKey: Hex;
|
|
14
14
|
preparedRegister: TransferWithAuthPayload;
|
|
@@ -16,7 +16,7 @@ interface PurchaseParams {
|
|
|
16
16
|
interface ClaimParams {
|
|
17
17
|
owner: Address;
|
|
18
18
|
schemaId: Hex;
|
|
19
|
-
|
|
19
|
+
name: string;
|
|
20
20
|
relayerPrivateKey: Hex;
|
|
21
21
|
preparedSettle: PrepareSettleResult;
|
|
22
22
|
}
|
|
@@ -26,7 +26,7 @@ interface DecryptParams {
|
|
|
26
26
|
schemaId: Hex;
|
|
27
27
|
nullifierHash: bigint;
|
|
28
28
|
/** The record tag — maps to the resourceId */
|
|
29
|
-
|
|
29
|
+
name: string;
|
|
30
30
|
/** The specific encrypted field within the record to decrypt */
|
|
31
31
|
field: string;
|
|
32
32
|
identity?: Identity;
|
|
@@ -40,7 +40,7 @@ interface DecryptParams {
|
|
|
40
40
|
interface AccessParams {
|
|
41
41
|
owner: Address;
|
|
42
42
|
schemaId: Hex;
|
|
43
|
-
|
|
43
|
+
name: string;
|
|
44
44
|
/** The encrypted field to decrypt after settling */
|
|
45
45
|
field: string;
|
|
46
46
|
identity: Identity;
|