@fangorn-network/sdk 2026.8.12-dev → 2026.8.18-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 (37) hide show
  1. package/README.md +175 -62
  2. package/lib/cli/cli.d.ts +0 -0
  3. package/lib/cli/cli.js +290 -17
  4. package/lib/config.d.ts +9 -8
  5. package/lib/config.js +6 -8
  6. package/lib/contracts/app-registry/abi.d.ts +466 -0
  7. package/lib/contracts/app-registry/abi.js +529 -0
  8. package/lib/contracts/app-registry/index.d.ts +139 -0
  9. package/lib/contracts/app-registry/index.js +271 -0
  10. package/lib/contracts/data-registry/abi.d.ts +124 -125
  11. package/lib/contracts/data-registry/abi.js +145 -142
  12. package/lib/contracts/data-registry/index.d.ts +16 -14
  13. package/lib/contracts/data-registry/index.js +19 -50
  14. package/lib/contracts/index.d.ts +8 -3
  15. package/lib/contracts/index.js +8 -3
  16. package/lib/contracts/settlement-registry/abi.d.ts +430 -11
  17. package/lib/contracts/settlement-registry/abi.js +528 -41
  18. package/lib/contracts/settlement-registry/index.d.ts +148 -10
  19. package/lib/contracts/settlement-registry/index.js +233 -9
  20. package/lib/contracts/subscription-registry/abi.d.ts +179 -0
  21. package/lib/contracts/subscription-registry/abi.js +195 -0
  22. package/lib/contracts/subscription-registry/index.d.ts +59 -0
  23. package/lib/contracts/subscription-registry/index.js +116 -0
  24. package/lib/contracts/types.d.ts +8 -0
  25. package/lib/contracts/types.js +10 -0
  26. package/lib/contracts/write.d.ts +27 -0
  27. package/lib/contracts/write.js +55 -0
  28. package/lib/crypto/encryption.js +4 -4
  29. package/lib/engine/index.d.ts +14 -12
  30. package/lib/engine/index.js +13 -7
  31. package/lib/fangorn.d.ts +35 -5
  32. package/lib/fangorn.js +49 -6
  33. package/lib/index.d.ts +8 -3
  34. package/lib/index.js +8 -3
  35. package/lib/test/testbed.js +9 -3
  36. package/lib/types/index.d.ts +6 -0
  37. package/package.json +72 -85
@@ -0,0 +1,271 @@
1
+ import { APP_REGISTRY_ABI } from "./abi.js";
2
+ import { requireWallet, sendWrite } from "../write.js";
3
+ import { PublisherStatus } from "../types.js";
4
+ import { encodeFunctionData, toHex } from "viem";
5
+
6
+ //#region src/contracts/app-registry/index.ts
7
+ /** True when the publisher joined but the app's terms have since changed. */
8
+ function needsReacceptance(info) {
9
+ return !info.registered && info.status === PublisherStatus.ACTIVE && info.acceptedTerms !== info.termsHash;
10
+ }
11
+ /**
12
+ * Client for the AppRegistry — app ids, per-app publisher terms, per-app join
13
+ * fees, and per-app membership.
14
+ *
15
+ * This contract owns what an "app" is. `registerApp` and `getAppOwner` used to
16
+ * live on the DataRegistry, which meant two contracts held partial opinions about
17
+ * apps and neither could enforce membership. The dependency now runs one way:
18
+ * `DataRegistry.commitStateRoot` cross-calls `isRegisteredForApp` here, so a
19
+ * publisher must join an app *before* they can write under it.
20
+ *
21
+ * Scoped to one `appId`, like `DataRegistryClient` — the app is an identity the
22
+ * client carries, not an argument every call repeats.
23
+ */
24
+ var AppRegistryClient = class {
25
+ constructor(contractAddress, appId, publicClient, walletClient) {
26
+ this.contractAddress = contractAddress;
27
+ this.appId = appId;
28
+ this.publicClient = publicClient;
29
+ this.walletClient = walletClient;
30
+ }
31
+ /** The app every call on this client is scoped to. */
32
+ getAppId() {
33
+ return this.appId;
34
+ }
35
+ /** Re-scope every subsequent call to a different app. */
36
+ setAppId(appId) {
37
+ this.appId = appId;
38
+ }
39
+ /** The deployed AppRegistry this client talks to. */
40
+ getAddress() {
41
+ return this.contractAddress;
42
+ }
43
+ /** Same gas buffer and fee styling as DataRegistryClient.executeWrite. */
44
+ executeWrite(functionName, args, value) {
45
+ return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, APP_REGISTRY_ABI, functionName, args, value);
46
+ }
47
+ /**
48
+ * Claim this client's `appId` and open it in one transaction — owner, terms
49
+ * and join fee together. First come, first served, and irreversible: there is
50
+ * no transfer and no release.
51
+ *
52
+ * `termsHash` must be real. An app whose terms hash is zero cannot be joined
53
+ * by anyone, so claiming an id without them creates a market nobody can enter.
54
+ */
55
+ async registerApp(termsHash, termsUri, fee = 0n) {
56
+ return this.executeWrite("registerApp", [
57
+ this.appId,
58
+ termsHash,
59
+ termsUri,
60
+ fee
61
+ ]);
62
+ }
63
+ /**
64
+ * Publish new terms for this app.
65
+ *
66
+ * This drops every publisher back to "must accept again" — `isRegisteredForApp`
67
+ * is false for anyone whose accepted hash no longer matches — so it is a real
68
+ * act with a real consequence, not a copy edit. Re-accepting is free for them.
69
+ */
70
+ async setAppTerms(termsHash, termsUri) {
71
+ return this.executeWrite("setAppTerms", [
72
+ this.appId,
73
+ termsHash,
74
+ termsUri
75
+ ]);
76
+ }
77
+ /** Set what joining this app costs, in wei. Paid to the app owner. */
78
+ async setAppFee(fee) {
79
+ return this.executeWrite("setAppFee", [this.appId, fee]);
80
+ }
81
+ /**
82
+ * Eject a publisher from THIS app. Their global DataRegistry standing and
83
+ * their membership of every other app are untouched.
84
+ */
85
+ async suspendForApp(publisher) {
86
+ return this.executeWrite("suspendForApp", [this.appId, publisher]);
87
+ }
88
+ async reinstateForApp(publisher) {
89
+ return this.executeWrite("reinstateForApp", [this.appId, publisher]);
90
+ }
91
+ /**
92
+ * Take this whole app down. Protocol-admin only, and a level above
93
+ * `suspendForApp`: every publisher of the app — its owner included — reads as
94
+ * unregistered, so nothing commits under it until it is reinstated. The
95
+ * memberships underneath are left intact, so reinstating restores them exactly.
96
+ */
97
+ async suspendApp() {
98
+ return this.executeWrite("suspendApp", [this.appId]);
99
+ }
100
+ /** Lift an admin takedown, restoring every membership as it was. */
101
+ async reinstateApp() {
102
+ return this.executeWrite("reinstateApp", [this.appId]);
103
+ }
104
+ /** The protocol admin of this AppRegistry — the only caller `suspendApp` accepts. */
105
+ async admin() {
106
+ return this.publicClient.readContract({
107
+ address: this.contractAddress,
108
+ abi: APP_REGISTRY_ABI,
109
+ functionName: "admin"
110
+ });
111
+ }
112
+ /**
113
+ * Join this app, accepting its current terms by the act of doing so.
114
+ *
115
+ * The hash is read immediately before sending and passed as an argument, and
116
+ * the contract reverts `TermsMismatch` if it no longer matches. That is the
117
+ * point: the transaction is only valid against the exact version that was in
118
+ * front of the caller, so terms cannot change under a pending registration and
119
+ * land as agreement to something else. A race here is a revert, not a
120
+ * surprise obligation.
121
+ *
122
+ * The join fee is read from the chain rather than passed in, for the same
123
+ * reason `DataRegistryClient.register()` reads the registration fee: a caller
124
+ * who supplies it can supply the wrong one.
125
+ */
126
+ async registerForApp() {
127
+ const [termsHash, , fee] = await this.readJoinInfoTuple(this.senderAddress());
128
+ if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
129
+ return this.executeWrite("registerForApp", [this.appId, termsHash], fee);
130
+ }
131
+ /**
132
+ * The same join, as an unsigned transaction for a wallet that signs elsewhere
133
+ * (the sond3r relay never holds a key). Mirrors `prepareCommitStateRoot`.
134
+ */
135
+ async prepareRegisterForApp(publisher) {
136
+ const [termsHash, , fee] = await this.readJoinInfoTuple(publisher);
137
+ if (termsHash === ZERO_HASH) throw new Error(`app ${this.appId} has published no terms — it cannot be joined until its owner sets them`);
138
+ const data = encodeFunctionData({
139
+ abi: APP_REGISTRY_ABI,
140
+ functionName: "registerForApp",
141
+ args: [this.appId, termsHash]
142
+ });
143
+ const fees = await this.publicClient.estimateFeesPerGas();
144
+ let gas;
145
+ try {
146
+ gas = await this.publicClient.estimateGas({
147
+ account: publisher,
148
+ to: this.contractAddress,
149
+ data,
150
+ value: fee
151
+ }) * 3n / 2n;
152
+ } catch (err) {
153
+ const message = err instanceof Error ? err.message : String(err);
154
+ if (/revert/i.test(message)) throw new Error(`joining would revert — this wallet may already be registered, or suspended from this app: ${message}`);
155
+ gas = 1000000n;
156
+ }
157
+ return {
158
+ to: this.contractAddress,
159
+ data,
160
+ chainId: this.publicClient.chain?.id,
161
+ gas: toHex(gas),
162
+ maxFeePerGas: toHex(fees.maxFeePerGas * 2n),
163
+ maxPriorityFeePerGas: toHex(fees.maxPriorityFeePerGas),
164
+ value: toHex(fee)
165
+ };
166
+ }
167
+ /** Owner of this client's app id, or the zero address if unclaimed. */
168
+ async getAppOwner() {
169
+ return this.publicClient.readContract({
170
+ address: this.contractAddress,
171
+ abi: APP_REGISTRY_ABI,
172
+ functionName: "getAppOwner",
173
+ args: [this.appId]
174
+ });
175
+ }
176
+ /**
177
+ * May this publisher commit under this app? This is the exact question
178
+ * `DataRegistry.commitStateRoot` asks on-chain, so a false here means a
179
+ * publish would revert.
180
+ */
181
+ async isRegisteredForApp(publisher) {
182
+ return this.publicClient.readContract({
183
+ address: this.contractAddress,
184
+ abi: APP_REGISTRY_ABI,
185
+ functionName: "isRegisteredForApp",
186
+ args: [this.appId, publisher]
187
+ });
188
+ }
189
+ /** Has the protocol admin taken this app down? */
190
+ async isAppSuspended() {
191
+ return this.publicClient.readContract({
192
+ address: this.contractAddress,
193
+ abi: APP_REGISTRY_ABI,
194
+ functionName: "isAppSuspended",
195
+ args: [this.appId]
196
+ });
197
+ }
198
+ /** This app's current terms hash, or the zero hash if it has published none. */
199
+ async appTerms() {
200
+ return this.publicClient.readContract({
201
+ address: this.contractAddress,
202
+ abi: APP_REGISTRY_ABI,
203
+ functionName: "appTerms",
204
+ args: [this.appId]
205
+ });
206
+ }
207
+ async appTermsUri() {
208
+ return this.publicClient.readContract({
209
+ address: this.contractAddress,
210
+ abi: APP_REGISTRY_ABI,
211
+ functionName: "appTermsUri",
212
+ args: [this.appId]
213
+ });
214
+ }
215
+ async appFee() {
216
+ return this.publicClient.readContract({
217
+ address: this.contractAddress,
218
+ abi: APP_REGISTRY_ABI,
219
+ functionName: "appFee",
220
+ args: [this.appId]
221
+ });
222
+ }
223
+ /** The terms hash this publisher accepted, or the zero hash. */
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
+ /**
233
+ * Everything a join screen needs. Two reads rather than one, because the
234
+ * accepted hash is what turns "not registered" into the far more useful "the
235
+ * terms changed since you joined".
236
+ */
237
+ async joinInfo(publisher) {
238
+ const [tuple, acceptedTerms, appSuspended] = await Promise.all([
239
+ this.readJoinInfoTuple(publisher),
240
+ this.acceptedTerms(publisher),
241
+ this.isAppSuspended()
242
+ ]);
243
+ const [termsHash, termsUri, fee, status, registered] = tuple;
244
+ return {
245
+ termsHash,
246
+ termsUri,
247
+ fee,
248
+ status,
249
+ registered,
250
+ acceptedTerms,
251
+ appSuspended
252
+ };
253
+ }
254
+ async readJoinInfoTuple(publisher) {
255
+ return this.publicClient.readContract({
256
+ address: this.contractAddress,
257
+ abi: APP_REGISTRY_ABI,
258
+ functionName: "joinInfo",
259
+ args: [this.appId, publisher]
260
+ });
261
+ }
262
+ senderAddress() {
263
+ const account = this.walletClient.account;
264
+ if (!account) throw new Error("Account required");
265
+ return account.address;
266
+ }
267
+ };
268
+ const ZERO_HASH = `0x${"0".repeat(64)}`;
269
+
270
+ //#endregion
271
+ 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 };