@fangorn-network/sdk 0.1.0-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 (102) hide show
  1. package/LICENSE.md +20 -0
  2. package/README.md +80 -0
  3. package/lib/cli/cli.d.ts +2 -0
  4. package/lib/cli/cli.js +571 -0
  5. package/lib/cli/index.d.ts +10 -0
  6. package/lib/cli/index.js +18 -0
  7. package/lib/cli/sell.d.ts +28 -0
  8. package/lib/cli/sell.js +95 -0
  9. package/lib/cli/stage.d.ts +19 -0
  10. package/lib/cli/stage.js +94 -0
  11. package/lib/config.d.ts +697 -0
  12. package/lib/config.js +45 -0
  13. package/lib/contracts/data-registry/abi.d.ts +442 -0
  14. package/lib/contracts/data-registry/abi.js +508 -0
  15. package/lib/contracts/data-registry/index.d.ts +131 -0
  16. package/lib/contracts/data-registry/index.js +183 -0
  17. package/lib/contracts/index.d.ts +6 -0
  18. package/lib/contracts/index.js +6 -0
  19. package/lib/contracts/logs.d.ts +17 -0
  20. package/lib/contracts/logs.js +28 -0
  21. package/lib/contracts/settlement-registry/abi.d.ts +810 -0
  22. package/lib/contracts/settlement-registry/abi.js +962 -0
  23. package/lib/contracts/settlement-registry/index.d.ts +143 -0
  24. package/lib/contracts/settlement-registry/index.js +213 -0
  25. package/lib/contracts/write.d.ts +59 -0
  26. package/lib/contracts/write.js +102 -0
  27. package/lib/crypto/aes.d.ts +19 -0
  28. package/lib/crypto/aes.js +26 -0
  29. package/lib/crypto/encryption.d.ts +38 -0
  30. package/lib/crypto/encryption.js +68 -0
  31. package/lib/crypto/paid.d.ts +104 -0
  32. package/lib/crypto/paid.js +147 -0
  33. package/lib/crypto/rand.d.ts +5 -0
  34. package/lib/crypto/rand.js +16 -0
  35. package/lib/crypto/types.d.ts +23 -0
  36. package/lib/crypto/types.js +1 -0
  37. package/lib/engine/car.d.ts +14 -0
  38. package/lib/engine/car.js +42 -0
  39. package/lib/engine/index.d.ts +206 -0
  40. package/lib/engine/index.js +377 -0
  41. package/lib/engine/records.d.ts +121 -0
  42. package/lib/engine/records.js +239 -0
  43. package/lib/engine/test-helpers.d.ts +38 -0
  44. package/lib/engine/test-helpers.js +63 -0
  45. package/lib/erc8004.d.ts +56 -0
  46. package/lib/erc8004.js +140 -0
  47. package/lib/fangorn.d.ts +342 -0
  48. package/lib/fangorn.js +642 -0
  49. package/lib/feed.d.ts +34 -0
  50. package/lib/feed.js +67 -0
  51. package/lib/index.d.ts +17 -0
  52. package/lib/index.js +16 -0
  53. package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
  54. package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
  55. package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
  56. package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
  57. package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
  58. package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
  59. package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
  60. package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
  61. package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
  62. package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
  63. package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
  64. package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
  65. package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
  66. package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
  67. package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
  68. package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
  69. package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
  70. package/lib/node_modules/chai/index.js +2874 -0
  71. package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
  72. package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
  73. package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
  74. package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
  75. package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
  76. package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
  77. package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
  78. package/lib/providers/storage/car.d.ts +36 -0
  79. package/lib/providers/storage/car.js +81 -0
  80. package/lib/providers/storage/index.d.ts +5 -0
  81. package/lib/providers/storage/index.js +5 -0
  82. package/lib/providers/storage/pinata.d.ts +53 -0
  83. package/lib/providers/storage/pinata.js +178 -0
  84. package/lib/providers/storage/signed-url.d.ts +54 -0
  85. package/lib/providers/storage/signed-url.js +122 -0
  86. package/lib/providers/storage/types.d.ts +62 -0
  87. package/lib/providers/storage/types.js +1 -0
  88. package/lib/providers/storage/utils.d.ts +30 -0
  89. package/lib/providers/storage/utils.js +107 -0
  90. package/lib/repo/index.d.ts +56 -0
  91. package/lib/repo/index.js +73 -0
  92. package/lib/test/key-server.d.ts +12 -0
  93. package/lib/test/key-server.js +30 -0
  94. package/lib/test/mem-registry.d.ts +46 -0
  95. package/lib/test/mem-registry.js +114 -0
  96. package/lib/test/sdk-suite.d.ts +10 -0
  97. package/lib/test/sdk-suite.js +155 -0
  98. package/lib/test/testbed.d.ts +16786 -0
  99. package/lib/test/testbed.js +175 -0
  100. package/lib/types/index.d.ts +37 -0
  101. package/lib/types/index.js +1 -0
  102. package/package.json +87 -0
@@ -0,0 +1,18 @@
1
+ import "./cli.js";
2
+ import { isCancel } from "@clack/prompts";
3
+
4
+ //#region src/cli/index.ts
5
+ const handleCancel = (value) => {
6
+ if (isCancel(value)) process.exit(0);
7
+ };
8
+ function parseGadgetArg(raw) {
9
+ const match = /^(\w+)\(([^)]*)\)$/.exec(raw);
10
+ if (!match) throw new Error(`Invalid gadget format: "${raw}". Expected e.g. Payment(0.00001)`);
11
+ return {
12
+ type: match[1].toLowerCase(),
13
+ args: match[2].split(",").map((s) => s.trim()).filter(Boolean)
14
+ };
15
+ }
16
+
17
+ //#endregion
18
+ export { handleCancel, parseGadgetArg };
@@ -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 };
@@ -0,0 +1,19 @@
1
+ import { RecordInput } from "../engine/index.js";
2
+
3
+ //#region src/cli/stage.d.ts
4
+ interface Staged {
5
+ records: RecordInput[];
6
+ deletes: string[];
7
+ }
8
+ interface StageOpts {
9
+ type?: string;
10
+ idColumn?: string;
11
+ }
12
+ /** RFC 4180 CSV → one object per row, keyed by the header. */
13
+ declare function parseCsv(text: string): Record<string, string>[];
14
+ /** Files under `path` (file or dir) as root-relative posix paths; dot entries skipped. */
15
+ declare function walk(root: string, path: string): string[];
16
+ /** One file → records. null for binary files (no text to stage). */
17
+ declare function fileToRecords(root: string, rel: string, opts: StageOpts): Staged | null;
18
+ //#endregion
19
+ export { StageOpts, Staged, fileToRecords, parseCsv, walk };
@@ -0,0 +1,94 @@
1
+ import { readFileSync, readdirSync, statSync } from "fs";
2
+ import { basename, extname, join, relative, sep } from "path";
3
+
4
+ //#region src/cli/stage.ts
5
+ /** RFC 4180 CSV → one object per row, keyed by the header. */
6
+ function parseCsv(text) {
7
+ const rows = [];
8
+ let row = [];
9
+ let field = "";
10
+ let quoted = false;
11
+ for (let i = 0; i < text.length; i++) {
12
+ const c = text[i];
13
+ if (quoted) if (c === "\"" && text[i + 1] === "\"") field += text[++i];
14
+ else if (c === "\"") quoted = false;
15
+ else field += c;
16
+ else if (c === "\"") quoted = true;
17
+ else if (c === ",") {
18
+ row.push(field);
19
+ field = "";
20
+ } else if (c === "\n" || c === "\r") {
21
+ if (c === "\r" && text[i + 1] === "\n") i++;
22
+ row.push(field);
23
+ rows.push(row);
24
+ row = [];
25
+ field = "";
26
+ } else field += c;
27
+ }
28
+ if (field || row.length) rows.push([...row, field]);
29
+ const [header = [], ...body] = rows;
30
+ return body.map((r) => Object.fromEntries(header.map((h, i) => [h, r[i] ?? ""])));
31
+ }
32
+ const safeId = (s) => s.replace(/[^\x21-\x7e]/g, "_");
33
+ /** Files under `path` (file or dir) as root-relative posix paths; dot entries skipped. */
34
+ function walk(root, path) {
35
+ const rel = (p) => relative(root, p).split(sep).join("/");
36
+ if (!statSync(path).isDirectory()) return [rel(path)];
37
+ return readdirSync(path, {
38
+ recursive: true,
39
+ withFileTypes: true
40
+ }).filter((e) => e.isFile()).map((e) => rel(join(e.parentPath, e.name))).filter((p) => !p.split("/").some((s) => s.startsWith(".")));
41
+ }
42
+ /** One file → records. null for binary files (no text to stage). */
43
+ function fileToRecords(root, rel, opts) {
44
+ const bytes = readFileSync(join(root, rel));
45
+ if (bytes.subarray(0, 8192).includes(0)) return null;
46
+ const text = bytes.toString("utf-8");
47
+ const ext = extname(rel).toLowerCase();
48
+ const type = opts.type ?? basename(rel, extname(rel));
49
+ const idCol = opts.idColumn ?? "id";
50
+ const many = (rows) => ({
51
+ records: rows.map((payload, i) => {
52
+ const id = payload[idCol];
53
+ return {
54
+ id: safeId((typeof id === "string" || typeof id === "number") && id !== "" ? String(id) : `${rel}#${(i + 1).toString()}`),
55
+ type,
56
+ payload
57
+ };
58
+ }),
59
+ deletes: []
60
+ });
61
+ if (ext === ".csv") return many(parseCsv(text));
62
+ if (ext === ".jsonl" || ext === ".ndjson") return many(text.split("\n").filter((l) => l.trim()).map((l) => JSON.parse(l)));
63
+ if (ext === ".json") {
64
+ const data = JSON.parse(text);
65
+ if (Array.isArray(data)) return many(data);
66
+ const obj = data;
67
+ if (Array.isArray(obj.records) || Array.isArray(obj.deletes)) return {
68
+ records: obj.records ?? [],
69
+ deletes: obj.deletes ?? []
70
+ };
71
+ return {
72
+ records: [{
73
+ id: safeId(rel),
74
+ type,
75
+ payload: data
76
+ }],
77
+ deletes: []
78
+ };
79
+ }
80
+ return {
81
+ records: [{
82
+ id: safeId(rel),
83
+ type: opts.type ?? "file",
84
+ payload: {
85
+ path: rel,
86
+ text
87
+ }
88
+ }],
89
+ deletes: []
90
+ };
91
+ }
92
+
93
+ //#endregion
94
+ export { fileToRecords, parseCsv, walk };