@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,175 @@
1
+ import { FangornConfig } from "../config.js";
2
+ import { PinataBackend } from "../providers/storage/pinata.js";
3
+ import { DATA_REGISTRY_ABI } from "../contracts/data-registry/abi.js";
4
+ import { SETTLEMENT_REGISTRY_ABI } from "../contracts/settlement-registry/abi.js";
5
+ import "../contracts/index.js";
6
+ import { Fangorn } from "../fangorn.js";
7
+ import { MemLedger, MemRegistry } from "./mem-registry.js";
8
+ import { MemStorage } from "../engine/test-helpers.js";
9
+ import { foundry } from "viem/chains";
10
+ import { createTestClient, encodeFunctionData, getAddress, http, parseEther, publicActions, walletActions } from "viem";
11
+ import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
12
+ import { spawn, spawnSync } from "node:child_process";
13
+ import { existsSync, readFileSync } from "node:fs";
14
+ import { resolve } from "node:path";
15
+
16
+ //#region src/test/testbed.ts
17
+ function memoryNetwork(Registry = MemRegistry) {
18
+ const ledger = new MemLedger();
19
+ const storage = new MemStorage();
20
+ return {
21
+ name: "memory",
22
+ ledger,
23
+ storage,
24
+ client(sk = generatePrivateKey(), R = Registry) {
25
+ return Promise.resolve(Fangorn.create({
26
+ privateKey: sk,
27
+ config: {
28
+ ...FangornConfig,
29
+ fromBlock: 0n
30
+ },
31
+ storage: { backend: storage },
32
+ registry: new R(privateKeyToAccount(sk).address, ledger)
33
+ }));
34
+ },
35
+ clock: () => Promise.resolve(ledger.t),
36
+ stop: () => void 0
37
+ };
38
+ }
39
+ /** The contracts checkout. Override with FANGORN_CONTRACTS_DIR. */
40
+ const CONTRACTS = process.env.FANGORN_CONTRACTS_DIR ?? resolve(import.meta.dirname, "../../../contracts/solidity");
41
+ const has = (cmd) => spawnSync(cmd, ["--version"]).status === 0;
42
+ /**
43
+ * Without anvil, forge or the contracts checkout, a chain suite shows as
44
+ * skipped — except in CI, where it has to fail rather than quietly stop testing.
45
+ */
46
+ const skipChain = !(has("anvil") && has("forge") && existsSync(CONTRACTS)) && !process.env.CI;
47
+ /** anvil's first prefunded account: the deployer, and the protocol admin. */
48
+ const ADMIN_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
49
+ async function startAnvil(chainId) {
50
+ const anvil = spawn("anvil", [
51
+ "--port",
52
+ "0",
53
+ "--chain-id",
54
+ String(chainId)
55
+ ], { stdio: [
56
+ "ignore",
57
+ "pipe",
58
+ "inherit"
59
+ ] });
60
+ return {
61
+ rpcUrl: await new Promise((res, rej) => {
62
+ let out = "";
63
+ anvil.stdout.on("data", (chunk) => {
64
+ out += chunk.toString();
65
+ const listening = /Listening on (\S+)/.exec(out);
66
+ if (listening) res(`http://${listening[1]}`);
67
+ });
68
+ anvil.once("error", rej);
69
+ anvil.once("exit", (code) => {
70
+ rej(/* @__PURE__ */ new Error(`anvil exited (${String(code)}) before it was listening`));
71
+ });
72
+ }),
73
+ anvil
74
+ };
75
+ }
76
+ /** `file`: the .sol it is in, when that isn't `name`.sol. */
77
+ function artifact(name, file = name) {
78
+ const json = JSON.parse(readFileSync(`${CONTRACTS}/out/${file}.sol/${name}.json`, "utf-8"));
79
+ return {
80
+ abi: json.abi,
81
+ bytecode: json.bytecode.object
82
+ };
83
+ }
84
+ /** Pinata when asked for (E2E_STORAGE=pinata) and configured; memory otherwise. */
85
+ function e2eStorage() {
86
+ if (process.env.E2E_STORAGE !== "pinata") return new MemStorage();
87
+ const { PINATA_JWT: jwt, PINATA_GATEWAY: gateway } = process.env;
88
+ if (!jwt || !gateway) throw new Error("E2E_STORAGE=pinata needs PINATA_JWT and PINATA_GATEWAY");
89
+ return new PinataBackend(jwt, gateway);
90
+ }
91
+ /**
92
+ * A fresh anvil with the contracts deployed from source, each behind its
93
+ * ERC-1967 proxy as Deploy.s.sol does, and wired to each other.
94
+ * Call `stop` from `afterAll`.
95
+ */
96
+ async function chainNetwork() {
97
+ if (spawnSync("forge", ["build"], {
98
+ cwd: CONTRACTS,
99
+ stdio: "inherit"
100
+ }).status !== 0) throw new Error(`forge build failed in ${CONTRACTS}`);
101
+ const { rpcUrl, anvil } = await startAnvil(FangornConfig.chain.id);
102
+ const chain = createTestClient({
103
+ mode: "anvil",
104
+ chain: {
105
+ ...foundry,
106
+ id: FangornConfig.chain.id
107
+ },
108
+ transport: http(rpcUrl)
109
+ }).extend(publicActions).extend(walletActions);
110
+ const admin = privateKeyToAccount(ADMIN_KEY);
111
+ const deploy = async (name, args, file) => {
112
+ const { abi, bytecode } = artifact(name, file);
113
+ const hash = await chain.deployContract({
114
+ account: admin,
115
+ abi,
116
+ bytecode,
117
+ args
118
+ });
119
+ const { contractAddress } = await chain.waitForTransactionReceipt({ hash });
120
+ if (!contractAddress) throw new Error(`${name} did not deploy`);
121
+ return getAddress(contractAddress);
122
+ };
123
+ const proxied = async (name, init) => deploy("ERC1967Proxy", [await deploy(name, []), init]);
124
+ const data = await proxied("DataRegistry", encodeFunctionData({
125
+ abi: DATA_REGISTRY_ABI,
126
+ functionName: "initialize",
127
+ args: [admin.address]
128
+ }));
129
+ const usdc = await deploy("MockUSDC", [], "Mocks");
130
+ const settlement = await proxied("SettlementRegistry", encodeFunctionData({
131
+ abi: SETTLEMENT_REGISTRY_ABI,
132
+ functionName: "initialize",
133
+ args: [
134
+ admin.address,
135
+ usdc,
136
+ await deploy("MockSemaphore", [], "Mocks")
137
+ ]
138
+ }));
139
+ const config = {
140
+ ...FangornConfig,
141
+ rpcUrl,
142
+ dataRegistryContractAddress: data,
143
+ settlementRegistryContractAddress: settlement,
144
+ fromBlock: 0n
145
+ };
146
+ const storage = e2eStorage();
147
+ /** A wallet with gas: `sk`, or a new one. */
148
+ const wallet = async (sk = generatePrivateKey()) => {
149
+ await chain.setBalance({
150
+ address: privateKeyToAccount(sk).address,
151
+ value: parseEther("1")
152
+ });
153
+ return sk;
154
+ };
155
+ /** A client with gas. */
156
+ const client = async (sk) => Fangorn.create({
157
+ privateKey: await wallet(sk),
158
+ config,
159
+ storage: { backend: storage }
160
+ });
161
+ return {
162
+ name: "chain",
163
+ storage,
164
+ config,
165
+ chain,
166
+ usdc,
167
+ wallet,
168
+ client,
169
+ clock: () => chain.getBlockNumber({ cacheTime: 0 }),
170
+ stop: () => anvil.kill()
171
+ };
172
+ }
173
+
174
+ //#endregion
175
+ export { MemLedger, MemRegistry, chainNetwork, memoryNetwork, skipChain };
@@ -0,0 +1,37 @@
1
+ import { Registry } from "../contracts/data-registry/index.js";
2
+ import "../contracts/index.js";
3
+ import { MetadataStorage } from "../providers/storage/types.js";
4
+ import { NetworkConfig } from "../config.js";
5
+ import { Hex, PublicClient, WalletClient } from "viem";
6
+
7
+ //#region src/types/index.d.ts
8
+ interface FangornContext {
9
+ config: NetworkConfig;
10
+ walletClient: WalletClient;
11
+ publicClient: PublicClient;
12
+ metadataStorage: MetadataStorage | undefined;
13
+ /** Heads and the update log (WHITEPAPER_1 §3.2). */
14
+ registry: Registry;
15
+ }
16
+ type StorageConfig = {
17
+ pinata: {
18
+ jwt: string;
19
+ gateway: string;
20
+ };
21
+ } | {
22
+ signedUrl: {
23
+ workerUrl?: string;
24
+ gateway?: string;
25
+ };
26
+ } | {
27
+ backend: MetadataStorage;
28
+ };
29
+ interface FangornCreateOptions {
30
+ storage?: StorageConfig;
31
+ config?: NetworkConfig;
32
+ privateKey?: Hex;
33
+ walletClient?: WalletClient;
34
+ registry?: Registry;
35
+ }
36
+ //#endregion
37
+ export { FangornContext, FangornCreateOptions, StorageConfig };
@@ -0,0 +1 @@
1
+ export { };
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@fangorn-network/sdk",
3
+ "version": "0.1.0-dev",
4
+ "description": "decentralized, verifiable and platformless content management framework",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/fangorn-network/fangorn.git"
8
+ },
9
+ "bin": {
10
+ "fangorn": "lib/cli/cli.js"
11
+ },
12
+ "license": "MIT",
13
+ "type": "module",
14
+ "main": "lib/index.js",
15
+ "files": [
16
+ "lib/"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsdown",
20
+ "format": "prettier . --write",
21
+ "lint": "eslint . --max-warnings 0",
22
+ "prepare": "husky",
23
+ "test": "dotenvx run -f .env -- vitest run",
24
+ "test:e2e": "dotenvx run -f .env -- vitest run --config vitest.e2e.config.ts",
25
+ "test:live": "vitest run --config vitest.e2e.config.ts src/e2e/live.e2e.test.ts",
26
+ "abis": "scripts/gen-abis.sh",
27
+ "tsc": "tsc",
28
+ "deploy": "dotenvx run -f .env -- tsx deploy.ts",
29
+ "setup:embeddings": "dotenvx run -f .env -- tsx src/test/setup-embeddings-testdata.ts",
30
+ "cleanup:embeddings": "dotenvx run -f .env -- tsx src/test/cleanup-embeddings-testdata.ts",
31
+ "prepare:cli": "npm run build",
32
+ "fangorn": "dotenvx run -f .env -- node dist/src/cli/cli.js"
33
+ },
34
+ "lint-staged": {
35
+ "*": "prettier --ignore-unknown --write"
36
+ },
37
+ "devDependencies": {
38
+ "@dotenvx/dotenvx": "^1.51.1",
39
+ "@eslint/js": "9.39.1",
40
+ "@noble/ciphers": "^1.3.0",
41
+ "@noble/curves": "^1.2.0",
42
+ "@types/node": "24.10.1",
43
+ "@vitest/coverage-v8": "4.0.15",
44
+ "@vitest/eslint-plugin": "1.5.1",
45
+ "eslint": "9.39.1",
46
+ "husky": "9.1.7",
47
+ "lint-staged": "16.2.7",
48
+ "prettier": "3.7.3",
49
+ "tsdown": "0.16.8",
50
+ "tsx": "^4.21.0",
51
+ "typescript": "5.8.3",
52
+ "typescript-eslint": "8.48.1",
53
+ "vitest": "^4.0.18"
54
+ },
55
+ "packageManager": "pnpm@10.4.0",
56
+ "engines": {
57
+ "node": ">=20.19.0"
58
+ },
59
+ "dependencies": {
60
+ "@clack/prompts": "^1.0.1",
61
+ "@ipld/car": "^5.4.6",
62
+ "@ipld/dag-cbor": "^10.0.1",
63
+ "@ipld/unixfs": "^3.0.0",
64
+ "@noble/ciphers": "^1.3.0",
65
+ "@noble/curves": "^1.9.7",
66
+ "@noble/hashes": "^2.2.0",
67
+ "commander": "^14.0.3",
68
+ "dotenv": "^17.2.3",
69
+ "multiformats": "^14.0.0",
70
+ "pinata": "^2.5.2",
71
+ "viem": "^2.47.1"
72
+ },
73
+ "overrides": {
74
+ "viem": "$viem"
75
+ },
76
+ "pnpm": {
77
+ "overrides": {
78
+ "write-file-atomic": "7.0.0"
79
+ },
80
+ "peerDependencyRules": {
81
+ "allowedVersions": {
82
+ "viem": ">=2.38.3",
83
+ "ethers": ">=5.7.2"
84
+ }
85
+ }
86
+ }
87
+ }