@fangorn-network/sdk 2026.11.3-dev → 2026.11.3

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 CHANGED
@@ -33,9 +33,15 @@ await fangorn.uploadBatch("rusty-anchor", [
33
33
  ]);
34
34
 
35
35
  // Anyone, from the head alone:
36
- const { contents } = await fangorn.readRepo(fangorn.getAddress(), "rusty-anchor");
37
-
38
- for await (const change of fangorn.subscribe({ repo: "rusty-anchor", owner: fangorn.getAddress() })) {
36
+ const { contents } = await fangorn.readRepo(
37
+ fangorn.getAddress(),
38
+ "rusty-anchor",
39
+ );
40
+
41
+ for await (const change of fangorn.subscribe({
42
+ repo: "rusty-anchor",
43
+ owner: fangorn.getAddress(),
44
+ })) {
39
45
  console.log(change.puts, change.deletes);
40
46
  }
41
47
  ```
package/lib/cli/cli.js CHANGED
@@ -2,8 +2,9 @@
2
2
  import getNetwork, { nonBlank } from "../config.js";
3
3
  import { agentCardUrl, parseAgentId } from "../erc8004.js";
4
4
  import { Fangorn } from "../fangorn.js";
5
+ import { sealRecords } from "./sell.js";
5
6
  import { handleCancel } from "./index.js";
6
- import { createPublicClient, formatEther, http, isAddress } from "viem";
7
+ import { createPublicClient, formatEther, http, isAddress, parseUnits } from "viem";
7
8
  import { privateKeyToAccount } from "viem/accounts";
8
9
  import { Command } from "commander";
9
10
  import { confirm, intro, outro, spinner, text } from "@clack/prompts";
@@ -265,16 +266,30 @@ program.command("repo").description("Repository operations").command("init").des
265
266
  process.exit(1);
266
267
  }
267
268
  });
268
- program.command("commit").description("Put/delete a namespace's records in a new local commit (does not push)").argument("<file>", "JSON file: {\"records\":[{\"id\",\"type\",\"payload\"}],\"deletes\":[\"id\"]}").option("--replace", "Snapshot semantics: the file replaces the namespace's previous contents (omitted entries are removed)").action(async (file, options) => {
269
+ program.command("commit").description("Put/delete a namespace's records in a new local commit (does not push)").argument("<file>", "JSON file: {\"records\":[{\"id\",\"type\",\"payload\"}],\"deletes\":[\"id\"]}").option("--replace", "Snapshot semantics: the file replaces the namespace's previous contents (omitted entries are removed)").option("--paid <fields>", "Comma-separated payload fields to sell: sealed (field-v1) and priced with signed terms").option("--price <usdc>", "Price of each paid record, in USDC (e.g. 0.01)").option("--share <percent>", "Share of each sale paid to the referrer (Fangorn's treasury, via its relay)", "5").option("--deadline-days <days>", "Days the signed terms stay buyable", "365").option("--key-server <url>", "Key server a new deposit is sealed to (an existing deposit keeps its own)", "https://keys-test.fangorn.network").action(async (file, options) => {
269
270
  try {
270
271
  if (!existsSync(file)) throw new Error(`file not found: ${file}`);
271
272
  const data = JSON.parse(readFileSync(file, "utf-8"));
272
- const records = data.records ?? [];
273
+ let records = data.records ?? [];
273
274
  const deletes = data.deletes ?? [];
274
275
  if (!Array.isArray(records) || !Array.isArray(deletes) || records.length + deletes.length === 0) throw new Error(`${file} must contain a non-empty "records" or "deletes" array`);
275
276
  const repo = LocalRepo.open();
276
277
  const fangorn = getFangorn();
277
278
  const parent = repo.head() ?? void 0;
279
+ if (options.paid) {
280
+ if (!options.price) throw new Error("--paid needs --price");
281
+ if (parent && parent !== await fangorn.onChainTip(fangorn.getAddress(), repo.namespace())) throw new Error("push your local commits before committing paid records");
282
+ const shareBps = Math.round(Number(options.share) * 100);
283
+ const days = Number(options.deadlineDays);
284
+ if (!Number.isFinite(days) || days <= 0) throw new Error(`--deadline-days must be a positive number, got ${options.deadlineDays}`);
285
+ records = await sealRecords(fangorn, repo.namespace(), records, {
286
+ fields: options.paid.split(",").map((f) => f.trim()).filter(Boolean),
287
+ price: parseUnits(options.price, 6),
288
+ shareBps,
289
+ deadline: BigInt(Math.floor(Date.now() / 1e3 + days * 86400)),
290
+ keyServer: options.keyServer
291
+ });
292
+ }
278
293
  const s = spinner();
279
294
  s.start(parent ? "Committing (building on local HEAD)..." : "Committing (initial)...");
280
295
  const result = await fangorn.commit({
@@ -0,0 +1,28 @@
1
+ import { RecordInput } from "../engine/index.js";
2
+ import { Fangorn } from "../fangorn.js";
3
+
4
+ //#region src/cli/sell.d.ts
5
+ interface SellOptions {
6
+ /** Payload fields to seal. A record with none of them is left as it is. */
7
+ fields: string[];
8
+ /** USDC base units (6 decimals). */
9
+ price: bigint;
10
+ /** The referrer's share, 0..10000. Through Barliman the referrer is Fangorn's treasury. */
11
+ shareBps: number;
12
+ /** Unix seconds after which the terms can't be bought. */
13
+ deadline: bigint;
14
+ /** Key server for a new deposit; the deposit names it for good. An existing deposit keeps its own. */
15
+ keyServer?: string;
16
+ }
17
+ /**
18
+ * Make records sellable: seal `fields` of each record (as JSON, under `field-v1`), and add the
19
+ * publisher's signed terms under "@terms". Returns the records to commit, plus the repo's
20
+ * `@deposit/<generation>` record if the repo had none for this generation.
21
+ *
22
+ * The deposit is read from the repo's on-chain head. Commit and push this before sealing more:
23
+ * a second call before the push makes a second deposit, and the first commit's fields would
24
+ * only open under the deposit it replaced.
25
+ */
26
+ declare function sealRecords(fangorn: Fangorn, repo: string, records: RecordInput[], o: SellOptions): Promise<RecordInput[]>;
27
+ //#endregion
28
+ export { SellOptions, sealRecords };
@@ -0,0 +1,95 @@
1
+ import { repoId } from "../contracts/data-registry/index.js";
2
+ import { WHOLE_REPO, recordKey, termsToJson } from "../contracts/settlement-registry/index.js";
3
+ import { makeDeposit, newRepoKey, requestKey, sealField } from "../crypto/paid.js";
4
+ import { hexToBytes } from "viem";
5
+
6
+ //#region src/cli/sell.ts
7
+ /**
8
+ * Make records sellable: seal `fields` of each record (as JSON, under `field-v1`), and add the
9
+ * publisher's signed terms under "@terms". Returns the records to commit, plus the repo's
10
+ * `@deposit/<generation>` record if the repo had none for this generation.
11
+ *
12
+ * The deposit is read from the repo's on-chain head. Commit and push this before sealing more:
13
+ * a second call before the push makes a second deposit, and the first commit's fields would
14
+ * only open under the deposit it replaced.
15
+ */
16
+ async function sealRecords(fangorn, repo, records, o) {
17
+ const settlement = fangorn.getSettlementRegistry();
18
+ const publisher = fangorn.getAddress();
19
+ const rid = repoId(repo);
20
+ const generation = await settlement.generationOf(publisher, rid);
21
+ const scope = {
22
+ chainId: fangorn.getConfig().chain.id,
23
+ registry: settlement.getAddress(),
24
+ publisher,
25
+ repo: rid,
26
+ generation
27
+ };
28
+ const depositId = `@deposit/${generation.toString()}`;
29
+ const wallet = fangorn.getWalletClient();
30
+ const signer = { signMessage: (m) => wallet.signMessage({
31
+ account: wallet.account ?? publisher,
32
+ ...m
33
+ }) };
34
+ const out = [];
35
+ let kRepo;
36
+ const existing = (await fangorn.engine.readRecord(publisher, repo, depositId).catch(() => null))?.payload;
37
+ if (existing) kRepo = await requestKey({
38
+ deposit: existing,
39
+ key: WHOLE_REPO,
40
+ signer
41
+ });
42
+ else {
43
+ if (!o.keyServer) throw new Error("no key server: pass keyServer (CLI: --key-server)");
44
+ const res = await fetch(`${o.keyServer}/pubkey`);
45
+ if (!res.ok) throw new Error(`key server ${o.keyServer}/pubkey: HTTP ${res.status.toString()}`);
46
+ const { pubkey } = await res.json();
47
+ kRepo = newRepoKey();
48
+ const deposit = makeDeposit({
49
+ kRepo,
50
+ keyServerPubkey: hexToBytes(pubkey),
51
+ keyServer: o.keyServer,
52
+ scope
53
+ });
54
+ out.push({
55
+ id: depositId,
56
+ type: "deposit",
57
+ payload: deposit
58
+ });
59
+ }
60
+ const enc = new TextEncoder();
61
+ for (const r of records) {
62
+ const payload = { ...r.payload };
63
+ const paid = o.fields.filter((f) => f in payload);
64
+ if (!paid.length) {
65
+ out.push(r);
66
+ continue;
67
+ }
68
+ if (!r.id) throw new Error(`a record with paid fields needs an id (type ${r.type})`);
69
+ const key = recordKey(r.id);
70
+ for (const field of paid) payload[field] = await sealField({
71
+ plaintext: enc.encode(JSON.stringify(payload[field])),
72
+ kRepo,
73
+ scope,
74
+ key,
75
+ field,
76
+ storage: fangorn.getStorage()
77
+ });
78
+ payload["@terms"] = termsToJson(await settlement.signTerms({
79
+ repo: rid,
80
+ key,
81
+ price: o.price,
82
+ shareBps: o.shareBps,
83
+ deadline: o.deadline,
84
+ generation
85
+ }));
86
+ out.push({
87
+ ...r,
88
+ payload
89
+ });
90
+ }
91
+ return out;
92
+ }
93
+
94
+ //#endregion
95
+ export { sealRecords };