@fangorn-network/sdk 2026.7.28 → 2026.7.29-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.
- package/README.md +13 -3
- package/lib/cli/cli.js +67 -9
- package/lib/cli/index.d.ts +13 -13
- package/lib/config.d.ts +84 -79
- package/lib/config.js +10 -2
- package/lib/contracts/data-registry/index.d.ts +2 -0
- package/lib/contracts/data-registry/index.js +4 -0
- package/lib/fangorn.d.ts +67 -0
- package/lib/fangorn.js +102 -2
- package/lib/feed.d.ts +32 -0
- package/lib/feed.js +65 -0
- package/lib/index.d.ts +3 -2
- package/lib/index.js +3 -2
- package/lib/test/testbed.js +3 -6
- package/lib/types/index.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -357,9 +357,19 @@ fangorn.subscribeApp({ namespace: "reviews" });
|
|
|
357
357
|
fangorn.subscribeApp({ owner: "0x..." });
|
|
358
358
|
```
|
|
359
359
|
|
|
360
|
-
The app
|
|
361
|
-
|
|
362
|
-
|
|
360
|
+
The app is per-client state, not part of the network config — set it when you
|
|
361
|
+
create the client, or switch at runtime:
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
const fangorn = Fangorn.create({ privateKey, storage, appId: "my-app" });
|
|
365
|
+
fangorn.setAppId("my-other-app"); // by name, or by 32-byte app id
|
|
366
|
+
fangorn.getAppId(); // 0x… — what the registry keys on
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
It must be claimed once on-chain with
|
|
370
|
+
`fangorn.getDataRegistry().registerApp()` before any publisher can commit under
|
|
371
|
+
it. From the CLI: `fangorn set-app my-app` (persists it) then `fangorn
|
|
372
|
+
register-app`.
|
|
363
373
|
|
|
364
374
|
### Storage
|
|
365
375
|
|
package/lib/cli/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { FangornConfig } from "../config.js";
|
|
2
|
+
import { DEFAULT_APP, FangornConfig, toAppId } from "../config.js";
|
|
3
3
|
import { Fangorn } from "../fangorn.js";
|
|
4
4
|
import { handleCancel } from "./index.js";
|
|
5
5
|
import { privateKeyToAccount } from "viem/accounts";
|
|
@@ -16,6 +16,14 @@ const CONFIG_PATH = join(CONFIG_DIR, "config.json");
|
|
|
16
16
|
let _config = null;
|
|
17
17
|
let _account = null;
|
|
18
18
|
let _fangorn = null;
|
|
19
|
+
function readStoredConfig() {
|
|
20
|
+
return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
21
|
+
}
|
|
22
|
+
function writeStoredConfig(stored) {
|
|
23
|
+
if (!existsSync(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
|
|
24
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(stored, null, 2), "utf-8");
|
|
25
|
+
chmodSync(CONFIG_PATH, 384);
|
|
26
|
+
}
|
|
19
27
|
function loadConfig() {
|
|
20
28
|
if (_config) return _config;
|
|
21
29
|
const privateKey = process.env.ETH_PRIVATE_KEY;
|
|
@@ -23,11 +31,13 @@ function loadConfig() {
|
|
|
23
31
|
const pinataGateway = process.env.PINATA_GATEWAY;
|
|
24
32
|
const accessWorkerUrl = process.env.ACCESS_WORKER_URL;
|
|
25
33
|
const signedUrlWorkerUrl = process.env.SIGNED_URL_WORKER_URL;
|
|
34
|
+
const envAppId = process.env.FANGORN_APP_ID;
|
|
26
35
|
if (existsSync(CONFIG_PATH)) {
|
|
27
|
-
const stored =
|
|
36
|
+
const stored = readStoredConfig();
|
|
28
37
|
_config = {
|
|
29
38
|
privateKey: stored.privateKey,
|
|
30
39
|
cfg: FangornConfig,
|
|
40
|
+
appId: envAppId ?? stored.appId ?? DEFAULT_APP,
|
|
31
41
|
pinataJwt: stored.pinataJwt,
|
|
32
42
|
pinataGateway: stored.pinataGateway,
|
|
33
43
|
accessWorkerUrl: stored.accessWorkerUrl ?? "",
|
|
@@ -40,6 +50,7 @@ function loadConfig() {
|
|
|
40
50
|
_config = {
|
|
41
51
|
privateKey,
|
|
42
52
|
cfg: FangornConfig,
|
|
53
|
+
appId: envAppId ?? DEFAULT_APP,
|
|
43
54
|
pinataJwt: pinataJwt ?? "",
|
|
44
55
|
pinataGateway: pinataGateway ?? "",
|
|
45
56
|
accessWorkerUrl: accessWorkerUrl ?? "",
|
|
@@ -68,7 +79,8 @@ function getFangorn() {
|
|
|
68
79
|
privateKey: cfg.privateKey,
|
|
69
80
|
storage,
|
|
70
81
|
domain: "localhost",
|
|
71
|
-
config: cfg.cfg
|
|
82
|
+
config: cfg.cfg,
|
|
83
|
+
appId: cfg.appId
|
|
72
84
|
});
|
|
73
85
|
return _fangorn;
|
|
74
86
|
}
|
|
@@ -147,19 +159,65 @@ program.command("init").description("Configure your Fangorn credentials").action
|
|
|
147
159
|
placeholder: "https://pinata-url-provider.your-subdomain.workers.dev"
|
|
148
160
|
});
|
|
149
161
|
handleCancel(signedUrlWorkerUrl);
|
|
150
|
-
|
|
162
|
+
writeStoredConfig({
|
|
151
163
|
privateKey,
|
|
152
164
|
chainName: "Arbitrum Sepolia",
|
|
153
165
|
pinataJwt,
|
|
154
166
|
pinataGateway,
|
|
155
167
|
accessWorkerUrl,
|
|
156
|
-
signedUrlWorkerUrl
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
writeFileSync(CONFIG_PATH, JSON.stringify(stored, null, 2), "utf-8");
|
|
160
|
-
chmodSync(CONFIG_PATH, 384);
|
|
168
|
+
signedUrlWorkerUrl,
|
|
169
|
+
appId: existsSync(CONFIG_PATH) ? readStoredConfig().appId : void 0
|
|
170
|
+
});
|
|
161
171
|
outro(`Config saved to ${CONFIG_PATH}`);
|
|
162
172
|
});
|
|
173
|
+
program.command("set-app").description("Set the app (global namespace) this CLI publishes and reads under").argument("[app]", "App name or 32-byte app id (omit to show the current one)").action((app) => {
|
|
174
|
+
try {
|
|
175
|
+
if (!app) {
|
|
176
|
+
const current = loadConfig().appId;
|
|
177
|
+
console.log(`App: ${current}`);
|
|
178
|
+
console.log(`App id: ${toAppId(current)}`);
|
|
179
|
+
process.exit(0);
|
|
180
|
+
}
|
|
181
|
+
if (!existsSync(CONFIG_PATH)) throw new Error(`no config file at ${CONFIG_PATH} — run \`fangorn init\` first, or set FANGORN_APP_ID in the environment.`);
|
|
182
|
+
writeStoredConfig({
|
|
183
|
+
...readStoredConfig(),
|
|
184
|
+
appId: app
|
|
185
|
+
});
|
|
186
|
+
console.log(`App: ${app}`);
|
|
187
|
+
console.log(`App id: ${toAppId(app)}`);
|
|
188
|
+
if (process.env.FANGORN_APP_ID) console.log(`\nNote: FANGORN_APP_ID=${process.env.FANGORN_APP_ID} is set and overrides this.`);
|
|
189
|
+
console.log(`\nSaved to ${CONFIG_PATH}. Run \`fangorn register-app\` if nobody has claimed it yet.`);
|
|
190
|
+
process.exit(0);
|
|
191
|
+
} catch (err) {
|
|
192
|
+
console.error("Failed:", err.message);
|
|
193
|
+
process.exit(1);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
program.command("register-app").description("Claim the configured app (global namespace) on-chain").action(async () => {
|
|
197
|
+
try {
|
|
198
|
+
const self = getAccount().address;
|
|
199
|
+
const registry = getFangorn().getDataRegistry();
|
|
200
|
+
const s = spinner();
|
|
201
|
+
s.start("Checking app ownership...");
|
|
202
|
+
const owner = await registry.getAppOwner();
|
|
203
|
+
s.stop();
|
|
204
|
+
if (owner !== "0x0000000000000000000000000000000000000000") {
|
|
205
|
+
console.log(owner.toLowerCase() === self.toLowerCase() ? `Already registered to you: ${registry.getAppId()}` : `App ${registry.getAppId()} is already owned by ${owner}. Pick another name with \`fangorn set-app\`.`);
|
|
206
|
+
process.exit(owner.toLowerCase() === self.toLowerCase() ? 0 : 1);
|
|
207
|
+
}
|
|
208
|
+
s.start("Registering app...");
|
|
209
|
+
const txHash = await registry.registerApp();
|
|
210
|
+
s.stop();
|
|
211
|
+
console.log(`App: ${loadConfig().appId}`);
|
|
212
|
+
console.log(`App id: ${registry.getAppId()}`);
|
|
213
|
+
console.log(`Owner: ${self}`);
|
|
214
|
+
console.log(`Tx: ${txHash}`);
|
|
215
|
+
process.exit(0);
|
|
216
|
+
} catch (err) {
|
|
217
|
+
console.error("Failed:", err.message);
|
|
218
|
+
process.exit(1);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
163
221
|
program.command("register").description("Register your wallet as a data publisher on-chain").action(async () => {
|
|
164
222
|
try {
|
|
165
223
|
const self = getAccount().address;
|
package/lib/cli/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./cli.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as viem89 from "viem";
|
|
3
3
|
|
|
4
4
|
//#region src/cli/index.d.ts
|
|
5
5
|
declare const getChain: (chainStr: string) => {
|
|
@@ -35,17 +35,17 @@ declare const getChain: (chainStr: string) => {
|
|
|
35
35
|
testnet: true;
|
|
36
36
|
custom?: Record<string, unknown> | undefined;
|
|
37
37
|
extendSchema?: Record<string, unknown> | undefined;
|
|
38
|
-
fees?:
|
|
38
|
+
fees?: viem89.ChainFees<undefined> | undefined;
|
|
39
39
|
formatters?: undefined;
|
|
40
|
-
prepareTransactionRequest?: ((args:
|
|
40
|
+
prepareTransactionRequest?: ((args: viem89.PrepareTransactionRequestParameters, options: {
|
|
41
41
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
42
|
-
}) => Promise<
|
|
42
|
+
}) => Promise<viem89.PrepareTransactionRequestParameters>) | [fn: ((args: viem89.PrepareTransactionRequestParameters, options: {
|
|
43
43
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
44
|
-
}) => Promise<
|
|
44
|
+
}) => Promise<viem89.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
45
45
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
46
46
|
}] | undefined;
|
|
47
|
-
serializers?:
|
|
48
|
-
verifyHash?: ((client:
|
|
47
|
+
serializers?: viem89.ChainSerializers<undefined, viem89.TransactionSerializable> | undefined;
|
|
48
|
+
verifyHash?: ((client: viem89.Client, parameters: viem89.VerifyHashActionParameters) => Promise<viem89.VerifyHashActionReturnType>) | undefined;
|
|
49
49
|
};
|
|
50
50
|
declare const handleCancel: (value: unknown) => void;
|
|
51
51
|
declare const selectChain: () => Promise<{
|
|
@@ -81,17 +81,17 @@ declare const selectChain: () => Promise<{
|
|
|
81
81
|
testnet: true;
|
|
82
82
|
custom?: Record<string, unknown> | undefined;
|
|
83
83
|
extendSchema?: Record<string, unknown> | undefined;
|
|
84
|
-
fees?:
|
|
84
|
+
fees?: viem89.ChainFees<undefined> | undefined;
|
|
85
85
|
formatters?: undefined;
|
|
86
|
-
prepareTransactionRequest?: ((args:
|
|
86
|
+
prepareTransactionRequest?: ((args: viem89.PrepareTransactionRequestParameters, options: {
|
|
87
87
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
88
|
-
}) => Promise<
|
|
88
|
+
}) => Promise<viem89.PrepareTransactionRequestParameters>) | [fn: ((args: viem89.PrepareTransactionRequestParameters, options: {
|
|
89
89
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
90
|
-
}) => Promise<
|
|
90
|
+
}) => Promise<viem89.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
91
91
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
92
92
|
}] | undefined;
|
|
93
|
-
serializers?:
|
|
94
|
-
verifyHash?: ((client:
|
|
93
|
+
serializers?: viem89.ChainSerializers<undefined, viem89.TransactionSerializable> | undefined;
|
|
94
|
+
verifyHash?: ((client: viem89.Client, parameters: viem89.VerifyHashActionParameters) => Promise<viem89.VerifyHashActionReturnType>) | undefined;
|
|
95
95
|
}>;
|
|
96
96
|
declare function parseGadgetArg(raw: string): {
|
|
97
97
|
type: string;
|
package/lib/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as viem0 from "viem";
|
|
2
2
|
import { Chain, Hex } from "viem";
|
|
3
3
|
import * as viem_chains0 from "viem/chains";
|
|
4
4
|
import * as abitype0 from "abitype";
|
|
@@ -9,6 +9,13 @@ import * as abitype0 from "abitype";
|
|
|
9
9
|
* with `registerApp`; it prefixes every namespace key the app's publishers write.
|
|
10
10
|
*/
|
|
11
11
|
declare function appId(name: string): Hex;
|
|
12
|
+
/** The app every client publishes under until told otherwise. */
|
|
13
|
+
declare const DEFAULT_APP = "fangorn";
|
|
14
|
+
/**
|
|
15
|
+
* Accept either form callers have on hand: a human-readable app name, or an
|
|
16
|
+
* already-derived app id (32-byte hex), which is what an app owner sees on-chain.
|
|
17
|
+
*/
|
|
18
|
+
declare function toAppId(nameOrId: string): Hex;
|
|
12
19
|
/**
|
|
13
20
|
* The networks supproted by Fangorn currently
|
|
14
21
|
*/
|
|
@@ -48,17 +55,17 @@ declare const SupportedNetworks: {
|
|
|
48
55
|
testnet: true;
|
|
49
56
|
custom?: Record<string, unknown> | undefined;
|
|
50
57
|
extendSchema?: Record<string, unknown> | undefined;
|
|
51
|
-
fees?:
|
|
58
|
+
fees?: viem0.ChainFees<undefined> | undefined;
|
|
52
59
|
formatters?: undefined;
|
|
53
|
-
prepareTransactionRequest?: ((args:
|
|
60
|
+
prepareTransactionRequest?: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
54
61
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
55
|
-
}) => Promise<
|
|
62
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | [fn: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
56
63
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
57
|
-
}) => Promise<
|
|
64
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
58
65
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
59
66
|
}] | undefined;
|
|
60
|
-
serializers?:
|
|
61
|
-
verifyHash?: ((client:
|
|
67
|
+
serializers?: viem0.ChainSerializers<undefined, viem0.TransactionSerializable> | undefined;
|
|
68
|
+
verifyHash?: ((client: viem0.Client, parameters: viem0.VerifyHashActionParameters) => Promise<viem0.VerifyHashActionReturnType>) | undefined;
|
|
62
69
|
};
|
|
63
70
|
};
|
|
64
71
|
BaseSepolia: {
|
|
@@ -136,7 +143,7 @@ declare const SupportedNetworks: {
|
|
|
136
143
|
testnet: true;
|
|
137
144
|
custom?: Record<string, unknown> | undefined;
|
|
138
145
|
extendSchema?: Record<string, unknown> | undefined;
|
|
139
|
-
fees?:
|
|
146
|
+
fees?: viem0.ChainFees<undefined> | undefined;
|
|
140
147
|
formatters: {
|
|
141
148
|
readonly block: {
|
|
142
149
|
exclude: [] | undefined;
|
|
@@ -145,28 +152,28 @@ declare const SupportedNetworks: {
|
|
|
145
152
|
blobGasUsed: bigint;
|
|
146
153
|
difficulty: bigint;
|
|
147
154
|
excessBlobGas: bigint;
|
|
148
|
-
extraData:
|
|
155
|
+
extraData: viem0.Hex;
|
|
149
156
|
gasLimit: bigint;
|
|
150
157
|
gasUsed: bigint;
|
|
151
158
|
hash: `0x${string}` | null;
|
|
152
159
|
logsBloom: `0x${string}` | null;
|
|
153
160
|
miner: abitype0.Address;
|
|
154
|
-
mixHash:
|
|
161
|
+
mixHash: viem0.Hash;
|
|
155
162
|
nonce: `0x${string}` | null;
|
|
156
163
|
number: bigint | null;
|
|
157
164
|
parentBeaconBlockRoot?: `0x${string}` | undefined;
|
|
158
|
-
parentHash:
|
|
159
|
-
receiptsRoot:
|
|
160
|
-
sealFields:
|
|
161
|
-
sha3Uncles:
|
|
165
|
+
parentHash: viem0.Hash;
|
|
166
|
+
receiptsRoot: viem0.Hex;
|
|
167
|
+
sealFields: viem0.Hex[];
|
|
168
|
+
sha3Uncles: viem0.Hash;
|
|
162
169
|
size: bigint;
|
|
163
|
-
stateRoot:
|
|
170
|
+
stateRoot: viem0.Hash;
|
|
164
171
|
timestamp: bigint;
|
|
165
172
|
totalDifficulty: bigint | null;
|
|
166
173
|
transactions: `0x${string}`[] | viem_chains0.OpStackTransaction<boolean>[];
|
|
167
|
-
transactionsRoot:
|
|
168
|
-
uncles:
|
|
169
|
-
withdrawals?:
|
|
174
|
+
transactionsRoot: viem0.Hash;
|
|
175
|
+
uncles: viem0.Hash[];
|
|
176
|
+
withdrawals?: viem0.Withdrawal[] | undefined | undefined;
|
|
170
177
|
withdrawalsRoot?: `0x${string}` | undefined;
|
|
171
178
|
} & {};
|
|
172
179
|
type: "block";
|
|
@@ -178,14 +185,14 @@ declare const SupportedNetworks: {
|
|
|
178
185
|
blockNumber: bigint | null;
|
|
179
186
|
from: abitype0.Address;
|
|
180
187
|
gas: bigint;
|
|
181
|
-
hash:
|
|
182
|
-
input:
|
|
188
|
+
hash: viem0.Hash;
|
|
189
|
+
input: viem0.Hex;
|
|
183
190
|
nonce: number;
|
|
184
|
-
r:
|
|
185
|
-
s:
|
|
191
|
+
r: viem0.Hex;
|
|
192
|
+
s: viem0.Hex;
|
|
186
193
|
to: abitype0.Address | null;
|
|
187
194
|
transactionIndex: number | null;
|
|
188
|
-
typeHex:
|
|
195
|
+
typeHex: viem0.Hex | null;
|
|
189
196
|
v: bigint;
|
|
190
197
|
value: bigint;
|
|
191
198
|
yParity: number;
|
|
@@ -195,11 +202,11 @@ declare const SupportedNetworks: {
|
|
|
195
202
|
maxPriorityFeePerGas: bigint;
|
|
196
203
|
isSystemTx?: boolean;
|
|
197
204
|
mint?: bigint | undefined | undefined;
|
|
198
|
-
sourceHash:
|
|
205
|
+
sourceHash: viem0.Hex;
|
|
199
206
|
type: "deposit";
|
|
200
207
|
} | {
|
|
201
|
-
r:
|
|
202
|
-
s:
|
|
208
|
+
r: viem0.Hex;
|
|
209
|
+
s: viem0.Hex;
|
|
203
210
|
v: bigint;
|
|
204
211
|
to: abitype0.Address | null;
|
|
205
212
|
from: abitype0.Address;
|
|
@@ -208,10 +215,10 @@ declare const SupportedNetworks: {
|
|
|
208
215
|
value: bigint;
|
|
209
216
|
blockHash: `0x${string}` | null;
|
|
210
217
|
blockNumber: bigint | null;
|
|
211
|
-
hash:
|
|
212
|
-
input:
|
|
218
|
+
hash: viem0.Hash;
|
|
219
|
+
input: viem0.Hex;
|
|
213
220
|
transactionIndex: number | null;
|
|
214
|
-
typeHex:
|
|
221
|
+
typeHex: viem0.Hex | null;
|
|
215
222
|
accessList?: undefined | undefined;
|
|
216
223
|
authorizationList?: undefined | undefined;
|
|
217
224
|
blobVersionedHashes?: undefined | undefined;
|
|
@@ -230,18 +237,18 @@ declare const SupportedNetworks: {
|
|
|
230
237
|
blockNumber: bigint | null;
|
|
231
238
|
from: abitype0.Address;
|
|
232
239
|
gas: bigint;
|
|
233
|
-
hash:
|
|
234
|
-
input:
|
|
240
|
+
hash: viem0.Hash;
|
|
241
|
+
input: viem0.Hex;
|
|
235
242
|
nonce: number;
|
|
236
|
-
r:
|
|
237
|
-
s:
|
|
243
|
+
r: viem0.Hex;
|
|
244
|
+
s: viem0.Hex;
|
|
238
245
|
to: abitype0.Address | null;
|
|
239
246
|
transactionIndex: number | null;
|
|
240
|
-
typeHex:
|
|
247
|
+
typeHex: viem0.Hex | null;
|
|
241
248
|
v: bigint;
|
|
242
249
|
value: bigint;
|
|
243
250
|
yParity: number;
|
|
244
|
-
accessList:
|
|
251
|
+
accessList: viem0.AccessList;
|
|
245
252
|
authorizationList?: undefined | undefined;
|
|
246
253
|
blobVersionedHashes?: undefined | undefined;
|
|
247
254
|
chainId: number;
|
|
@@ -258,18 +265,18 @@ declare const SupportedNetworks: {
|
|
|
258
265
|
blockNumber: bigint | null;
|
|
259
266
|
from: abitype0.Address;
|
|
260
267
|
gas: bigint;
|
|
261
|
-
hash:
|
|
262
|
-
input:
|
|
268
|
+
hash: viem0.Hash;
|
|
269
|
+
input: viem0.Hex;
|
|
263
270
|
nonce: number;
|
|
264
|
-
r:
|
|
265
|
-
s:
|
|
271
|
+
r: viem0.Hex;
|
|
272
|
+
s: viem0.Hex;
|
|
266
273
|
to: abitype0.Address | null;
|
|
267
274
|
transactionIndex: number | null;
|
|
268
|
-
typeHex:
|
|
275
|
+
typeHex: viem0.Hex | null;
|
|
269
276
|
v: bigint;
|
|
270
277
|
value: bigint;
|
|
271
278
|
yParity: number;
|
|
272
|
-
accessList:
|
|
279
|
+
accessList: viem0.AccessList;
|
|
273
280
|
authorizationList?: undefined | undefined;
|
|
274
281
|
blobVersionedHashes?: undefined | undefined;
|
|
275
282
|
chainId: number;
|
|
@@ -286,20 +293,20 @@ declare const SupportedNetworks: {
|
|
|
286
293
|
blockNumber: bigint | null;
|
|
287
294
|
from: abitype0.Address;
|
|
288
295
|
gas: bigint;
|
|
289
|
-
hash:
|
|
290
|
-
input:
|
|
296
|
+
hash: viem0.Hash;
|
|
297
|
+
input: viem0.Hex;
|
|
291
298
|
nonce: number;
|
|
292
|
-
r:
|
|
293
|
-
s:
|
|
299
|
+
r: viem0.Hex;
|
|
300
|
+
s: viem0.Hex;
|
|
294
301
|
to: abitype0.Address | null;
|
|
295
302
|
transactionIndex: number | null;
|
|
296
|
-
typeHex:
|
|
303
|
+
typeHex: viem0.Hex | null;
|
|
297
304
|
v: bigint;
|
|
298
305
|
value: bigint;
|
|
299
306
|
yParity: number;
|
|
300
|
-
accessList:
|
|
307
|
+
accessList: viem0.AccessList;
|
|
301
308
|
authorizationList?: undefined | undefined;
|
|
302
|
-
blobVersionedHashes: readonly
|
|
309
|
+
blobVersionedHashes: readonly viem0.Hex[];
|
|
303
310
|
chainId: number;
|
|
304
311
|
type: "eip4844";
|
|
305
312
|
gasPrice?: undefined | undefined;
|
|
@@ -314,19 +321,19 @@ declare const SupportedNetworks: {
|
|
|
314
321
|
blockNumber: bigint | null;
|
|
315
322
|
from: abitype0.Address;
|
|
316
323
|
gas: bigint;
|
|
317
|
-
hash:
|
|
318
|
-
input:
|
|
324
|
+
hash: viem0.Hash;
|
|
325
|
+
input: viem0.Hex;
|
|
319
326
|
nonce: number;
|
|
320
|
-
r:
|
|
321
|
-
s:
|
|
327
|
+
r: viem0.Hex;
|
|
328
|
+
s: viem0.Hex;
|
|
322
329
|
to: abitype0.Address | null;
|
|
323
330
|
transactionIndex: number | null;
|
|
324
|
-
typeHex:
|
|
331
|
+
typeHex: viem0.Hex | null;
|
|
325
332
|
v: bigint;
|
|
326
333
|
value: bigint;
|
|
327
334
|
yParity: number;
|
|
328
|
-
accessList:
|
|
329
|
-
authorizationList:
|
|
335
|
+
accessList: viem0.AccessList;
|
|
336
|
+
authorizationList: viem0.SignedAuthorizationList;
|
|
330
337
|
blobVersionedHashes?: undefined | undefined;
|
|
331
338
|
chainId: number;
|
|
332
339
|
type: "eip7702";
|
|
@@ -345,7 +352,7 @@ declare const SupportedNetworks: {
|
|
|
345
352
|
format: (args: viem_chains0.OpStackRpcTransactionReceipt, action?: string | undefined) => {
|
|
346
353
|
blobGasPrice?: bigint | undefined;
|
|
347
354
|
blobGasUsed?: bigint | undefined;
|
|
348
|
-
blockHash:
|
|
355
|
+
blockHash: viem0.Hash;
|
|
349
356
|
blockNumber: bigint;
|
|
350
357
|
blockTimestamp?: bigint | undefined;
|
|
351
358
|
contractAddress: abitype0.Address | null | undefined;
|
|
@@ -353,14 +360,14 @@ declare const SupportedNetworks: {
|
|
|
353
360
|
effectiveGasPrice: bigint;
|
|
354
361
|
from: abitype0.Address;
|
|
355
362
|
gasUsed: bigint;
|
|
356
|
-
logs:
|
|
357
|
-
logsBloom:
|
|
363
|
+
logs: viem0.Log<bigint, number, false>[];
|
|
364
|
+
logsBloom: viem0.Hex;
|
|
358
365
|
root?: `0x${string}` | undefined;
|
|
359
366
|
status: "success" | "reverted";
|
|
360
367
|
to: abitype0.Address | null;
|
|
361
|
-
transactionHash:
|
|
368
|
+
transactionHash: viem0.Hash;
|
|
362
369
|
transactionIndex: number;
|
|
363
|
-
type:
|
|
370
|
+
type: viem0.TransactionType;
|
|
364
371
|
l1GasPrice: bigint | null;
|
|
365
372
|
l1GasUsed: bigint | null;
|
|
366
373
|
l1Fee: bigint | null;
|
|
@@ -369,17 +376,17 @@ declare const SupportedNetworks: {
|
|
|
369
376
|
type: "transactionReceipt";
|
|
370
377
|
};
|
|
371
378
|
};
|
|
372
|
-
prepareTransactionRequest?: ((args:
|
|
379
|
+
prepareTransactionRequest?: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
373
380
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
374
|
-
}) => Promise<
|
|
381
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | [fn: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
375
382
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
376
|
-
}) => Promise<
|
|
383
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
377
384
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
378
385
|
}] | undefined;
|
|
379
386
|
serializers: {
|
|
380
387
|
readonly transaction: typeof viem_chains0.serializeTransactionOpStack;
|
|
381
388
|
};
|
|
382
|
-
verifyHash?: ((client:
|
|
389
|
+
verifyHash?: ((client: viem0.Client, parameters: viem0.VerifyHashActionParameters) => Promise<viem0.VerifyHashActionReturnType>) | undefined;
|
|
383
390
|
readonly network: "base-sepolia";
|
|
384
391
|
};
|
|
385
392
|
};
|
|
@@ -422,21 +429,20 @@ declare function getNetwork(name: string): {
|
|
|
422
429
|
testnet: true;
|
|
423
430
|
custom?: Record<string, unknown> | undefined;
|
|
424
431
|
extendSchema?: Record<string, unknown> | undefined;
|
|
425
|
-
fees?:
|
|
432
|
+
fees?: viem0.ChainFees<undefined> | undefined;
|
|
426
433
|
formatters?: undefined;
|
|
427
|
-
prepareTransactionRequest?: ((args:
|
|
434
|
+
prepareTransactionRequest?: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
428
435
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
429
|
-
}) => Promise<
|
|
436
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | [fn: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
430
437
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
431
|
-
}) => Promise<
|
|
438
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
432
439
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
433
440
|
}] | undefined;
|
|
434
|
-
serializers?:
|
|
435
|
-
verifyHash?: ((client:
|
|
441
|
+
serializers?: viem0.ChainSerializers<undefined, viem0.TransactionSerializable> | undefined;
|
|
442
|
+
verifyHash?: ((client: viem0.Client, parameters: viem0.VerifyHashActionParameters) => Promise<viem0.VerifyHashActionReturnType>) | undefined;
|
|
436
443
|
};
|
|
437
444
|
interface AppConfig {
|
|
438
445
|
dataRegistryContractAddress: Hex;
|
|
439
|
-
appId: Hex;
|
|
440
446
|
chain: Chain;
|
|
441
447
|
rpcUrl: string;
|
|
442
448
|
caip2: number;
|
|
@@ -444,7 +450,6 @@ interface AppConfig {
|
|
|
444
450
|
}
|
|
445
451
|
declare const FangornConfig: {
|
|
446
452
|
dataRegistryContractAddress: "0xdec430f03cdc022ab2338490e9b30ef5c63f7340";
|
|
447
|
-
appId: `0x${string}`;
|
|
448
453
|
chain: {
|
|
449
454
|
blockExplorers: {
|
|
450
455
|
readonly default: {
|
|
@@ -478,21 +483,21 @@ declare const FangornConfig: {
|
|
|
478
483
|
testnet: true;
|
|
479
484
|
custom?: Record<string, unknown> | undefined;
|
|
480
485
|
extendSchema?: Record<string, unknown> | undefined;
|
|
481
|
-
fees?:
|
|
486
|
+
fees?: viem0.ChainFees<undefined> | undefined;
|
|
482
487
|
formatters?: undefined;
|
|
483
|
-
prepareTransactionRequest?: ((args:
|
|
488
|
+
prepareTransactionRequest?: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
484
489
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
485
|
-
}) => Promise<
|
|
490
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | [fn: ((args: viem0.PrepareTransactionRequestParameters, options: {
|
|
486
491
|
phase: "beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters";
|
|
487
|
-
}) => Promise<
|
|
492
|
+
}) => Promise<viem0.PrepareTransactionRequestParameters>) | undefined, options: {
|
|
488
493
|
runAt: readonly ("beforeFillTransaction" | "beforeFillParameters" | "afterFillParameters")[];
|
|
489
494
|
}] | undefined;
|
|
490
|
-
serializers?:
|
|
491
|
-
verifyHash?: ((client:
|
|
495
|
+
serializers?: viem0.ChainSerializers<undefined, viem0.TransactionSerializable> | undefined;
|
|
496
|
+
verifyHash?: ((client: viem0.Client, parameters: viem0.VerifyHashActionParameters) => Promise<viem0.VerifyHashActionReturnType>) | undefined;
|
|
492
497
|
};
|
|
493
498
|
rpcUrl: string;
|
|
494
499
|
caip2: number;
|
|
495
500
|
ipfsGateway: string;
|
|
496
501
|
};
|
|
497
502
|
//#endregion
|
|
498
|
-
export { AppConfig, FangornConfig, SupportedNetworks, appId, getNetwork as default };
|
|
503
|
+
export { AppConfig, DEFAULT_APP, FangornConfig, SupportedNetworks, appId, getNetwork as default, toAppId };
|
package/lib/config.js
CHANGED
|
@@ -9,6 +9,15 @@ import { arbitrumSepolia, baseSepolia } from "viem/chains";
|
|
|
9
9
|
function appId(name) {
|
|
10
10
|
return keccak256(toHex(name));
|
|
11
11
|
}
|
|
12
|
+
/** The app every client publishes under until told otherwise. */
|
|
13
|
+
const DEFAULT_APP = "fangorn";
|
|
14
|
+
/**
|
|
15
|
+
* Accept either form callers have on hand: a human-readable app name, or an
|
|
16
|
+
* already-derived app id (32-byte hex), which is what an app owner sees on-chain.
|
|
17
|
+
*/
|
|
18
|
+
function toAppId(nameOrId) {
|
|
19
|
+
return /^0x[0-9a-fA-F]{64}$/.test(nameOrId) ? nameOrId : appId(nameOrId);
|
|
20
|
+
}
|
|
12
21
|
/**
|
|
13
22
|
* The networks supproted by Fangorn currently
|
|
14
23
|
*/
|
|
@@ -33,7 +42,6 @@ function getNetwork(name) {
|
|
|
33
42
|
}
|
|
34
43
|
const FangornConfig = {
|
|
35
44
|
dataRegistryContractAddress: "0xdec430f03cdc022ab2338490e9b30ef5c63f7340",
|
|
36
|
-
appId: appId("fangorn"),
|
|
37
45
|
chain: arbitrumSepolia,
|
|
38
46
|
rpcUrl: "https://sepolia-rollup.arbitrum.io/rpc",
|
|
39
47
|
caip2: 421614,
|
|
@@ -41,4 +49,4 @@ const FangornConfig = {
|
|
|
41
49
|
};
|
|
42
50
|
|
|
43
51
|
//#endregion
|
|
44
|
-
export { FangornConfig, SupportedNetworks, appId, getNetwork as default };
|
|
52
|
+
export { DEFAULT_APP, FangornConfig, SupportedNetworks, appId, getNetwork as default, toAppId };
|
|
@@ -70,6 +70,8 @@ declare class DataRegistryClient {
|
|
|
70
70
|
constructor(contractAddress: Address, appId: Hex, publicClient: PublicClient, walletClient: WalletClient);
|
|
71
71
|
/** The app namespace every call on this client is scoped to. */
|
|
72
72
|
getAppId(): Hex;
|
|
73
|
+
/** Re-scope every subsequent call to a different app. See `Fangorn.setAppId`. */
|
|
74
|
+
setAppId(appId: Hex): void;
|
|
73
75
|
private getWriteConfig;
|
|
74
76
|
/**
|
|
75
77
|
* Internal helper to execute state-mutating transactions
|
|
@@ -54,6 +54,10 @@ var DataRegistryClient = class {
|
|
|
54
54
|
getAppId() {
|
|
55
55
|
return this.appId;
|
|
56
56
|
}
|
|
57
|
+
/** Re-scope every subsequent call to a different app. See `Fangorn.setAppId`. */
|
|
58
|
+
setAppId(appId) {
|
|
59
|
+
this.appId = appId;
|
|
60
|
+
}
|
|
57
61
|
getWriteConfig() {
|
|
58
62
|
if (!this.walletClient.chain) throw new Error("Chain required");
|
|
59
63
|
if (!this.walletClient.account) throw new Error("Account required");
|
package/lib/fangorn.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import "./contracts/index.js";
|
|
|
4
4
|
import { MetadataStorage } from "./providers/storage/types.js";
|
|
5
5
|
import { CommitDiff, FangornEngine, MetagraphRegistry, NamespaceContents, NamespaceDiff } from "./engine/index.js";
|
|
6
6
|
import { FangornCreateOptions, StorageConfig } from "./types/index.js";
|
|
7
|
+
import { AppFeed } from "./feed.js";
|
|
7
8
|
import { Hex, WalletClient } from "viem";
|
|
8
9
|
|
|
9
10
|
//#region src/fangorn.d.ts
|
|
@@ -38,6 +39,9 @@ declare class Fangorn {
|
|
|
38
39
|
private readonly ctx;
|
|
39
40
|
private _engine;
|
|
40
41
|
private readonly _metagraph;
|
|
42
|
+
private _feed;
|
|
43
|
+
/** Insertion order = LRU order; see `readNamespace`. */
|
|
44
|
+
private readonly nsCache;
|
|
41
45
|
private constructor();
|
|
42
46
|
get engine(): FangornEngine;
|
|
43
47
|
get metagraph(): MetagraphRegistry;
|
|
@@ -176,6 +180,56 @@ declare class Fangorn {
|
|
|
176
180
|
}>;
|
|
177
181
|
/** The owner's current on-chain tip for a namespace, or null if they have none. */
|
|
178
182
|
onChainTip(owner: Hex, namespace: string): Promise<string | null>;
|
|
183
|
+
/**
|
|
184
|
+
* A publisher's namespace contents, cached against its on-chain tip.
|
|
185
|
+
*
|
|
186
|
+
* Reading a namespace walks the pail tree from the root — many sequential
|
|
187
|
+
* gateway fetches. The tip is one cheap contract read and only moves on
|
|
188
|
+
* publish, so it doubles as the cache key: no TTL to tune, no invalidation
|
|
189
|
+
* to remember after a settle, and never stale.
|
|
190
|
+
*
|
|
191
|
+
* LRU-bounded so an always-on process can't accumulate every namespace it
|
|
192
|
+
* ever touched. The durable copy is on-chain — an evicted entry re-walks.
|
|
193
|
+
* ponytail: fixed entry count, not byte-aware; switch to a size budget if a
|
|
194
|
+
* few huge namespaces blow memory before the count cap bites.
|
|
195
|
+
*/
|
|
196
|
+
readNamespace(owner: Hex, namespace: string): Promise<{
|
|
197
|
+
tip: string | null;
|
|
198
|
+
contents: NamespaceContents;
|
|
199
|
+
}>;
|
|
200
|
+
/**
|
|
201
|
+
* Every (publisher, namespace) pair that has ever committed under this app,
|
|
202
|
+
* at its latest root — the app's directory, from ONE `getLogs`.
|
|
203
|
+
*
|
|
204
|
+
* This is what cross-publisher discovery looks like now: the registry already
|
|
205
|
+
* indexes `app_id`, so the commit log IS the index. No per-publisher fan-out,
|
|
206
|
+
* no separate registration sweep, no central list — and unlike a publisher
|
|
207
|
+
* roster it only surfaces publishers with actual content.
|
|
208
|
+
*
|
|
209
|
+
* Namespaces come back as `subspaceId` (the hash), not the name, because the
|
|
210
|
+
* event never carries the name — recovering it costs one content fetch per
|
|
211
|
+
* entry. Pass `namespace` when you know the name you're after (the usual
|
|
212
|
+
* case: an app with one well-known namespace) and the filter is applied
|
|
213
|
+
* on-chain instead.
|
|
214
|
+
*/
|
|
215
|
+
appNamespaces(opts?: {
|
|
216
|
+
namespace?: string;
|
|
217
|
+
owner?: Hex;
|
|
218
|
+
fromBlock?: bigint;
|
|
219
|
+
}): Promise<{
|
|
220
|
+
owner: Hex;
|
|
221
|
+
subspaceId: Hex;
|
|
222
|
+
root: Hex;
|
|
223
|
+
blockNumber: bigint;
|
|
224
|
+
}[]>;
|
|
225
|
+
/**
|
|
226
|
+
* The app's commit stream as ONE shared, ref-counted subscription (see
|
|
227
|
+
* `AppFeed`). Prefer this over `subscribeApp` anywhere more than one consumer
|
|
228
|
+
* wants the same feed — a server fanning out to connections, say.
|
|
229
|
+
*/
|
|
230
|
+
appFeed(opts?: {
|
|
231
|
+
pollingInterval?: number;
|
|
232
|
+
}): AppFeed;
|
|
179
233
|
/** Walk commit history from `head` (newest first). */
|
|
180
234
|
log(head: string, max?: number): AsyncGenerator<{
|
|
181
235
|
cid: string;
|
|
@@ -222,6 +276,19 @@ declare class Fangorn {
|
|
|
222
276
|
private toNamespaceChange;
|
|
223
277
|
getConfig(): AppConfig;
|
|
224
278
|
getDataRegistry(): DataRegistryClient;
|
|
279
|
+
/** The app (global namespace) this client publishes and reads under. */
|
|
280
|
+
getAppId(): Hex;
|
|
281
|
+
/**
|
|
282
|
+
* Point this client at a different app — by name (`"my-app"`) or by app id —
|
|
283
|
+
* without rebuilding it. The app id prefixes every namespace key, so every
|
|
284
|
+
* read, publish and subscription after this call lands in the new app.
|
|
285
|
+
*
|
|
286
|
+
* Namespace reads are dropped from the cache (its keys are app-agnostic) and
|
|
287
|
+
* the shared `appFeed` is released: a feed watches one app's commit stream, so
|
|
288
|
+
* the next `appFeed()` opens a fresh subscription. Generators already running
|
|
289
|
+
* from `subscribe`/`subscribeApp` keep their original filter until aborted.
|
|
290
|
+
*/
|
|
291
|
+
setAppId(nameOrId: string): void;
|
|
225
292
|
getStorage(): MetadataStorage;
|
|
226
293
|
/**
|
|
227
294
|
* Swap the storage backend at runtime — e.g. move from access-worker signed
|
package/lib/fangorn.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import { FangornConfig } from "./config.js";
|
|
1
|
+
import { DEFAULT_APP, FangornConfig, toAppId } from "./config.js";
|
|
2
2
|
import { SignedUrlBackend } from "./providers/storage/signed-url.js";
|
|
3
3
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
4
4
|
import { FangornEngine, MetagraphRegistry, assertValidNamespace, rootHexFromCid } from "./engine/index.js";
|
|
5
5
|
import { DataRegistryClient, subspaceId } from "./contracts/data-registry/index.js";
|
|
6
6
|
import "./contracts/index.js";
|
|
7
|
+
import { AppFeed } from "./feed.js";
|
|
7
8
|
import { createPublicClient, createWalletClient, http } from "viem";
|
|
8
9
|
import { privateKeyToAccount } from "viem/accounts";
|
|
9
10
|
import { CID } from "multiformats/cid";
|
|
10
11
|
|
|
11
12
|
//#region src/fangorn.ts
|
|
12
13
|
const ZERO_BYTES32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
14
|
+
/** Max namespaces held in `readNamespace`'s tip-keyed cache. */
|
|
15
|
+
const NS_CACHE_MAX = 256;
|
|
13
16
|
/** Build a signer for the access worker's ownership handshake from a wallet. */
|
|
14
17
|
function walletSigner(walletClient) {
|
|
15
18
|
const account = walletClient.account;
|
|
@@ -32,6 +35,9 @@ var Fangorn = class Fangorn {
|
|
|
32
35
|
ctx;
|
|
33
36
|
_engine = null;
|
|
34
37
|
_metagraph = new MetagraphRegistry();
|
|
38
|
+
_feed = null;
|
|
39
|
+
/** Insertion order = LRU order; see `readNamespace`. */
|
|
40
|
+
nsCache = /* @__PURE__ */ new Map();
|
|
35
41
|
constructor(ctx) {
|
|
36
42
|
this.ctx = ctx;
|
|
37
43
|
}
|
|
@@ -58,7 +64,7 @@ var Fangorn = class Fangorn {
|
|
|
58
64
|
walletClient,
|
|
59
65
|
metadataStorage,
|
|
60
66
|
domain,
|
|
61
|
-
dataRegistry: new DataRegistryClient(resolvedConfig.dataRegistryContractAddress,
|
|
67
|
+
dataRegistry: new DataRegistryClient(resolvedConfig.dataRegistryContractAddress, toAppId(options.appId ?? DEFAULT_APP), publicClient, walletClient),
|
|
62
68
|
config: resolvedConfig
|
|
63
69
|
});
|
|
64
70
|
}
|
|
@@ -275,6 +281,81 @@ var Fangorn = class Fangorn {
|
|
|
275
281
|
const cid = await this.engine.resolveHeadCommit(owner, namespace);
|
|
276
282
|
return cid ? cid.toString() : null;
|
|
277
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* A publisher's namespace contents, cached against its on-chain tip.
|
|
286
|
+
*
|
|
287
|
+
* Reading a namespace walks the pail tree from the root — many sequential
|
|
288
|
+
* gateway fetches. The tip is one cheap contract read and only moves on
|
|
289
|
+
* publish, so it doubles as the cache key: no TTL to tune, no invalidation
|
|
290
|
+
* to remember after a settle, and never stale.
|
|
291
|
+
*
|
|
292
|
+
* LRU-bounded so an always-on process can't accumulate every namespace it
|
|
293
|
+
* ever touched. The durable copy is on-chain — an evicted entry re-walks.
|
|
294
|
+
* ponytail: fixed entry count, not byte-aware; switch to a size budget if a
|
|
295
|
+
* few huge namespaces blow memory before the count cap bites.
|
|
296
|
+
*/
|
|
297
|
+
async readNamespace(owner, namespace) {
|
|
298
|
+
const key = `${owner.toLowerCase()}/${namespace}`;
|
|
299
|
+
const tip = await this.onChainTip(owner, namespace);
|
|
300
|
+
const hit = this.nsCache.get(key);
|
|
301
|
+
if (hit?.tip === tip) {
|
|
302
|
+
this.nsCache.delete(key);
|
|
303
|
+
this.nsCache.set(key, hit);
|
|
304
|
+
return hit;
|
|
305
|
+
}
|
|
306
|
+
const entry = {
|
|
307
|
+
tip,
|
|
308
|
+
contents: await this.engine.listNamespace(namespace, owner)
|
|
309
|
+
};
|
|
310
|
+
this.nsCache.delete(key);
|
|
311
|
+
this.nsCache.set(key, entry);
|
|
312
|
+
if (this.nsCache.size > NS_CACHE_MAX) {
|
|
313
|
+
const oldest = this.nsCache.keys().next().value;
|
|
314
|
+
if (oldest !== void 0) this.nsCache.delete(oldest);
|
|
315
|
+
}
|
|
316
|
+
return entry;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Every (publisher, namespace) pair that has ever committed under this app,
|
|
320
|
+
* at its latest root — the app's directory, from ONE `getLogs`.
|
|
321
|
+
*
|
|
322
|
+
* This is what cross-publisher discovery looks like now: the registry already
|
|
323
|
+
* indexes `app_id`, so the commit log IS the index. No per-publisher fan-out,
|
|
324
|
+
* no separate registration sweep, no central list — and unlike a publisher
|
|
325
|
+
* roster it only surfaces publishers with actual content.
|
|
326
|
+
*
|
|
327
|
+
* Namespaces come back as `subspaceId` (the hash), not the name, because the
|
|
328
|
+
* event never carries the name — recovering it costs one content fetch per
|
|
329
|
+
* entry. Pass `namespace` when you know the name you're after (the usual
|
|
330
|
+
* case: an app with one well-known namespace) and the filter is applied
|
|
331
|
+
* on-chain instead.
|
|
332
|
+
*/
|
|
333
|
+
async appNamespaces(opts = {}) {
|
|
334
|
+
const logs = await this.getDataRegistry().getStateCommittedLogs({
|
|
335
|
+
publisher: opts.owner,
|
|
336
|
+
namespace: opts.namespace
|
|
337
|
+
}, opts.fromBlock ?? 0n);
|
|
338
|
+
const latest = /* @__PURE__ */ new Map();
|
|
339
|
+
for (const log of logs) latest.set(log.namespaceKey, {
|
|
340
|
+
owner: log.publisher,
|
|
341
|
+
subspaceId: log.subspaceId,
|
|
342
|
+
root: log.newRoot,
|
|
343
|
+
blockNumber: log.blockNumber
|
|
344
|
+
});
|
|
345
|
+
return [...latest.values()];
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The app's commit stream as ONE shared, ref-counted subscription (see
|
|
349
|
+
* `AppFeed`). Prefer this over `subscribeApp` anywhere more than one consumer
|
|
350
|
+
* wants the same feed — a server fanning out to connections, say.
|
|
351
|
+
*/
|
|
352
|
+
appFeed(opts = {}) {
|
|
353
|
+
this._feed ??= new AppFeed((signal) => this.subscribeApp({
|
|
354
|
+
...opts,
|
|
355
|
+
signal
|
|
356
|
+
}));
|
|
357
|
+
return this._feed;
|
|
358
|
+
}
|
|
278
359
|
/** Walk commit history from `head` (newest first). */
|
|
279
360
|
async *log(head, max) {
|
|
280
361
|
for await (const { cid, commit } of this.engine.walkParents(CID.parse(head), max)) yield {
|
|
@@ -405,6 +486,25 @@ var Fangorn = class Fangorn {
|
|
|
405
486
|
getDataRegistry() {
|
|
406
487
|
return this.ctx.dataRegistry;
|
|
407
488
|
}
|
|
489
|
+
/** The app (global namespace) this client publishes and reads under. */
|
|
490
|
+
getAppId() {
|
|
491
|
+
return this.ctx.dataRegistry.getAppId();
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Point this client at a different app — by name (`"my-app"`) or by app id —
|
|
495
|
+
* without rebuilding it. The app id prefixes every namespace key, so every
|
|
496
|
+
* read, publish and subscription after this call lands in the new app.
|
|
497
|
+
*
|
|
498
|
+
* Namespace reads are dropped from the cache (its keys are app-agnostic) and
|
|
499
|
+
* the shared `appFeed` is released: a feed watches one app's commit stream, so
|
|
500
|
+
* the next `appFeed()` opens a fresh subscription. Generators already running
|
|
501
|
+
* from `subscribe`/`subscribeApp` keep their original filter until aborted.
|
|
502
|
+
*/
|
|
503
|
+
setAppId(nameOrId) {
|
|
504
|
+
this.ctx.dataRegistry.setAppId(toAppId(nameOrId));
|
|
505
|
+
this.nsCache.clear();
|
|
506
|
+
this._feed = null;
|
|
507
|
+
}
|
|
408
508
|
getStorage() {
|
|
409
509
|
if (!this.ctx.metadataStorage) throw new Error("storage is not configured. Pass { pinata: { ... } } or { signedUrl: { ... } } to Fangorn.create()");
|
|
410
510
|
return this.ctx.metadataStorage;
|
package/lib/feed.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { NamespaceChange } from "./fangorn.js";
|
|
2
|
+
|
|
3
|
+
//#region src/feed.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One shared app-wide commit stream, fanned out to many consumers.
|
|
7
|
+
*
|
|
8
|
+
* `subscribeApp` opens a fresh RPC watch per call. A server that subscribes per
|
|
9
|
+
* connection × per tracked namespace opens N×M pollers for what is the *same*
|
|
10
|
+
* app-level topic filter. This opens exactly one, ref-counted: it starts with
|
|
11
|
+
* the first listener and stops with the last. Consumers filter in memory — the
|
|
12
|
+
* events are already narrowed to the app on-chain.
|
|
13
|
+
*/
|
|
14
|
+
declare class AppFeed {
|
|
15
|
+
private readonly open;
|
|
16
|
+
/** Delay before reopening the watch after it fails or ends. */
|
|
17
|
+
private readonly restartDelayMs;
|
|
18
|
+
private readonly listeners;
|
|
19
|
+
private abort;
|
|
20
|
+
constructor(open: (signal: AbortSignal) => AsyncGenerator<NamespaceChange>, /** Delay before reopening the watch after it fails or ends. */
|
|
21
|
+
restartDelayMs?: number);
|
|
22
|
+
/** Number of live listeners — 0 means the underlying watch is closed. */
|
|
23
|
+
get size(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Register a listener, starting the feed if it was idle. Returns the
|
|
26
|
+
* unsubscribe function; the feed stops once the last listener leaves.
|
|
27
|
+
*/
|
|
28
|
+
on(onChange: (change: NamespaceChange) => void, onError?: (err: Error) => void): () => void;
|
|
29
|
+
private run;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { AppFeed };
|
package/lib/feed.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//#region src/feed.ts
|
|
2
|
+
/**
|
|
3
|
+
* One shared app-wide commit stream, fanned out to many consumers.
|
|
4
|
+
*
|
|
5
|
+
* `subscribeApp` opens a fresh RPC watch per call. A server that subscribes per
|
|
6
|
+
* connection × per tracked namespace opens N×M pollers for what is the *same*
|
|
7
|
+
* app-level topic filter. This opens exactly one, ref-counted: it starts with
|
|
8
|
+
* the first listener and stops with the last. Consumers filter in memory — the
|
|
9
|
+
* events are already narrowed to the app on-chain.
|
|
10
|
+
*/
|
|
11
|
+
var AppFeed = class {
|
|
12
|
+
listeners = /* @__PURE__ */ new Set();
|
|
13
|
+
abort = null;
|
|
14
|
+
constructor(open, restartDelayMs = 2e3) {
|
|
15
|
+
this.open = open;
|
|
16
|
+
this.restartDelayMs = restartDelayMs;
|
|
17
|
+
}
|
|
18
|
+
/** Number of live listeners — 0 means the underlying watch is closed. */
|
|
19
|
+
get size() {
|
|
20
|
+
return this.listeners.size;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Register a listener, starting the feed if it was idle. Returns the
|
|
24
|
+
* unsubscribe function; the feed stops once the last listener leaves.
|
|
25
|
+
*/
|
|
26
|
+
on(onChange, onError) {
|
|
27
|
+
const listener = {
|
|
28
|
+
onChange,
|
|
29
|
+
onError
|
|
30
|
+
};
|
|
31
|
+
this.listeners.add(listener);
|
|
32
|
+
if (!this.abort) this.run();
|
|
33
|
+
return () => {
|
|
34
|
+
this.listeners.delete(listener);
|
|
35
|
+
if (this.listeners.size === 0) {
|
|
36
|
+
this.abort?.abort();
|
|
37
|
+
this.abort = null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
run() {
|
|
42
|
+
const abort = new AbortController();
|
|
43
|
+
this.abort = abort;
|
|
44
|
+
const stopped = () => abort.signal.aborted;
|
|
45
|
+
(async () => {
|
|
46
|
+
while (!stopped()) {
|
|
47
|
+
try {
|
|
48
|
+
for await (const change of this.open(abort.signal)) for (const l of [...this.listeners]) try {
|
|
49
|
+
l.onChange(change);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
l.onError?.(asError(err));
|
|
52
|
+
}
|
|
53
|
+
} catch (err) {
|
|
54
|
+
if (stopped()) return;
|
|
55
|
+
for (const l of [...this.listeners]) l.onError?.(asError(err));
|
|
56
|
+
}
|
|
57
|
+
if (!stopped()) await new Promise((r) => setTimeout(r, this.restartDelayMs));
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const asError = (err) => err instanceof Error ? err : new Error(String(err));
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { AppFeed };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppConfig, FangornConfig, SupportedNetworks, appId } from "./config.js";
|
|
1
|
+
import { AppConfig, DEFAULT_APP, FangornConfig, SupportedNetworks, appId, toAppId } from "./config.js";
|
|
2
2
|
import { DATA_REGISTRY_ABI } from "./contracts/data-registry/abi.js";
|
|
3
3
|
import { CommitFilter, DataRegistryClient, PreparedTx, PublisherStatus, StateCommittedLog, namespaceKey, subspaceId } from "./contracts/data-registry/index.js";
|
|
4
4
|
import { SettlementRegistryClient } from "./contracts/settlement-registry/index.js";
|
|
@@ -6,9 +6,10 @@ import { SETTLEMENT_REGISTRY_ABI } from "./contracts/settlement-registry/abi.js"
|
|
|
6
6
|
import "./contracts/index.js";
|
|
7
7
|
import { AccessRequest, AccessSigner, DecryptHandleParams, EncryptAndUploadParams, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf } from "./crypto/encryption.js";
|
|
8
8
|
import { FangornContext, FangornCreateOptions, StorageConfig } from "./types/index.js";
|
|
9
|
+
import { AppFeed } from "./feed.js";
|
|
9
10
|
import { Fangorn, NamespaceChange, SubscribeOptions } from "./fangorn.js";
|
|
10
11
|
import { AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, Edge, FileFacade, ProcessorResult, Vertex, buildAssetGraph, buildMarkdownGraph, extractMarkdownLinks } from "./harness/index.js";
|
|
11
12
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
12
13
|
import { DEFAULT_SIGNED_URL_WORKER, SignedUrlBackend, SignedUrlSigner } from "./providers/storage/signed-url.js";
|
|
13
14
|
import { MAX_UPLOAD_ATTEMPTS, deserialize, fetchRawByCid, isTransientUpload, retrieveByCid, serialize, withUploadRetry } from "./providers/storage/utils.js";
|
|
14
|
-
export { AccessRequest, AccessSigner, AppConfig, AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, CommitFilter, DATA_REGISTRY_ABI, DEFAULT_SIGNED_URL_WORKER, DataRegistryClient, DecryptHandleParams, Edge, EncryptAndUploadParams, Fangorn, FangornConfig, FangornContext, FangornCreateOptions, FileFacade, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, MAX_UPLOAD_ATTEMPTS, NamespaceChange, PinataBackend, PreparedTx, ProcessorResult, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, SignedUrlBackend, SignedUrlSigner, StateCommittedLog, StorageConfig, SubscribeOptions, SupportedNetworks, Vertex, X25519_PUBKEY_LENGTH, accessMessageHash, appId, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, fetchRawByCid, isTransientUpload, namespaceKey, retrieveByCid, seal, sealSelf, serialize, sha256Hex, subspaceId, unseal, unsealSelf, withUploadRetry };
|
|
15
|
+
export { AccessRequest, AccessSigner, AppConfig, AppFeed, AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, CommitFilter, DATA_REGISTRY_ABI, DEFAULT_APP, DEFAULT_SIGNED_URL_WORKER, DataRegistryClient, DecryptHandleParams, Edge, EncryptAndUploadParams, Fangorn, FangornConfig, FangornContext, FangornCreateOptions, FileFacade, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, MAX_UPLOAD_ATTEMPTS, NamespaceChange, PinataBackend, PreparedTx, ProcessorResult, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, SignedUrlBackend, SignedUrlSigner, StateCommittedLog, StorageConfig, SubscribeOptions, SupportedNetworks, Vertex, X25519_PUBKEY_LENGTH, accessMessageHash, appId, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, fetchRawByCid, isTransientUpload, namespaceKey, retrieveByCid, seal, sealSelf, serialize, sha256Hex, subspaceId, toAppId, unseal, unsealSelf, withUploadRetry };
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FangornConfig, SupportedNetworks, appId } from "./config.js";
|
|
1
|
+
import { DEFAULT_APP, FangornConfig, SupportedNetworks, appId, toAppId } from "./config.js";
|
|
2
2
|
import { DEFAULT_SIGNED_URL_WORKER, SignedUrlBackend } from "./providers/storage/signed-url.js";
|
|
3
3
|
import { MAX_UPLOAD_ATTEMPTS, deserialize, fetchRawByCid, isTransientUpload, retrieveByCid, serialize, withUploadRetry } from "./providers/storage/utils.js";
|
|
4
4
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
@@ -7,8 +7,9 @@ import { DataRegistryClient, PublisherStatus, namespaceKey, subspaceId } from ".
|
|
|
7
7
|
import { SETTLEMENT_REGISTRY_ABI } from "./contracts/settlement-registry/abi.js";
|
|
8
8
|
import { SettlementRegistryClient } from "./contracts/settlement-registry/index.js";
|
|
9
9
|
import "./contracts/index.js";
|
|
10
|
+
import { AppFeed } from "./feed.js";
|
|
10
11
|
import { Fangorn } from "./fangorn.js";
|
|
11
12
|
import { buildAssetGraph, buildMarkdownGraph, extractMarkdownLinks } from "./harness/index.js";
|
|
12
13
|
import { GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf } from "./crypto/encryption.js";
|
|
13
14
|
|
|
14
|
-
export { DATA_REGISTRY_ABI, DEFAULT_SIGNED_URL_WORKER, DataRegistryClient, Fangorn, FangornConfig, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, MAX_UPLOAD_ATTEMPTS, PinataBackend, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, SignedUrlBackend, SupportedNetworks, X25519_PUBKEY_LENGTH, accessMessageHash, appId, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, fetchRawByCid, isTransientUpload, namespaceKey, retrieveByCid, seal, sealSelf, serialize, sha256Hex, subspaceId, unseal, unsealSelf, withUploadRetry };
|
|
15
|
+
export { AppFeed, DATA_REGISTRY_ABI, DEFAULT_APP, DEFAULT_SIGNED_URL_WORKER, DataRegistryClient, Fangorn, FangornConfig, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, MAX_UPLOAD_ATTEMPTS, PinataBackend, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, SignedUrlBackend, SupportedNetworks, X25519_PUBKEY_LENGTH, accessMessageHash, appId, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, fetchRawByCid, isTransientUpload, namespaceKey, retrieveByCid, seal, sealSelf, serialize, sha256Hex, subspaceId, toAppId, unseal, unsealSelf, withUploadRetry };
|
package/lib/test/testbed.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FangornConfig
|
|
1
|
+
import { FangornConfig } from "../config.js";
|
|
2
2
|
import { Fangorn } from "../fangorn.js";
|
|
3
3
|
|
|
4
4
|
//#region src/test/testbed.ts
|
|
@@ -12,15 +12,12 @@ var TestBed = class TestBed {
|
|
|
12
12
|
* own. Pass an unclaimed name to exercise the AppNotFound path.
|
|
13
13
|
*/
|
|
14
14
|
static init(sks, appName) {
|
|
15
|
-
const config = appName ? {
|
|
16
|
-
...FangornConfig,
|
|
17
|
-
appId: appId(appName)
|
|
18
|
-
} : FangornConfig;
|
|
19
15
|
const f_list = [];
|
|
20
16
|
sks.forEach((sk) => {
|
|
21
17
|
f_list.push(Fangorn.create({
|
|
22
18
|
privateKey: sk,
|
|
23
|
-
config,
|
|
19
|
+
config: FangornConfig,
|
|
20
|
+
appId: appName,
|
|
24
21
|
storage: { pinata: {
|
|
25
22
|
jwt: process.env.PINATA_JWT ?? "",
|
|
26
23
|
gateway: process.env.PINATA_GATEWAY ?? ""
|
package/lib/types/index.d.ts
CHANGED