@sig-net/midnight 0.0.1 → 0.0.3

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 (62) hide show
  1. package/dist/index.d.ts +2 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2 -4
  4. package/dist/index.js.map +1 -1
  5. package/dist/managed/compiler/contract-info.json +57 -0
  6. package/dist/managed/compiler/contract-manifest.json +8 -8
  7. package/dist/managed/contract/index.d.ts +13 -0
  8. package/dist/managed/contract/index.js +138 -73
  9. package/dist/managed/contract/index.js.map +1 -1
  10. package/dist/signature-requests-state-reader.d.ts +5 -5
  11. package/dist/signature-requests-state-reader.js +5 -5
  12. package/dist/signet-contract-state-reader.d.ts +88 -2
  13. package/dist/signet-contract-state-reader.d.ts.map +1 -1
  14. package/dist/signet-contract-state-reader.js +106 -3
  15. package/dist/signet-contract-state-reader.js.map +1 -1
  16. package/dist/signet-request-feed.d.ts +40 -21
  17. package/dist/signet-request-feed.d.ts.map +1 -1
  18. package/dist/signet-request-feed.js +79 -34
  19. package/dist/signet-request-feed.js.map +1 -1
  20. package/dist/signet-request-resolver.d.ts +11 -11
  21. package/dist/signet-request-resolver.d.ts.map +1 -1
  22. package/dist/signet-request-resolver.js +22 -22
  23. package/dist/signet-request-resolver.js.map +1 -1
  24. package/dist/signet-requests.d.ts +18 -4
  25. package/dist/signet-requests.d.ts.map +1 -1
  26. package/dist/signet-requests.js +29 -6
  27. package/dist/signet-requests.js.map +1 -1
  28. package/package.json +2 -2
  29. package/src/Signet.compact +75 -99
  30. package/src/circuits.compact +5 -2
  31. package/dist/signet-event-observer.d.ts +0 -102
  32. package/dist/signet-event-observer.d.ts.map +0 -1
  33. package/dist/signet-event-observer.js +0 -127
  34. package/dist/signet-event-observer.js.map +0 -1
  35. package/dist/signet-events.d.ts +0 -175
  36. package/dist/signet-events.d.ts.map +0 -1
  37. package/dist/signet-events.js +0 -210
  38. package/dist/signet-events.js.map +0 -1
  39. package/dist/signet-response-feed.d.ts +0 -161
  40. package/dist/signet-response-feed.d.ts.map +0 -1
  41. package/dist/signet-response-feed.js +0 -234
  42. package/dist/signet-response-feed.js.map +0 -1
  43. package/src/constants.ts +0 -97
  44. package/src/epsilon-derivation.ts +0 -62
  45. package/src/index.ts +0 -31
  46. package/src/managed/compiler/contract-info.json +0 -165
  47. package/src/managed/compiler/contract-manifest.json +0 -32
  48. package/src/managed/contract/index.d.ts +0 -65
  49. package/src/managed/contract/index.js +0 -611
  50. package/src/managed/contract/index.js.map +0 -8
  51. package/src/schnorr.ts +0 -210
  52. package/src/signature-requests-state-reader.ts +0 -223
  53. package/src/signature-response-verification.ts +0 -64
  54. package/src/signature-state-reading.ts +0 -87
  55. package/src/signet-contract-state-reader.ts +0 -391
  56. package/src/signet-event-observer.ts +0 -186
  57. package/src/signet-events.ts +0 -337
  58. package/src/signet-request-feed.ts +0 -158
  59. package/src/signet-request-resolver.ts +0 -116
  60. package/src/signet-request-response-reader.ts +0 -324
  61. package/src/signet-requests.ts +0 -737
  62. package/src/signet-response-feed.ts +0 -325
@@ -1,325 +0,0 @@
1
- // The requesting client's entry point for discovering MPC signature responses
2
- // by event observation: watch the central signet contract's
3
- // SignatureRespondedEvent notifications, and when one announces a post for a
4
- // request the client cares about, read the response log back and verify every
5
- // post against the request — the response-direction mirror of
6
- // signet-request-feed.ts. The event is only the TRIGGER for a state read; the
7
- // verdicts come from the ledger via the same SignetRequestResponseReader all
8
- // other consumers use, so an event/state indexing skew in either direction is
9
- // tolerated (a post visible in state before its event is still yielded; an
10
- // event whose write has not indexed yet is retried next cycle).
11
- //
12
- // Trust model (see "Signet Contract Ledger Layout" in Signet.compact): the
13
- // response log is UNAUTHENTICATED — neither the event nor the stored record
14
- // confers authenticity. Only the off-chain signature verification does, which
15
- // is why the feed yields per-post VERDICTS rather than raw responses.
16
- //
17
- // The MPC's OTHER response kind — the respond-bidirectional attestation of a
18
- // request's remote execution — gets the same event-gated treatment via
19
- // SignetRespondBidirectionalFeed (RespondBidirectionalEvent → index read),
20
- // but with the opposite trust model: the signet contract verified the
21
- // attestation IN-CIRCUIT at post time, so the feed yields the stored record
22
- // itself, no verdicts.
23
-
24
- import {
25
- SignetEventObserver,
26
- sleepUnlessAborted,
27
- type SignetEventSource,
28
- } from "./signet-event-observer.ts";
29
- import {
30
- respondBidirectionalEventCodec,
31
- signatureRespondedEventCodec,
32
- type RespondBidirectionalEvent,
33
- type SignatureRespondedEvent,
34
- } from "./signet-events.ts";
35
- import {
36
- readSignetContractLedgerFromState,
37
- type RespondBidirectional,
38
- } from "./signet-contract-state-reader.ts";
39
- import {
40
- SignetRequestResponseReader,
41
- type SignatureResponseVerdict,
42
- type SignetPublicStateSource,
43
- } from "./signet-request-response-reader.ts";
44
- import type {
45
- RequestIdHex,
46
- SignBidirectionalRequest,
47
- } from "./signet-requests.ts";
48
-
49
- /** Default gap between poll cycles of {@link SignetResponseFeed.verdicts}. */
50
- export const DEFAULT_RESPONSE_FEED_POLL_INTERVAL_MS = 3000;
51
-
52
- /** Everything a {@link SignetResponseFeed} needs. */
53
- export interface SignetResponseFeedConfig {
54
- /** Address of the central signet contract whose events and response log to watch. */
55
- readonly signetContractAddress: string;
56
- /** Address of the signet-compliant requester contract holding the request records (e.g. the vault). */
57
- readonly requesterContractAddress: string;
58
- /**
59
- * Source of BOTH events and raw contract state — a full
60
- * `indexerPublicDataProvider` satisfies both halves.
61
- */
62
- readonly source: SignetEventSource & SignetPublicStateSource;
63
- /** Durable resume floor for the underlying observer (see {@link SignetEventObserverConfig.fromEventId}). */
64
- readonly fromEventId?: number;
65
- /** Poll cadence for {@link SignetResponseFeed.verdicts}; default {@link DEFAULT_RESPONSE_FEED_POLL_INTERVAL_MS}. */
66
- readonly pollIntervalMs?: number;
67
- }
68
-
69
- /**
70
- * The event-driven signature response feed. Composes a
71
- * {@link SignetEventObserver} of {@link SignatureRespondedEvent} notifications
72
- * (discovery) with a {@link SignetRequestResponseReader} (verification),
73
- * yielding one {@link SignatureResponseVerdict} per posted response, in count
74
- * order, each exactly once per feed lifetime. Consumers act on the first
75
- * valid verdict and surface the rejected ones as they see fit — the feed,
76
- * like the reader, never logs.
77
- */
78
- export class SignetResponseFeed {
79
- private readonly observer: SignetEventObserver<SignatureRespondedEvent>;
80
- private readonly reader: SignetRequestResponseReader;
81
- private readonly pollIntervalMs: number;
82
-
83
- // Post counts already yielded, per request. NOT a trust boundary (the
84
- // verdicts are) — just an exactly-once gate so a post is not re-judged.
85
- private readonly yielded = new Map<RequestIdHex, Set<bigint>>();
86
-
87
- /**
88
- * @param config - The contract pair, combined event/state source, and
89
- * resume floor.
90
- */
91
- constructor(config: SignetResponseFeedConfig) {
92
- this.observer = new SignetEventObserver({
93
- signetContractAddress: config.signetContractAddress,
94
- source: config.source,
95
- codec: signatureRespondedEventCodec,
96
- fromEventId: config.fromEventId,
97
- pollIntervalMs: config.pollIntervalMs,
98
- });
99
- this.reader = new SignetRequestResponseReader({
100
- requesterContractAddress: config.requesterContractAddress,
101
- signetContractAddress: config.signetContractAddress,
102
- publicDataProvider: config.source,
103
- });
104
- this.pollIntervalMs =
105
- config.pollIntervalMs ?? DEFAULT_RESPONSE_FEED_POLL_INTERVAL_MS;
106
- }
107
-
108
- /** The yielded-count set for `requestId`, created on first use. */
109
- private yieldedCounts(requestId: RequestIdHex): Set<bigint> {
110
- let counts = this.yielded.get(requestId);
111
- if (counts === undefined) {
112
- counts = new Set();
113
- this.yielded.set(requestId, counts);
114
- }
115
- return counts;
116
- }
117
-
118
- /**
119
- * One-shot: the verdicts on every response post for `requestId` not yielded
120
- * before, in count order. The response log is only read when a currently-
121
- * visible event announces a post this feed has not yielded yet — no new
122
- * event, no state query. Ledger posts the read then surfaces beyond the
123
- * announced ones are yielded too (events are the trigger, the ledger is the
124
- * source of truth), and a post whose event is visible but whose write has
125
- * not indexed yet is simply retried next cycle.
126
- *
127
- * @param requestId - The request whose response posts to judge.
128
- * @param expectedSigner - The EVM address (0x hex, any case) the genuine
129
- * response must be signed by — the requester's MPC-derived address.
130
- * @returns The newly-judged verdicts this cycle (possibly empty).
131
- * @throws Error when either contract has no state, the request is not on
132
- * the requester's ledger, or the responses ledger is inconsistent.
133
- */
134
- async poll(
135
- requestId: RequestIdHex,
136
- expectedSigner: string,
137
- ): Promise<SignatureResponseVerdict[]> {
138
- const events = await this.observer.currentEvents();
139
- const counts = this.yieldedCounts(requestId);
140
- const announcesNew = events.some(
141
- (event) => event.requestId === requestId && !counts.has(event.count),
142
- );
143
- if (!announcesNew) {
144
- return [];
145
- }
146
- const { verdicts } = await this.reader.getVerifiedSignatureResponse(
147
- requestId,
148
- expectedSigner,
149
- );
150
- const fresh = verdicts.filter((verdict) => !counts.has(verdict.count));
151
- for (const verdict of fresh) {
152
- counts.add(verdict.count);
153
- }
154
- return fresh;
155
- }
156
-
157
- /**
158
- * Live stream: poll + sleep, yielding the verdict on each posted response
159
- * exactly once, in count order, until `opts.signal` aborts. The natural
160
- * consumption is to warn on each rejected verdict and act on the first
161
- * valid one (see the cli's poll-signature-response command).
162
- *
163
- * @param requestId - The request whose response posts to judge.
164
- * @param expectedSigner - The EVM address (0x hex, any case) the genuine
165
- * response must be signed by — the requester's MPC-derived address.
166
- * @param opts.signal - Abort to stop the stream.
167
- * @yields Each post's verdict, in discovery order.
168
- * @throws Error when either contract has no state, the request is not on
169
- * the requester's ledger, or the responses ledger is inconsistent.
170
- */
171
- async *verdicts(
172
- requestId: RequestIdHex,
173
- expectedSigner: string,
174
- opts?: { signal?: AbortSignal },
175
- ): AsyncIterableIterator<SignatureResponseVerdict> {
176
- while (!opts?.signal?.aborted) {
177
- const batch = await this.poll(requestId, expectedSigner);
178
- for (const verdict of batch) yield verdict;
179
- if (opts?.signal?.aborted) break;
180
- await sleepUnlessAborted(this.pollIntervalMs, opts?.signal);
181
- }
182
- }
183
-
184
- /**
185
- * The request record a verdict's response answers, read through the feed's
186
- * own reader (cached after the first fetch — free after any {@link poll}
187
- * that reached verification). What a consumer needs to assemble the signed
188
- * transaction from a valid verdict via
189
- * `signBidirectionalRequestToSignedEVMTransaction`.
190
- *
191
- * @param requestId - The request id to look up.
192
- * @returns The stored request record.
193
- * @throws Error when the requester contract has no state or holds no
194
- * request under `requestId`.
195
- */
196
- async getSignatureRequest(
197
- requestId: RequestIdHex,
198
- ): Promise<SignBidirectionalRequest> {
199
- return this.reader.getSignatureRequest(requestId);
200
- }
201
- }
202
-
203
- /** Everything a {@link SignetRespondBidirectionalFeed} needs. */
204
- export interface SignetRespondBidirectionalFeedConfig {
205
- /**
206
- * Address of the central signet contract whose events and
207
- * respond-bidirectional index to watch. Attestations live entirely on the
208
- * signet contract, so — unlike {@link SignetResponseFeedConfig} — no
209
- * requester contract address is needed.
210
- */
211
- readonly signetContractAddress: string;
212
- /**
213
- * Source of BOTH events and raw contract state — a full
214
- * `indexerPublicDataProvider` satisfies both halves.
215
- */
216
- readonly source: SignetEventSource & SignetPublicStateSource;
217
- /** Durable resume floor for the underlying observer (see {@link SignetEventObserverConfig.fromEventId}). */
218
- readonly fromEventId?: number;
219
- /** Poll cadence for {@link SignetRespondBidirectionalFeed.attestation}; default {@link DEFAULT_RESPONSE_FEED_POLL_INTERVAL_MS}. */
220
- readonly pollIntervalMs?: number;
221
- }
222
-
223
- /**
224
- * The event-driven respond-bidirectional feed: composes a
225
- * {@link SignetEventObserver} of {@link RespondBidirectionalEvent}
226
- * notifications (discovery) with a read of the signet contract's
227
- * respond-bidirectional index (the source of truth), yielding the MPC's
228
- * attestation of a request's remote execution. No verdicts and no off-chain
229
- * verification — the signet contract verified the attestation IN-CIRCUIT at
230
- * post time (Schnorr over `(requestId, hash(serializedOutput, outputLen))`
231
- * against its sealed MPC key), so the stored record is authentic by
232
- * construction and there is exactly one per request (first valid write wins).
233
- */
234
- export class SignetRespondBidirectionalFeed {
235
- private readonly observer: SignetEventObserver<RespondBidirectionalEvent>;
236
- private readonly signetContractAddress: string;
237
- private readonly source: SignetPublicStateSource;
238
- private readonly pollIntervalMs: number;
239
-
240
- // Attestation records never change once stored (one authenticated slot per
241
- // request, first valid write wins), so cache them across polls.
242
- private readonly attestations = new Map<RequestIdHex, RespondBidirectional>();
243
-
244
- /**
245
- * @param config - The signet contract, combined event/state source, and
246
- * resume floor.
247
- */
248
- constructor(config: SignetRespondBidirectionalFeedConfig) {
249
- this.observer = new SignetEventObserver({
250
- signetContractAddress: config.signetContractAddress,
251
- source: config.source,
252
- codec: respondBidirectionalEventCodec,
253
- fromEventId: config.fromEventId,
254
- pollIntervalMs: config.pollIntervalMs,
255
- });
256
- this.signetContractAddress = config.signetContractAddress;
257
- this.source = config.source;
258
- this.pollIntervalMs =
259
- config.pollIntervalMs ?? DEFAULT_RESPONSE_FEED_POLL_INTERVAL_MS;
260
- }
261
-
262
- /**
263
- * One-shot: the attestation for `requestId`, or `undefined` while none is
264
- * discoverable yet. The index is only read when a currently-visible event
265
- * announces `requestId`'s attestation — no event, no state query. An event
266
- * whose ledger write has not indexed yet is simply retried next cycle
267
- * (events are the trigger, the ledger is the source of truth).
268
- *
269
- * @param requestId - The request whose attestation to look for.
270
- * @returns The stored attestation record, or `undefined` when its event is
271
- * not yet visible or its write not yet indexed.
272
- * @throws Error when the signet contract has no state on-chain.
273
- */
274
- async poll(requestId: RequestIdHex): Promise<RespondBidirectional | undefined> {
275
- const cached = this.attestations.get(requestId);
276
- if (cached !== undefined) {
277
- return cached;
278
- }
279
- const events = await this.observer.currentEvents();
280
- if (!events.some((event) => event.requestId === requestId)) {
281
- return undefined;
282
- }
283
- const state = await this.source.queryContractState(
284
- this.signetContractAddress,
285
- );
286
- if (!state?.data) {
287
- throw new Error(
288
- `no state data found for signet contract '${this.signetContractAddress}' - is it deployed?`,
289
- );
290
- }
291
- const record = readSignetContractLedgerFromState(
292
- state.data,
293
- ).respondBidirectionalIndex.get(requestId);
294
- if (record === undefined) {
295
- return undefined;
296
- }
297
- this.attestations.set(requestId, record);
298
- return record;
299
- }
300
-
301
- /**
302
- * Live wait: poll + sleep until `requestId`'s attestation is discoverable,
303
- * and return it. Runs until `opts.signal` aborts — the caller owns the
304
- * give-up policy (see the cli's poll-respond-bidirectional command).
305
- *
306
- * @param requestId - The request whose attestation to wait for.
307
- * @param opts.signal - Abort to stop waiting.
308
- * @returns The stored attestation record, or `undefined` when aborted
309
- * before one was discovered.
310
- * @throws Error when the signet contract has no state on-chain.
311
- */
312
- async attestation(
313
- requestId: RequestIdHex,
314
- opts?: { signal?: AbortSignal },
315
- ): Promise<RespondBidirectional | undefined> {
316
- while (!opts?.signal?.aborted) {
317
- const record = await this.poll(requestId);
318
- if (record !== undefined) {
319
- return record;
320
- }
321
- await sleepUnlessAborted(this.pollIntervalMs, opts?.signal);
322
- }
323
- return undefined;
324
- }
325
- }