@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.
- package/LICENSE.md +20 -0
- package/README.md +80 -0
- package/lib/cli/cli.d.ts +2 -0
- package/lib/cli/cli.js +571 -0
- package/lib/cli/index.d.ts +10 -0
- package/lib/cli/index.js +18 -0
- package/lib/cli/sell.d.ts +28 -0
- package/lib/cli/sell.js +95 -0
- package/lib/cli/stage.d.ts +19 -0
- package/lib/cli/stage.js +94 -0
- package/lib/config.d.ts +697 -0
- package/lib/config.js +45 -0
- package/lib/contracts/data-registry/abi.d.ts +442 -0
- package/lib/contracts/data-registry/abi.js +508 -0
- package/lib/contracts/data-registry/index.d.ts +131 -0
- package/lib/contracts/data-registry/index.js +183 -0
- package/lib/contracts/index.d.ts +6 -0
- package/lib/contracts/index.js +6 -0
- package/lib/contracts/logs.d.ts +17 -0
- package/lib/contracts/logs.js +28 -0
- package/lib/contracts/settlement-registry/abi.d.ts +810 -0
- package/lib/contracts/settlement-registry/abi.js +962 -0
- package/lib/contracts/settlement-registry/index.d.ts +143 -0
- package/lib/contracts/settlement-registry/index.js +213 -0
- package/lib/contracts/write.d.ts +59 -0
- package/lib/contracts/write.js +102 -0
- package/lib/crypto/aes.d.ts +19 -0
- package/lib/crypto/aes.js +26 -0
- package/lib/crypto/encryption.d.ts +38 -0
- package/lib/crypto/encryption.js +68 -0
- package/lib/crypto/paid.d.ts +104 -0
- package/lib/crypto/paid.js +147 -0
- package/lib/crypto/rand.d.ts +5 -0
- package/lib/crypto/rand.js +16 -0
- package/lib/crypto/types.d.ts +23 -0
- package/lib/crypto/types.js +1 -0
- package/lib/engine/car.d.ts +14 -0
- package/lib/engine/car.js +42 -0
- package/lib/engine/index.d.ts +206 -0
- package/lib/engine/index.js +377 -0
- package/lib/engine/records.d.ts +121 -0
- package/lib/engine/records.js +239 -0
- package/lib/engine/test-helpers.d.ts +38 -0
- package/lib/engine/test-helpers.js +63 -0
- package/lib/erc8004.d.ts +56 -0
- package/lib/erc8004.js +140 -0
- package/lib/fangorn.d.ts +342 -0
- package/lib/fangorn.js +642 -0
- package/lib/feed.d.ts +34 -0
- package/lib/feed.js +67 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +16 -0
- package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
- package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
- package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
- package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
- package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
- package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
- package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
- package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
- package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
- package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
- package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
- package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
- package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
- package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
- package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
- package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
- package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
- package/lib/node_modules/chai/index.js +2874 -0
- package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
- package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
- package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
- package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
- package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
- package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
- package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
- package/lib/providers/storage/car.d.ts +36 -0
- package/lib/providers/storage/car.js +81 -0
- package/lib/providers/storage/index.d.ts +5 -0
- package/lib/providers/storage/index.js +5 -0
- package/lib/providers/storage/pinata.d.ts +53 -0
- package/lib/providers/storage/pinata.js +178 -0
- package/lib/providers/storage/signed-url.d.ts +54 -0
- package/lib/providers/storage/signed-url.js +122 -0
- package/lib/providers/storage/types.d.ts +62 -0
- package/lib/providers/storage/types.js +1 -0
- package/lib/providers/storage/utils.d.ts +30 -0
- package/lib/providers/storage/utils.js +107 -0
- package/lib/repo/index.d.ts +56 -0
- package/lib/repo/index.js +73 -0
- package/lib/test/key-server.d.ts +12 -0
- package/lib/test/key-server.js +30 -0
- package/lib/test/mem-registry.d.ts +46 -0
- package/lib/test/mem-registry.js +114 -0
- package/lib/test/sdk-suite.d.ts +10 -0
- package/lib/test/sdk-suite.js +155 -0
- package/lib/test/testbed.d.ts +16786 -0
- package/lib/test/testbed.js +175 -0
- package/lib/types/index.d.ts +37 -0
- package/lib/types/index.js +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { DATA_REGISTRY_ABI } from "./abi.js";
|
|
2
|
+
import { getLogsInWindows } from "../logs.js";
|
|
3
|
+
import { prepareWrite, requireWallet, sendWrite } from "../write.js";
|
|
4
|
+
import { keccak256, toHex } from "viem";
|
|
5
|
+
|
|
6
|
+
//#region src/contracts/data-registry/index.ts
|
|
7
|
+
/** A repo's on-chain id: keccak256 of its name's UTF-8 bytes, never of the name as hex. */
|
|
8
|
+
function repoId(repo) {
|
|
9
|
+
return keccak256(toHex(repo));
|
|
10
|
+
}
|
|
11
|
+
const ZERO_HEAD = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
12
|
+
/** η: epoch length in seconds. e(t) = ⌊t / η⌋. */
|
|
13
|
+
const EPOCH_SECONDS = 3600;
|
|
14
|
+
/** W: how many epochs a commit may lag the ordering's clock. */
|
|
15
|
+
const WINDOW = 24;
|
|
16
|
+
function epochOf(unixSeconds) {
|
|
17
|
+
return Math.floor(Number(unixSeconds) / EPOCH_SECONDS);
|
|
18
|
+
}
|
|
19
|
+
const UPDATE_TYPES = { Update: [
|
|
20
|
+
{
|
|
21
|
+
name: "publisher",
|
|
22
|
+
type: "address"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "repo",
|
|
26
|
+
type: "string"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "epoch",
|
|
30
|
+
type: "uint64"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "parent",
|
|
34
|
+
type: "bytes32"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "root",
|
|
38
|
+
type: "bytes32"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "car",
|
|
42
|
+
type: "bytes"
|
|
43
|
+
}
|
|
44
|
+
] };
|
|
45
|
+
const dataDomain = (chainId, registry) => ({
|
|
46
|
+
name: "Fangorn Data",
|
|
47
|
+
version: "1",
|
|
48
|
+
chainId,
|
|
49
|
+
verifyingContract: registry
|
|
50
|
+
});
|
|
51
|
+
const updateMessage = (publisher, u) => ({
|
|
52
|
+
publisher,
|
|
53
|
+
repo: u.repo,
|
|
54
|
+
epoch: BigInt(u.epoch),
|
|
55
|
+
parent: u.parent,
|
|
56
|
+
root: u.root,
|
|
57
|
+
car: u.car
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* The registry on the DataRegistry contract.
|
|
61
|
+
*
|
|
62
|
+
* Gap against Definition 3.7: the log is read as events through a trusted RPC,
|
|
63
|
+
* not proven against the contract's mountain range.
|
|
64
|
+
*/
|
|
65
|
+
var ChainRegistry = class {
|
|
66
|
+
data;
|
|
67
|
+
logWindow;
|
|
68
|
+
constructor(config, publicClient, walletClient) {
|
|
69
|
+
this.publicClient = publicClient;
|
|
70
|
+
this.walletClient = walletClient;
|
|
71
|
+
this.data = config.dataRegistryContractAddress;
|
|
72
|
+
this.logWindow = config.logWindow;
|
|
73
|
+
}
|
|
74
|
+
async epoch() {
|
|
75
|
+
return epochOf((await this.publicClient.getBlock()).timestamp);
|
|
76
|
+
}
|
|
77
|
+
async head(ref) {
|
|
78
|
+
const [, head] = await this.publicClient.readContract({
|
|
79
|
+
address: this.data,
|
|
80
|
+
abi: DATA_REGISTRY_ABI,
|
|
81
|
+
functionName: "getHead",
|
|
82
|
+
args: [ref.publisher, repoId(ref.repo)]
|
|
83
|
+
});
|
|
84
|
+
return head;
|
|
85
|
+
}
|
|
86
|
+
update(u) {
|
|
87
|
+
return this.write("update", updateArgs(u));
|
|
88
|
+
}
|
|
89
|
+
prepareUpdate(publisher, u) {
|
|
90
|
+
return prepareWrite(this.publicClient, publisher, this.data, DATA_REGISTRY_ABI, "update", updateArgs(u), { fallbackGas: 300000n });
|
|
91
|
+
}
|
|
92
|
+
async signUpdate(u) {
|
|
93
|
+
const wallet = requireWallet(this.walletClient, "signUpdate");
|
|
94
|
+
if (!wallet.account) throw new Error("signUpdate: the wallet client has no account");
|
|
95
|
+
return wallet.signTypedData({
|
|
96
|
+
account: wallet.account,
|
|
97
|
+
domain: dataDomain(await this.publicClient.getChainId(), this.data),
|
|
98
|
+
types: UPDATE_TYPES,
|
|
99
|
+
primaryType: "Update",
|
|
100
|
+
message: updateMessage(wallet.account.address, u)
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
updateBySig(publisher, u, signature) {
|
|
104
|
+
return this.write("updateBySig", [
|
|
105
|
+
publisher,
|
|
106
|
+
...updateArgs(u),
|
|
107
|
+
signature
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
write(fn, args) {
|
|
111
|
+
return sendWrite(this.publicClient, requireWallet(this.walletClient, fn), this.data, DATA_REGISTRY_ABI, fn, args);
|
|
112
|
+
}
|
|
113
|
+
/** U's `Updated` events, narrowed on-chain by publisher and repo. */
|
|
114
|
+
source(filter) {
|
|
115
|
+
const ids = filter.repo === void 0 ? {} : { repo_id: repoId(filter.repo) };
|
|
116
|
+
const who = filter.publisher === void 0 ? {} : { publisher: filter.publisher };
|
|
117
|
+
return {
|
|
118
|
+
address: this.data,
|
|
119
|
+
abi: DATA_REGISTRY_ABI,
|
|
120
|
+
eventName: "Updated",
|
|
121
|
+
args: {
|
|
122
|
+
...who,
|
|
123
|
+
...ids
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async log(filter, from, to) {
|
|
128
|
+
const sorted = (await getLogsInWindows(this.publicClient, from, to, (fromBlock, toBlock) => this.publicClient.getContractEvents({
|
|
129
|
+
...this.source(filter),
|
|
130
|
+
fromBlock,
|
|
131
|
+
toBlock,
|
|
132
|
+
strict: true
|
|
133
|
+
}), this.logWindow)).sort((a, b) => a.blockNumber === b.blockNumber ? a.logIndex - b.logIndex : a.blockNumber < b.blockNumber ? -1 : 1);
|
|
134
|
+
return this.withTimestamps(sorted);
|
|
135
|
+
}
|
|
136
|
+
/** Logs don't carry their block's timestamp: one block read per distinct block. */
|
|
137
|
+
async withTimestamps(logs) {
|
|
138
|
+
const blocks = [...new Set(logs.map((l) => l.blockNumber))];
|
|
139
|
+
const times = new Map(await Promise.all(blocks.map(async (n) => [n, (await this.publicClient.getBlock({ blockNumber: n })).timestamp])));
|
|
140
|
+
return logs.map((l) => toEntry(l, times.get(l.blockNumber) ?? 0n));
|
|
141
|
+
}
|
|
142
|
+
watch(filter, onEntry, onError, pollingInterval) {
|
|
143
|
+
let queue = Promise.resolve();
|
|
144
|
+
return this.publicClient.watchContractEvent({
|
|
145
|
+
...this.source(filter),
|
|
146
|
+
strict: true,
|
|
147
|
+
pollingInterval,
|
|
148
|
+
onLogs: (logs) => {
|
|
149
|
+
queue = queue.then(() => this.withTimestamps(logs)).then((entries) => {
|
|
150
|
+
for (const e of entries) onEntry(e);
|
|
151
|
+
}).catch((e) => onError?.(e));
|
|
152
|
+
},
|
|
153
|
+
onError
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
clock() {
|
|
157
|
+
return this.publicClient.getBlockNumber({ cacheTime: 0 });
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
function updateArgs(u) {
|
|
161
|
+
return [
|
|
162
|
+
u.repo,
|
|
163
|
+
BigInt(u.epoch),
|
|
164
|
+
u.parent,
|
|
165
|
+
u.root,
|
|
166
|
+
u.car
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
function toEntry(log, timestamp) {
|
|
170
|
+
return {
|
|
171
|
+
publisher: log.args.publisher,
|
|
172
|
+
repo: log.args.repo,
|
|
173
|
+
pos: Number(log.args.pos),
|
|
174
|
+
parent: log.args.parent,
|
|
175
|
+
head: log.args.head,
|
|
176
|
+
t: log.blockNumber,
|
|
177
|
+
timestamp,
|
|
178
|
+
id: `${log.transactionHash}:${log.logIndex.toString()}`
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
export { ChainRegistry, EPOCH_SECONDS, UPDATE_TYPES, WINDOW, ZERO_HEAD, dataDomain, epochOf, repoId, updateMessage };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PreparedTx } from "./write.js";
|
|
2
|
+
import { ChainRegistry, ChainRegistryConfig, CommitUpdate, EPOCH_SECONDS, LogFilter, Registry, RepoRef, UPDATE_TYPES, UpdateEntry, WINDOW, ZERO_HEAD, dataDomain, epochOf, repoId, updateMessage } from "./data-registry/index.js";
|
|
3
|
+
import { DATA_REGISTRY_ABI } from "./data-registry/abi.js";
|
|
4
|
+
import { RegisterArgs, SettleArgs, SettlementPayment, SettlementRegistryClient, TERMS_TYPES, Terms, TermsJson, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson } from "./settlement-registry/index.js";
|
|
5
|
+
import { SETTLEMENT_REGISTRY_ABI } from "./settlement-registry/abi.js";
|
|
6
|
+
export { ChainRegistry, ChainRegistryConfig, CommitUpdate, DATA_REGISTRY_ABI, EPOCH_SECONDS, LogFilter, type PreparedTx, RegisterArgs, Registry, RepoRef, SETTLEMENT_REGISTRY_ABI, SettleArgs, SettlementPayment, SettlementRegistryClient, TERMS_TYPES, Terms, TermsJson, UPDATE_TYPES, UpdateEntry, WHOLE_REPO, WINDOW, ZERO_HEAD, dataDomain, epochOf, recordKey, registerNonce, repoId, settlementDomain, subjectOf, termsFromJson, termsToJson, updateMessage };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DATA_REGISTRY_ABI } from "./data-registry/abi.js";
|
|
2
|
+
import { ChainRegistry, EPOCH_SECONDS, UPDATE_TYPES, WINDOW, ZERO_HEAD, dataDomain, epochOf, repoId, updateMessage } from "./data-registry/index.js";
|
|
3
|
+
import { SETTLEMENT_REGISTRY_ABI } from "./settlement-registry/abi.js";
|
|
4
|
+
import { SettlementRegistryClient, TERMS_TYPES, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson } from "./settlement-registry/index.js";
|
|
5
|
+
|
|
6
|
+
export { ChainRegistry, DATA_REGISTRY_ABI, EPOCH_SECONDS, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, TERMS_TYPES, UPDATE_TYPES, WHOLE_REPO, WINDOW, ZERO_HEAD, dataDomain, epochOf, recordKey, registerNonce, repoId, settlementDomain, subjectOf, termsFromJson, termsToJson, updateMessage };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PublicClient } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/logs.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Run `fetch` over [fromBlock, toBlock] in consecutive block windows and
|
|
7
|
+
* concatenate the results, oldest window first.
|
|
8
|
+
*
|
|
9
|
+
* RPC providers limit how many blocks one `eth_getLogs` call can cover (the
|
|
10
|
+
* Base Sepolia one 50,000, sepolia.base.org 200), and a call past the limit fails
|
|
11
|
+
* outright, taking whatever depends on it (a catch-up, a directory listing) with
|
|
12
|
+
* it. Each network's config says what its RPC takes (`NetworkConfig.logWindow`, passed
|
|
13
|
+
* here as `defaultWindow`); `FANGORN_LOG_WINDOW` overrides it, for a private RPC.
|
|
14
|
+
*/
|
|
15
|
+
declare function getLogsInWindows<T>(publicClient: PublicClient, fromBlock: bigint, toBlock: bigint | undefined, fetch: (fromBlock: bigint, toBlock: bigint) => Promise<T[]>, defaultWindow?: bigint | undefined): Promise<T[]>;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { getLogsInWindows };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { nonBlank } from "../config.js";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/logs.ts
|
|
4
|
+
/**
|
|
5
|
+
* Run `fetch` over [fromBlock, toBlock] in consecutive block windows and
|
|
6
|
+
* concatenate the results, oldest window first.
|
|
7
|
+
*
|
|
8
|
+
* RPC providers limit how many blocks one `eth_getLogs` call can cover (the
|
|
9
|
+
* Base Sepolia one 50,000, sepolia.base.org 200), and a call past the limit fails
|
|
10
|
+
* outright, taking whatever depends on it (a catch-up, a directory listing) with
|
|
11
|
+
* it. Each network's config says what its RPC takes (`NetworkConfig.logWindow`, passed
|
|
12
|
+
* here as `defaultWindow`); `FANGORN_LOG_WINDOW` overrides it, for a private RPC.
|
|
13
|
+
*/
|
|
14
|
+
async function getLogsInWindows(publicClient, fromBlock, toBlock, fetch, defaultWindow = 1000n) {
|
|
15
|
+
const tuned = typeof process !== "undefined" ? nonBlank(process.env.FANGORN_LOG_WINDOW) : void 0;
|
|
16
|
+
const window = tuned === void 0 ? defaultWindow : BigInt(tuned);
|
|
17
|
+
if (window <= 0n) throw new Error(`FANGORN_LOG_WINDOW must be a positive number of blocks, not "${String(tuned)}"`);
|
|
18
|
+
const end = toBlock ?? await publicClient.getBlockNumber({ cacheTime: 0 });
|
|
19
|
+
const out = [];
|
|
20
|
+
for (let start = fromBlock; start <= end; start += window) {
|
|
21
|
+
const stop = start + window - 1n < end ? start + window - 1n : end;
|
|
22
|
+
out.push(...await fetch(start, stop));
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { getLogsInWindows };
|