@genkaku/protocol 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/dist/bridge.d.ts +46 -0
  3. package/dist/bridge.d.ts.map +1 -0
  4. package/dist/bridge.js +609 -0
  5. package/dist/capabilities.d.ts +50 -0
  6. package/dist/capabilities.d.ts.map +1 -0
  7. package/dist/capabilities.js +66 -0
  8. package/dist/capabilities.test.d.ts +2 -0
  9. package/dist/capabilities.test.d.ts.map +1 -0
  10. package/dist/capabilities.test.js +39 -0
  11. package/dist/chain.d.ts +496 -0
  12. package/dist/chain.d.ts.map +1 -0
  13. package/dist/chain.js +763 -0
  14. package/dist/credit.d.ts +35 -0
  15. package/dist/credit.d.ts.map +1 -0
  16. package/dist/credit.js +112 -0
  17. package/dist/dial.d.ts +51 -0
  18. package/dist/dial.d.ts.map +1 -0
  19. package/dist/dial.js +287 -0
  20. package/dist/directory.d.ts +39 -0
  21. package/dist/directory.d.ts.map +1 -0
  22. package/dist/directory.js +149 -0
  23. package/dist/discovery.d.ts +20 -0
  24. package/dist/discovery.d.ts.map +1 -0
  25. package/dist/discovery.js +58 -0
  26. package/dist/hello.d.ts +14 -0
  27. package/dist/hello.d.ts.map +1 -0
  28. package/dist/hello.js +42 -0
  29. package/dist/index.d.ts +32 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +32 -0
  32. package/dist/keys.d.ts +30 -0
  33. package/dist/keys.d.ts.map +1 -0
  34. package/dist/keys.js +126 -0
  35. package/dist/llmBridge.d.ts +100 -0
  36. package/dist/llmBridge.d.ts.map +1 -0
  37. package/dist/llmBridge.js +284 -0
  38. package/dist/lp.d.ts +31 -0
  39. package/dist/lp.d.ts.map +1 -0
  40. package/dist/lp.js +116 -0
  41. package/dist/modeManager.d.ts +30 -0
  42. package/dist/modeManager.d.ts.map +1 -0
  43. package/dist/modeManager.js +81 -0
  44. package/dist/netconfig.d.ts +11 -0
  45. package/dist/netconfig.d.ts.map +1 -0
  46. package/dist/netconfig.js +39 -0
  47. package/dist/offer.d.ts +89 -0
  48. package/dist/offer.d.ts.map +1 -0
  49. package/dist/offer.js +86 -0
  50. package/dist/offer.test.d.ts +2 -0
  51. package/dist/offer.test.d.ts.map +1 -0
  52. package/dist/offer.test.js +33 -0
  53. package/dist/ollama.d.ts +53 -0
  54. package/dist/ollama.d.ts.map +1 -0
  55. package/dist/ollama.js +162 -0
  56. package/dist/p2pNode.d.ts +52 -0
  57. package/dist/p2pNode.d.ts.map +1 -0
  58. package/dist/p2pNode.js +454 -0
  59. package/dist/paywall.d.ts +30 -0
  60. package/dist/paywall.d.ts.map +1 -0
  61. package/dist/paywall.js +84 -0
  62. package/dist/publishOptIn.d.ts +15 -0
  63. package/dist/publishOptIn.d.ts.map +1 -0
  64. package/dist/publishOptIn.js +34 -0
  65. package/dist/receipts.d.ts +61 -0
  66. package/dist/receipts.d.ts.map +1 -0
  67. package/dist/receipts.js +135 -0
  68. package/dist/registry.d.ts +19 -0
  69. package/dist/registry.d.ts.map +1 -0
  70. package/dist/registry.js +50 -0
  71. package/dist/rpc.d.ts +24 -0
  72. package/dist/rpc.d.ts.map +1 -0
  73. package/dist/rpc.js +143 -0
  74. package/dist/status.d.ts +56 -0
  75. package/dist/status.d.ts.map +1 -0
  76. package/dist/status.js +354 -0
  77. package/dist/userSession.d.ts +77 -0
  78. package/dist/userSession.d.ts.map +1 -0
  79. package/dist/userSession.js +132 -0
  80. package/dist/workerSession.d.ts +87 -0
  81. package/dist/workerSession.d.ts.map +1 -0
  82. package/dist/workerSession.js +97 -0
  83. package/dist/workerSession.test.d.ts +2 -0
  84. package/dist/workerSession.test.d.ts.map +1 -0
  85. package/dist/workerSession.test.js +49 -0
  86. package/package.json +56 -3
  87. package/README.md +0 -3
@@ -0,0 +1,66 @@
1
+ /**
2
+ * What a shared model can actually do.
3
+ *
4
+ * Ollama reports a `capabilities` list per model (`completion`, `tools`,
5
+ * `thinking`, `vision`, …). A model that lists `tools` accepts the field, which
6
+ * is not the same as calling tools well: a 3B model usually emits malformed
7
+ * calls. So the tier combines the capability with the parameter count, and the
8
+ * UI labels only the bottom tier. Nothing here blocks a model from being
9
+ * offered — the marketplace marks it instead.
10
+ */
11
+ /** Size a model needs before its tool calls are worth advertising to an agent. */
12
+ export const AGENT_MIN_BILLIONS = 9;
13
+ /** Below this a model with the `tools` capability is only a light agent. */
14
+ export const LIGHT_MIN_BILLIONS = 3;
15
+ /**
16
+ * Parse Ollama's `details.parameter_size` ('1.2B', '9b', '70B', '135M').
17
+ * @param size - the raw string from `/api/tags`.
18
+ * @returns billions of parameters, or undefined when the string is not a size.
19
+ */
20
+ export function parseParameterSize(size) {
21
+ if (size == null)
22
+ return undefined;
23
+ const match = /^\s*([\d.]+)\s*([bm])/i.exec(size);
24
+ if (match == null)
25
+ return undefined;
26
+ const value = Number.parseFloat(match[1] ?? '');
27
+ if (!Number.isFinite(value))
28
+ return undefined;
29
+ return match[2]?.toLowerCase() === 'm' ? value / 1000 : value;
30
+ }
31
+ /**
32
+ * Classify a model for the marketplace.
33
+ *
34
+ * @param capabilities - Ollama's capability list.
35
+ * @param parametersB - billions of parameters, when known.
36
+ */
37
+ export function tierOf(capabilities, parametersB) {
38
+ if (!capabilities.includes('tools'))
39
+ return 'chat';
40
+ // Unknown size with the tools capability: treat as a light agent rather than
41
+ // claiming it can drive an agent loop.
42
+ if (parametersB == null)
43
+ return 'light';
44
+ if (parametersB >= AGENT_MIN_BILLIONS)
45
+ return 'agent';
46
+ if (parametersB >= LIGHT_MIN_BILLIONS)
47
+ return 'light';
48
+ return 'chat';
49
+ }
50
+ /**
51
+ * Fold `/api/show` capabilities and `/api/tags` details into one profile.
52
+ * @param model - model name as Ollama knows it.
53
+ * @param facts - whatever the two endpoints reported.
54
+ */
55
+ export function profileOf(model, facts) {
56
+ const capabilities = facts.capabilities ?? [];
57
+ const parametersB = parseParameterSize(facts.parameterSize);
58
+ return {
59
+ model,
60
+ capabilities,
61
+ ...(facts.parameterSize === undefined ? {} : { parameterSize: facts.parameterSize }),
62
+ ...(parametersB === undefined ? {} : { parametersB }),
63
+ tier: tierOf(capabilities, parametersB),
64
+ };
65
+ }
66
+ //# sourceMappingURL=capabilities.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=capabilities.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capabilities.test.d.ts","sourceRoot":"","sources":["../src/capabilities.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'node:test';
3
+ import { parseParameterSize, profileOf, tierOf } from "./capabilities.js";
4
+ test('parseParameterSize reads the forms Ollama reports', () => {
5
+ assert.equal(parseParameterSize('1.2B'), 1.2);
6
+ assert.equal(parseParameterSize('9b'), 9);
7
+ assert.equal(parseParameterSize('70B'), 70);
8
+ assert.equal(parseParameterSize('135M'), 0.135);
9
+ assert.equal(parseParameterSize(undefined), undefined);
10
+ assert.equal(parseParameterSize('unknown'), undefined);
11
+ });
12
+ test('a model without the tools capability is chat-only, whatever its size', () => {
13
+ assert.equal(tierOf(['completion'], 70), 'chat');
14
+ assert.equal(tierOf([], 70), 'chat');
15
+ assert.equal(tierOf(['completion', 'vision'], 70), 'chat');
16
+ });
17
+ test('tools + size decides agent or light', () => {
18
+ assert.equal(tierOf(['completion', 'tools'], 9), 'agent');
19
+ assert.equal(tierOf(['completion', 'tools'], 27), 'agent');
20
+ assert.equal(tierOf(['completion', 'tools'], 3.8), 'light');
21
+ assert.equal(tierOf(['completion', 'tools'], 1.2), 'chat');
22
+ });
23
+ test('tools with an unknown size is light, never agent', () => {
24
+ assert.equal(tierOf(['completion', 'tools'], undefined), 'light');
25
+ });
26
+ test('profileOf folds both endpoints into one row', () => {
27
+ const profile = profileOf('qwen3.8:27b', { capabilities: ['completion', 'tools', 'thinking'], parameterSize: '27B' });
28
+ assert.deepEqual(profile, {
29
+ model: 'qwen3.8:27b',
30
+ capabilities: ['completion', 'tools', 'thinking'],
31
+ parameterSize: '27B',
32
+ parametersB: 27,
33
+ tier: 'agent',
34
+ });
35
+ });
36
+ test('a failed probe degrades to chat instead of claiming agent support', () => {
37
+ assert.equal(profileOf('mystery', {}).tier, 'chat');
38
+ });
39
+ //# sourceMappingURL=capabilities.test.js.map
@@ -0,0 +1,496 @@
1
+ import type { Account, Chain, PublicClient, Transport, WalletClient } from 'viem';
2
+ import type { NetEnvironment } from './netconfig.ts';
3
+ import { type ReceiptPayloadV1 } from './receipts.ts';
4
+ export declare const ESCROW_ABI: readonly [{
5
+ readonly name: "openSession";
6
+ readonly type: "function";
7
+ readonly stateMutability: "nonpayable";
8
+ readonly inputs: readonly [{
9
+ readonly name: "worker";
10
+ readonly type: "address";
11
+ }, {
12
+ readonly name: "amount";
13
+ readonly type: "uint128";
14
+ }];
15
+ readonly outputs: readonly [{
16
+ readonly name: "id";
17
+ readonly type: "uint256";
18
+ }];
19
+ }, {
20
+ readonly name: "settle";
21
+ readonly type: "function";
22
+ readonly stateMutability: "nonpayable";
23
+ readonly inputs: readonly [{
24
+ readonly name: "id";
25
+ readonly type: "uint256";
26
+ }, {
27
+ readonly name: "receiptHash";
28
+ readonly type: "bytes32";
29
+ }, {
30
+ readonly name: "spend";
31
+ readonly type: "uint128";
32
+ }, {
33
+ readonly name: "workerSig";
34
+ readonly type: "bytes";
35
+ }];
36
+ readonly outputs: readonly [];
37
+ }, {
38
+ readonly name: "claim";
39
+ readonly type: "function";
40
+ readonly stateMutability: "nonpayable";
41
+ readonly inputs: readonly [];
42
+ readonly outputs: readonly [];
43
+ }, {
44
+ readonly name: "refund";
45
+ readonly type: "function";
46
+ readonly stateMutability: "nonpayable";
47
+ readonly inputs: readonly [{
48
+ readonly name: "id";
49
+ readonly type: "uint256";
50
+ }];
51
+ readonly outputs: readonly [];
52
+ }, {
53
+ readonly name: "sessions";
54
+ readonly type: "function";
55
+ readonly stateMutability: "view";
56
+ readonly inputs: readonly [{
57
+ readonly name: "id";
58
+ readonly type: "uint256";
59
+ }];
60
+ readonly outputs: readonly [{
61
+ readonly name: "user";
62
+ readonly type: "address";
63
+ }, {
64
+ readonly name: "worker";
65
+ readonly type: "address";
66
+ }, {
67
+ readonly name: "amount";
68
+ readonly type: "uint128";
69
+ }, {
70
+ readonly name: "openedAt";
71
+ readonly type: "uint64";
72
+ }, {
73
+ readonly name: "settled";
74
+ readonly type: "bool";
75
+ }, {
76
+ readonly name: "refunded";
77
+ readonly type: "bool";
78
+ }];
79
+ }, {
80
+ readonly name: "accrued";
81
+ readonly type: "function";
82
+ readonly stateMutability: "view";
83
+ readonly inputs: readonly [{
84
+ readonly name: "worker";
85
+ readonly type: "address";
86
+ }];
87
+ readonly outputs: readonly [{
88
+ readonly name: "balance";
89
+ readonly type: "uint128";
90
+ }];
91
+ }, {
92
+ readonly name: "SessionOpened";
93
+ readonly type: "event";
94
+ readonly anonymous: false;
95
+ readonly inputs: readonly [{
96
+ readonly name: "id";
97
+ readonly type: "uint256";
98
+ readonly indexed: true;
99
+ }, {
100
+ readonly name: "user";
101
+ readonly type: "address";
102
+ readonly indexed: true;
103
+ }, {
104
+ readonly name: "worker";
105
+ readonly type: "address";
106
+ readonly indexed: true;
107
+ }, {
108
+ readonly name: "amount";
109
+ readonly type: "uint128";
110
+ readonly indexed: false;
111
+ }];
112
+ }, {
113
+ readonly name: "SessionSettled";
114
+ readonly type: "event";
115
+ readonly anonymous: false;
116
+ readonly inputs: readonly [{
117
+ readonly name: "id";
118
+ readonly type: "uint256";
119
+ readonly indexed: true;
120
+ }, {
121
+ readonly name: "worker";
122
+ readonly type: "address";
123
+ readonly indexed: true;
124
+ }, {
125
+ readonly name: "user";
126
+ readonly type: "address";
127
+ readonly indexed: true;
128
+ }, {
129
+ readonly name: "spend";
130
+ readonly type: "uint128";
131
+ readonly indexed: false;
132
+ }, {
133
+ readonly name: "fee";
134
+ readonly type: "uint128";
135
+ readonly indexed: false;
136
+ }, {
137
+ readonly name: "leftover";
138
+ readonly type: "uint128";
139
+ readonly indexed: false;
140
+ }, {
141
+ readonly name: "receiptHash";
142
+ readonly type: "bytes32";
143
+ readonly indexed: false;
144
+ }];
145
+ }, {
146
+ readonly name: "SessionRefunded";
147
+ readonly type: "event";
148
+ readonly anonymous: false;
149
+ readonly inputs: readonly [{
150
+ readonly name: "id";
151
+ readonly type: "uint256";
152
+ readonly indexed: true;
153
+ }, {
154
+ readonly name: "user";
155
+ readonly type: "address";
156
+ readonly indexed: true;
157
+ }, {
158
+ readonly name: "amount";
159
+ readonly type: "uint128";
160
+ readonly indexed: false;
161
+ }];
162
+ }];
163
+ /** One escrow session as the worker must see it before spending GPU time. */
164
+ export type EscrowSession = {
165
+ id: bigint;
166
+ user: `0x${string}`;
167
+ worker: `0x${string}`;
168
+ amount: bigint;
169
+ openedAt: number;
170
+ settled: boolean;
171
+ refunded: boolean;
172
+ };
173
+ /** Read the escrow record a client claims to have funded. The paywall (P4.9d)
174
+ * refuses to serve unless this returns a live session that pays THIS worker. */
175
+ export declare function readEscrowSession(publicC: GenkakuPublic, env: NetEnvironment, id: bigint): Promise<EscrowSession | null>;
176
+ /** WorkerRegistry v0.2 (P4.9a) — wallet ⇄ PeerId binding + on-chain directory.
177
+ * Register/advertise writes are worker-key-gas (self-paid); reads are the
178
+ * data-plane compare. The locator lives ONLY in the `WorkerLocated` log, so
179
+ * the directory is rebuilt by scanning logs — never by storage reads. */
180
+ export declare const REGISTRY_ABI: readonly [{
181
+ readonly name: "register";
182
+ readonly type: "function";
183
+ readonly stateMutability: "nonpayable";
184
+ readonly inputs: readonly [{
185
+ readonly name: "peerId";
186
+ readonly type: "bytes32";
187
+ }, {
188
+ readonly name: "policyHash";
189
+ readonly type: "bytes32";
190
+ }];
191
+ readonly outputs: readonly [];
192
+ }, {
193
+ readonly name: "registerWithLocator";
194
+ readonly type: "function";
195
+ readonly stateMutability: "nonpayable";
196
+ readonly inputs: readonly [{
197
+ readonly name: "peerId";
198
+ readonly type: "bytes32";
199
+ }, {
200
+ readonly name: "policyHash";
201
+ readonly type: "bytes32";
202
+ }, {
203
+ readonly name: "locator";
204
+ readonly type: "string";
205
+ }];
206
+ readonly outputs: readonly [];
207
+ }, {
208
+ readonly name: "advertise";
209
+ readonly type: "function";
210
+ readonly stateMutability: "nonpayable";
211
+ readonly inputs: readonly [{
212
+ readonly name: "locator";
213
+ readonly type: "string";
214
+ }];
215
+ readonly outputs: readonly [];
216
+ }, {
217
+ readonly name: "selfDeactivate";
218
+ readonly type: "function";
219
+ readonly stateMutability: "nonpayable";
220
+ readonly inputs: readonly [];
221
+ readonly outputs: readonly [];
222
+ }, {
223
+ readonly name: "MAX_LOCATOR_BYTES";
224
+ readonly type: "function";
225
+ readonly stateMutability: "view";
226
+ readonly inputs: readonly [];
227
+ readonly outputs: readonly [{
228
+ readonly name: "";
229
+ readonly type: "uint256";
230
+ }];
231
+ }, {
232
+ readonly name: "WorkerLocated";
233
+ readonly type: "event";
234
+ readonly inputs: readonly [{
235
+ readonly name: "wallet";
236
+ readonly type: "address";
237
+ readonly indexed: true;
238
+ }, {
239
+ readonly name: "peerId";
240
+ readonly type: "bytes32";
241
+ readonly indexed: true;
242
+ }, {
243
+ readonly name: "locator";
244
+ readonly type: "string";
245
+ readonly indexed: false;
246
+ }, {
247
+ readonly name: "updatedAt";
248
+ readonly type: "uint64";
249
+ readonly indexed: false;
250
+ }];
251
+ }, {
252
+ readonly name: "WorkerDeactivated";
253
+ readonly type: "event";
254
+ readonly inputs: readonly [{
255
+ readonly name: "wallet";
256
+ readonly type: "address";
257
+ readonly indexed: true;
258
+ }, {
259
+ readonly name: "reason";
260
+ readonly type: "string";
261
+ readonly indexed: false;
262
+ }];
263
+ }, {
264
+ readonly name: "workers";
265
+ readonly type: "function";
266
+ readonly stateMutability: "view";
267
+ readonly inputs: readonly [{
268
+ readonly name: "";
269
+ readonly type: "address";
270
+ }];
271
+ readonly outputs: readonly [{
272
+ readonly name: "peerId";
273
+ readonly type: "bytes32";
274
+ }, {
275
+ readonly name: "policyHash";
276
+ readonly type: "bytes32";
277
+ }, {
278
+ readonly name: "registeredAt";
279
+ readonly type: "uint64";
280
+ }, {
281
+ readonly name: "lastActiveAt";
282
+ readonly type: "uint64";
283
+ }, {
284
+ readonly name: "active";
285
+ readonly type: "bool";
286
+ }];
287
+ }, {
288
+ readonly name: "isActive";
289
+ readonly type: "function";
290
+ readonly stateMutability: "view";
291
+ readonly inputs: readonly [{
292
+ readonly name: "wallet";
293
+ readonly type: "address";
294
+ }];
295
+ readonly outputs: readonly [{
296
+ readonly name: "";
297
+ readonly type: "bool";
298
+ }];
299
+ }];
300
+ export declare const USDC_ABI: readonly [{
301
+ readonly name: "approve";
302
+ readonly type: "function";
303
+ readonly stateMutability: "nonpayable";
304
+ readonly inputs: readonly [{
305
+ readonly name: "spender";
306
+ readonly type: "address";
307
+ }, {
308
+ readonly name: "amount";
309
+ readonly type: "uint256";
310
+ }];
311
+ readonly outputs: readonly [{
312
+ readonly name: "";
313
+ readonly type: "bool";
314
+ }];
315
+ }, {
316
+ readonly name: "allowance";
317
+ readonly type: "function";
318
+ readonly stateMutability: "view";
319
+ readonly inputs: readonly [{
320
+ readonly name: "owner";
321
+ readonly type: "address";
322
+ }, {
323
+ readonly name: "spender";
324
+ readonly type: "address";
325
+ }];
326
+ readonly outputs: readonly [{
327
+ readonly name: "";
328
+ readonly type: "uint256";
329
+ }];
330
+ }, {
331
+ readonly name: "balanceOf";
332
+ readonly type: "function";
333
+ readonly stateMutability: "view";
334
+ readonly inputs: readonly [{
335
+ readonly name: "owner";
336
+ readonly type: "address";
337
+ }];
338
+ readonly outputs: readonly [{
339
+ readonly name: "";
340
+ readonly type: "uint256";
341
+ }];
342
+ }];
343
+ /** Public reads. The transport is a failover list (P4.9b): a rate-limited or
344
+ * dead endpoint rolls to the next one without surfacing anything to the user. */
345
+ /** Explicit return type: viem's inferred client type points into its own
346
+ * package-internal modules, which cannot be re-exported from a declaration. */
347
+ export declare function publicClientFor(env: NetEnvironment): PublicClient;
348
+ /** Local-account wallet client — signs and pays gas itself. Writes go through
349
+ * the same failover list, so a flaky endpoint cannot strand a registration. */
350
+ /** Explicit return type, same reason as {@link publicClientFor}. */
351
+ export declare function walletClientFor(env: NetEnvironment, key: `0x${string}`): WalletClient<Transport, Chain, Account>;
352
+ export type GenkakuPublic = ReturnType<typeof publicClientFor>;
353
+ export type GenkakuWallet = ReturnType<typeof walletClientFor>;
354
+ /** The worker's settle co-sign digest: keccak256(abi.encodePacked(id,
355
+ * receiptHash, spend)) — an EIP-191 signature over this is what settle's
356
+ * `_verify` checks against the session's WORKER address. */
357
+ export declare function settleDigest(escrowId: bigint, receiptHash: `0x${string}`, spend: bigint): Uint8Array;
358
+ /** The on-chain `receiptHash` argument: the digest of the worker-held max
359
+ * cumulative receipt (bit-for-bit what both data-plane sides verify). */
360
+ export declare function receiptHashFor(receipt: ReceiptPayloadV1): `0x${string}`;
361
+ export declare function readAccrued(publicC: GenkakuPublic, env: NetEnvironment, worker: `0x${string}`): Promise<bigint>;
362
+ /** Native gas and USDC balances for one wallet, read in parallel. */
363
+ export type WalletBalances = {
364
+ eth: bigint;
365
+ usdc: bigint;
366
+ };
367
+ /**
368
+ * Read the balances a user needs before opening an escrow: USDC to pay and ETH
369
+ * for gas. A chain without a deployed escrow still has canonical USDC.
370
+ * @param publicC - public client for the selected network.
371
+ * @param env - network whose USDC address is used.
372
+ * @param wallet - account to read.
373
+ * @returns gas and 6-decimal USDC balances.
374
+ */
375
+ export declare function readWalletBalances(publicC: GenkakuPublic, env: NetEnvironment, wallet: `0x${string}`): Promise<WalletBalances>;
376
+ /** The worker account's own wallet address (P4.5 binds peerId ⇄ wallet via
377
+ * WorkerRegistry; offers self-report it until then). */
378
+ export declare function walletAddress(key: `0x${string}`): `0x${string}`;
379
+ /** Approve (only when the allowance is short) + open. The numeric escrow id
380
+ * is parsed from SessionOpened's OWN log — never from a counter probe (a
381
+ * concurrent open would desync the id). */
382
+ export declare function openEscrow(publicC: GenkakuPublic, walletC: GenkakuWallet, env: NetEnvironment, worker: `0x${string}`, amount: bigint): Promise<{
383
+ escrowId: bigint;
384
+ approved: bigint;
385
+ }>;
386
+ export declare function settleSession(walletC: GenkakuWallet, env: NetEnvironment, args: {
387
+ escrowId: bigint;
388
+ receiptHash: `0x${string}`;
389
+ spend: bigint;
390
+ workerSig: `0x${string}`;
391
+ }): Promise<`0x${string}`>;
392
+ /** Reclaim an unsettled escrow. A short session can bill zero started minutes;
393
+ * without this the funds would sit in the contract until the user found a
394
+ * manual refund, which a non-technical user never will. */
395
+ export declare function refundSession(walletC: GenkakuWallet, env: NetEnvironment, escrowId: bigint): Promise<`0x${string}`>;
396
+ export declare function claimAccrual(walletC: GenkakuWallet, env: NetEnvironment): Promise<`0x${string}`>;
397
+ /** The chain view of one worker's registry record (null when never active). */
398
+ export declare function readWorker(publicC: GenkakuPublic, env: NetEnvironment, wallet: `0x${string}`): Promise<{
399
+ active: boolean;
400
+ peerId: `0x${string}`;
401
+ policyHash: `0x${string}`;
402
+ } | null>;
403
+ /** Register (or re-pin) the CALLER's wallet to its PeerId digest + policy.
404
+ * The caller is the worker wallet itself (msg.sender == the binding) — the
405
+ * desktop pays gas from the worker key. Testnet-only (viemChain refuses
406
+ * mainnet until P6). */
407
+ export declare function registerWorker(walletC: GenkakuWallet, env: NetEnvironment, args: {
408
+ peerIdDigest: `0x${string}`;
409
+ policyHash: `0x${string}`;
410
+ }): Promise<{
411
+ txHash: `0x${string}`;
412
+ rebind?: boolean;
413
+ }>;
414
+ /** First block that can hold a v0.2 directory entry (the scan floor). */
415
+ export declare const REGISTRY_DEPLOY_BLOCK: Record<NetEnvironment, bigint>;
416
+ /** First block that can hold a v0.2 escrow session (same deployment batch). */
417
+ export declare const ESCROW_DEPLOY_BLOCK: Record<NetEnvironment, bigint>;
418
+ /** The contract's locator ceiling (mirrors MAX_LOCATOR_BYTES). */
419
+ export declare const MAX_LOCATOR_BYTES = 256;
420
+ /** Four addresses is enough for LAN + public + relay without paying for more. */
421
+ export declare const MAX_LOCATOR_ADDRS = 4;
422
+ /** Build the on-chain locator from the addresses libp2p actually announces.
423
+ * Loopback is dropped (useless to a remote peer) and the list is capped so it
424
+ * always fits MAX_LOCATOR_BYTES. Public addrs come first: a client that can
425
+ * only use one should try the routable one. Separator is `;` — a multiaddr
426
+ * never contains it, so parsing stays unambiguous. */
427
+ export declare function locatorOf(addrs: readonly string[], maxBytes?: number, maxCount?: number): string;
428
+ /** True when this address is reachable from the public internet. LAN, loopback,
429
+ * link-local and CGNAT (100.64/10) ranges are local-only; a relay address is
430
+ * signaling, not a data path (P4.9g). A user-supplied DNS name is trusted. */
431
+ export declare function isPublicAddr(addr: string): boolean;
432
+ /** Does this peer announce any address the global directory can use? Without
433
+ * one, on-chain presence only advertises a dead end (P4.9g §39). */
434
+ export declare function hasPublicAddr(addrs: readonly string[]): boolean;
435
+ /** Split a locator back into multiaddrs (tolerates `;`, newline, whitespace). */
436
+ export declare function parseLocator(locator: string): string[];
437
+ /** Register (or re-pin) AND publish the locator in one transaction. */
438
+ export declare function registerWorkerWithLocator(walletC: GenkakuWallet, env: NetEnvironment, args: {
439
+ peerIdDigest: `0x${string}`;
440
+ policyHash: `0x${string}`;
441
+ locator: string;
442
+ }): Promise<`0x${string}`>;
443
+ /** Publish a new locator for an already-registered worker. Only call this when
444
+ * the announced address set actually changed — each call is a chain write. */
445
+ export declare function advertiseWorker(walletC: GenkakuWallet, env: NetEnvironment, locator: string): Promise<`0x${string}`>;
446
+ /** Remove this wallet from the live directory. Used when a worker loses the
447
+ * public address it advertised — a stale entry is worse than none. */
448
+ export declare function deactivateWorker(walletC: GenkakuWallet, env: NetEnvironment): Promise<`0x${string}`>;
449
+ /** One live directory entry: the newest locator a worker published. */
450
+ export type DirectoryEntry = {
451
+ wallet: `0x${string}`;
452
+ peerId: `0x${string}`;
453
+ locator: string;
454
+ addrs: string[];
455
+ updatedAt: number;
456
+ blockNumber: bigint;
457
+ };
458
+ /** The shape every log scan touches; viem returns far more, we ignore it. */
459
+ type RawLog = {
460
+ args: unknown;
461
+ blockNumber: bigint | null;
462
+ };
463
+ /** Any event this module scans (registry directory or escrow sessions). */
464
+ type ChainEventAbi = Extract<(typeof REGISTRY_ABI)[number] | (typeof ESCROW_ABI)[number], {
465
+ type: 'event';
466
+ }>;
467
+ /** Walk a block range, halving it whenever the RPC refuses the span.
468
+ * Public Base endpoints disagree wildly on `eth_getLogs` limits (some reject
469
+ * even a single block), so the scan floor is discovered at runtime instead of
470
+ * assumed. */
471
+ export declare function collectLogs(publicC: GenkakuPublic, address: `0x${string}`, event: ChainEventAbi, fromBlock: bigint, toBlock: bigint, args?: Record<string, unknown>, depth?: number): Promise<readonly RawLog[]>;
472
+ /** Rebuild the worker directory from chain logs — the whole point of P4.9a.
473
+ * Returns only entries still live: a wallet whose LAST directory-relevant
474
+ * event is `WorkerDeactivated` is dropped (the tombstone stays in history,
475
+ * which is exactly what an auditor needs). */
476
+ export declare function readWorkerLocators(publicC: GenkakuPublic, env: NetEnvironment, opts?: {
477
+ fromBlock?: bigint;
478
+ toBlock?: bigint;
479
+ chunkSize?: bigint;
480
+ wallet?: `0x${string}`;
481
+ }): Promise<DirectoryEntry[]>;
482
+ /** Wallets that tombstoned themselves in a block range. Incremental readers
483
+ * need these separately: `readWorkerLocators` only reports live entries for
484
+ * the range it scanned, so a cached entry must be evicted by a later death. */
485
+ export declare function readWorkerDeactivations(publicC: GenkakuPublic, env: NetEnvironment, opts: {
486
+ fromBlock: bigint;
487
+ toBlock?: bigint;
488
+ chunkSize?: bigint;
489
+ }): Promise<{
490
+ wallet: `0x${string}`;
491
+ blockNumber: bigint;
492
+ }[]>;
493
+ /** Receipt signing re-export (single crypto door for callers). */
494
+ export declare function signReceiptPayload(key: `0x${string}`, receipt: ReceiptPayloadV1): Promise<string>;
495
+ export {};
496
+ //# sourceMappingURL=chain.d.ts.map
@@ -0,0 +1 @@
1
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