@fangorn-network/sdk 0.1.0 → 0.1.2
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 +1 -1
- package/lib/cli/cli.js +126 -325
- package/lib/cli/index.d.ts +2 -3
- package/lib/cli/index.js +10 -15
- 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 +71 -81
- package/lib/config.js +22 -38
- package/lib/contracts/data-registry/abi.d.ts +195 -19
- package/lib/contracts/data-registry/abi.js +409 -206
- package/lib/contracts/data-registry/index.d.ts +126 -2
- package/lib/contracts/data-registry/index.js +174 -1
- package/lib/contracts/index.d.ts +4 -7
- package/lib/contracts/index.js +4 -7
- package/lib/contracts/logs.d.ts +1 -1
- package/lib/contracts/logs.js +1 -1
- package/lib/contracts/{membership-registry → settlement-registry}/abi.d.ts +301 -636
- 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/crypto/aes.d.ts +2 -2
- package/lib/crypto/aes.js +4 -4
- package/lib/crypto/encryption.d.ts +8 -109
- package/lib/crypto/encryption.js +16 -114
- package/lib/crypto/paid.d.ts +104 -0
- package/lib/crypto/paid.js +147 -0
- package/lib/engine/car.d.ts +5 -1
- package/lib/engine/car.js +2 -2
- package/lib/engine/index.d.ts +33 -14
- package/lib/engine/index.js +98 -41
- package/lib/engine/records.d.ts +19 -8
- package/lib/engine/records.js +81 -7
- package/lib/engine/test-helpers.d.ts +2 -3
- package/lib/engine/test-helpers.js +7 -5
- package/lib/erc8004.js +6 -6
- package/lib/fangorn.d.ts +65 -86
- package/lib/fangorn.js +115 -153
- package/lib/feed.d.ts +7 -8
- package/lib/feed.js +7 -8
- package/lib/index.d.ts +10 -13
- package/lib/index.js +10 -13
- package/lib/providers/storage/signed-url.d.ts +2 -10
- package/lib/providers/storage/signed-url.js +5 -15
- package/lib/providers/storage/types.d.ts +1 -1
- package/lib/providers/storage/utils.js +5 -3
- package/lib/test/key-server.d.ts +12 -0
- package/lib/test/key-server.js +30 -0
- package/lib/test/mem-registry.d.ts +14 -19
- package/lib/test/mem-registry.js +44 -37
- package/lib/test/sdk-suite.d.ts +1 -4
- package/lib/test/sdk-suite.js +16 -87
- package/lib/test/testbed.d.ts +12 -22
- package/lib/test/testbed.js +29 -56
- package/lib/types/index.d.ts +6 -11
- package/package.json +1 -1
- package/lib/contracts/app-registry/abi.d.ts +0 -1265
- package/lib/contracts/app-registry/abi.js +0 -1454
- package/lib/contracts/app-registry/index.d.ts +0 -161
- package/lib/contracts/app-registry/index.js +0 -274
- package/lib/contracts/membership-registry/abi.js +0 -1333
- package/lib/contracts/membership-registry/index.d.ts +0 -79
- package/lib/contracts/membership-registry/index.js +0 -104
- package/lib/contracts/types.d.ts +0 -10
- package/lib/contracts/types.js +0 -12
- package/lib/harness/examples/hackmd/build-markdown-graph.d.ts +0 -1
- package/lib/harness/examples/hackmd/build-markdown-graph.js +0 -17
- package/lib/harness/index.d.ts +0 -58
- package/lib/harness/index.js +0 -73
- package/lib/registry.d.ts +0 -118
- package/lib/registry.js +0 -184
|
@@ -29,21 +29,13 @@ interface SignedUrlSigner {
|
|
|
29
29
|
* JWT-only operations (server-side metadata queries / deletes) are unavailable
|
|
30
30
|
* here. Switch to a PinataBackend they're needed.
|
|
31
31
|
*
|
|
32
|
-
*
|
|
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.
|
|
32
|
+
* Every upload is charged to the signing publisher's storage quota.
|
|
38
33
|
*/
|
|
39
34
|
declare class SignedUrlBackend implements MetadataStorage {
|
|
40
35
|
private readonly signer;
|
|
41
36
|
private readonly gateway;
|
|
42
|
-
/** The client's current app id, read per upload (see setAppId). */
|
|
43
|
-
private readonly appId;
|
|
44
37
|
private readonly workerUrl;
|
|
45
|
-
constructor(workerUrl: string | undefined, signer: SignedUrlSigner, gateway: string
|
|
46
|
-
appId: () => Hex | undefined);
|
|
38
|
+
constructor(workerUrl: string | undefined, signer: SignedUrlSigner, gateway: string);
|
|
47
39
|
/** Run the worker challenge handshake → one-time presigned upload URL + network. */
|
|
48
40
|
private requestUploadUrl;
|
|
49
41
|
private workerJson;
|
|
@@ -18,7 +18,7 @@ function workerError(r) {
|
|
|
18
18
|
*
|
|
19
19
|
* Uploads go through the pinata-url-provider Worker. Prove wallet ownership by
|
|
20
20
|
* signing the worker's challenge, and it hands back a short-lived, single-use
|
|
21
|
-
* Pinata presigned upload URL that users POST the file to.
|
|
21
|
+
* Pinata presigned upload URL that users POST the file to.
|
|
22
22
|
* Reads need no auth: every Fangorn block is public content, so it's
|
|
23
23
|
* fetched straight from a public IPFS gateway by CID (privacy is cryptographic,
|
|
24
24
|
* not access-controlled). Each upload runs a fresh handshake because the
|
|
@@ -27,31 +27,22 @@ function workerError(r) {
|
|
|
27
27
|
* JWT-only operations (server-side metadata queries / deletes) are unavailable
|
|
28
28
|
* here. Switch to a PinataBackend they're needed.
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
|
-
* subscription pays for it. Each request carries the app id, the signer must be
|
|
32
|
-
* one of that app's publishers, and the pins are filed under the app's group.
|
|
33
|
-
* There is no app-less upload: with no app this refuses before contacting the
|
|
34
|
-
* worker. The id is read per request so a later `setAppId` takes effect
|
|
35
|
-
* immediately.
|
|
30
|
+
* Every upload is charged to the signing publisher's storage quota.
|
|
36
31
|
*/
|
|
37
32
|
var SignedUrlBackend = class {
|
|
38
33
|
workerUrl;
|
|
39
|
-
constructor(workerUrl, signer, gateway
|
|
34
|
+
constructor(workerUrl, signer, gateway) {
|
|
40
35
|
this.signer = signer;
|
|
41
36
|
this.gateway = gateway;
|
|
42
|
-
this.appId = appId;
|
|
43
37
|
this.workerUrl = workerUrl?.trim() ? workerUrl : DEFAULT_SIGNED_URL_WORKER;
|
|
44
38
|
}
|
|
45
39
|
/** Run the worker challenge handshake → one-time presigned upload URL + network. */
|
|
46
40
|
async requestUploadUrl(size, uploadId) {
|
|
47
41
|
const address = this.signer.address;
|
|
48
|
-
const appId = this.appId();
|
|
49
|
-
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 })");
|
|
50
42
|
const challenge = await this.workerJson({
|
|
51
43
|
address,
|
|
52
44
|
size,
|
|
53
|
-
uploadId
|
|
54
|
-
appId
|
|
45
|
+
uploadId
|
|
55
46
|
});
|
|
56
47
|
if (!challenge.challenge) throw new Error(`Presigned-URL worker did not issue a challenge: ${workerError(challenge)}`);
|
|
57
48
|
const signature = await this.signer.signMessage(challenge.challenge);
|
|
@@ -60,8 +51,7 @@ var SignedUrlBackend = class {
|
|
|
60
51
|
message: challenge.challenge,
|
|
61
52
|
signature,
|
|
62
53
|
size,
|
|
63
|
-
uploadId
|
|
64
|
-
appId
|
|
54
|
+
uploadId
|
|
65
55
|
});
|
|
66
56
|
if (!grant.ok || !grant.uploadUrl) throw new Error(`Presigned-URL worker denied an upload URL: ${workerError(grant)}`);
|
|
67
57
|
return {
|
|
@@ -36,7 +36,7 @@ interface MetadataStorage {
|
|
|
36
36
|
*/
|
|
37
37
|
putBlock(block: RawBlock): Promise<void>;
|
|
38
38
|
/**
|
|
39
|
-
* Store an opaque byte payload (e.g. a CAR file) and return
|
|
39
|
+
* Store an opaque byte payload (e.g. a CAR file) and return its CID.
|
|
40
40
|
* The backend is NOT expected to understand the contents.
|
|
41
41
|
*/
|
|
42
42
|
putFile(bytes: Uint8Array, name: string): Promise<string>;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { DEFAULT_SIGNED_URL_WORKER, SignedUrlBackend } from "./signed-url.js";
|
|
2
2
|
import { PinataBackend } from "./pinata.js";
|
|
3
3
|
import { CID } from "multiformats/cid";
|
|
4
|
+
import { base64pad } from "multiformats/bases/base64";
|
|
4
5
|
|
|
5
6
|
//#region src/providers/storage/utils.ts
|
|
6
7
|
const RAW_CODE = 85;
|
|
7
8
|
const REVIVERS = [(_key, value) => {
|
|
8
|
-
if (value !== null && typeof value === "object" && "__type" in value && "data" in value && value.__type === "Uint8Array") return
|
|
9
|
+
if (value !== null && typeof value === "object" && "__type" in value && "data" in value && value.__type === "Uint8Array") return base64pad.baseDecode(value.data);
|
|
9
10
|
return value;
|
|
10
11
|
}];
|
|
11
12
|
function serialize(data) {
|
|
@@ -13,7 +14,7 @@ function serialize(data) {
|
|
|
13
14
|
return JSON.stringify(data, (_key, value) => {
|
|
14
15
|
if (value instanceof Uint8Array) return {
|
|
15
16
|
__type: "Uint8Array",
|
|
16
|
-
data:
|
|
17
|
+
data: base64pad.baseEncode(value)
|
|
17
18
|
};
|
|
18
19
|
return value;
|
|
19
20
|
});
|
|
@@ -27,7 +28,8 @@ function deserialize(text) {
|
|
|
27
28
|
return value;
|
|
28
29
|
});
|
|
29
30
|
}
|
|
30
|
-
const
|
|
31
|
+
const env = globalThis.process?.env;
|
|
32
|
+
const MAX_UPLOAD_ATTEMPTS = Math.max(1, Number(env?.PINATA_UPLOAD_RETRIES ?? 6));
|
|
31
33
|
function isTransientUpload(err) {
|
|
32
34
|
const e = err;
|
|
33
35
|
const s = typeof e.statusCode === "number" ? e.statusCode : 0;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AccessRequest } from "../crypto/paid.js";
|
|
2
|
+
import { Address, Hex } from "viem";
|
|
3
|
+
|
|
4
|
+
//#region src/test/key-server.d.ts
|
|
5
|
+
declare function testKeyServer(body: AccessRequest, ks: {
|
|
6
|
+
secret: Uint8Array;
|
|
7
|
+
chainId: number;
|
|
8
|
+
registry: Address;
|
|
9
|
+
canRead(reader: Address, publisher: Address, repo: Hex, key: Hex): Promise<boolean>;
|
|
10
|
+
}): Promise<Response>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { testKeyServer };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { WHOLE_REPO } from "../contracts/settlement-registry/index.js";
|
|
2
|
+
import { seal, unseal } from "../crypto/encryption.js";
|
|
3
|
+
import { accessMessage, deriveRecordKey, statement } from "../crypto/paid.js";
|
|
4
|
+
import { bytesToHex, hexToBytes, recoverMessageAddress } from "viem";
|
|
5
|
+
|
|
6
|
+
//#region src/test/key-server.ts
|
|
7
|
+
async function testKeyServer(body, ks) {
|
|
8
|
+
const stmt = statement({
|
|
9
|
+
...ks,
|
|
10
|
+
publisher: body.publisher,
|
|
11
|
+
repo: body.repo,
|
|
12
|
+
generation: BigInt(body.generation)
|
|
13
|
+
});
|
|
14
|
+
const reader = await recoverMessageAddress({
|
|
15
|
+
message: { raw: accessMessage(stmt, body.key, body.readerKey, body.timestamp) },
|
|
16
|
+
signature: body.signature
|
|
17
|
+
});
|
|
18
|
+
if (!(reader.toLowerCase() === body.publisher.toLowerCase()) && !await ks.canRead(reader, body.publisher, body.repo, body.key)) return new Response("not settled", { status: 403 });
|
|
19
|
+
let kRepo;
|
|
20
|
+
try {
|
|
21
|
+
kRepo = unseal(hexToBytes(body.sealed), ks.secret, stmt);
|
|
22
|
+
} catch {
|
|
23
|
+
return new Response("deposit does not match", { status: 400 });
|
|
24
|
+
}
|
|
25
|
+
const out = body.key === WHOLE_REPO ? kRepo : deriveRecordKey(kRepo, stmt, body.key);
|
|
26
|
+
return Response.json({ sealed: bytesToHex(seal(out, hexToBytes(body.readerKey), stmt)) });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { testKeyServer };
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { PreparedTx } from "../contracts/write.js";
|
|
2
|
+
import { CommitUpdate, LogFilter, Registry, RepoRef, UpdateEntry } from "../contracts/data-registry/index.js";
|
|
2
3
|
import "../contracts/index.js";
|
|
3
|
-
import
|
|
4
|
-
import { Address, Hash, Hex } from "viem";
|
|
4
|
+
import * as viem161 from "viem";
|
|
5
|
+
import { Address, Hash, Hex, LocalAccount } from "viem";
|
|
5
6
|
|
|
6
7
|
//#region src/test/mem-registry.d.ts
|
|
8
|
+
/** The EIP-712 domain a MemRegistry signs under. */
|
|
9
|
+
declare const MEM_DOMAIN: viem161.TypedDataDomain;
|
|
7
10
|
/** One ordering and the registry state over it, shared by every signer of a MemRegistry. */
|
|
8
11
|
declare class MemLedger {
|
|
9
12
|
/** H: position and head */
|
|
@@ -11,10 +14,6 @@ declare class MemLedger {
|
|
|
11
14
|
pos: number;
|
|
12
15
|
head: Hex;
|
|
13
16
|
}>;
|
|
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
17
|
/** U */
|
|
19
18
|
entries: UpdateEntry[];
|
|
20
19
|
/** The clock: one tick per entry of the ordering. */
|
|
@@ -23,29 +22,25 @@ declare class MemLedger {
|
|
|
23
22
|
append(entry: Omit<UpdateEntry, "t" | "id">): void;
|
|
24
23
|
}
|
|
25
24
|
/**
|
|
26
|
-
* The paper's Apply, signing as `signer`.
|
|
27
|
-
*
|
|
28
|
-
* business. Signers that share a ledger see each other's writes, like wallets on
|
|
29
|
-
* one chain.
|
|
25
|
+
* The paper's Apply, signing as `signer`. Signers that share a ledger see each
|
|
26
|
+
* other's writes, like wallets on one chain.
|
|
30
27
|
*/
|
|
31
28
|
declare class MemRegistry implements Registry {
|
|
32
29
|
signer: Address;
|
|
33
30
|
ledger: MemLedger;
|
|
34
31
|
now: () => number;
|
|
35
|
-
|
|
32
|
+
account?: LocalAccount | undefined;
|
|
33
|
+
constructor(signer: Address, ledger?: MemLedger, now?: () => number, account?: LocalAccount | undefined);
|
|
36
34
|
epoch(): Promise<number>;
|
|
37
35
|
head(ref: RepoRef): Promise<Hex>;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
update(u: CommitUpdate): Promise<Hash>;
|
|
37
|
+
signUpdate(u: CommitUpdate): Promise<Hex>;
|
|
38
|
+
updateBySig(publisher: Address, u: CommitUpdate, signature: Hex): Promise<Hash>;
|
|
39
|
+
private apply;
|
|
41
40
|
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
41
|
log(filter: LogFilter, from: bigint, to?: bigint): Promise<UpdateEntry[]>;
|
|
47
42
|
watch(filter: LogFilter, onEntry: (entry: UpdateEntry) => void): () => void;
|
|
48
43
|
clock(): Promise<bigint>;
|
|
49
44
|
}
|
|
50
45
|
//#endregion
|
|
51
|
-
export { MemLedger, MemRegistry };
|
|
46
|
+
export { MEM_DOMAIN, MemLedger, MemRegistry };
|
package/lib/test/mem-registry.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { ZERO_HEAD, epochOf } from "../registry.js";
|
|
1
|
+
import { UPDATE_TYPES, WINDOW, ZERO_HEAD, dataDomain, epochOf, updateMessage } from "../contracts/data-registry/index.js";
|
|
2
|
+
import "../contracts/index.js";
|
|
3
|
+
import { commitHead } from "../engine/index.js";
|
|
4
|
+
import { verifyTypedData } from "viem";
|
|
2
5
|
|
|
3
6
|
//#region src/test/mem-registry.ts
|
|
7
|
+
/** The EIP-712 domain a MemRegistry signs under. */
|
|
8
|
+
const MEM_DOMAIN = dataDomain(31337, "0x000000000000000000000000000000000000da7a");
|
|
4
9
|
const refKey = (r) => `${r.publisher.toLowerCase()}/${r.repo}`;
|
|
5
|
-
const exclKey = (x) => `${x.publisher.toLowerCase()}/${x.repo ?? ""}/${x.key ?? ""}`;
|
|
6
10
|
/** One ordering and the registry state over it, shared by every signer of a MemRegistry. */
|
|
7
11
|
var MemLedger = class {
|
|
8
12
|
/** H: position and head */
|
|
9
13
|
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
14
|
/** U */
|
|
15
15
|
entries = [];
|
|
16
16
|
/** The clock: one tick per entry of the ordering. */
|
|
@@ -29,16 +29,15 @@ var MemLedger = class {
|
|
|
29
29
|
};
|
|
30
30
|
const matches = (filter, e) => (!filter.publisher || e.publisher.toLowerCase() === filter.publisher.toLowerCase()) && (!filter.repo || e.repo === filter.repo);
|
|
31
31
|
/**
|
|
32
|
-
* The paper's Apply, signing as `signer`.
|
|
33
|
-
*
|
|
34
|
-
* business. Signers that share a ledger see each other's writes, like wallets on
|
|
35
|
-
* one chain.
|
|
32
|
+
* The paper's Apply, signing as `signer`. Signers that share a ledger see each
|
|
33
|
+
* other's writes, like wallets on one chain.
|
|
36
34
|
*/
|
|
37
35
|
var MemRegistry = class {
|
|
38
|
-
constructor(signer, ledger = new MemLedger(), now = () => Math.floor(Date.now() / 1e3)) {
|
|
36
|
+
constructor(signer, ledger = new MemLedger(), now = () => Math.floor(Date.now() / 1e3), account) {
|
|
39
37
|
this.signer = signer;
|
|
40
38
|
this.ledger = ledger;
|
|
41
39
|
this.now = now;
|
|
40
|
+
this.account = account;
|
|
42
41
|
}
|
|
43
42
|
epoch() {
|
|
44
43
|
return Promise.resolve(epochOf(this.now()));
|
|
@@ -46,48 +45,56 @@ var MemRegistry = class {
|
|
|
46
45
|
head(ref) {
|
|
47
46
|
return Promise.resolve(this.ledger.heads.get(refKey(ref))?.head ?? ZERO_HEAD);
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
return
|
|
48
|
+
update(u) {
|
|
49
|
+
return this.apply(this.signer, u);
|
|
51
50
|
}
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
signUpdate(u) {
|
|
52
|
+
if (!this.account) return Promise.reject(/* @__PURE__ */ new Error("MemRegistry has no account to sign with"));
|
|
53
|
+
return this.account.signTypedData({
|
|
54
|
+
domain: MEM_DOMAIN,
|
|
55
|
+
types: UPDATE_TYPES,
|
|
56
|
+
primaryType: "Update",
|
|
57
|
+
message: updateMessage(this.account.address, u)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async updateBySig(publisher, u, signature) {
|
|
61
|
+
if (!await verifyTypedData({
|
|
62
|
+
address: publisher,
|
|
63
|
+
domain: MEM_DOMAIN,
|
|
64
|
+
types: UPDATE_TYPES,
|
|
65
|
+
primaryType: "Update",
|
|
66
|
+
message: updateMessage(publisher, u),
|
|
67
|
+
signature
|
|
68
|
+
})) throw new Error("BadSignature");
|
|
69
|
+
return this.apply(publisher, u);
|
|
54
70
|
}
|
|
55
|
-
async
|
|
71
|
+
async apply(publisher, u) {
|
|
72
|
+
const now = epochOf(this.now());
|
|
73
|
+
if (u.epoch > now || u.epoch + WINDOW < now) throw new Error("EpochOutOfWindow");
|
|
56
74
|
const ref = {
|
|
57
|
-
publisher
|
|
58
|
-
repo
|
|
75
|
+
publisher,
|
|
76
|
+
repo: u.repo
|
|
59
77
|
};
|
|
60
|
-
if (await this.head(ref) !== parent) throw new Error("StaleStateRoot");
|
|
78
|
+
if (await this.head(ref) !== u.parent) throw new Error("StaleStateRoot");
|
|
79
|
+
const head = await commitHead(u);
|
|
61
80
|
const pos = (this.ledger.heads.get(refKey(ref))?.pos ?? 0) + 1;
|
|
62
81
|
this.ledger.heads.set(refKey(ref), {
|
|
63
82
|
pos,
|
|
64
83
|
head
|
|
65
84
|
});
|
|
66
85
|
this.ledger.append({
|
|
67
|
-
publisher
|
|
68
|
-
repo,
|
|
86
|
+
publisher,
|
|
87
|
+
repo: u.repo,
|
|
69
88
|
pos,
|
|
70
|
-
parent,
|
|
71
|
-
head
|
|
89
|
+
parent: u.parent,
|
|
90
|
+
head,
|
|
91
|
+
timestamp: BigInt(this.now())
|
|
72
92
|
});
|
|
73
93
|
return "0x00";
|
|
74
94
|
}
|
|
75
95
|
prepareUpdate() {
|
|
76
96
|
return Promise.reject(/* @__PURE__ */ new Error("MemRegistry has no transactions to prepare"));
|
|
77
97
|
}
|
|
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
98
|
log(filter, from, to) {
|
|
92
99
|
return Promise.resolve(this.ledger.entries.filter((e) => e.t >= from && (to === void 0 || e.t <= to) && matches(filter, e)));
|
|
93
100
|
}
|
|
@@ -104,4 +111,4 @@ var MemRegistry = class {
|
|
|
104
111
|
};
|
|
105
112
|
|
|
106
113
|
//#endregion
|
|
107
|
-
export { MemLedger, MemRegistry };
|
|
114
|
+
export { MEM_DOMAIN, MemLedger, MemRegistry };
|
package/lib/test/sdk-suite.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { Fangorn, RepoChange } from "../fangorn.js";
|
|
2
2
|
import { Network } from "./testbed.js";
|
|
3
|
-
import { Hex } from "viem";
|
|
4
3
|
|
|
5
4
|
//#region src/test/sdk-suite.d.ts
|
|
6
|
-
/** The app a test client was made under. */
|
|
7
|
-
declare function appOf(f: Fangorn): Hex;
|
|
8
5
|
declare const head: (f: Fangorn, repo: string) => Promise<`0x${string}`>;
|
|
9
6
|
/** Take the first n entries of the stream and then stop. */
|
|
10
7
|
declare function take(stream: AsyncGenerator<RepoChange>, n: number): Promise<RepoChange[]>;
|
|
11
8
|
declare function sdkSuite(makeNetwork: () => Promise<Network>, timeout?: number): void;
|
|
12
9
|
//#endregion
|
|
13
|
-
export {
|
|
10
|
+
export { head, sdkSuite, take };
|
package/lib/test/sdk-suite.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import { ZERO_HEAD } from "../registry.js";
|
|
1
|
+
import { ZERO_HEAD } from "../contracts/data-registry/index.js";
|
|
2
|
+
import "../contracts/index.js";
|
|
2
3
|
import { GADGET_SELF_HKDF_V1, sealSelf, unsealSelf } from "../crypto/encryption.js";
|
|
3
4
|
import { afterAll, beforeAll, describe, it } from "../node_modules/.pnpm/@vitest_runner@4.0.18/node_modules/@vitest/runner/dist/index.js";
|
|
4
5
|
import { globalExpect } from "../node_modules/.pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest/dist/chunks/vi.2VT5v0um.js";
|
|
5
|
-
import { TEST_APP } from "./testbed.js";
|
|
6
6
|
import { bytesToHex, hexToBytes, keccak256, stringToBytes } from "viem";
|
|
7
7
|
import { generatePrivateKey } from "viem/accounts";
|
|
8
8
|
|
|
9
9
|
//#region src/test/sdk-suite.ts
|
|
10
|
-
/** The app a test client was made under. */
|
|
11
|
-
function appOf(f) {
|
|
12
|
-
const app = f.getAppId();
|
|
13
|
-
if (!app) throw new Error("this client has no app");
|
|
14
|
-
return app;
|
|
15
|
-
}
|
|
16
10
|
const head = (f, repo) => f.getRegistry().head({
|
|
17
11
|
publisher: f.getAddress(),
|
|
18
12
|
repo
|
|
@@ -26,8 +20,6 @@ async function take(stream, n) {
|
|
|
26
20
|
}
|
|
27
21
|
return out;
|
|
28
22
|
}
|
|
29
|
-
/** A fresh app name, so a test's owner and exclusions are its own. */
|
|
30
|
-
const freshApp = () => `app-${Math.random().toString(36).slice(2)}`;
|
|
31
23
|
function sdkSuite(makeNetwork, timeout = 5e3) {
|
|
32
24
|
let net;
|
|
33
25
|
beforeAll(async () => {
|
|
@@ -79,7 +71,7 @@ function sdkSuite(makeNetwork, timeout = 5e3) {
|
|
|
79
71
|
}, timeout);
|
|
80
72
|
it("stores a payload sealed to its publisher as opaque bytes", async () => {
|
|
81
73
|
const sk = generatePrivateKey();
|
|
82
|
-
const f = await net.client(
|
|
74
|
+
const f = await net.client(sk);
|
|
83
75
|
await f.initRepo("private");
|
|
84
76
|
const resourceId = keccak256(stringToBytes("private:secret"));
|
|
85
77
|
const ciphertext = sealSelf(new TextEncoder().encode("the eagles are coming"), hexToBytes(sk), resourceId);
|
|
@@ -111,94 +103,31 @@ function sdkSuite(makeNetwork, timeout = 5e3) {
|
|
|
111
103
|
id: "x",
|
|
112
104
|
type: "t",
|
|
113
105
|
payload: {}
|
|
114
|
-
}]
|
|
115
|
-
message: "m"
|
|
106
|
+
}]
|
|
116
107
|
});
|
|
117
108
|
await globalExpect(f.push("r", stale.commitCid)).rejects.toThrow(/stale parent/);
|
|
118
109
|
}, timeout);
|
|
119
110
|
});
|
|
120
|
-
describe("
|
|
121
|
-
it("
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
await
|
|
126
|
-
await
|
|
127
|
-
const { payloadCid } = await guest.upload("catalog", { title: "from the guest" }, "entry", "g");
|
|
128
|
-
const { contents } = await owner.readRepo(guest.getAddress(), "catalog");
|
|
111
|
+
describe("reading other publishers", () => {
|
|
112
|
+
it("any client reads any publisher's repo, and lists every repo", async () => {
|
|
113
|
+
const reader = await net.client();
|
|
114
|
+
const pub = await net.client();
|
|
115
|
+
await pub.initRepo("catalog");
|
|
116
|
+
const { payloadCid } = await pub.upload("catalog", { title: "t" }, "entry", "g");
|
|
117
|
+
const { contents } = await reader.readRepo(pub.getAddress(), "catalog");
|
|
129
118
|
globalExpect(contents.records.map((r) => r.cid)).toEqual([payloadCid]);
|
|
130
|
-
|
|
131
|
-
globalExpect(listed.map((d) => d.owner).sort()).toEqual([owner.getAddress(), guest.getAddress()].sort());
|
|
132
|
-
globalExpect(listed.every((d) => d.repo === "catalog")).toBe(true);
|
|
133
|
-
}, timeout);
|
|
134
|
-
it("an app presents its members' repos and nobody else's; an update names no app", async () => {
|
|
135
|
-
const app = freshApp();
|
|
136
|
-
const owner = await net.client(app);
|
|
137
|
-
const loner = await net.client(null);
|
|
138
|
-
await loner.initRepo("notes");
|
|
139
|
-
await loner.upload("notes", { n: 1 }, "t", "x");
|
|
140
|
-
globalExpect((await owner.readRepo(loner.getAddress(), "notes")).contents.records).toEqual([]);
|
|
141
|
-
globalExpect(await owner.appRepos()).toEqual([]);
|
|
142
|
-
globalExpect((await (await net.client(null)).readRepo(loner.getAddress(), "notes")).contents.records).toHaveLength(1);
|
|
143
|
-
}, timeout);
|
|
144
|
-
it("an excluded publisher disappears from the app's reads, and a restore brings it back", async () => {
|
|
145
|
-
const app = freshApp();
|
|
146
|
-
const owner = await net.client(app);
|
|
147
|
-
const guest = await net.client(app);
|
|
148
|
-
await guest.initRepo("catalog");
|
|
149
|
-
await guest.upload("catalog", { title: "t" }, "entry", "g");
|
|
150
|
-
const x = { publisher: guest.getAddress() };
|
|
151
|
-
const appId = appOf(owner);
|
|
152
|
-
await owner.getRegistry().exclude(appId, x);
|
|
153
|
-
globalExpect(await owner.appRepos()).toEqual([]);
|
|
154
|
-
globalExpect((await owner.readRepo(guest.getAddress(), "catalog")).contents.records).toEqual([]);
|
|
155
|
-
await owner.getRegistry().restore(appId, x);
|
|
156
|
-
globalExpect(await owner.appRepos()).toHaveLength(1);
|
|
157
|
-
globalExpect((await owner.readRepo(guest.getAddress(), "catalog")).contents.records).toHaveLength(1);
|
|
158
|
-
}, timeout);
|
|
159
|
-
it("a repo exclusion hides that repo only", async () => {
|
|
160
|
-
const app = freshApp();
|
|
161
|
-
const owner = await net.client(app);
|
|
162
|
-
const guest = await net.client(app);
|
|
163
|
-
for (const repo of ["keep", "drop"]) {
|
|
164
|
-
await guest.initRepo(repo);
|
|
165
|
-
await guest.upload(repo, { repo }, "t", "x");
|
|
166
|
-
}
|
|
167
|
-
await owner.getRegistry().exclude(appOf(owner), {
|
|
168
|
-
publisher: guest.getAddress(),
|
|
169
|
-
repo: "drop"
|
|
170
|
-
});
|
|
171
|
-
globalExpect((await owner.appRepos()).map((d) => d.repo)).toEqual(["keep"]);
|
|
172
|
-
globalExpect((await owner.readRepo(guest.getAddress(), "drop")).contents.records).toEqual([]);
|
|
173
|
-
globalExpect((await owner.readRepo(guest.getAddress(), "keep")).contents.records).toHaveLength(1);
|
|
174
|
-
}, timeout);
|
|
175
|
-
it("an exclusion in one app hides nothing in another that presents the same publisher", async () => {
|
|
176
|
-
const [appA, appB] = [freshApp(), freshApp()];
|
|
177
|
-
const ownerA = await net.client(appA);
|
|
178
|
-
const ownerB = await net.client(appB);
|
|
179
|
-
const sk = generatePrivateKey();
|
|
180
|
-
const guest = await net.client(appA, sk);
|
|
181
|
-
await net.client(appB, sk);
|
|
182
|
-
await guest.initRepo("catalog");
|
|
183
|
-
await guest.upload("catalog", { title: "t" }, "entry", "g");
|
|
184
|
-
const ref = {
|
|
185
|
-
publisher: guest.getAddress(),
|
|
186
|
-
repo: "catalog"
|
|
187
|
-
};
|
|
188
|
-
await ownerA.getRegistry().exclude(appOf(ownerA), { publisher: ref.publisher });
|
|
189
|
-
globalExpect((await ownerA.readRepo(ref.publisher, "catalog")).contents.records).toEqual([]);
|
|
190
|
-
globalExpect((await ownerB.readRepo(ref.publisher, "catalog")).contents.records).toHaveLength(1);
|
|
119
|
+
globalExpect((await reader.repos({ owner: pub.getAddress() })).map((d) => [d.owner, d.repo])).toEqual([[pub.getAddress(), "catalog"]]);
|
|
191
120
|
}, timeout);
|
|
192
121
|
});
|
|
193
122
|
describe("subscriptions", () => {
|
|
194
123
|
it("replays from a block, then follows live", async () => {
|
|
195
|
-
const
|
|
196
|
-
const pub = await net.client(app);
|
|
124
|
+
const pub = await net.client();
|
|
197
125
|
const from = await net.clock();
|
|
198
126
|
await pub.initRepo("feed");
|
|
199
127
|
await pub.upload("feed", { n: 1 }, "t", "a");
|
|
200
128
|
const ctl = new AbortController();
|
|
201
|
-
const stream = (await net.client(
|
|
129
|
+
const stream = (await net.client()).subscribeAll({
|
|
130
|
+
owner: pub.getAddress(),
|
|
202
131
|
fromBlock: from,
|
|
203
132
|
signal: ctl.signal,
|
|
204
133
|
pollingInterval: 100
|
|
@@ -223,4 +152,4 @@ function sdkSuite(makeNetwork, timeout = 5e3) {
|
|
|
223
152
|
}
|
|
224
153
|
|
|
225
154
|
//#endregion
|
|
226
|
-
export {
|
|
155
|
+
export { head, sdkSuite, take };
|
package/lib/test/testbed.d.ts
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
import { AppConfig } from "../config.js";
|
|
2
1
|
import { MetadataStorage } from "../providers/storage/types.js";
|
|
2
|
+
import { NetworkConfig } from "../config.js";
|
|
3
|
+
import { Fangorn } from "../fangorn.js";
|
|
3
4
|
import { MemLedger, MemRegistry } from "./mem-registry.js";
|
|
4
5
|
import { MemStorage } from "../engine/test-helpers.js";
|
|
5
|
-
import { Fangorn } from "../fangorn.js";
|
|
6
6
|
import * as viem162 from "viem";
|
|
7
7
|
import { Abi, Address, Hex } from "viem";
|
|
8
8
|
|
|
9
9
|
//#region src/test/testbed.d.ts
|
|
10
|
-
/** The app every client is accepted into unless told otherwise. */
|
|
11
|
-
declare const TEST_APP = "test-app";
|
|
12
10
|
/** What a suite needs of a network: clients on one ordering and one storage. */
|
|
13
11
|
interface Network {
|
|
14
12
|
name: string;
|
|
15
13
|
storage: MetadataStorage;
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
* `null`. On chain it is a joined member of the app: the first client of an
|
|
19
|
-
* app claims it, and the claimer accepts each later one.
|
|
20
|
-
*/
|
|
21
|
-
client(app?: string | null, sk?: Hex): Promise<Fangorn>;
|
|
14
|
+
/** A client for a fresh wallet, or for `sk`. */
|
|
15
|
+
client(sk?: Hex): Promise<Fangorn>;
|
|
22
16
|
/** The ordering's clock (a block number on chain). */
|
|
23
17
|
clock(): Promise<bigint>;
|
|
24
18
|
stop(): void;
|
|
@@ -27,8 +21,7 @@ declare function memoryNetwork(Registry?: typeof MemRegistry): {
|
|
|
27
21
|
name: string;
|
|
28
22
|
ledger: MemLedger;
|
|
29
23
|
storage: MemStorage;
|
|
30
|
-
|
|
31
|
-
client(app?: string | null, sk?: Hex, R?: typeof MemRegistry): Promise<Fangorn>;
|
|
24
|
+
client(sk?: Hex, R?: typeof MemRegistry): Promise<Fangorn>;
|
|
32
25
|
clock: () => Promise<bigint>;
|
|
33
26
|
stop: () => undefined;
|
|
34
27
|
};
|
|
@@ -39,13 +32,13 @@ declare function memoryNetwork(Registry?: typeof MemRegistry): {
|
|
|
39
32
|
declare const skipChain: boolean;
|
|
40
33
|
/**
|
|
41
34
|
* A fresh anvil with the contracts deployed from source, each behind its
|
|
42
|
-
* ERC-1967 proxy as Deploy.s.sol does, and wired to each other.
|
|
35
|
+
* ERC-1967 proxy as Deploy.s.sol does, and wired to each other.
|
|
43
36
|
* Call `stop` from `afterAll`.
|
|
44
37
|
*/
|
|
45
38
|
declare function chainNetwork(): Promise<{
|
|
46
39
|
name: string;
|
|
47
40
|
storage: MetadataStorage;
|
|
48
|
-
config:
|
|
41
|
+
config: NetworkConfig;
|
|
49
42
|
chain: viem162.Client<viem162.HttpTransport<undefined, false>, {
|
|
50
43
|
id: 84532;
|
|
51
44
|
blockExplorers?: {
|
|
@@ -7314,7 +7307,6 @@ declare function chainNetwork(): Promise<{
|
|
|
7314
7307
|
}, undefined, chainOverride, calls>) => Promise<{
|
|
7315
7308
|
id: string;
|
|
7316
7309
|
chainId: number;
|
|
7317
|
-
version: string;
|
|
7318
7310
|
atomic: boolean;
|
|
7319
7311
|
capabilities?: {
|
|
7320
7312
|
[key: string]: any;
|
|
@@ -7322,6 +7314,7 @@ declare function chainNetwork(): Promise<{
|
|
|
7322
7314
|
[x: string]: any;
|
|
7323
7315
|
} | undefined;
|
|
7324
7316
|
receipts?: viem162.WalletCallReceipt<bigint, "success" | "reverted">[] | undefined;
|
|
7317
|
+
version: string;
|
|
7325
7318
|
statusCode: number;
|
|
7326
7319
|
status: "pending" | "success" | "failure" | undefined;
|
|
7327
7320
|
}>;
|
|
@@ -16780,16 +16773,13 @@ declare function chainNetwork(): Promise<{
|
|
|
16780
16773
|
} & {
|
|
16781
16774
|
mode: "anvil";
|
|
16782
16775
|
} & viem162.TestActions>;
|
|
16783
|
-
/**
|
|
16784
|
-
|
|
16776
|
+
/** MockUSDC: `balanceOf` is what each address has been paid. */
|
|
16777
|
+
usdc: `0x${string}`;
|
|
16785
16778
|
wallet: (sk?: Hex) => Promise<Hex>;
|
|
16786
|
-
|
|
16787
|
-
/** The client that claimed `app` through `client`. */
|
|
16788
|
-
owner: (app?: string) => Fangorn | undefined;
|
|
16789
|
-
client(app?: string | null, sk?: Hex): Promise<Fangorn>;
|
|
16779
|
+
client: (sk?: Hex) => Promise<Fangorn>;
|
|
16790
16780
|
clock: () => Promise<bigint>;
|
|
16791
16781
|
stop: () => boolean;
|
|
16792
16782
|
}>;
|
|
16793
16783
|
type ChainNetwork = Awaited<ReturnType<typeof chainNetwork>>;
|
|
16794
16784
|
//#endregion
|
|
16795
|
-
export { ChainNetwork, MemLedger, MemRegistry, Network,
|
|
16785
|
+
export { ChainNetwork, MemLedger, MemRegistry, Network, chainNetwork, memoryNetwork, skipChain };
|