@fangorn-network/sdk 2026.8.12-dev → 2026.9.22-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.
Files changed (40) hide show
  1. package/README.md +175 -62
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +348 -17
  4. package/lib/cli/index.d.ts +13 -13
  5. package/lib/config.d.ts +28 -352
  6. package/lib/config.js +11 -20
  7. package/lib/contracts/app-registry/abi.d.ts +509 -0
  8. package/lib/contracts/app-registry/abi.js +575 -0
  9. package/lib/contracts/app-registry/index.d.ts +120 -0
  10. package/lib/contracts/app-registry/index.js +266 -0
  11. package/lib/contracts/data-registry/abi.d.ts +124 -125
  12. package/lib/contracts/data-registry/abi.js +145 -142
  13. package/lib/contracts/data-registry/index.d.ts +20 -27
  14. package/lib/contracts/data-registry/index.js +32 -78
  15. package/lib/contracts/index.d.ts +8 -3
  16. package/lib/contracts/index.js +8 -3
  17. package/lib/contracts/logs.d.ts +18 -0
  18. package/lib/contracts/logs.js +25 -0
  19. package/lib/contracts/settlement-registry/abi.d.ts +430 -11
  20. package/lib/contracts/settlement-registry/abi.js +528 -41
  21. package/lib/contracts/settlement-registry/index.d.ts +148 -10
  22. package/lib/contracts/settlement-registry/index.js +233 -9
  23. package/lib/contracts/subscription-registry/abi.d.ts +179 -0
  24. package/lib/contracts/subscription-registry/abi.js +195 -0
  25. package/lib/contracts/subscription-registry/index.d.ts +59 -0
  26. package/lib/contracts/subscription-registry/index.js +116 -0
  27. package/lib/contracts/types.d.ts +8 -0
  28. package/lib/contracts/types.js +10 -0
  29. package/lib/contracts/write.d.ts +27 -0
  30. package/lib/contracts/write.js +55 -0
  31. package/lib/crypto/encryption.js +4 -4
  32. package/lib/engine/index.d.ts +14 -12
  33. package/lib/engine/index.js +13 -7
  34. package/lib/fangorn.d.ts +71 -6
  35. package/lib/fangorn.js +86 -7
  36. package/lib/index.d.ts +9 -4
  37. package/lib/index.js +9 -4
  38. package/lib/test/testbed.js +9 -3
  39. package/lib/types/index.d.ts +6 -0
  40. package/package.json +73 -85
@@ -0,0 +1,266 @@
1
+ import { APP_REGISTRY_ABI } from "./abi.js";
2
+ import { requireWallet, sendWrite } from "../write.js";
3
+ import { getLogsInWindows } from "../logs.js";
4
+ import { PublisherStatus } from "../types.js";
5
+ import { encodeFunctionData, toHex } from "viem";
6
+
7
+ //#region src/contracts/app-registry/index.ts
8
+ /** True when the publisher joined but the app's terms have since changed. */
9
+ function needsReacceptance(info) {
10
+ return !info.registered && info.status === PublisherStatus.ACTIVE && info.acceptedTerms !== info.termsHash;
11
+ }
12
+ /**
13
+ * Client for the AppRegistry
14
+ */
15
+ var AppRegistryClient = class {
16
+ constructor(contractAddress, appId, publicClient, walletClient) {
17
+ this.contractAddress = contractAddress;
18
+ this.appId = appId;
19
+ this.publicClient = publicClient;
20
+ this.walletClient = walletClient;
21
+ }
22
+ getAppId() {
23
+ return this.appId;
24
+ }
25
+ setAppId(appId) {
26
+ this.appId = appId;
27
+ }
28
+ getAddress() {
29
+ return this.contractAddress;
30
+ }
31
+ executeWrite(functionName, args, value) {
32
+ return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, APP_REGISTRY_ABI, functionName, args, value);
33
+ }
34
+ /**
35
+ * try to register an app (if it has not been claimed)
36
+ */
37
+ async registerApp(termsHash, termsUri, fee = 0n) {
38
+ return this.executeWrite("registerApp", [
39
+ this.appId,
40
+ termsHash,
41
+ termsUri,
42
+ fee
43
+ ]);
44
+ }
45
+ /**
46
+ * Publish new terms for this app.
47
+ *
48
+ * Every registered must accept the new terms in order to publish again
49
+ */
50
+ async setAppTerms(termsHash, termsUri) {
51
+ return this.executeWrite("setAppTerms", [
52
+ this.appId,
53
+ termsHash,
54
+ termsUri
55
+ ]);
56
+ }
57
+ /**
58
+ * Point this app at its ERC-8004 agent card.
59
+ *
60
+ * Safe to call whenever the card moves. It is a separate field from the terms
61
+ * URI for one reason: publishers are bound to `termsHash`, so anything sharing
62
+ * that slot unregisters every publisher in the app the moment it changes (see
63
+ * `needsReacceptance`). A card holds endpoints and pubkeys, which rotate.
64
+ */
65
+ async setAppAgentUri(agentUri) {
66
+ return this.executeWrite("setAppAgentUri", [this.appId, agentUri]);
67
+ }
68
+ async setAppFee(fee) {
69
+ return this.executeWrite("setAppFee", [this.appId, fee]);
70
+ }
71
+ /**
72
+ * Suspend a publisher from this app
73
+ * only callable by the app owner
74
+ */
75
+ async suspendForApp(publisher) {
76
+ return this.executeWrite("suspendForApp", [this.appId, publisher]);
77
+ }
78
+ async reinstateForApp(publisher) {
79
+ return this.executeWrite("reinstateForApp", [this.appId, publisher]);
80
+ }
81
+ /**
82
+ * Suspend an app and all publish capabilities
83
+ */
84
+ async suspendApp() {
85
+ return this.executeWrite("suspendApp", [this.appId]);
86
+ }
87
+ async reinstateApp() {
88
+ return this.executeWrite("reinstateApp", [this.appId]);
89
+ }
90
+ async admin() {
91
+ return this.publicClient.readContract({
92
+ address: this.contractAddress,
93
+ abi: APP_REGISTRY_ABI,
94
+ functionName: "admin"
95
+ });
96
+ }
97
+ /**
98
+ * Register as a publisher within an app
99
+ * Registration = accepting its current terms
100
+ */
101
+ async registerForApp() {
102
+ const [termsHash, , fee] = await this.readJoinInfoTuple(this.senderAddress());
103
+ if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
104
+ return this.executeWrite("registerForApp", [this.appId, termsHash], fee);
105
+ }
106
+ /**
107
+ * prepare the tx to register as a publisher for an app
108
+ */
109
+ async prepareRegisterForApp(publisher) {
110
+ const [termsHash, , fee] = await this.readJoinInfoTuple(publisher);
111
+ if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
112
+ const data = encodeFunctionData({
113
+ abi: APP_REGISTRY_ABI,
114
+ functionName: "registerForApp",
115
+ args: [this.appId, termsHash]
116
+ });
117
+ const fees = await this.publicClient.estimateFeesPerGas();
118
+ let gas;
119
+ try {
120
+ gas = await this.publicClient.estimateGas({
121
+ account: publisher,
122
+ to: this.contractAddress,
123
+ data,
124
+ value: fee
125
+ }) * 3n / 2n;
126
+ } catch (err) {
127
+ const message = err instanceof Error ? err.message : String(err);
128
+ if (/revert/i.test(message)) throw new Error(`joining would revert — this wallet may already be registered, or suspended from this app: ${message}`);
129
+ gas = 1000000n;
130
+ }
131
+ return {
132
+ to: this.contractAddress,
133
+ data,
134
+ chainId: this.publicClient.chain?.id,
135
+ gas: toHex(gas),
136
+ maxFeePerGas: toHex(fees.maxFeePerGas * 2n),
137
+ maxPriorityFeePerGas: toHex(fees.maxPriorityFeePerGas),
138
+ value: toHex(fee)
139
+ };
140
+ }
141
+ async getAppOwner() {
142
+ return this.publicClient.readContract({
143
+ address: this.contractAddress,
144
+ abi: APP_REGISTRY_ABI,
145
+ functionName: "getAppOwner",
146
+ args: [this.appId]
147
+ });
148
+ }
149
+ async isRegisteredForApp(publisher) {
150
+ return this.publicClient.readContract({
151
+ address: this.contractAddress,
152
+ abi: APP_REGISTRY_ABI,
153
+ functionName: "isRegisteredForApp",
154
+ args: [this.appId, publisher]
155
+ });
156
+ }
157
+ async isAppSuspended() {
158
+ return this.publicClient.readContract({
159
+ address: this.contractAddress,
160
+ abi: APP_REGISTRY_ABI,
161
+ functionName: "isAppSuspended",
162
+ args: [this.appId]
163
+ });
164
+ }
165
+ async appTerms() {
166
+ return this.publicClient.readContract({
167
+ address: this.contractAddress,
168
+ abi: APP_REGISTRY_ABI,
169
+ functionName: "appTerms",
170
+ args: [this.appId]
171
+ });
172
+ }
173
+ async appTermsUri() {
174
+ return this.publicClient.readContract({
175
+ address: this.contractAddress,
176
+ abi: APP_REGISTRY_ABI,
177
+ functionName: "appTermsUri",
178
+ args: [this.appId]
179
+ });
180
+ }
181
+ /** The agent card URI of this app, or of `appId`. Empty if none is set. */
182
+ async appAgentUri(appId = this.appId) {
183
+ return this.publicClient.readContract({
184
+ address: this.contractAddress,
185
+ abi: APP_REGISTRY_ABI,
186
+ functionName: "appAgentUri",
187
+ args: [appId]
188
+ });
189
+ }
190
+ /**
191
+ * `AppAgentChanged` events, oldest first. The last one for an app is its
192
+ * current card; an empty `agentUri` means the owner unset it.
193
+ *
194
+ * Leave `appId` out to list every app that has ever set a card. The chain is
195
+ * the directory: one windowed log scan, no indexer and no registry namespace.
196
+ * `fromBlock` is required because scanning from genesis on an L2 costs
197
+ * hundreds of thousands of RPC calls. Pass the AppRegistry's deploy block, or
198
+ * a block you know to be before the app's registration.
199
+ */
200
+ async getAppAgentLogs(opts) {
201
+ return (await getLogsInWindows(this.publicClient, opts.fromBlock, opts.toBlock, (from, to) => this.publicClient.getContractEvents({
202
+ address: this.contractAddress,
203
+ abi: APP_REGISTRY_ABI,
204
+ eventName: "AppAgentChanged",
205
+ args: opts.appId ? { app_id: opts.appId } : {},
206
+ fromBlock: from,
207
+ toBlock: to,
208
+ strict: true
209
+ }))).map((log) => ({
210
+ appId: log.args.app_id,
211
+ agentUri: log.args.agent_uri,
212
+ blockNumber: log.blockNumber,
213
+ transactionHash: log.transactionHash
214
+ }));
215
+ }
216
+ async appFee() {
217
+ return this.publicClient.readContract({
218
+ address: this.contractAddress,
219
+ abi: APP_REGISTRY_ABI,
220
+ functionName: "appFee",
221
+ args: [this.appId]
222
+ });
223
+ }
224
+ async acceptedTerms(publisher) {
225
+ return this.publicClient.readContract({
226
+ address: this.contractAddress,
227
+ abi: APP_REGISTRY_ABI,
228
+ functionName: "acceptedTerms",
229
+ args: [this.appId, publisher]
230
+ });
231
+ }
232
+ async joinInfo(publisher) {
233
+ const [tuple, acceptedTerms, appSuspended] = await Promise.all([
234
+ this.readJoinInfoTuple(publisher),
235
+ this.acceptedTerms(publisher),
236
+ this.isAppSuspended()
237
+ ]);
238
+ const [termsHash, termsUri, fee, status, registered] = tuple;
239
+ return {
240
+ termsHash,
241
+ termsUri,
242
+ fee,
243
+ status,
244
+ registered,
245
+ acceptedTerms,
246
+ appSuspended
247
+ };
248
+ }
249
+ async readJoinInfoTuple(publisher) {
250
+ return this.publicClient.readContract({
251
+ address: this.contractAddress,
252
+ abi: APP_REGISTRY_ABI,
253
+ functionName: "joinInfo",
254
+ args: [this.appId, publisher]
255
+ });
256
+ }
257
+ senderAddress() {
258
+ const account = this.walletClient.account;
259
+ if (!account) throw new Error("Account required");
260
+ return account.address;
261
+ }
262
+ };
263
+ const ZERO_HASH = `0x${"0".repeat(64)}`;
264
+
265
+ //#endregion
266
+ export { AppRegistryClient, needsReacceptance };
@@ -5,15 +5,11 @@ declare const DATA_REGISTRY_ABI: readonly [{
5
5
  readonly type: "error";
6
6
  }, {
7
7
  readonly inputs: readonly [];
8
- readonly name: "AppAlreadyRegistered";
9
- readonly type: "error";
10
- }, {
11
- readonly inputs: readonly [];
12
- readonly name: "AppNotFound";
8
+ readonly name: "NotRegistered";
13
9
  readonly type: "error";
14
10
  }, {
15
11
  readonly inputs: readonly [];
16
- readonly name: "NotRegistered";
12
+ readonly name: "NotRegisteredForApp";
17
13
  readonly type: "error";
18
14
  }, {
19
15
  readonly inputs: readonly [];
@@ -32,108 +28,18 @@ declare const DATA_REGISTRY_ABI: readonly [{
32
28
  readonly name: "Unauthorized";
33
29
  readonly type: "error";
34
30
  }, {
35
- readonly anonymous: false;
36
- readonly inputs: readonly [{
37
- readonly indexed: true;
38
- readonly internalType: "bytes32";
39
- readonly name: "app_id";
40
- readonly type: "bytes32";
41
- }, {
42
- readonly indexed: true;
43
- readonly internalType: "address";
44
- readonly name: "owner";
45
- readonly type: "address";
46
- }];
47
- readonly name: "AppRegistered";
48
- readonly type: "event";
49
- }, {
50
- readonly anonymous: false;
51
- readonly inputs: readonly [{
52
- readonly indexed: true;
53
- readonly internalType: "address";
54
- readonly name: "publisher";
55
- readonly type: "address";
56
- }, {
57
- readonly indexed: false;
58
- readonly internalType: "bytes32";
59
- readonly name: "initial_root";
60
- readonly type: "bytes32";
61
- }];
62
- readonly name: "PublisherRegistered";
63
- readonly type: "event";
64
- }, {
65
- readonly anonymous: false;
66
- readonly inputs: readonly [{
67
- readonly indexed: true;
68
- readonly internalType: "address";
69
- readonly name: "publisher";
70
- readonly type: "address";
71
- }, {
72
- readonly indexed: false;
73
- readonly internalType: "bytes32";
74
- readonly name: "current_root";
75
- readonly type: "bytes32";
76
- }];
77
- readonly name: "PublisherReactivated";
78
- readonly type: "event";
79
- }, {
80
- readonly anonymous: false;
81
- readonly inputs: readonly [{
82
- readonly indexed: true;
83
- readonly internalType: "address";
84
- readonly name: "publisher";
85
- readonly type: "address";
86
- }];
87
- readonly name: "PublisherSuspended";
88
- readonly type: "event";
89
- }, {
90
- readonly anonymous: false;
91
- readonly inputs: readonly [{
92
- readonly indexed: false;
93
- readonly internalType: "uint256";
94
- readonly name: "fee";
95
- readonly type: "uint256";
96
- }];
97
- readonly name: "RegistrationFeeChanged";
98
- readonly type: "event";
99
- }, {
100
- readonly anonymous: false;
101
- readonly inputs: readonly [{
102
- readonly indexed: true;
103
- readonly internalType: "bytes32";
104
- readonly name: "namespace_key";
105
- readonly type: "bytes32";
106
- }, {
107
- readonly indexed: true;
108
- readonly internalType: "bytes32";
109
- readonly name: "app_id";
110
- readonly type: "bytes32";
111
- }, {
112
- readonly indexed: true;
31
+ readonly inputs: readonly [];
32
+ readonly name: "admin";
33
+ readonly outputs: readonly [{
113
34
  readonly internalType: "address";
114
- readonly name: "publisher";
35
+ readonly name: "";
115
36
  readonly type: "address";
116
- }, {
117
- readonly indexed: false;
118
- readonly internalType: "bytes32";
119
- readonly name: "subspace_id";
120
- readonly type: "bytes32";
121
- }, {
122
- readonly indexed: false;
123
- readonly internalType: "bytes32";
124
- readonly name: "old_root";
125
- readonly type: "bytes32";
126
- }, {
127
- readonly indexed: false;
128
- readonly internalType: "bytes32";
129
- readonly name: "new_root";
130
- readonly type: "bytes32";
131
37
  }];
132
- readonly name: "StateCommitted";
133
- readonly type: "event";
38
+ readonly stateMutability: "view";
39
+ readonly type: "function";
134
40
  }, {
135
41
  readonly inputs: readonly [];
136
- readonly name: "admin";
42
+ readonly name: "appRegistry";
137
43
  readonly outputs: readonly [{
138
44
  readonly internalType: "address";
139
45
  readonly name: "";
@@ -163,20 +69,6 @@ declare const DATA_REGISTRY_ABI: readonly [{
163
69
  readonly outputs: readonly [];
164
70
  readonly stateMutability: "nonpayable";
165
71
  readonly type: "function";
166
- }, {
167
- readonly inputs: readonly [{
168
- readonly internalType: "bytes32";
169
- readonly name: "app_id";
170
- readonly type: "bytes32";
171
- }];
172
- readonly name: "getAppOwner";
173
- readonly outputs: readonly [{
174
- readonly internalType: "address";
175
- readonly name: "";
176
- readonly type: "address";
177
- }];
178
- readonly stateMutability: "view";
179
- readonly type: "function";
180
72
  }, {
181
73
  readonly inputs: readonly [{
182
74
  readonly internalType: "bytes32";
@@ -243,25 +135,47 @@ declare const DATA_REGISTRY_ABI: readonly [{
243
135
  readonly outputs: readonly [];
244
136
  readonly stateMutability: "payable";
245
137
  readonly type: "function";
138
+ }, {
139
+ readonly inputs: readonly [];
140
+ readonly name: "registrationFee";
141
+ readonly outputs: readonly [{
142
+ readonly internalType: "uint256";
143
+ readonly name: "";
144
+ readonly type: "uint256";
145
+ }];
146
+ readonly stateMutability: "view";
147
+ readonly type: "function";
246
148
  }, {
247
149
  readonly inputs: readonly [{
248
150
  readonly internalType: "bytes32";
249
151
  readonly name: "app_id";
250
152
  readonly type: "bytes32";
153
+ }, {
154
+ readonly internalType: "address";
155
+ readonly name: "publisher";
156
+ readonly type: "address";
157
+ }, {
158
+ readonly internalType: "bytes32";
159
+ readonly name: "subspace_id";
160
+ readonly type: "bytes32";
161
+ }, {
162
+ readonly internalType: "bytes32";
163
+ readonly name: "root";
164
+ readonly type: "bytes32";
251
165
  }];
252
- readonly name: "registerApp";
166
+ readonly name: "seedNamespaceHead";
253
167
  readonly outputs: readonly [];
254
168
  readonly stateMutability: "nonpayable";
255
169
  readonly type: "function";
256
170
  }, {
257
- readonly inputs: readonly [];
258
- readonly name: "registrationFee";
259
- readonly outputs: readonly [{
260
- readonly internalType: "uint256";
261
- readonly name: "";
262
- readonly type: "uint256";
171
+ readonly inputs: readonly [{
172
+ readonly internalType: "address";
173
+ readonly name: "registry";
174
+ readonly type: "address";
263
175
  }];
264
- readonly stateMutability: "view";
176
+ readonly name: "setAppRegistry";
177
+ readonly outputs: readonly [];
178
+ readonly stateMutability: "nonpayable";
265
179
  readonly type: "function";
266
180
  }, {
267
181
  readonly inputs: readonly [{
@@ -283,6 +197,91 @@ declare const DATA_REGISTRY_ABI: readonly [{
283
197
  readonly outputs: readonly [];
284
198
  readonly stateMutability: "nonpayable";
285
199
  readonly type: "function";
200
+ }, {
201
+ readonly anonymous: false;
202
+ readonly inputs: readonly [{
203
+ readonly indexed: true;
204
+ readonly internalType: "address";
205
+ readonly name: "publisher";
206
+ readonly type: "address";
207
+ }, {
208
+ readonly indexed: false;
209
+ readonly internalType: "bytes32";
210
+ readonly name: "initial_root";
211
+ readonly type: "bytes32";
212
+ }];
213
+ readonly name: "PublisherRegistered";
214
+ readonly type: "event";
215
+ }, {
216
+ readonly anonymous: false;
217
+ readonly inputs: readonly [{
218
+ readonly indexed: true;
219
+ readonly internalType: "address";
220
+ readonly name: "publisher";
221
+ readonly type: "address";
222
+ }, {
223
+ readonly indexed: false;
224
+ readonly internalType: "bytes32";
225
+ readonly name: "current_root";
226
+ readonly type: "bytes32";
227
+ }];
228
+ readonly name: "PublisherReactivated";
229
+ readonly type: "event";
230
+ }, {
231
+ readonly anonymous: false;
232
+ readonly inputs: readonly [{
233
+ readonly indexed: true;
234
+ readonly internalType: "address";
235
+ readonly name: "publisher";
236
+ readonly type: "address";
237
+ }];
238
+ readonly name: "PublisherSuspended";
239
+ readonly type: "event";
240
+ }, {
241
+ readonly anonymous: false;
242
+ readonly inputs: readonly [{
243
+ readonly indexed: false;
244
+ readonly internalType: "uint256";
245
+ readonly name: "fee";
246
+ readonly type: "uint256";
247
+ }];
248
+ readonly name: "RegistrationFeeChanged";
249
+ readonly type: "event";
250
+ }, {
251
+ readonly anonymous: false;
252
+ readonly inputs: readonly [{
253
+ readonly indexed: true;
254
+ readonly internalType: "bytes32";
255
+ readonly name: "namespace_key";
256
+ readonly type: "bytes32";
257
+ }, {
258
+ readonly indexed: true;
259
+ readonly internalType: "bytes32";
260
+ readonly name: "app_id";
261
+ readonly type: "bytes32";
262
+ }, {
263
+ readonly indexed: true;
264
+ readonly internalType: "address";
265
+ readonly name: "publisher";
266
+ readonly type: "address";
267
+ }, {
268
+ readonly indexed: false;
269
+ readonly internalType: "bytes32";
270
+ readonly name: "subspace_id";
271
+ readonly type: "bytes32";
272
+ }, {
273
+ readonly indexed: false;
274
+ readonly internalType: "bytes32";
275
+ readonly name: "old_root";
276
+ readonly type: "bytes32";
277
+ }, {
278
+ readonly indexed: false;
279
+ readonly internalType: "bytes32";
280
+ readonly name: "new_root";
281
+ readonly type: "bytes32";
282
+ }];
283
+ readonly name: "StateCommitted";
284
+ readonly type: "event";
286
285
  }];
287
286
  //#endregion
288
287
  export { DATA_REGISTRY_ABI };