@fangorn-network/sdk 2026.4.7 → 2026.4.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -150
- package/lib/cli/cli.js +134 -226
- 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 +3 -20
- package/lib/fangorn.js +15 -52
- package/lib/index.d.ts +3 -7
- package/lib/index.js +4 -7
- 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 -2
- package/lib/providers/storage/pinata.d.ts +14 -0
- package/lib/providers/storage/pinata.js +32 -0
- 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/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 +17 -42
- package/lib/roles/consumer/index.js +87 -58
- package/lib/roles/consumer/types.d.ts +15 -34
- package/lib/roles/publisher/index.d.ts +22 -40
- package/lib/roles/publisher/index.js +69 -113
- package/lib/roles/publisher/types.d.ts +40 -41
- package/lib/roles/schema/index.d.ts +4 -30
- package/lib/roles/schema/index.js +7 -60
- package/lib/roles/schema/types.d.ts +16 -2
- package/lib/test/testbed.d.ts +12 -36
- package/lib/test/testbed.js +35 -68
- package/lib/types/index.d.ts +14 -57
- package/package.json +1 -6
- package/lib/modules/encryption/lit.d.ts +0 -21
- package/lib/modules/encryption/lit.js +0 -127
- 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 -31
- package/lib/modules/gadgets/settledGadget.js +0 -75
- package/lib/modules/gadgets/types.d.ts +0 -27
- 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/{modules/gadgets → providers/storage}/types.js +0 -0
|
@@ -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,63 +1,38 @@
|
|
|
1
|
-
import { ReadableStorage } from "../../providers/storage/index.js";
|
|
2
1
|
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
2
|
+
import { Manifest, 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
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { ClaimParams, ClaimResult, DecryptParams, PurchaseParams, PurchaseResult } from "./types.js";
|
|
8
|
-
import { Address, Hex } from "viem";
|
|
5
|
+
import { ClaimParams, ClaimResult, FetchParams, FetchResult, PurchaseParams, PurchaseResult } from "./types.js";
|
|
6
|
+
import { Address, Hex, WalletClient } from "viem";
|
|
9
7
|
import { Identity } from "@semaphore-protocol/identity";
|
|
10
8
|
|
|
11
9
|
//#region src/roles/consumer/index.d.ts
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The consumer namespace encapsulates all functionality relative to accessing and consuming data.
|
|
15
|
-
*/
|
|
16
10
|
declare class ConsumerRole {
|
|
17
11
|
private readonly dataSourceRegistry;
|
|
18
12
|
private readonly settlementRegistry;
|
|
19
|
-
|
|
20
|
-
private readonly encryptionService;
|
|
21
|
-
private readonly domain;
|
|
22
|
-
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry, storage: ReadableStorage<unknown>, encryptionService: EncryptionService, domain: string);
|
|
23
|
-
/**
|
|
24
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
13
|
+
constructor(dataSourceRegistry: DataSourceRegistry, settlementRegistry: SettlementRegistry);
|
|
28
14
|
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
15
|
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
16
|
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
17
|
claim(params: ClaimParams): Promise<ClaimResult>;
|
|
48
18
|
/**
|
|
49
|
-
*
|
|
19
|
+
* Fetch a handle field's content from the Fangorn access worker.
|
|
50
20
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
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.
|
|
54
24
|
*
|
|
55
25
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
56
26
|
*/
|
|
57
|
-
|
|
58
|
-
getManifest(owner: Address, schemaId: Hex): Promise<Manifest | undefined>;
|
|
59
|
-
getEntry(owner: Address, schemaId: Hex,
|
|
60
|
-
|
|
27
|
+
fetch(params: FetchParams): Promise<FetchResult>;
|
|
28
|
+
getManifest(owner: Address, schemaId: Hex, name: string): Promise<Manifest | undefined>;
|
|
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>;
|
|
35
|
+
isRegistered(owner: Address, schemaId: Hex, name: string, identity: Identity): Promise<boolean>;
|
|
61
36
|
private deriveResourceId;
|
|
62
37
|
}
|
|
63
38
|
//#endregion
|
|
@@ -1,40 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DataSourceRegistry } from "../../registries/datasource-registry/index.js";
|
|
2
|
+
import { PinataBackend } from "../../providers/storage/pinata.js";
|
|
3
|
+
import "../../providers/storage/index.js";
|
|
4
|
+
import { encodePacked, keccak256 } from "viem";
|
|
2
5
|
|
|
3
6
|
//#region src/roles/consumer/index.ts
|
|
4
|
-
/**
|
|
5
|
-
* The consumer namespace encapsulates all functionality relative to accessing and consuming data.
|
|
6
|
-
*/
|
|
7
7
|
var ConsumerRole = class {
|
|
8
|
-
constructor(dataSourceRegistry, settlementRegistry
|
|
8
|
+
constructor(dataSourceRegistry, settlementRegistry) {
|
|
9
9
|
this.dataSourceRegistry = dataSourceRegistry;
|
|
10
10
|
this.settlementRegistry = settlementRegistry;
|
|
11
|
-
this.storage = storage;
|
|
12
|
-
this.encryptionService = encryptionService;
|
|
13
|
-
this.domain = domain;
|
|
14
11
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Builds the transfer with auth (ERC-3009) and signs it (EIP-712)
|
|
17
|
-
* @param params
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
12
|
async prepareRegister(params) {
|
|
21
13
|
return this.settlementRegistry.prepareTransferWithAuth(params);
|
|
22
14
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds the zkp to prove you're in a semaphore group
|
|
25
|
-
* @param params
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
15
|
async prepareSettle(params) {
|
|
29
16
|
return this.settlementRegistry.prepareSettle(params);
|
|
30
17
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Purchase the resource and register in the semaphore group (on success)
|
|
33
|
-
* @param params
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
18
|
async register(params) {
|
|
37
|
-
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.
|
|
19
|
+
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.name);
|
|
38
20
|
return {
|
|
39
21
|
txHash: await this.settlementRegistry.register({
|
|
40
22
|
resourceId,
|
|
@@ -45,14 +27,8 @@ var ConsumerRole = class {
|
|
|
45
27
|
resourceId
|
|
46
28
|
};
|
|
47
29
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Claim access to a resource (prove settlement)
|
|
50
|
-
* Must register first.
|
|
51
|
-
* @param params
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
30
|
async claim(params) {
|
|
55
|
-
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.
|
|
31
|
+
const resourceId = this.deriveResourceId(params.owner, params.schemaId, params.name);
|
|
56
32
|
const { hash, nullifier } = await this.settlementRegistry.settle({
|
|
57
33
|
relayerPrivateKey: params.relayerPrivateKey,
|
|
58
34
|
preparedSettle: params.preparedSettle
|
|
@@ -64,51 +40,104 @@ var ConsumerRole = class {
|
|
|
64
40
|
};
|
|
65
41
|
}
|
|
66
42
|
/**
|
|
67
|
-
*
|
|
43
|
+
* Fetch a handle field's content from the Fangorn access worker.
|
|
68
44
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
45
|
+
* The consumer signs { nullifier, resourceId, objectKey, timestamp } with
|
|
46
|
+
* their stealth address private key. The worker recovers the address,
|
|
47
|
+
* verifies is_settled() on-chain, and proxies the R2 bytes.
|
|
72
48
|
*
|
|
73
49
|
* Plain fields can be read freely via getEntry() without any of this.
|
|
74
50
|
*/
|
|
75
|
-
async
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
51
|
+
async fetch(params) {
|
|
52
|
+
const { nullifier, resourceId, objectKey, workerUrl, walletClient } = params;
|
|
53
|
+
const timestamp = Math.floor(Date.now() / 1e3);
|
|
54
|
+
const msgHash = keccak256(encodePacked([
|
|
55
|
+
"uint256",
|
|
56
|
+
"bytes32",
|
|
57
|
+
"string",
|
|
58
|
+
"uint64"
|
|
59
|
+
], [
|
|
60
|
+
BigInt(nullifier),
|
|
61
|
+
resourceId,
|
|
62
|
+
objectKey,
|
|
63
|
+
BigInt(timestamp)
|
|
64
|
+
]));
|
|
65
|
+
const account = walletClient.account;
|
|
66
|
+
if (!account) throw new Error("walletClient account must be defined");
|
|
67
|
+
const signature = await walletClient.signMessage({
|
|
68
|
+
message: { raw: msgHash },
|
|
69
|
+
account
|
|
70
|
+
});
|
|
71
|
+
const res = await globalThis.fetch(`${workerUrl}/access`, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { "Content-Type": "application/json" },
|
|
74
|
+
body: JSON.stringify({
|
|
75
|
+
nullifier,
|
|
76
|
+
resourceId,
|
|
77
|
+
objectKey,
|
|
78
|
+
timestamp,
|
|
79
|
+
signature
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
if (!res.ok) {
|
|
83
|
+
const data = await res.json().catch(() => ({}));
|
|
84
|
+
const message = data && typeof data === "object" && "error" in data && typeof data.error === "string" ? data.error : res.statusText;
|
|
85
|
+
throw new Error(`Worker fetch failed: ${message}`);
|
|
80
86
|
}
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return (await this.encryptionService.decrypt(encrypted, authContext)).data;
|
|
87
|
+
const buffer = await res.arrayBuffer();
|
|
88
|
+
return {
|
|
89
|
+
data: new Uint8Array(buffer),
|
|
90
|
+
contentType: res.headers.get("Content-Type") ?? "application/octet-stream"
|
|
91
|
+
};
|
|
87
92
|
}
|
|
88
|
-
async getManifest(owner, schemaId) {
|
|
93
|
+
async getManifest(owner, schemaId, name) {
|
|
89
94
|
try {
|
|
90
|
-
const ds = await this.dataSourceRegistry.
|
|
95
|
+
const ds = await this.dataSourceRegistry.get(owner, schemaId, name);
|
|
91
96
|
if (!ds.manifestCid || ds.manifestCid === "") return void 0;
|
|
92
|
-
return await
|
|
97
|
+
return await PinataBackend.getStatic(ds.manifestCid);
|
|
93
98
|
} catch {
|
|
94
99
|
return;
|
|
95
100
|
}
|
|
96
101
|
}
|
|
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: "${
|
|
102
|
+
async getEntry(owner, schemaId, name) {
|
|
103
|
+
const manifest = await this.getManifest(owner, schemaId, name);
|
|
104
|
+
if (!manifest) throw new Error(`No manifest found for owner ${owner} / schemaId ${schemaId} / name ${name}`);
|
|
105
|
+
const entry = manifest.entries.find((e) => e.name === name);
|
|
106
|
+
if (!entry) throw new Error(`Entry not found: "${name}"`);
|
|
102
107
|
return entry;
|
|
103
108
|
}
|
|
104
|
-
|
|
105
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Resolves the handle URI for a specific field
|
|
111
|
+
* and fetches it via the worker in one call.
|
|
112
|
+
*/
|
|
113
|
+
async fetchField(owner, schemaId, name, field, nullifier, walletClient) {
|
|
114
|
+
const fieldValue = (await this.getEntry(owner, schemaId, name)).fields[field];
|
|
115
|
+
if (!fieldValue || typeof fieldValue !== "object" || !("@type" in fieldValue)) throw new Error(`Field "${field}" is missing or is not a handle field`);
|
|
116
|
+
const handleField = fieldValue;
|
|
117
|
+
if (handleField["@type"] !== "handle") throw new Error(`Field "${field}" is not a handle field. Read it directly from the entry`);
|
|
118
|
+
const objectKey = parseObjectKey(handleField.uri);
|
|
119
|
+
const resourceId = this.deriveResourceId(owner, schemaId, name);
|
|
120
|
+
return this.fetch({
|
|
121
|
+
nullifier,
|
|
122
|
+
resourceId,
|
|
123
|
+
objectKey,
|
|
124
|
+
workerUrl: handleField.workerUrl,
|
|
125
|
+
walletClient
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async isRegistered(owner, schemaId, name, identity) {
|
|
129
|
+
const resourceId = this.deriveResourceId(owner, schemaId, name);
|
|
106
130
|
return this.settlementRegistry.isRegistered(resourceId, identity.commitment);
|
|
107
131
|
}
|
|
108
|
-
deriveResourceId(owner, schemaId,
|
|
109
|
-
return
|
|
132
|
+
deriveResourceId(owner, schemaId, name) {
|
|
133
|
+
return DataSourceRegistry.resourceIdLocal(owner, schemaId, name);
|
|
110
134
|
}
|
|
111
135
|
};
|
|
136
|
+
function parseObjectKey(uri) {
|
|
137
|
+
if (uri.startsWith("r2://")) return uri.slice(5);
|
|
138
|
+
if (uri.startsWith("ipfs://")) return uri.slice(7);
|
|
139
|
+
return uri;
|
|
140
|
+
}
|
|
112
141
|
|
|
113
142
|
//#endregion
|
|
114
143
|
export { ConsumerRole };
|