@fangorn-network/sdk 2026.9.22-dev → 2026.10.19-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 +177 -192
- package/lib/cli/cli.d.ts +0 -0
- package/lib/cli/cli.js +165 -194
- package/lib/config.d.ts +32 -27
- package/lib/config.js +17 -7
- package/lib/contracts/app-registry/abi.d.ts +1033 -277
- package/lib/contracts/app-registry/abi.js +1214 -335
- package/lib/contracts/app-registry/index.d.ts +105 -65
- package/lib/contracts/app-registry/index.js +182 -175
- package/lib/contracts/data-registry/abi.d.ts +166 -187
- package/lib/contracts/data-registry/abi.js +207 -237
- package/lib/contracts/data-registry/index.d.ts +4 -151
- package/lib/contracts/data-registry/index.js +5 -279
- package/lib/contracts/index.d.ts +6 -7
- package/lib/contracts/index.js +6 -8
- package/lib/contracts/logs.d.ts +4 -1
- package/lib/contracts/logs.js +9 -2
- package/lib/contracts/membership-registry/abi.d.ts +1145 -0
- package/lib/contracts/membership-registry/abi.js +1333 -0
- package/lib/contracts/membership-registry/index.d.ts +79 -0
- package/lib/contracts/membership-registry/index.js +104 -0
- package/lib/contracts/types.d.ts +3 -1
- package/lib/contracts/types.js +3 -1
- package/lib/contracts/write.d.ts +34 -2
- package/lib/contracts/write.js +48 -1
- package/lib/crypto/encryption.js +4 -4
- package/lib/engine/car.d.ts +2 -2
- package/lib/engine/car.js +10 -5
- package/lib/engine/index.d.ts +99 -143
- package/lib/engine/index.js +156 -295
- package/lib/engine/records.d.ts +110 -0
- package/lib/engine/records.js +165 -0
- package/lib/engine/test-helpers.d.ts +7 -14
- package/lib/engine/test-helpers.js +7 -18
- package/lib/fangorn.d.ts +129 -156
- package/lib/fangorn.js +250 -250
- package/lib/feed.d.ts +7 -4
- package/lib/feed.js +4 -1
- package/lib/index.d.ts +9 -9
- package/lib/index.js +8 -9
- 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/signed-url.d.ts +16 -6
- package/lib/providers/storage/signed-url.js +20 -8
- package/lib/registry.d.ts +116 -0
- package/lib/registry.js +182 -0
- package/lib/test/mem-registry.d.ts +51 -0
- package/lib/test/mem-registry.js +107 -0
- package/lib/test/sdk-suite.d.ts +13 -0
- package/lib/test/sdk-suite.js +226 -0
- package/lib/test/testbed.d.ts +16411 -61
- package/lib/test/testbed.js +190 -84
- package/lib/types/index.d.ts +13 -11
- package/package.json +89 -73
- package/lib/contracts/data-registry/test.d.ts +0 -1
- package/lib/contracts/data-registry/test.js +0 -1
- package/lib/contracts/settlement-registry/abi.d.ts +0 -466
- package/lib/contracts/settlement-registry/abi.js +0 -535
- package/lib/contracts/settlement-registry/index.d.ts +0 -163
- package/lib/contracts/settlement-registry/index.js +0 -264
- package/lib/contracts/subscription-registry/abi.d.ts +0 -179
- package/lib/contracts/subscription-registry/abi.js +0 -195
- package/lib/contracts/subscription-registry/index.d.ts +0 -59
- package/lib/contracts/subscription-registry/index.js +0 -116
- package/lib/engine/graph.d.ts +0 -70
- package/lib/engine/graph.js +0 -73
|
@@ -18,22 +18,32 @@ interface SignedUrlSigner {
|
|
|
18
18
|
/**
|
|
19
19
|
* Storage backend for users who don't bring their own Pinata JWT.
|
|
20
20
|
*
|
|
21
|
-
* Uploads go through the pinata-url-provider Worker
|
|
21
|
+
* Uploads go through the pinata-url-provider Worker. Prove wallet ownership by
|
|
22
22
|
* signing the worker's challenge, and it hands back a short-lived, single-use
|
|
23
|
-
* Pinata presigned upload URL that
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* Pinata presigned upload URL that users POST the file to.
|
|
24
|
+
* Reads need no auth: every Fangorn block is public content, so it's
|
|
25
|
+
* fetched straight from a public IPFS gateway by CID (privacy is cryptographic,
|
|
26
26
|
* not access-controlled). Each upload runs a fresh handshake because the
|
|
27
27
|
* presigned URL is one-time and expires in minutes.
|
|
28
28
|
*
|
|
29
29
|
* JWT-only operations (server-side metadata queries / deletes) are unavailable
|
|
30
|
-
* here
|
|
30
|
+
* here. Switch to a PinataBackend they're needed.
|
|
31
|
+
*
|
|
32
|
+
* Apps provide storage: every upload is billed to the client's app, whose
|
|
33
|
+
* subscription pays for it. Each request carries the app id, the signer must be
|
|
34
|
+
* one of that app's publishers, and the pins are filed under the app's group.
|
|
35
|
+
* There is no app-less upload: with no app this refuses before contacting the
|
|
36
|
+
* worker. The id is read per request so a later `setAppId` takes effect
|
|
37
|
+
* immediately.
|
|
31
38
|
*/
|
|
32
39
|
declare class SignedUrlBackend implements MetadataStorage {
|
|
33
40
|
private readonly signer;
|
|
34
41
|
private readonly gateway;
|
|
42
|
+
/** The client's current app id, read per upload (see setAppId). */
|
|
43
|
+
private readonly appId;
|
|
35
44
|
private readonly workerUrl;
|
|
36
|
-
constructor(workerUrl: string | undefined, signer: SignedUrlSigner, gateway: string)
|
|
45
|
+
constructor(workerUrl: string | undefined, signer: SignedUrlSigner, gateway: string, /** The client's current app id, read per upload (see setAppId). */
|
|
46
|
+
appId: () => Hex | undefined);
|
|
37
47
|
/** Run the worker challenge handshake → one-time presigned upload URL + network. */
|
|
38
48
|
private requestUploadUrl;
|
|
39
49
|
private workerJson;
|
|
@@ -15,31 +15,42 @@ function workerError(r) {
|
|
|
15
15
|
/**
|
|
16
16
|
* Storage backend for users who don't bring their own Pinata JWT.
|
|
17
17
|
*
|
|
18
|
-
* Uploads go through the pinata-url-provider Worker
|
|
18
|
+
* Uploads go through the pinata-url-provider Worker. Prove wallet ownership by
|
|
19
19
|
* signing the worker's challenge, and it hands back a short-lived, single-use
|
|
20
|
-
* Pinata presigned upload URL that
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* Pinata presigned upload URL that users POST the file to.
|
|
21
|
+
* Reads need no auth: every Fangorn block is public content, so it's
|
|
22
|
+
* fetched straight from a public IPFS gateway by CID (privacy is cryptographic,
|
|
23
23
|
* not access-controlled). Each upload runs a fresh handshake because the
|
|
24
24
|
* presigned URL is one-time and expires in minutes.
|
|
25
25
|
*
|
|
26
26
|
* JWT-only operations (server-side metadata queries / deletes) are unavailable
|
|
27
|
-
* here
|
|
27
|
+
* here. Switch to a PinataBackend they're needed.
|
|
28
|
+
*
|
|
29
|
+
* Apps provide storage: every upload is billed to the client's app, whose
|
|
30
|
+
* subscription pays for it. Each request carries the app id, the signer must be
|
|
31
|
+
* one of that app's publishers, and the pins are filed under the app's group.
|
|
32
|
+
* There is no app-less upload: with no app this refuses before contacting the
|
|
33
|
+
* worker. The id is read per request so a later `setAppId` takes effect
|
|
34
|
+
* immediately.
|
|
28
35
|
*/
|
|
29
36
|
var SignedUrlBackend = class {
|
|
30
37
|
workerUrl;
|
|
31
|
-
constructor(workerUrl, signer, gateway) {
|
|
38
|
+
constructor(workerUrl, signer, gateway, appId) {
|
|
32
39
|
this.signer = signer;
|
|
33
40
|
this.gateway = gateway;
|
|
41
|
+
this.appId = appId;
|
|
34
42
|
this.workerUrl = workerUrl?.trim() ? workerUrl : DEFAULT_SIGNED_URL_WORKER;
|
|
35
43
|
}
|
|
36
44
|
/** Run the worker challenge handshake → one-time presigned upload URL + network. */
|
|
37
45
|
async requestUploadUrl(size, uploadId) {
|
|
38
46
|
const address = this.signer.address;
|
|
47
|
+
const appId = this.appId();
|
|
48
|
+
if (!appId) throw new Error("signed-url uploads are billed to an app: pass `appId` to Fangorn.create() or call setAppId(), or bring your own storage ({ pinata } or { backend })");
|
|
39
49
|
const challenge = await this.workerJson({
|
|
40
50
|
address,
|
|
41
51
|
size,
|
|
42
|
-
uploadId
|
|
52
|
+
uploadId,
|
|
53
|
+
appId
|
|
43
54
|
});
|
|
44
55
|
if (!challenge.challenge) throw new Error(`Presigned-URL worker did not issue a challenge: ${workerError(challenge)}`);
|
|
45
56
|
const signature = await this.signer.signMessage(challenge.challenge);
|
|
@@ -48,7 +59,8 @@ var SignedUrlBackend = class {
|
|
|
48
59
|
message: challenge.challenge,
|
|
49
60
|
signature,
|
|
50
61
|
size,
|
|
51
|
-
uploadId
|
|
62
|
+
uploadId,
|
|
63
|
+
appId
|
|
52
64
|
});
|
|
53
65
|
if (!grant.ok || !grant.uploadUrl) throw new Error(`Presigned-URL worker denied an upload URL: ${workerError(grant)}`);
|
|
54
66
|
return {
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { PreparedTx } from "./contracts/write.js";
|
|
2
|
+
import "./contracts/index.js";
|
|
3
|
+
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
4
|
+
|
|
5
|
+
//#region src/registry.d.ts
|
|
6
|
+
declare const ZERO_HEAD: Hex;
|
|
7
|
+
/** η: epoch length in seconds. e(t) = ⌊t / η⌋. */
|
|
8
|
+
declare const EPOCH_SECONDS = 3600;
|
|
9
|
+
declare function epochOf(unixSeconds: number | bigint): number;
|
|
10
|
+
/** ρ = (P, s): one publisher's repo. */
|
|
11
|
+
interface RepoRef {
|
|
12
|
+
publisher: Address;
|
|
13
|
+
repo: string;
|
|
14
|
+
}
|
|
15
|
+
/** What an exclusion names in an app: a whole publisher, one of its repos, or one record key in a repo. */
|
|
16
|
+
interface Exclusion {
|
|
17
|
+
publisher: Address;
|
|
18
|
+
repo?: string;
|
|
19
|
+
key?: string;
|
|
20
|
+
}
|
|
21
|
+
/** One entry of the update log U, (P, s, i, h, t): a repo's head moved from `parent` to `head`. */
|
|
22
|
+
interface UpdateEntry {
|
|
23
|
+
publisher: Address;
|
|
24
|
+
repo: string;
|
|
25
|
+
/** The head's position in the repo's history, from 1. */
|
|
26
|
+
pos: number;
|
|
27
|
+
parent: Hex;
|
|
28
|
+
head: Hex;
|
|
29
|
+
/** The ordering's clock at the entry (a block number). */
|
|
30
|
+
t: bigint;
|
|
31
|
+
/** Unique per entry, for de-duplication across catch-up and live. */
|
|
32
|
+
id: string;
|
|
33
|
+
}
|
|
34
|
+
/** A slice of the update log: every repo, one publisher's, one repo name across publishers, or one repo. */
|
|
35
|
+
interface LogFilter {
|
|
36
|
+
publisher?: Address;
|
|
37
|
+
repo?: string;
|
|
38
|
+
}
|
|
39
|
+
interface Registry {
|
|
40
|
+
/** e(t) at the ordering's current clock. A commit's epoch must be within W of it. */
|
|
41
|
+
epoch(): Promise<number>;
|
|
42
|
+
/** H(ρ): the committed head, or ZERO_HEAD for a repo never written. */
|
|
43
|
+
head(ref: RepoRef): Promise<Hex>;
|
|
44
|
+
/**
|
|
45
|
+
* Does app A present `publisher`? Exactly while it provides the publisher's
|
|
46
|
+
* storage (`canPublish`): an app presents its publishers' latest heads, less
|
|
47
|
+
* its exclusions. ponytail: the paper's per-head acceptance (α_A), once apps
|
|
48
|
+
* validate what they present.
|
|
49
|
+
*/
|
|
50
|
+
presents(app: Hex, publisher: Address): Promise<boolean>;
|
|
51
|
+
/** C(A, X). Odd means X is in force (Proposition 3.2). */
|
|
52
|
+
exclusionCount(app: Hex, x: Exclusion): Promise<number>;
|
|
53
|
+
/** update(s, parent → head), as the signer. Valid only while `parent` is the head (Theorem 3). It names no app. */
|
|
54
|
+
update(repo: string, parent: Hex, head: Hex): Promise<Hash>;
|
|
55
|
+
/** `update` as an unsigned transaction for `publisher` to sign. */
|
|
56
|
+
prepareUpdate(publisher: Address, repo: string, parent: Hex, head: Hex): Promise<PreparedTx>;
|
|
57
|
+
exclude(app: Hex, x: Exclusion): Promise<Hash>;
|
|
58
|
+
restore(app: Hex, x: Exclusion): Promise<Hash>;
|
|
59
|
+
/** The entries of a log between two clock values, oldest first. */
|
|
60
|
+
log(filter: LogFilter, from: bigint, to?: bigint): Promise<UpdateEntry[]>;
|
|
61
|
+
/** Follow the log live. Returns an unsubscribe function. */
|
|
62
|
+
watch(filter: LogFilter, onEntry: (entry: UpdateEntry) => void, onError?: (err: Error) => void, pollingInterval?: number): () => void;
|
|
63
|
+
/** The ordering's current clock value (a block number). */
|
|
64
|
+
clock(): Promise<bigint>;
|
|
65
|
+
}
|
|
66
|
+
/** True if exclusion `x` covers record `key` of repo `ref`. */
|
|
67
|
+
declare function covers(x: Exclusion, ref: RepoRef, key?: string): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Is anything of `ref` hidden in app `app`? Checks the publisher and the repo
|
|
70
|
+
* exclusions, the two that can hide a whole repo (ReadSlice line 3).
|
|
71
|
+
*/
|
|
72
|
+
declare function repoExcluded(registry: Registry, app: Hex, ref: RepoRef): Promise<boolean>;
|
|
73
|
+
/** The slot an exclusion is counted in: keccak256(abi.encode(P, N(s), N(k))), zeros for levels it doesn't name. */
|
|
74
|
+
declare function exclusionId(x: Exclusion): Hex;
|
|
75
|
+
interface ChainRegistryConfig {
|
|
76
|
+
dataRegistryContractAddress: Address;
|
|
77
|
+
appRegistryContractAddress: Address;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The registry on the contracts: H and U in the DataRegistry; membership, X and C
|
|
81
|
+
* in the AppRegistry.
|
|
82
|
+
*
|
|
83
|
+
* Gaps against Definition 3.1, each closed by the contract spec
|
|
84
|
+
* (docs/REGISTRY_SPEC.md):
|
|
85
|
+
* • the contract doesn't check the commit's epoch, nor compute the head from
|
|
86
|
+
* (e, parent, root). The client checks the parent before it sends an update;
|
|
87
|
+
* • no per-head acceptance: an app presents the latest heads of the publishers
|
|
88
|
+
* whose storage it provides (`canPublish`). χ_A is the owner's say. No rule
|
|
89
|
+
* contracts, no witnesses;
|
|
90
|
+
* • the protocol admin's network-wide ban, which the paper doesn't have, reads
|
|
91
|
+
* as a publisher exclusion in every app;
|
|
92
|
+
* • the logs are events read through a trusted RPC: no MMR, no proofs.
|
|
93
|
+
*/
|
|
94
|
+
declare class ChainRegistry implements Registry {
|
|
95
|
+
private publicClient;
|
|
96
|
+
private walletClient?;
|
|
97
|
+
private readonly data;
|
|
98
|
+
private readonly apps;
|
|
99
|
+
constructor(config: ChainRegistryConfig, publicClient: PublicClient, walletClient?: WalletClient | undefined);
|
|
100
|
+
epoch(): Promise<number>;
|
|
101
|
+
head(ref: RepoRef): Promise<Hex>;
|
|
102
|
+
presents(app: Hex, publisher: Address): Promise<boolean>;
|
|
103
|
+
exclusionCount(app: Hex, x: Exclusion): Promise<number>;
|
|
104
|
+
update(repo: string, parent: Hex, head: Hex): Promise<Hash>;
|
|
105
|
+
prepareUpdate(publisher: Address, repo: string, parent: Hex, head: Hex): Promise<PreparedTx>;
|
|
106
|
+
exclude(app: Hex, x: Exclusion): Promise<Hash>;
|
|
107
|
+
restore(app: Hex, x: Exclusion): Promise<Hash>;
|
|
108
|
+
private write;
|
|
109
|
+
/** U's `Updated` events, narrowed on-chain by publisher and repo. */
|
|
110
|
+
private source;
|
|
111
|
+
log(filter: LogFilter, from: bigint, to?: bigint): Promise<UpdateEntry[]>;
|
|
112
|
+
watch(filter: LogFilter, onEntry: (entry: UpdateEntry) => void, onError?: (err: Error) => void, pollingInterval?: number): () => void;
|
|
113
|
+
clock(): Promise<bigint>;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
export { ChainRegistry, ChainRegistryConfig, EPOCH_SECONDS, Exclusion, LogFilter, Registry, RepoRef, UpdateEntry, ZERO_HEAD, covers, epochOf, exclusionId, repoExcluded };
|
package/lib/registry.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { APP_REGISTRY_ABI } from "./contracts/app-registry/abi.js";
|
|
2
|
+
import { prepareWrite, requireWallet, sendWrite } from "./contracts/write.js";
|
|
3
|
+
import { getLogsInWindows } from "./contracts/logs.js";
|
|
4
|
+
import { repoId } from "./contracts/data-registry/index.js";
|
|
5
|
+
import { DATA_REGISTRY_ABI } from "./contracts/data-registry/abi.js";
|
|
6
|
+
import "./contracts/index.js";
|
|
7
|
+
import { encodeAbiParameters, keccak256 } from "viem";
|
|
8
|
+
|
|
9
|
+
//#region src/registry.ts
|
|
10
|
+
const ZERO_HEAD = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
11
|
+
/** η: epoch length in seconds. e(t) = ⌊t / η⌋. */
|
|
12
|
+
const EPOCH_SECONDS = 3600;
|
|
13
|
+
function epochOf(unixSeconds) {
|
|
14
|
+
return Math.floor(Number(unixSeconds) / EPOCH_SECONDS);
|
|
15
|
+
}
|
|
16
|
+
/** True if exclusion `x` covers record `key` of repo `ref`. */
|
|
17
|
+
function covers(x, ref, key) {
|
|
18
|
+
if (x.publisher.toLowerCase() !== ref.publisher.toLowerCase()) return false;
|
|
19
|
+
if (x.repo === void 0) return true;
|
|
20
|
+
if (x.repo !== ref.repo) return false;
|
|
21
|
+
return x.key === void 0 || x.key === key;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Is anything of `ref` hidden in app `app`? Checks the publisher and the repo
|
|
25
|
+
* exclusions, the two that can hide a whole repo (ReadSlice line 3).
|
|
26
|
+
*/
|
|
27
|
+
async function repoExcluded(registry, app, ref) {
|
|
28
|
+
return (await Promise.all([registry.exclusionCount(app, { publisher: ref.publisher }), registry.exclusionCount(app, ref)])).some((c) => c % 2 === 1);
|
|
29
|
+
}
|
|
30
|
+
const ZERO = ZERO_HEAD;
|
|
31
|
+
/** The slot an exclusion is counted in: keccak256(abi.encode(P, N(s), N(k))), zeros for levels it doesn't name. */
|
|
32
|
+
function exclusionId(x) {
|
|
33
|
+
return keccak256(encodeAbiParameters([
|
|
34
|
+
{ type: "address" },
|
|
35
|
+
{ type: "bytes32" },
|
|
36
|
+
{ type: "bytes32" }
|
|
37
|
+
], [
|
|
38
|
+
x.publisher,
|
|
39
|
+
x.repo === void 0 ? ZERO : repoId(x.repo),
|
|
40
|
+
x.key === void 0 ? ZERO : repoId(x.key)
|
|
41
|
+
]));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The registry on the contracts: H and U in the DataRegistry; membership, X and C
|
|
45
|
+
* in the AppRegistry.
|
|
46
|
+
*
|
|
47
|
+
* Gaps against Definition 3.1, each closed by the contract spec
|
|
48
|
+
* (docs/REGISTRY_SPEC.md):
|
|
49
|
+
* • the contract doesn't check the commit's epoch, nor compute the head from
|
|
50
|
+
* (e, parent, root). The client checks the parent before it sends an update;
|
|
51
|
+
* • no per-head acceptance: an app presents the latest heads of the publishers
|
|
52
|
+
* whose storage it provides (`canPublish`). χ_A is the owner's say. No rule
|
|
53
|
+
* contracts, no witnesses;
|
|
54
|
+
* • the protocol admin's network-wide ban, which the paper doesn't have, reads
|
|
55
|
+
* as a publisher exclusion in every app;
|
|
56
|
+
* • the logs are events read through a trusted RPC: no MMR, no proofs.
|
|
57
|
+
*/
|
|
58
|
+
var ChainRegistry = class {
|
|
59
|
+
data;
|
|
60
|
+
apps;
|
|
61
|
+
constructor(config, publicClient, walletClient) {
|
|
62
|
+
this.publicClient = publicClient;
|
|
63
|
+
this.walletClient = walletClient;
|
|
64
|
+
this.data = config.dataRegistryContractAddress;
|
|
65
|
+
this.apps = config.appRegistryContractAddress;
|
|
66
|
+
}
|
|
67
|
+
async epoch() {
|
|
68
|
+
return epochOf((await this.publicClient.getBlock()).timestamp);
|
|
69
|
+
}
|
|
70
|
+
async head(ref) {
|
|
71
|
+
const [, head] = await this.publicClient.readContract({
|
|
72
|
+
address: this.data,
|
|
73
|
+
abi: DATA_REGISTRY_ABI,
|
|
74
|
+
functionName: "getHead",
|
|
75
|
+
args: [ref.publisher, repoId(ref.repo)]
|
|
76
|
+
});
|
|
77
|
+
return head;
|
|
78
|
+
}
|
|
79
|
+
presents(app, publisher) {
|
|
80
|
+
return this.publicClient.readContract({
|
|
81
|
+
address: this.apps,
|
|
82
|
+
abi: APP_REGISTRY_ABI,
|
|
83
|
+
functionName: "canPublish",
|
|
84
|
+
args: [app, publisher]
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async exclusionCount(app, x) {
|
|
88
|
+
const [count, banned] = await Promise.all([this.publicClient.readContract({
|
|
89
|
+
address: this.apps,
|
|
90
|
+
abi: APP_REGISTRY_ABI,
|
|
91
|
+
functionName: "exclusionCount",
|
|
92
|
+
args: [app, exclusionId(x)]
|
|
93
|
+
}), x.repo === void 0 ? this.publicClient.readContract({
|
|
94
|
+
address: this.apps,
|
|
95
|
+
abi: APP_REGISTRY_ABI,
|
|
96
|
+
functionName: "globalSuspensions",
|
|
97
|
+
args: [x.publisher]
|
|
98
|
+
}) : false]);
|
|
99
|
+
return banned ? Number(count) | 1 : Number(count);
|
|
100
|
+
}
|
|
101
|
+
update(repo, parent, head) {
|
|
102
|
+
return this.write(this.data, DATA_REGISTRY_ABI, "update", [
|
|
103
|
+
repo,
|
|
104
|
+
parent,
|
|
105
|
+
head
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
prepareUpdate(publisher, repo, parent, head) {
|
|
109
|
+
return prepareWrite(this.publicClient, publisher, this.data, DATA_REGISTRY_ABI, "update", [
|
|
110
|
+
repo,
|
|
111
|
+
parent,
|
|
112
|
+
head
|
|
113
|
+
], { fallbackGas: 200000n });
|
|
114
|
+
}
|
|
115
|
+
exclude(app, x) {
|
|
116
|
+
return this.write(this.apps, APP_REGISTRY_ABI, "exclude", exclusionArgs(app, x));
|
|
117
|
+
}
|
|
118
|
+
restore(app, x) {
|
|
119
|
+
return this.write(this.apps, APP_REGISTRY_ABI, "restore", exclusionArgs(app, x));
|
|
120
|
+
}
|
|
121
|
+
write(address, abi, fn, args) {
|
|
122
|
+
return sendWrite(this.publicClient, requireWallet(this.walletClient, fn), address, abi, fn, args);
|
|
123
|
+
}
|
|
124
|
+
/** U's `Updated` events, narrowed on-chain by publisher and repo. */
|
|
125
|
+
source(filter) {
|
|
126
|
+
const ids = filter.repo === void 0 ? {} : { repo_id: repoId(filter.repo) };
|
|
127
|
+
const who = filter.publisher === void 0 ? {} : { publisher: filter.publisher };
|
|
128
|
+
return {
|
|
129
|
+
address: this.data,
|
|
130
|
+
abi: DATA_REGISTRY_ABI,
|
|
131
|
+
eventName: "Updated",
|
|
132
|
+
args: {
|
|
133
|
+
...who,
|
|
134
|
+
...ids
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
async log(filter, from, to) {
|
|
139
|
+
return (await getLogsInWindows(this.publicClient, from, to, (fromBlock, toBlock) => this.publicClient.getContractEvents({
|
|
140
|
+
...this.source(filter),
|
|
141
|
+
fromBlock,
|
|
142
|
+
toBlock,
|
|
143
|
+
strict: true
|
|
144
|
+
}))).sort((a, b) => a.blockNumber === b.blockNumber ? a.logIndex - b.logIndex : a.blockNumber < b.blockNumber ? -1 : 1).map(toEntry);
|
|
145
|
+
}
|
|
146
|
+
watch(filter, onEntry, onError, pollingInterval) {
|
|
147
|
+
return this.publicClient.watchContractEvent({
|
|
148
|
+
...this.source(filter),
|
|
149
|
+
strict: true,
|
|
150
|
+
pollingInterval,
|
|
151
|
+
onLogs: (logs) => {
|
|
152
|
+
for (const log of logs) onEntry(toEntry(log));
|
|
153
|
+
},
|
|
154
|
+
onError
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
clock() {
|
|
158
|
+
return this.publicClient.getBlockNumber({ cacheTime: 0 });
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
function exclusionArgs(app, x) {
|
|
162
|
+
return [
|
|
163
|
+
app,
|
|
164
|
+
x.publisher,
|
|
165
|
+
x.repo === void 0 ? ZERO : repoId(x.repo),
|
|
166
|
+
x.key === void 0 ? ZERO : repoId(x.key)
|
|
167
|
+
];
|
|
168
|
+
}
|
|
169
|
+
function toEntry(log) {
|
|
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
|
+
id: `${log.transactionHash}:${log.logIndex.toString()}`
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
export { ChainRegistry, EPOCH_SECONDS, ZERO_HEAD, covers, epochOf, exclusionId, repoExcluded };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PreparedTx } from "../contracts/write.js";
|
|
2
|
+
import "../contracts/index.js";
|
|
3
|
+
import { Exclusion, LogFilter, Registry, RepoRef, UpdateEntry } from "../registry.js";
|
|
4
|
+
import { Address, Hash, Hex } from "viem";
|
|
5
|
+
|
|
6
|
+
//#region src/test/mem-registry.d.ts
|
|
7
|
+
/** One ordering and the registry state over it, shared by every signer of a MemRegistry. */
|
|
8
|
+
declare class MemLedger {
|
|
9
|
+
/** H: position and head */
|
|
10
|
+
heads: Map<string, {
|
|
11
|
+
pos: number;
|
|
12
|
+
head: Hex;
|
|
13
|
+
}>;
|
|
14
|
+
/** `app/publisher` for each publisher an app presents (provides storage to) */
|
|
15
|
+
members: Set<string>;
|
|
16
|
+
/** C, per app */
|
|
17
|
+
counts: Map<string, number>;
|
|
18
|
+
/** U */
|
|
19
|
+
entries: UpdateEntry[];
|
|
20
|
+
/** The clock: one tick per entry of the ordering. */
|
|
21
|
+
t: bigint;
|
|
22
|
+
listeners: Set<(entry: UpdateEntry) => void>;
|
|
23
|
+
append(entry: Omit<UpdateEntry, "t" | "id">): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The paper's Apply, signing as `signer`. Membership is whatever the ledger is
|
|
27
|
+
* told (`members`): its rules (invites, terms, suspensions) are the contracts'
|
|
28
|
+
* business. Signers that share a ledger see each other's writes, like wallets on
|
|
29
|
+
* one chain.
|
|
30
|
+
*/
|
|
31
|
+
declare class MemRegistry implements Registry {
|
|
32
|
+
signer: Address;
|
|
33
|
+
ledger: MemLedger;
|
|
34
|
+
now: () => number;
|
|
35
|
+
constructor(signer: Address, ledger?: MemLedger, now?: () => number);
|
|
36
|
+
epoch(): Promise<number>;
|
|
37
|
+
head(ref: RepoRef): Promise<Hex>;
|
|
38
|
+
presents(app: Hex, publisher: Address): Promise<boolean>;
|
|
39
|
+
exclusionCount(app: Hex, x: Exclusion): Promise<number>;
|
|
40
|
+
update(repo: string, parent: Hex, head: Hex): Promise<Hash>;
|
|
41
|
+
prepareUpdate(): Promise<PreparedTx>;
|
|
42
|
+
exclude(app: Hex, x: Exclusion): Promise<Hash>;
|
|
43
|
+
restore(app: Hex, x: Exclusion): Promise<Hash>;
|
|
44
|
+
/** exclude needs an even count, restore an odd one; both add one. */
|
|
45
|
+
private flip;
|
|
46
|
+
log(filter: LogFilter, from: bigint, to?: bigint): Promise<UpdateEntry[]>;
|
|
47
|
+
watch(filter: LogFilter, onEntry: (entry: UpdateEntry) => void): () => void;
|
|
48
|
+
clock(): Promise<bigint>;
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { MemLedger, MemRegistry };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { ZERO_HEAD, epochOf } from "../registry.js";
|
|
2
|
+
|
|
3
|
+
//#region src/test/mem-registry.ts
|
|
4
|
+
const refKey = (r) => `${r.publisher.toLowerCase()}/${r.repo}`;
|
|
5
|
+
const exclKey = (x) => `${x.publisher.toLowerCase()}/${x.repo ?? ""}/${x.key ?? ""}`;
|
|
6
|
+
/** One ordering and the registry state over it, shared by every signer of a MemRegistry. */
|
|
7
|
+
var MemLedger = class {
|
|
8
|
+
/** H: position and head */
|
|
9
|
+
heads = /* @__PURE__ */ new Map();
|
|
10
|
+
/** `app/publisher` for each publisher an app presents (provides storage to) */
|
|
11
|
+
members = /* @__PURE__ */ new Set();
|
|
12
|
+
/** C, per app */
|
|
13
|
+
counts = /* @__PURE__ */ new Map();
|
|
14
|
+
/** U */
|
|
15
|
+
entries = [];
|
|
16
|
+
/** The clock: one tick per entry of the ordering. */
|
|
17
|
+
t = 0n;
|
|
18
|
+
listeners = /* @__PURE__ */ new Set();
|
|
19
|
+
append(entry) {
|
|
20
|
+
this.t++;
|
|
21
|
+
const e = {
|
|
22
|
+
...entry,
|
|
23
|
+
t: this.t,
|
|
24
|
+
id: this.entries.length.toString()
|
|
25
|
+
};
|
|
26
|
+
this.entries.push(e);
|
|
27
|
+
for (const listener of this.listeners) listener(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const matches = (filter, e) => (!filter.publisher || e.publisher.toLowerCase() === filter.publisher.toLowerCase()) && (!filter.repo || e.repo === filter.repo);
|
|
31
|
+
/**
|
|
32
|
+
* The paper's Apply, signing as `signer`. Membership is whatever the ledger is
|
|
33
|
+
* told (`members`): its rules (invites, terms, suspensions) are the contracts'
|
|
34
|
+
* business. Signers that share a ledger see each other's writes, like wallets on
|
|
35
|
+
* one chain.
|
|
36
|
+
*/
|
|
37
|
+
var MemRegistry = class {
|
|
38
|
+
constructor(signer, ledger = new MemLedger(), now = () => Math.floor(Date.now() / 1e3)) {
|
|
39
|
+
this.signer = signer;
|
|
40
|
+
this.ledger = ledger;
|
|
41
|
+
this.now = now;
|
|
42
|
+
}
|
|
43
|
+
epoch() {
|
|
44
|
+
return Promise.resolve(epochOf(this.now()));
|
|
45
|
+
}
|
|
46
|
+
head(ref) {
|
|
47
|
+
return Promise.resolve(this.ledger.heads.get(refKey(ref))?.head ?? ZERO_HEAD);
|
|
48
|
+
}
|
|
49
|
+
presents(app, publisher) {
|
|
50
|
+
return Promise.resolve(this.ledger.members.has(`${app}/${publisher.toLowerCase()}`));
|
|
51
|
+
}
|
|
52
|
+
exclusionCount(app, x) {
|
|
53
|
+
return Promise.resolve(this.ledger.counts.get(`${app}/${exclKey(x)}`) ?? 0);
|
|
54
|
+
}
|
|
55
|
+
async update(repo, parent, head) {
|
|
56
|
+
const ref = {
|
|
57
|
+
publisher: this.signer,
|
|
58
|
+
repo
|
|
59
|
+
};
|
|
60
|
+
if (await this.head(ref) !== parent) throw new Error("StaleStateRoot");
|
|
61
|
+
const pos = (this.ledger.heads.get(refKey(ref))?.pos ?? 0) + 1;
|
|
62
|
+
this.ledger.heads.set(refKey(ref), {
|
|
63
|
+
pos,
|
|
64
|
+
head
|
|
65
|
+
});
|
|
66
|
+
this.ledger.append({
|
|
67
|
+
publisher: this.signer,
|
|
68
|
+
repo,
|
|
69
|
+
pos,
|
|
70
|
+
parent,
|
|
71
|
+
head
|
|
72
|
+
});
|
|
73
|
+
return "0x00";
|
|
74
|
+
}
|
|
75
|
+
prepareUpdate() {
|
|
76
|
+
return Promise.reject(/* @__PURE__ */ new Error("MemRegistry has no transactions to prepare"));
|
|
77
|
+
}
|
|
78
|
+
exclude(app, x) {
|
|
79
|
+
return this.flip(app, x, 0);
|
|
80
|
+
}
|
|
81
|
+
restore(app, x) {
|
|
82
|
+
return this.flip(app, x, 1);
|
|
83
|
+
}
|
|
84
|
+
/** exclude needs an even count, restore an odd one; both add one. */
|
|
85
|
+
async flip(app, x, parity) {
|
|
86
|
+
const n = await this.exclusionCount(app, x);
|
|
87
|
+
if (n % 2 !== parity) throw new Error(parity ? "NotExcluded" : "AlreadyExcluded");
|
|
88
|
+
this.ledger.counts.set(`${app}/${exclKey(x)}`, n + 1);
|
|
89
|
+
return "0x00";
|
|
90
|
+
}
|
|
91
|
+
log(filter, from, to) {
|
|
92
|
+
return Promise.resolve(this.ledger.entries.filter((e) => e.t >= from && (to === void 0 || e.t <= to) && matches(filter, e)));
|
|
93
|
+
}
|
|
94
|
+
watch(filter, onEntry) {
|
|
95
|
+
const listener = (e) => {
|
|
96
|
+
if (matches(filter, e)) onEntry(e);
|
|
97
|
+
};
|
|
98
|
+
this.ledger.listeners.add(listener);
|
|
99
|
+
return () => this.ledger.listeners.delete(listener);
|
|
100
|
+
}
|
|
101
|
+
clock() {
|
|
102
|
+
return Promise.resolve(this.ledger.t);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
export { MemLedger, MemRegistry };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Fangorn, RepoChange } from "../fangorn.js";
|
|
2
|
+
import { Network } from "./testbed.js";
|
|
3
|
+
import { Hex } from "viem";
|
|
4
|
+
|
|
5
|
+
//#region src/test/sdk-suite.d.ts
|
|
6
|
+
/** The app a test client was made under. */
|
|
7
|
+
declare function appOf(f: Fangorn): Hex;
|
|
8
|
+
declare const head: (f: Fangorn, repo: string) => Promise<`0x${string}`>;
|
|
9
|
+
/** Take the first n entries of the stream and then stop. */
|
|
10
|
+
declare function take(stream: AsyncGenerator<RepoChange>, n: number): Promise<RepoChange[]>;
|
|
11
|
+
declare function sdkSuite(makeNetwork: () => Promise<Network>, timeout?: number): void;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { appOf, head, sdkSuite, take };
|