@fangorn-network/sdk 2026.7.24 → 2026.7.28
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 +163 -8
- package/lib/cli/cli.js +43 -43
- package/lib/config.d.ts +10 -3
- package/lib/config.js +11 -2
- package/lib/contracts/data-registry/abi.d.ts +80 -2
- package/lib/contracts/data-registry/abi.js +107 -12
- package/lib/contracts/data-registry/index.d.ts +91 -19
- package/lib/contracts/data-registry/index.js +153 -24
- package/lib/contracts/index.d.ts +2 -2
- package/lib/contracts/index.js +2 -2
- package/lib/engine/index.d.ts +36 -12
- package/lib/engine/index.js +51 -19
- package/lib/engine/test-helpers.d.ts +7 -4
- package/lib/engine/test-helpers.js +9 -6
- package/lib/fangorn.d.ts +83 -13
- package/lib/fangorn.js +129 -39
- package/lib/index.d.ts +10 -3
- package/lib/index.js +9 -3
- package/lib/providers/storage/index.d.ts +2 -1
- package/lib/providers/storage/index.js +2 -1
- package/lib/providers/storage/pinata.js +1 -19
- package/lib/providers/storage/signed-url.d.ts +52 -0
- package/lib/providers/storage/signed-url.js +119 -0
- package/lib/providers/storage/utils.d.ts +19 -1
- package/lib/providers/storage/utils.js +55 -1
- package/lib/test/testbed.d.ts +21 -3
- package/lib/test/testbed.js +38 -7
- package/lib/types/index.d.ts +5 -0
- package/package.json +1 -1
|
@@ -5,6 +5,16 @@ const DATA_REGISTRY_ABI = [
|
|
|
5
5
|
"name": "AlreadyRegistered",
|
|
6
6
|
"type": "error"
|
|
7
7
|
},
|
|
8
|
+
{
|
|
9
|
+
"inputs": [],
|
|
10
|
+
"name": "AppAlreadyRegistered",
|
|
11
|
+
"type": "error"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"inputs": [],
|
|
15
|
+
"name": "AppNotFound",
|
|
16
|
+
"type": "error"
|
|
17
|
+
},
|
|
8
18
|
{
|
|
9
19
|
"inputs": [],
|
|
10
20
|
"name": "NotRegistered",
|
|
@@ -30,6 +40,22 @@ const DATA_REGISTRY_ABI = [
|
|
|
30
40
|
"name": "Unauthorized",
|
|
31
41
|
"type": "error"
|
|
32
42
|
},
|
|
43
|
+
{
|
|
44
|
+
"anonymous": false,
|
|
45
|
+
"inputs": [{
|
|
46
|
+
"indexed": true,
|
|
47
|
+
"internalType": "bytes32",
|
|
48
|
+
"name": "app_id",
|
|
49
|
+
"type": "bytes32"
|
|
50
|
+
}, {
|
|
51
|
+
"indexed": true,
|
|
52
|
+
"internalType": "address",
|
|
53
|
+
"name": "owner",
|
|
54
|
+
"type": "address"
|
|
55
|
+
}],
|
|
56
|
+
"name": "AppRegistered",
|
|
57
|
+
"type": "event"
|
|
58
|
+
},
|
|
33
59
|
{
|
|
34
60
|
"anonymous": false,
|
|
35
61
|
"inputs": [{
|
|
@@ -87,6 +113,18 @@ const DATA_REGISTRY_ABI = [
|
|
|
87
113
|
{
|
|
88
114
|
"anonymous": false,
|
|
89
115
|
"inputs": [
|
|
116
|
+
{
|
|
117
|
+
"indexed": true,
|
|
118
|
+
"internalType": "bytes32",
|
|
119
|
+
"name": "namespace_key",
|
|
120
|
+
"type": "bytes32"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"indexed": true,
|
|
124
|
+
"internalType": "bytes32",
|
|
125
|
+
"name": "app_id",
|
|
126
|
+
"type": "bytes32"
|
|
127
|
+
},
|
|
90
128
|
{
|
|
91
129
|
"indexed": true,
|
|
92
130
|
"internalType": "address",
|
|
@@ -94,13 +132,19 @@ const DATA_REGISTRY_ABI = [
|
|
|
94
132
|
"type": "address"
|
|
95
133
|
},
|
|
96
134
|
{
|
|
97
|
-
"indexed":
|
|
135
|
+
"indexed": false,
|
|
136
|
+
"internalType": "bytes32",
|
|
137
|
+
"name": "subspace_id",
|
|
138
|
+
"type": "bytes32"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
"indexed": false,
|
|
98
142
|
"internalType": "bytes32",
|
|
99
143
|
"name": "old_root",
|
|
100
144
|
"type": "bytes32"
|
|
101
145
|
},
|
|
102
146
|
{
|
|
103
|
-
"indexed":
|
|
147
|
+
"indexed": false,
|
|
104
148
|
"internalType": "bytes32",
|
|
105
149
|
"name": "new_root",
|
|
106
150
|
"type": "bytes32"
|
|
@@ -121,15 +165,28 @@ const DATA_REGISTRY_ABI = [
|
|
|
121
165
|
"type": "function"
|
|
122
166
|
},
|
|
123
167
|
{
|
|
124
|
-
"inputs": [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
168
|
+
"inputs": [
|
|
169
|
+
{
|
|
170
|
+
"internalType": "bytes32",
|
|
171
|
+
"name": "app_id",
|
|
172
|
+
"type": "bytes32"
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"internalType": "bytes32",
|
|
176
|
+
"name": "subspace_id",
|
|
177
|
+
"type": "bytes32"
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"internalType": "bytes32",
|
|
181
|
+
"name": "old_root",
|
|
182
|
+
"type": "bytes32"
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"internalType": "bytes32",
|
|
186
|
+
"name": "new_root",
|
|
187
|
+
"type": "bytes32"
|
|
188
|
+
}
|
|
189
|
+
],
|
|
133
190
|
"name": "commitStateRoot",
|
|
134
191
|
"outputs": [],
|
|
135
192
|
"stateMutability": "nonpayable",
|
|
@@ -137,10 +194,37 @@ const DATA_REGISTRY_ABI = [
|
|
|
137
194
|
},
|
|
138
195
|
{
|
|
139
196
|
"inputs": [{
|
|
197
|
+
"internalType": "bytes32",
|
|
198
|
+
"name": "app_id",
|
|
199
|
+
"type": "bytes32"
|
|
200
|
+
}],
|
|
201
|
+
"name": "getAppOwner",
|
|
202
|
+
"outputs": [{
|
|
140
203
|
"internalType": "address",
|
|
141
|
-
"name": "
|
|
204
|
+
"name": "",
|
|
142
205
|
"type": "address"
|
|
143
206
|
}],
|
|
207
|
+
"stateMutability": "view",
|
|
208
|
+
"type": "function"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"inputs": [
|
|
212
|
+
{
|
|
213
|
+
"internalType": "bytes32",
|
|
214
|
+
"name": "app_id",
|
|
215
|
+
"type": "bytes32"
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
"internalType": "address",
|
|
219
|
+
"name": "publisher",
|
|
220
|
+
"type": "address"
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"internalType": "bytes32",
|
|
224
|
+
"name": "subspace_id",
|
|
225
|
+
"type": "bytes32"
|
|
226
|
+
}
|
|
227
|
+
],
|
|
144
228
|
"name": "getNamespaceHead",
|
|
145
229
|
"outputs": [{
|
|
146
230
|
"internalType": "bytes32",
|
|
@@ -198,6 +282,17 @@ const DATA_REGISTRY_ABI = [
|
|
|
198
282
|
"stateMutability": "payable",
|
|
199
283
|
"type": "function"
|
|
200
284
|
},
|
|
285
|
+
{
|
|
286
|
+
"inputs": [{
|
|
287
|
+
"internalType": "bytes32",
|
|
288
|
+
"name": "app_id",
|
|
289
|
+
"type": "bytes32"
|
|
290
|
+
}],
|
|
291
|
+
"name": "registerApp",
|
|
292
|
+
"outputs": [],
|
|
293
|
+
"stateMutability": "nonpayable",
|
|
294
|
+
"type": "function"
|
|
295
|
+
},
|
|
201
296
|
{
|
|
202
297
|
"inputs": [],
|
|
203
298
|
"name": "registrationFee",
|
|
@@ -7,42 +7,111 @@ declare enum PublisherStatus {
|
|
|
7
7
|
SUSPENDED = 2,
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
|
|
10
|
+
* The on-chain id of a namespace within an app: the SDK's human-readable
|
|
11
|
+
* namespace name, hashed. Names never touch storage — the contract only ever
|
|
12
|
+
* sees these 32 bytes.
|
|
13
|
+
*/
|
|
14
|
+
declare function subspaceId(namespace: string): Hex;
|
|
15
|
+
/**
|
|
16
|
+
* The composite storage key the contract derives for `app:publisher:subspace`.
|
|
17
|
+
* Mirrors `namespace_key()` in contracts/data_registry — keccak256 over the
|
|
18
|
+
* packed 84 bytes. Computing it client-side is what lets a subscriber filter
|
|
19
|
+
* one exact subspace with a single indexed topic.
|
|
20
|
+
*/
|
|
21
|
+
declare function namespaceKey(appId: Hex, publisher: Address, subspace: Hex): Hex;
|
|
22
|
+
/**
|
|
23
|
+
* A decoded `StateCommitted` log — the single on-chain signal that one
|
|
24
|
+
* hierarchical namespace's state root advanced. `logId` uniquely identifies the
|
|
25
|
+
* emitting log (tx + index) so consumers can dedupe across the catch-up
|
|
26
|
+
* (getLogs) and live (watch) paths.
|
|
14
27
|
*/
|
|
15
28
|
interface StateCommittedLog {
|
|
29
|
+
namespaceKey: Hex;
|
|
30
|
+
appId: Hex;
|
|
31
|
+
publisher: Address;
|
|
32
|
+
subspaceId: Hex;
|
|
16
33
|
oldRoot: Hex;
|
|
17
34
|
newRoot: Hex;
|
|
18
35
|
blockNumber: bigint;
|
|
19
36
|
logId: string;
|
|
20
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Which slice of the app's commit stream to read. Every field is an indexed
|
|
40
|
+
* topic, so filtering happens on the RPC node — no indexer, no subgraph:
|
|
41
|
+
*
|
|
42
|
+
* {} — the whole app, every publisher and subspace
|
|
43
|
+
* { publisher } — one publisher's commits across the app
|
|
44
|
+
* { publisher, namespace } — one exact subspace (the tightest filter)
|
|
45
|
+
*
|
|
46
|
+
* `namespace` alone is not filterable: the key binds the publisher too, so a
|
|
47
|
+
* subspace name across all publishers means filtering the app stream in memory.
|
|
48
|
+
*/
|
|
49
|
+
interface CommitFilter {
|
|
50
|
+
publisher?: Address;
|
|
51
|
+
namespace?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* An unsigned transaction, hex-encoded and ready to hand to a browser wallet's
|
|
55
|
+
* `eth_sendTransaction` — no `from`, since the signing wallet supplies it.
|
|
56
|
+
*/
|
|
57
|
+
interface PreparedTx {
|
|
58
|
+
to: Address;
|
|
59
|
+
data: Hex;
|
|
60
|
+
chainId: number | undefined;
|
|
61
|
+
gas: Hex;
|
|
62
|
+
maxFeePerGas: Hex;
|
|
63
|
+
maxPriorityFeePerGas: Hex;
|
|
64
|
+
}
|
|
21
65
|
declare class DataRegistryClient {
|
|
22
66
|
private contractAddress;
|
|
67
|
+
private appId;
|
|
23
68
|
private publicClient;
|
|
24
69
|
private walletClient;
|
|
25
|
-
constructor(contractAddress: Address, publicClient: PublicClient, walletClient: WalletClient);
|
|
70
|
+
constructor(contractAddress: Address, appId: Hex, publicClient: PublicClient, walletClient: WalletClient);
|
|
71
|
+
/** The app namespace every call on this client is scoped to. */
|
|
72
|
+
getAppId(): Hex;
|
|
26
73
|
private getWriteConfig;
|
|
27
74
|
/**
|
|
28
75
|
* Internal helper to execute state-mutating transactions
|
|
29
76
|
* matching the exact gas buffer and aggressive fee styling of the protocol.
|
|
30
77
|
*/
|
|
31
78
|
private executeWrite;
|
|
79
|
+
/** The composite key for one of this app's namespaces. */
|
|
80
|
+
namespaceKey(publisher: Address, namespace: string): Hex;
|
|
81
|
+
/**
|
|
82
|
+
* Claim this client's `appId` on-chain. First come, first served — an app
|
|
83
|
+
* must own its id before any publisher can commit under it.
|
|
84
|
+
*/
|
|
85
|
+
registerApp(): Promise<Hash>;
|
|
32
86
|
/**
|
|
33
87
|
* Register as a new data publisher or reactivate a suspended account.
|
|
34
88
|
* Automatically reads the required on-chain fee and attaches it to the call.
|
|
35
89
|
*/
|
|
36
90
|
register(): Promise<Hash>;
|
|
37
91
|
/**
|
|
38
|
-
* The single mutating state gateway route.
|
|
39
|
-
*
|
|
92
|
+
* The single mutating state gateway route. Enforces the sequential
|
|
93
|
+
* Compare-And-Swap (CAS) timeline rule — per namespace, so concurrent pushes
|
|
94
|
+
* to *different* namespaces never contend.
|
|
95
|
+
*/
|
|
96
|
+
commitStateRoot(namespace: string, oldRoot: Hex, newRoot: Hex): Promise<Hash>;
|
|
97
|
+
/**
|
|
98
|
+
* Build the `commitStateRoot` call as an UNSIGNED transaction for `publisher`
|
|
99
|
+
* to sign, instead of sending it from this client's wallet.
|
|
100
|
+
*
|
|
101
|
+
* This is the self-custodial publish path: a relay server builds and pins the
|
|
102
|
+
* commit with a keyless service wallet, then hands this back for the user's
|
|
103
|
+
* browser wallet to sign. The server never holds the key that moves the head,
|
|
104
|
+
* and the on-chain sender IS the publisher — which is what authenticates the
|
|
105
|
+
* publish, since the contract keys the namespace by `msg_sender`.
|
|
40
106
|
*/
|
|
41
|
-
|
|
107
|
+
prepareCommitStateRoot(publisher: Address, namespace: string, oldRoot: Hex, newRoot: Hex): Promise<PreparedTx>;
|
|
42
108
|
/**
|
|
43
|
-
* Obtains the authoritative timeline tracking anchor for
|
|
109
|
+
* Obtains the authoritative timeline tracking anchor for one publisher's
|
|
110
|
+
* namespace within this app.
|
|
44
111
|
*/
|
|
45
|
-
getNamespaceHead(publisher: Address): Promise<Hex>;
|
|
112
|
+
getNamespaceHead(publisher: Address, namespace: string): Promise<Hex>;
|
|
113
|
+
/** Owner of this client's app id, or the zero address if unclaimed. */
|
|
114
|
+
getAppOwner(): Promise<Address>;
|
|
46
115
|
/**
|
|
47
116
|
* Reads the explicit enum lifecycle state from the registry status machine.
|
|
48
117
|
*/
|
|
@@ -53,18 +122,21 @@ declare class DataRegistryClient {
|
|
|
53
122
|
isRegistered(publisher: Address): Promise<boolean>;
|
|
54
123
|
/** Current chain head block number — the default "start here" for a fresh subscription. */
|
|
55
124
|
currentBlock(): Promise<bigint>;
|
|
125
|
+
/** Translate a `CommitFilter` into indexed-topic args for this app. */
|
|
126
|
+
private topicsFor;
|
|
56
127
|
/**
|
|
57
|
-
* Historical `StateCommitted` logs for one
|
|
58
|
-
* catch-up path for a subscriber resuming from a saved block
|
|
59
|
-
* filtered by
|
|
128
|
+
* Historical `StateCommitted` logs for one slice of this app, oldest →
|
|
129
|
+
* newest — the catch-up path for a subscriber resuming from a saved block
|
|
130
|
+
* cursor. Node-side filtered by indexed topics, so no indexer is involved.
|
|
60
131
|
*/
|
|
61
|
-
getStateCommittedLogs(
|
|
132
|
+
getStateCommittedLogs(filter: CommitFilter, fromBlock: bigint, toBlock?: bigint): Promise<StateCommittedLog[]>;
|
|
62
133
|
/**
|
|
63
|
-
* Live-watch `StateCommitted` for one
|
|
64
|
-
* function. Uses the client's transport (polling over HTTP, push
|
|
65
|
-
* WebSocket transport) — reads logs straight from the RPC node, no
|
|
134
|
+
* Live-watch `StateCommitted` for one slice of this app. Returns an
|
|
135
|
+
* unsubscribe function. Uses the client's transport (polling over HTTP, push
|
|
136
|
+
* over a WebSocket transport) — reads logs straight from the RPC node, no
|
|
137
|
+
* subgraph.
|
|
66
138
|
*/
|
|
67
|
-
watchStateCommitted(
|
|
139
|
+
watchStateCommitted(filter: CommitFilter, onCommit: (log: StateCommittedLog) => void, onError?: (err: Error) => void, pollingInterval?: number): () => void;
|
|
68
140
|
publisherCount(): Promise<bigint>;
|
|
69
141
|
registrationFee(): Promise<bigint>;
|
|
70
142
|
admin(): Promise<Address>;
|
|
@@ -78,4 +150,4 @@ declare class DataRegistryClient {
|
|
|
78
150
|
setRegistrationFee(fee: bigint): Promise<Hash>;
|
|
79
151
|
}
|
|
80
152
|
//#endregion
|
|
81
|
-
export { DataRegistryClient, PublisherStatus, StateCommittedLog };
|
|
153
|
+
export { CommitFilter, DataRegistryClient, PreparedTx, PublisherStatus, StateCommittedLog, namespaceKey, subspaceId };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DATA_REGISTRY_ABI } from "./abi.js";
|
|
2
|
+
import { concatHex, encodeFunctionData, keccak256, toHex } from "viem";
|
|
2
3
|
|
|
3
4
|
//#region src/contracts/data-registry/index.ts
|
|
4
5
|
let PublisherStatus = /* @__PURE__ */ function(PublisherStatus$1) {
|
|
@@ -7,21 +8,52 @@ let PublisherStatus = /* @__PURE__ */ function(PublisherStatus$1) {
|
|
|
7
8
|
PublisherStatus$1[PublisherStatus$1["SUSPENDED"] = 2] = "SUSPENDED";
|
|
8
9
|
return PublisherStatus$1;
|
|
9
10
|
}({});
|
|
11
|
+
/**
|
|
12
|
+
* The on-chain id of a namespace within an app: the SDK's human-readable
|
|
13
|
+
* namespace name, hashed. Names never touch storage — the contract only ever
|
|
14
|
+
* sees these 32 bytes.
|
|
15
|
+
*/
|
|
16
|
+
function subspaceId(namespace) {
|
|
17
|
+
return keccak256(toHex(namespace));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The composite storage key the contract derives for `app:publisher:subspace`.
|
|
21
|
+
* Mirrors `namespace_key()` in contracts/data_registry — keccak256 over the
|
|
22
|
+
* packed 84 bytes. Computing it client-side is what lets a subscriber filter
|
|
23
|
+
* one exact subspace with a single indexed topic.
|
|
24
|
+
*/
|
|
25
|
+
function namespaceKey(appId, publisher, subspace) {
|
|
26
|
+
return keccak256(concatHex([
|
|
27
|
+
appId,
|
|
28
|
+
publisher,
|
|
29
|
+
subspace
|
|
30
|
+
]));
|
|
31
|
+
}
|
|
10
32
|
function decodeStateCommitted(log) {
|
|
11
|
-
|
|
33
|
+
const a = log.args;
|
|
34
|
+
if (a.namespace_key === void 0 || a.app_id === void 0 || a.publisher === void 0 || a.subspace_id === void 0 || a.old_root === void 0 || a.new_root === void 0) throw new Error("Malformed log event: incomplete StateCommitted args");
|
|
12
35
|
return {
|
|
13
|
-
|
|
14
|
-
|
|
36
|
+
namespaceKey: a.namespace_key,
|
|
37
|
+
appId: a.app_id,
|
|
38
|
+
publisher: a.publisher,
|
|
39
|
+
subspaceId: a.subspace_id,
|
|
40
|
+
oldRoot: a.old_root,
|
|
41
|
+
newRoot: a.new_root,
|
|
15
42
|
blockNumber: log.blockNumber,
|
|
16
43
|
logId: `${log.transactionHash}:${log.logIndex.toString()}`
|
|
17
44
|
};
|
|
18
45
|
}
|
|
19
46
|
var DataRegistryClient = class {
|
|
20
|
-
constructor(contractAddress, publicClient, walletClient) {
|
|
47
|
+
constructor(contractAddress, appId, publicClient, walletClient) {
|
|
21
48
|
this.contractAddress = contractAddress;
|
|
49
|
+
this.appId = appId;
|
|
22
50
|
this.publicClient = publicClient;
|
|
23
51
|
this.walletClient = walletClient;
|
|
24
52
|
}
|
|
53
|
+
/** The app namespace every call on this client is scoped to. */
|
|
54
|
+
getAppId() {
|
|
55
|
+
return this.appId;
|
|
56
|
+
}
|
|
25
57
|
getWriteConfig() {
|
|
26
58
|
if (!this.walletClient.chain) throw new Error("Chain required");
|
|
27
59
|
if (!this.walletClient.account) throw new Error("Account required");
|
|
@@ -60,6 +92,17 @@ var DataRegistryClient = class {
|
|
|
60
92
|
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
61
93
|
return hash;
|
|
62
94
|
}
|
|
95
|
+
/** The composite key for one of this app's namespaces. */
|
|
96
|
+
namespaceKey(publisher, namespace) {
|
|
97
|
+
return namespaceKey(this.appId, publisher, subspaceId(namespace));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Claim this client's `appId` on-chain. First come, first served — an app
|
|
101
|
+
* must own its id before any publisher can commit under it.
|
|
102
|
+
*/
|
|
103
|
+
async registerApp() {
|
|
104
|
+
return this.executeWrite("registerApp", [this.appId]);
|
|
105
|
+
}
|
|
63
106
|
/**
|
|
64
107
|
* Register as a new data publisher or reactivate a suspended account.
|
|
65
108
|
* Automatically reads the required on-chain fee and attaches it to the call.
|
|
@@ -69,21 +112,84 @@ var DataRegistryClient = class {
|
|
|
69
112
|
return this.executeWrite("register", [], fee);
|
|
70
113
|
}
|
|
71
114
|
/**
|
|
72
|
-
* The single mutating state gateway route.
|
|
73
|
-
*
|
|
115
|
+
* The single mutating state gateway route. Enforces the sequential
|
|
116
|
+
* Compare-And-Swap (CAS) timeline rule — per namespace, so concurrent pushes
|
|
117
|
+
* to *different* namespaces never contend.
|
|
74
118
|
*/
|
|
75
|
-
async commitStateRoot(oldRoot, newRoot) {
|
|
76
|
-
return this.executeWrite("commitStateRoot", [
|
|
119
|
+
async commitStateRoot(namespace, oldRoot, newRoot) {
|
|
120
|
+
return this.executeWrite("commitStateRoot", [
|
|
121
|
+
this.appId,
|
|
122
|
+
subspaceId(namespace),
|
|
123
|
+
oldRoot,
|
|
124
|
+
newRoot
|
|
125
|
+
]);
|
|
77
126
|
}
|
|
78
127
|
/**
|
|
79
|
-
*
|
|
128
|
+
* Build the `commitStateRoot` call as an UNSIGNED transaction for `publisher`
|
|
129
|
+
* to sign, instead of sending it from this client's wallet.
|
|
130
|
+
*
|
|
131
|
+
* This is the self-custodial publish path: a relay server builds and pins the
|
|
132
|
+
* commit with a keyless service wallet, then hands this back for the user's
|
|
133
|
+
* browser wallet to sign. The server never holds the key that moves the head,
|
|
134
|
+
* and the on-chain sender IS the publisher — which is what authenticates the
|
|
135
|
+
* publish, since the contract keys the namespace by `msg_sender`.
|
|
80
136
|
*/
|
|
81
|
-
async
|
|
137
|
+
async prepareCommitStateRoot(publisher, namespace, oldRoot, newRoot) {
|
|
138
|
+
const data = encodeFunctionData({
|
|
139
|
+
abi: DATA_REGISTRY_ABI,
|
|
140
|
+
functionName: "commitStateRoot",
|
|
141
|
+
args: [
|
|
142
|
+
this.appId,
|
|
143
|
+
subspaceId(namespace),
|
|
144
|
+
oldRoot,
|
|
145
|
+
newRoot
|
|
146
|
+
]
|
|
147
|
+
});
|
|
148
|
+
const fees = await this.publicClient.estimateFeesPerGas();
|
|
149
|
+
let gas;
|
|
150
|
+
try {
|
|
151
|
+
gas = await this.publicClient.estimateGas({
|
|
152
|
+
account: publisher,
|
|
153
|
+
to: this.contractAddress,
|
|
154
|
+
data
|
|
155
|
+
}) * 3n / 2n;
|
|
156
|
+
} catch (err) {
|
|
157
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
158
|
+
if (/revert/i.test(message)) throw new Error(`settlement would revert — the head may have moved on-chain, or the app is unregistered: ${message}`);
|
|
159
|
+
gas = 5000000n;
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
to: this.contractAddress,
|
|
163
|
+
data,
|
|
164
|
+
chainId: this.publicClient.chain?.id,
|
|
165
|
+
gas: toHex(gas),
|
|
166
|
+
maxFeePerGas: toHex(fees.maxFeePerGas * 2n),
|
|
167
|
+
maxPriorityFeePerGas: toHex(fees.maxPriorityFeePerGas)
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Obtains the authoritative timeline tracking anchor for one publisher's
|
|
172
|
+
* namespace within this app.
|
|
173
|
+
*/
|
|
174
|
+
async getNamespaceHead(publisher, namespace) {
|
|
82
175
|
return this.publicClient.readContract({
|
|
83
176
|
address: this.contractAddress,
|
|
84
177
|
abi: DATA_REGISTRY_ABI,
|
|
85
178
|
functionName: "getNamespaceHead",
|
|
86
|
-
args: [
|
|
179
|
+
args: [
|
|
180
|
+
this.appId,
|
|
181
|
+
publisher,
|
|
182
|
+
subspaceId(namespace)
|
|
183
|
+
]
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/** Owner of this client's app id, or the zero address if unclaimed. */
|
|
187
|
+
async getAppOwner() {
|
|
188
|
+
return this.publicClient.readContract({
|
|
189
|
+
address: this.contractAddress,
|
|
190
|
+
abi: DATA_REGISTRY_ABI,
|
|
191
|
+
functionName: "getAppOwner",
|
|
192
|
+
args: [this.appId]
|
|
87
193
|
});
|
|
88
194
|
}
|
|
89
195
|
/**
|
|
@@ -112,35 +218,48 @@ var DataRegistryClient = class {
|
|
|
112
218
|
async currentBlock() {
|
|
113
219
|
return this.publicClient.getBlockNumber();
|
|
114
220
|
}
|
|
221
|
+
/** Translate a `CommitFilter` into indexed-topic args for this app. */
|
|
222
|
+
topicsFor(filter) {
|
|
223
|
+
const { publisher, namespace } = filter;
|
|
224
|
+
return {
|
|
225
|
+
app_id: this.appId,
|
|
226
|
+
...publisher ? { publisher } : {},
|
|
227
|
+
...publisher && namespace ? { namespace_key: this.namespaceKey(publisher, namespace) } : {}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
115
230
|
/**
|
|
116
|
-
* Historical `StateCommitted` logs for one
|
|
117
|
-
* catch-up path for a subscriber resuming from a saved block
|
|
118
|
-
* filtered by
|
|
231
|
+
* Historical `StateCommitted` logs for one slice of this app, oldest →
|
|
232
|
+
* newest — the catch-up path for a subscriber resuming from a saved block
|
|
233
|
+
* cursor. Node-side filtered by indexed topics, so no indexer is involved.
|
|
119
234
|
*/
|
|
120
|
-
async getStateCommittedLogs(
|
|
235
|
+
async getStateCommittedLogs(filter, fromBlock, toBlock) {
|
|
121
236
|
return (await this.publicClient.getContractEvents({
|
|
122
237
|
address: this.contractAddress,
|
|
123
238
|
abi: DATA_REGISTRY_ABI,
|
|
124
239
|
eventName: "StateCommitted",
|
|
125
|
-
args:
|
|
240
|
+
args: this.topicsFor(filter),
|
|
126
241
|
fromBlock,
|
|
127
242
|
toBlock: toBlock ?? "latest"
|
|
128
|
-
})).map(decodeStateCommitted).sort((a, b) => a.blockNumber === b.blockNumber ? a.logId.localeCompare(b.logId) : a.blockNumber < b.blockNumber ? -1 : 1);
|
|
243
|
+
})).map(decodeStateCommitted).filter((log) => matchesFilter(log, filter)).sort((a, b) => a.blockNumber === b.blockNumber ? a.logId.localeCompare(b.logId) : a.blockNumber < b.blockNumber ? -1 : 1);
|
|
129
244
|
}
|
|
130
245
|
/**
|
|
131
|
-
* Live-watch `StateCommitted` for one
|
|
132
|
-
* function. Uses the client's transport (polling over HTTP, push
|
|
133
|
-
* WebSocket transport) — reads logs straight from the RPC node, no
|
|
246
|
+
* Live-watch `StateCommitted` for one slice of this app. Returns an
|
|
247
|
+
* unsubscribe function. Uses the client's transport (polling over HTTP, push
|
|
248
|
+
* over a WebSocket transport) — reads logs straight from the RPC node, no
|
|
249
|
+
* subgraph.
|
|
134
250
|
*/
|
|
135
|
-
watchStateCommitted(
|
|
251
|
+
watchStateCommitted(filter, onCommit, onError, pollingInterval) {
|
|
136
252
|
return this.publicClient.watchContractEvent({
|
|
137
253
|
address: this.contractAddress,
|
|
138
254
|
abi: DATA_REGISTRY_ABI,
|
|
139
255
|
eventName: "StateCommitted",
|
|
140
|
-
args:
|
|
256
|
+
args: this.topicsFor(filter),
|
|
141
257
|
pollingInterval,
|
|
142
258
|
onLogs: (logs) => {
|
|
143
|
-
for (const
|
|
259
|
+
for (const raw of logs) {
|
|
260
|
+
const log = decodeStateCommitted(raw);
|
|
261
|
+
if (matchesFilter(log, filter)) onCommit(log);
|
|
262
|
+
}
|
|
144
263
|
},
|
|
145
264
|
onError
|
|
146
265
|
});
|
|
@@ -179,6 +298,16 @@ var DataRegistryClient = class {
|
|
|
179
298
|
return this.executeWrite("setRegistrationFee", [fee]);
|
|
180
299
|
}
|
|
181
300
|
};
|
|
301
|
+
/**
|
|
302
|
+
* The in-memory half of a filter. `subspace_id` is not an indexed topic (the
|
|
303
|
+
* three slots go to namespace_key/app_id/publisher), so a namespace-without-
|
|
304
|
+
* publisher filter — "every publisher's `docs` namespace" — narrows here
|
|
305
|
+
* instead of on the node.
|
|
306
|
+
*/
|
|
307
|
+
function matchesFilter(log, filter) {
|
|
308
|
+
if (!filter.namespace || filter.publisher) return true;
|
|
309
|
+
return log.subspaceId === subspaceId(filter.namespace);
|
|
310
|
+
}
|
|
182
311
|
|
|
183
312
|
//#endregion
|
|
184
|
-
export { DataRegistryClient, PublisherStatus };
|
|
313
|
+
export { DataRegistryClient, PublisherStatus, namespaceKey, subspaceId };
|
package/lib/contracts/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DATA_REGISTRY_ABI } from "./data-registry/abi.js";
|
|
2
|
-
import { DataRegistryClient, PublisherStatus, StateCommittedLog } from "./data-registry/index.js";
|
|
2
|
+
import { CommitFilter, DataRegistryClient, PreparedTx, PublisherStatus, StateCommittedLog, namespaceKey, subspaceId } from "./data-registry/index.js";
|
|
3
3
|
import { SettlementRegistryClient } from "./settlement-registry/index.js";
|
|
4
4
|
import { SETTLEMENT_REGISTRY_ABI } from "./settlement-registry/abi.js";
|
|
5
|
-
export { DATA_REGISTRY_ABI, DataRegistryClient, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, StateCommittedLog };
|
|
5
|
+
export { CommitFilter, DATA_REGISTRY_ABI, DataRegistryClient, PreparedTx, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, StateCommittedLog, namespaceKey, subspaceId };
|
package/lib/contracts/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DATA_REGISTRY_ABI } from "./data-registry/abi.js";
|
|
2
|
-
import { DataRegistryClient, PublisherStatus } from "./data-registry/index.js";
|
|
2
|
+
import { DataRegistryClient, PublisherStatus, namespaceKey, subspaceId } from "./data-registry/index.js";
|
|
3
3
|
import { SETTLEMENT_REGISTRY_ABI } from "./settlement-registry/abi.js";
|
|
4
4
|
import { SettlementRegistryClient } from "./settlement-registry/index.js";
|
|
5
5
|
|
|
6
|
-
export { DATA_REGISTRY_ABI, DataRegistryClient, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient };
|
|
6
|
+
export { DATA_REGISTRY_ABI, DataRegistryClient, PublisherStatus, SETTLEMENT_REGISTRY_ABI, SettlementRegistryClient, namespaceKey, subspaceId };
|