@bcts/frost-hubert 1.0.0-alpha.17
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/LICENSE +48 -0
- package/README.md +35 -0
- package/dist/bin/frost.cjs +109 -0
- package/dist/bin/frost.cjs.map +1 -0
- package/dist/bin/frost.d.cts +1 -0
- package/dist/bin/frost.d.mts +1 -0
- package/dist/bin/frost.mjs +109 -0
- package/dist/bin/frost.mjs.map +1 -0
- package/dist/chunk-CQwRTUmo.cjs +53 -0
- package/dist/chunk-D3JzZLW2.mjs +21 -0
- package/dist/cmd/index.cjs +45 -0
- package/dist/cmd/index.d.cts +4 -0
- package/dist/cmd/index.d.mts +4 -0
- package/dist/cmd/index.mjs +7 -0
- package/dist/cmd-C8pmNd28.mjs +4664 -0
- package/dist/cmd-C8pmNd28.mjs.map +1 -0
- package/dist/cmd-CxUgryx_.cjs +4803 -0
- package/dist/cmd-CxUgryx_.cjs.map +1 -0
- package/dist/dkg/index.cjs +7 -0
- package/dist/dkg/index.d.cts +2 -0
- package/dist/dkg/index.d.mts +2 -0
- package/dist/dkg/index.mjs +3 -0
- package/dist/dkg-D4RcblWl.cjs +364 -0
- package/dist/dkg-D4RcblWl.cjs.map +1 -0
- package/dist/dkg-DqGrAV81.mjs +334 -0
- package/dist/dkg-DqGrAV81.mjs.map +1 -0
- package/dist/frost/index.cjs +37 -0
- package/dist/frost/index.d.cts +207 -0
- package/dist/frost/index.d.cts.map +1 -0
- package/dist/frost/index.d.mts +207 -0
- package/dist/frost/index.d.mts.map +1 -0
- package/dist/frost/index.mjs +3 -0
- package/dist/frost-CMH1K0Cw.cjs +511 -0
- package/dist/frost-CMH1K0Cw.cjs.map +1 -0
- package/dist/frost-Csp0IOrd.mjs +326 -0
- package/dist/frost-Csp0IOrd.mjs.map +1 -0
- package/dist/index-BGVoWW5P.d.cts +172 -0
- package/dist/index-BGVoWW5P.d.cts.map +1 -0
- package/dist/index-BJeUYrdE.d.mts +396 -0
- package/dist/index-BJeUYrdE.d.mts.map +1 -0
- package/dist/index-ByMDUYKw.d.mts +1098 -0
- package/dist/index-ByMDUYKw.d.mts.map +1 -0
- package/dist/index-DejLkr_F.d.mts +172 -0
- package/dist/index-DejLkr_F.d.mts.map +1 -0
- package/dist/index-Dib1OE-e.d.cts +1098 -0
- package/dist/index-Dib1OE-e.d.cts.map +1 -0
- package/dist/index-DnvBKgec.d.cts +396 -0
- package/dist/index-DnvBKgec.d.cts.map +1 -0
- package/dist/index.cjs +85 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +24 -0
- package/dist/index.mjs.map +1 -0
- package/dist/registry/index.cjs +13 -0
- package/dist/registry/index.d.cts +2 -0
- package/dist/registry/index.d.mts +2 -0
- package/dist/registry/index.mjs +3 -0
- package/dist/registry-CBjRRqNv.mjs +144 -0
- package/dist/registry-CBjRRqNv.mjs.map +1 -0
- package/dist/registry-CWp2amuo.mjs +789 -0
- package/dist/registry-CWp2amuo.mjs.map +1 -0
- package/dist/registry-D5yh293y.cjs +857 -0
- package/dist/registry-D5yh293y.cjs.map +1 -0
- package/dist/registry-DNUNW6SH.cjs +163 -0
- package/dist/registry-DNUNW6SH.cjs.map +1 -0
- package/package.json +119 -0
- package/src/bin/frost.ts +218 -0
- package/src/cmd/busy.ts +64 -0
- package/src/cmd/check.ts +20 -0
- package/src/cmd/common.ts +40 -0
- package/src/cmd/dkg/common.ts +275 -0
- package/src/cmd/dkg/coordinator/finalize.ts +592 -0
- package/src/cmd/dkg/coordinator/index.ts +12 -0
- package/src/cmd/dkg/coordinator/invite.ts +217 -0
- package/src/cmd/dkg/coordinator/round1.ts +889 -0
- package/src/cmd/dkg/coordinator/round2.ts +959 -0
- package/src/cmd/dkg/index.ts +11 -0
- package/src/cmd/dkg/participant/finalize.ts +575 -0
- package/src/cmd/dkg/participant/index.ts +12 -0
- package/src/cmd/dkg/participant/receive.ts +348 -0
- package/src/cmd/dkg/participant/round1.ts +464 -0
- package/src/cmd/dkg/participant/round2.ts +627 -0
- package/src/cmd/index.ts +18 -0
- package/src/cmd/parallel.ts +334 -0
- package/src/cmd/registry/index.ts +88 -0
- package/src/cmd/registry/owner/index.ts +9 -0
- package/src/cmd/registry/owner/set.ts +70 -0
- package/src/cmd/registry/participant/add.ts +70 -0
- package/src/cmd/registry/participant/index.ts +9 -0
- package/src/cmd/sign/common.ts +108 -0
- package/src/cmd/sign/coordinator/index.ts +11 -0
- package/src/cmd/sign/coordinator/invite.ts +431 -0
- package/src/cmd/sign/coordinator/round1.ts +751 -0
- package/src/cmd/sign/coordinator/round2.ts +836 -0
- package/src/cmd/sign/index.ts +11 -0
- package/src/cmd/sign/participant/finalize.ts +823 -0
- package/src/cmd/sign/participant/index.ts +12 -0
- package/src/cmd/sign/participant/receive.ts +378 -0
- package/src/cmd/sign/participant/round1.ts +479 -0
- package/src/cmd/sign/participant/round2.ts +748 -0
- package/src/cmd/storage.ts +116 -0
- package/src/dkg/group-invite.ts +414 -0
- package/src/dkg/index.ts +10 -0
- package/src/dkg/proposed-participant.ts +132 -0
- package/src/frost/index.ts +456 -0
- package/src/index.ts +45 -0
- package/src/registry/group-record.ts +392 -0
- package/src/registry/index.ts +12 -0
- package/src/registry/owner-record.ts +146 -0
- package/src/registry/participant-record.ts +186 -0
- package/src/registry/registry-impl.ts +364 -0
|
@@ -0,0 +1,959 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DKG coordinator round 2 command.
|
|
3
|
+
*
|
|
4
|
+
* Port of cmd/dkg/coordinator/round2.rs from frost-hubert-rust.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
|
|
12
|
+
import { ARID, type PrivateKeys, XID } from "@bcts/components";
|
|
13
|
+
import { Envelope } from "@bcts/envelope";
|
|
14
|
+
import { SealedRequest, SealedResponse } from "@bcts/gstp";
|
|
15
|
+
import type { XIDDocument } from "@bcts/xid";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
type GroupRecord,
|
|
19
|
+
PendingRequests,
|
|
20
|
+
Registry,
|
|
21
|
+
resolveRegistryPath,
|
|
22
|
+
} from "../../../registry/index.js";
|
|
23
|
+
import { groupStateDir, isVerbose } from "../../common.js";
|
|
24
|
+
import {
|
|
25
|
+
type CollectionResult,
|
|
26
|
+
parallelFetch,
|
|
27
|
+
parallelSend,
|
|
28
|
+
type ParallelFetchConfig,
|
|
29
|
+
parallelFetchConfigWithTimeout,
|
|
30
|
+
} from "../../parallel.js";
|
|
31
|
+
import { type StorageClient } from "../../storage.js";
|
|
32
|
+
import { getWithIndicator, putWithIndicator } from "../../busy.js";
|
|
33
|
+
import { parseAridUr } from "../common.js";
|
|
34
|
+
|
|
35
|
+
// -----------------------------------------------------------------------------
|
|
36
|
+
// Types
|
|
37
|
+
// -----------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Options for the DKG round2 command.
|
|
41
|
+
*/
|
|
42
|
+
export interface DkgRound2Options {
|
|
43
|
+
registryPath?: string;
|
|
44
|
+
groupId: string;
|
|
45
|
+
parallel?: boolean;
|
|
46
|
+
timeoutSeconds?: number;
|
|
47
|
+
verbose?: boolean;
|
|
48
|
+
preview?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Result of the DKG round2 command.
|
|
53
|
+
*/
|
|
54
|
+
export interface DkgRound2Result {
|
|
55
|
+
accepted: number;
|
|
56
|
+
rejected: number;
|
|
57
|
+
errors: number;
|
|
58
|
+
timeouts: number;
|
|
59
|
+
displayPath?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Data extracted from a successful Round 2 response.
|
|
64
|
+
*
|
|
65
|
+
* Port of `struct Round2ResponseData` from round2.rs lines 601-604.
|
|
66
|
+
*/
|
|
67
|
+
export interface Round2ResponseData {
|
|
68
|
+
packages: [XID, unknown][];
|
|
69
|
+
nextResponseArid: ARID;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Round 2 collection result (for sequential path).
|
|
74
|
+
*
|
|
75
|
+
* Port of `struct Round2Collection` from round2.rs lines 206-213.
|
|
76
|
+
*/
|
|
77
|
+
interface Round2Collection {
|
|
78
|
+
packages: Map<string, [XID, unknown][]>; // Map<XID.urString(), [recipient XID, package][]>
|
|
79
|
+
nextResponseArids: [XID, ARID][];
|
|
80
|
+
displayPath: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Entry for a collected Round 2 response.
|
|
85
|
+
*
|
|
86
|
+
* Port of `struct CollectedRound2Entry` from round2.rs lines 359-362.
|
|
87
|
+
*/
|
|
88
|
+
interface CollectedRound2Entry {
|
|
89
|
+
packages: [XID, unknown][];
|
|
90
|
+
nextResponseArid: ARID;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// -----------------------------------------------------------------------------
|
|
94
|
+
// Validation
|
|
95
|
+
// -----------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Validate that the owner is the coordinator of the group.
|
|
99
|
+
*
|
|
100
|
+
* Port of coordinator check from round2.rs lines 86-94.
|
|
101
|
+
*/
|
|
102
|
+
function validateCoordinator(groupRecord: GroupRecord, ownerXid: XID): void {
|
|
103
|
+
if (groupRecord.coordinator().xid().urString() !== ownerXid.urString()) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Only the coordinator can collect Round 2 responses and send finalize packages. ` +
|
|
106
|
+
`Coordinator: ${groupRecord.coordinator().xid().urString()}, Owner: ${ownerXid.urString()}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// -----------------------------------------------------------------------------
|
|
112
|
+
// Response validation and extraction
|
|
113
|
+
// -----------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Validate envelope and extract Round 2 data (for parallel fetch).
|
|
117
|
+
*
|
|
118
|
+
* Port of `validate_and_extract_round2_response()` from round2.rs lines 646-708.
|
|
119
|
+
*/
|
|
120
|
+
export function validateAndExtractRound2Response(
|
|
121
|
+
envelope: Envelope,
|
|
122
|
+
coordinatorKeys: PrivateKeys,
|
|
123
|
+
expectedGroupId: ARID,
|
|
124
|
+
expectedSender: XID,
|
|
125
|
+
): Round2ResponseData | { rejected: string } {
|
|
126
|
+
const now = new Date();
|
|
127
|
+
|
|
128
|
+
let sealed: SealedResponse;
|
|
129
|
+
try {
|
|
130
|
+
sealed = SealedResponse.tryFromEncryptedEnvelope(envelope, undefined, now, coordinatorKeys);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
return {
|
|
133
|
+
rejected: `Failed to decrypt/parse response: ${err instanceof Error ? err.message : String(err)}`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Check for error response
|
|
138
|
+
if (!sealed.isOk()) {
|
|
139
|
+
try {
|
|
140
|
+
const error = sealed.error();
|
|
141
|
+
const reasonEnv = error.optionalObjectForPredicate("reason");
|
|
142
|
+
const reason = reasonEnv?.extractString() ?? "unknown reason";
|
|
143
|
+
return { rejected: `Participant reported error: ${reason}` };
|
|
144
|
+
} catch {
|
|
145
|
+
return { rejected: "Participant reported error: unknown reason" };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Get and validate result
|
|
150
|
+
let result: Envelope;
|
|
151
|
+
try {
|
|
152
|
+
result = sealed.result();
|
|
153
|
+
} catch {
|
|
154
|
+
return { rejected: "Response has no result envelope" };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Validate response type
|
|
158
|
+
try {
|
|
159
|
+
result.checkSubjectUnit();
|
|
160
|
+
result.checkType("dkgRound2Response");
|
|
161
|
+
} catch (err) {
|
|
162
|
+
return {
|
|
163
|
+
rejected: `Invalid response type: ${err instanceof Error ? err.message : String(err)}`,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Validate group ID
|
|
168
|
+
try {
|
|
169
|
+
const groupEnv = result.objectForPredicate("group");
|
|
170
|
+
const groupId = groupEnv.extractSubject<ARID>((cbor) => ARID.fromTaggedCbor(cbor));
|
|
171
|
+
if (groupId.urString() !== expectedGroupId.urString()) {
|
|
172
|
+
return {
|
|
173
|
+
rejected: `Response group ID ${groupId.urString()} does not match expected ${expectedGroupId.urString()}`,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
} catch (err) {
|
|
177
|
+
return {
|
|
178
|
+
rejected: `Failed to extract group: ${err instanceof Error ? err.message : String(err)}`,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Validate participant/sender
|
|
183
|
+
try {
|
|
184
|
+
const participantEnv = result.objectForPredicate("participant");
|
|
185
|
+
const senderXid = participantEnv.extractSubject<XID>((cbor) => XID.fromTaggedCbor(cbor));
|
|
186
|
+
if (senderXid.urString() !== expectedSender.urString()) {
|
|
187
|
+
return {
|
|
188
|
+
rejected: `Response participant ${senderXid.urString()} does not match expected ${expectedSender.urString()}`,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
} catch (err) {
|
|
192
|
+
return {
|
|
193
|
+
rejected: `Failed to extract participant: ${err instanceof Error ? err.message : String(err)}`,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Extract next response ARID
|
|
198
|
+
let nextResponseArid: ARID;
|
|
199
|
+
try {
|
|
200
|
+
const responseAridEnv = result.objectForPredicate("response_arid");
|
|
201
|
+
nextResponseArid = responseAridEnv.extractSubject<ARID>((cbor) => ARID.fromTaggedCbor(cbor));
|
|
202
|
+
} catch (err) {
|
|
203
|
+
return {
|
|
204
|
+
rejected: `Failed to extract response_arid: ${err instanceof Error ? err.message : String(err)}`,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Extract round2 packages
|
|
209
|
+
const packages: [XID, unknown][] = [];
|
|
210
|
+
try {
|
|
211
|
+
const pkgEnvelopes = result.objectsForPredicate("round2Package");
|
|
212
|
+
for (const pkgEnv of pkgEnvelopes) {
|
|
213
|
+
const recipientEnv = pkgEnv.objectForPredicate("recipient");
|
|
214
|
+
const recipient = recipientEnv.extractSubject<XID>((cbor) => XID.fromTaggedCbor(cbor));
|
|
215
|
+
const pkgJsonStr = pkgEnv.extractString();
|
|
216
|
+
const pkg: unknown = JSON.parse(pkgJsonStr);
|
|
217
|
+
packages.push([recipient, pkg]);
|
|
218
|
+
}
|
|
219
|
+
} catch (err) {
|
|
220
|
+
return {
|
|
221
|
+
rejected: `Failed to extract round2 packages: ${err instanceof Error ? err.message : String(err)}`,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { packages, nextResponseArid };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// -----------------------------------------------------------------------------
|
|
229
|
+
// Sequential collection
|
|
230
|
+
// -----------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Fetch a Round 2 response sequentially.
|
|
234
|
+
*
|
|
235
|
+
* Port of `fetch_round2_response()` from round2.rs lines 364-442.
|
|
236
|
+
*/
|
|
237
|
+
async function fetchRound2Response(
|
|
238
|
+
client: StorageClient,
|
|
239
|
+
arid: ARID,
|
|
240
|
+
timeout: number | undefined,
|
|
241
|
+
coordinatorKeys: PrivateKeys,
|
|
242
|
+
expectedGroup: ARID,
|
|
243
|
+
expectedSender: XID,
|
|
244
|
+
): Promise<CollectedRound2Entry> {
|
|
245
|
+
const envelope = await getWithIndicator(
|
|
246
|
+
client,
|
|
247
|
+
arid,
|
|
248
|
+
expectedSender.urString(),
|
|
249
|
+
timeout,
|
|
250
|
+
isVerbose(),
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
if (envelope === null || envelope === undefined) {
|
|
254
|
+
throw new Error("Response not found in Hubert storage");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const result = validateAndExtractRound2Response(
|
|
258
|
+
envelope,
|
|
259
|
+
coordinatorKeys,
|
|
260
|
+
expectedGroup,
|
|
261
|
+
expectedSender,
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
if ("rejected" in result) {
|
|
265
|
+
throw new Error(result.rejected);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
packages: result.packages,
|
|
270
|
+
nextResponseArid: result.nextResponseArid,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Collect Round 2 responses sequentially.
|
|
276
|
+
*
|
|
277
|
+
* Port of `collect_round2()` from round2.rs lines 216-357.
|
|
278
|
+
*/
|
|
279
|
+
async function collectRound2(
|
|
280
|
+
client: StorageClient,
|
|
281
|
+
registryPath: string,
|
|
282
|
+
registry: Registry,
|
|
283
|
+
coordinatorKeys: PrivateKeys,
|
|
284
|
+
groupId: ARID,
|
|
285
|
+
pendingRequests: PendingRequests,
|
|
286
|
+
timeout: number | undefined,
|
|
287
|
+
): Promise<Round2Collection> {
|
|
288
|
+
if (isVerbose()) {
|
|
289
|
+
console.error(`Collecting Round 2 responses from ${pendingRequests.len()} participants...`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const allPackages = new Map<string, [XID, unknown][]>();
|
|
293
|
+
const nextResponseArids: [XID, ARID][] = [];
|
|
294
|
+
const errors: [XID, string][] = [];
|
|
295
|
+
|
|
296
|
+
for (const [participantXid, collectFromArid] of pendingRequests.iterCollect()) {
|
|
297
|
+
const participantRecord = registry.participant(participantXid);
|
|
298
|
+
const participantName = participantRecord?.petName() ?? participantXid.urString();
|
|
299
|
+
|
|
300
|
+
if (isVerbose()) {
|
|
301
|
+
console.error(`${participantName}...`);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
const collected = await fetchRound2Response(
|
|
306
|
+
client,
|
|
307
|
+
collectFromArid,
|
|
308
|
+
timeout,
|
|
309
|
+
coordinatorKeys,
|
|
310
|
+
groupId,
|
|
311
|
+
participantXid,
|
|
312
|
+
);
|
|
313
|
+
allPackages.set(participantXid.urString(), collected.packages);
|
|
314
|
+
nextResponseArids.push([participantXid, collected.nextResponseArid]);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
if (isVerbose()) {
|
|
317
|
+
console.error(`error: ${err instanceof Error ? err.message : String(err)}`);
|
|
318
|
+
}
|
|
319
|
+
errors.push([participantXid, err instanceof Error ? err.message : String(err)]);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (errors.length > 0) {
|
|
324
|
+
if (isVerbose()) {
|
|
325
|
+
console.error();
|
|
326
|
+
console.error(`Failed to collect from ${errors.length} participants:`);
|
|
327
|
+
}
|
|
328
|
+
for (const [xid, error] of errors) {
|
|
329
|
+
console.error(` ${xid.urString()}: ${error}`);
|
|
330
|
+
}
|
|
331
|
+
throw new Error(
|
|
332
|
+
`Round 2 collection incomplete: ${errors.length} of ${pendingRequests.len()} responses failed`,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Persist collected round2 packages
|
|
337
|
+
const displayPath = persistRound2PackagesFromMap(
|
|
338
|
+
registryPath,
|
|
339
|
+
groupId,
|
|
340
|
+
allPackages,
|
|
341
|
+
nextResponseArids,
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// Update pending requests
|
|
345
|
+
const newPending = new PendingRequests();
|
|
346
|
+
for (const [xid, sendToArid] of nextResponseArids) {
|
|
347
|
+
newPending.addSendOnly(xid, sendToArid);
|
|
348
|
+
}
|
|
349
|
+
const groupRecord = registry.group(groupId);
|
|
350
|
+
if (groupRecord === undefined) {
|
|
351
|
+
throw new Error("Group not found in registry");
|
|
352
|
+
}
|
|
353
|
+
groupRecord.setPendingRequests(newPending);
|
|
354
|
+
registry.save(registryPath);
|
|
355
|
+
|
|
356
|
+
return {
|
|
357
|
+
packages: allPackages,
|
|
358
|
+
nextResponseArids,
|
|
359
|
+
displayPath,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Persist Round 2 packages from a Map (sequential collection).
|
|
365
|
+
*/
|
|
366
|
+
function persistRound2PackagesFromMap(
|
|
367
|
+
registryPath: string,
|
|
368
|
+
groupId: ARID,
|
|
369
|
+
allPackages: Map<string, [XID, unknown][]>,
|
|
370
|
+
nextResponseArids: [XID, ARID][],
|
|
371
|
+
): string {
|
|
372
|
+
const stateDir = groupStateDir(registryPath, groupId.hex());
|
|
373
|
+
fs.mkdirSync(stateDir, { recursive: true });
|
|
374
|
+
|
|
375
|
+
const collectedPath = path.join(stateDir, "collected_round2.json");
|
|
376
|
+
const root: Record<string, unknown> = {};
|
|
377
|
+
|
|
378
|
+
for (const [senderUrString, packages] of allPackages) {
|
|
379
|
+
const senderMap: Record<string, unknown> = {};
|
|
380
|
+
|
|
381
|
+
// Find response_arid for this sender
|
|
382
|
+
const responseArid = nextResponseArids.find(([xid]) => xid.urString() === senderUrString)?.[1];
|
|
383
|
+
|
|
384
|
+
if (responseArid !== undefined) {
|
|
385
|
+
senderMap["response_arid"] = responseArid.urString();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const packagesJson: Record<string, unknown> = {};
|
|
389
|
+
for (const [recipient, pkg] of packages) {
|
|
390
|
+
packagesJson[recipient.urString()] = pkg;
|
|
391
|
+
}
|
|
392
|
+
senderMap["packages"] = packagesJson;
|
|
393
|
+
|
|
394
|
+
root[senderUrString] = senderMap;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
fs.writeFileSync(collectedPath, JSON.stringify(root, null, 2));
|
|
398
|
+
|
|
399
|
+
// Return relative path if possible
|
|
400
|
+
const cwd = process.cwd();
|
|
401
|
+
if (collectedPath.startsWith(cwd)) {
|
|
402
|
+
return collectedPath.slice(cwd.length + 1);
|
|
403
|
+
}
|
|
404
|
+
return collectedPath;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// -----------------------------------------------------------------------------
|
|
408
|
+
// Parallel collection
|
|
409
|
+
// -----------------------------------------------------------------------------
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Collect Round 2 responses in parallel with progress display.
|
|
413
|
+
*
|
|
414
|
+
* Port of `collect_round2_parallel()` from round2.rs lines 607-643.
|
|
415
|
+
*/
|
|
416
|
+
export async function collectRound2Parallel(
|
|
417
|
+
client: StorageClient,
|
|
418
|
+
registry: Registry,
|
|
419
|
+
pendingRequests: PendingRequests,
|
|
420
|
+
coordinatorKeys: PrivateKeys,
|
|
421
|
+
expectedGroupId: ARID,
|
|
422
|
+
timeout: number | undefined,
|
|
423
|
+
): Promise<CollectionResult<Round2ResponseData>> {
|
|
424
|
+
const requests: [XID, ARID, string][] = [];
|
|
425
|
+
|
|
426
|
+
for (const [xid, arid] of pendingRequests.iterCollect()) {
|
|
427
|
+
const record = registry.participant(xid);
|
|
428
|
+
const name = record?.petName() ?? xid.urString();
|
|
429
|
+
requests.push([xid, arid, name]);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const config: ParallelFetchConfig = parallelFetchConfigWithTimeout(timeout);
|
|
433
|
+
|
|
434
|
+
return parallelFetch(
|
|
435
|
+
client,
|
|
436
|
+
requests,
|
|
437
|
+
(envelope: Envelope, xid: XID) =>
|
|
438
|
+
validateAndExtractRound2Response(envelope, coordinatorKeys, expectedGroupId, xid),
|
|
439
|
+
config,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// -----------------------------------------------------------------------------
|
|
444
|
+
// Persist Round 2 packages (parallel)
|
|
445
|
+
// -----------------------------------------------------------------------------
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Persist Round 2 packages from parallel collection results.
|
|
449
|
+
*
|
|
450
|
+
* Port of `persist_round2_packages()` from round2.rs lines 712-758.
|
|
451
|
+
*/
|
|
452
|
+
export function persistRound2Packages(
|
|
453
|
+
registryPath: string,
|
|
454
|
+
groupId: ARID,
|
|
455
|
+
successes: [XID, Round2ResponseData][],
|
|
456
|
+
): string {
|
|
457
|
+
const stateDir = groupStateDir(registryPath, groupId.hex());
|
|
458
|
+
fs.mkdirSync(stateDir, { recursive: true });
|
|
459
|
+
|
|
460
|
+
const collectedPath = path.join(stateDir, "collected_round2.json");
|
|
461
|
+
const root: Record<string, unknown> = {};
|
|
462
|
+
|
|
463
|
+
for (const [sender, data] of successes) {
|
|
464
|
+
const senderMap: Record<string, unknown> = {};
|
|
465
|
+
senderMap["response_arid"] = data.nextResponseArid.urString();
|
|
466
|
+
|
|
467
|
+
const packagesJson: Record<string, unknown> = {};
|
|
468
|
+
for (const [recipient, pkg] of data.packages) {
|
|
469
|
+
packagesJson[recipient.urString()] = pkg;
|
|
470
|
+
}
|
|
471
|
+
senderMap["packages"] = packagesJson;
|
|
472
|
+
|
|
473
|
+
root[sender.urString()] = senderMap;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
fs.writeFileSync(collectedPath, JSON.stringify(root, null, 2));
|
|
477
|
+
|
|
478
|
+
// Return relative path if possible
|
|
479
|
+
const cwd = process.cwd();
|
|
480
|
+
if (collectedPath.startsWith(cwd)) {
|
|
481
|
+
return collectedPath.slice(cwd.length + 1);
|
|
482
|
+
}
|
|
483
|
+
return collectedPath;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// -----------------------------------------------------------------------------
|
|
487
|
+
// Update pending requests (parallel)
|
|
488
|
+
// -----------------------------------------------------------------------------
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Update pending requests from parallel collection results.
|
|
492
|
+
*
|
|
493
|
+
* Port of `update_pending_for_finalize_from_collection()` from round2.rs lines 761-777.
|
|
494
|
+
*/
|
|
495
|
+
export function updatePendingForFinalizeFromCollection(
|
|
496
|
+
registry: Registry,
|
|
497
|
+
registryPath: string,
|
|
498
|
+
groupId: ARID,
|
|
499
|
+
successes: [XID, Round2ResponseData][],
|
|
500
|
+
): void {
|
|
501
|
+
const newPending = new PendingRequests();
|
|
502
|
+
for (const [xid, data] of successes) {
|
|
503
|
+
newPending.addSendOnly(xid, data.nextResponseArid);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const groupRecord = registry.group(groupId);
|
|
507
|
+
if (groupRecord === undefined) {
|
|
508
|
+
throw new Error("Group not found in registry");
|
|
509
|
+
}
|
|
510
|
+
groupRecord.setPendingRequests(newPending);
|
|
511
|
+
registry.save(registryPath);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// -----------------------------------------------------------------------------
|
|
515
|
+
// Finalize request building
|
|
516
|
+
// -----------------------------------------------------------------------------
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Gather packages FOR a specific recipient (from all other senders).
|
|
520
|
+
*
|
|
521
|
+
* Port of `gather_packages_for_recipient()` from round2.rs lines 552-571.
|
|
522
|
+
*/
|
|
523
|
+
function gatherPackagesForRecipient(
|
|
524
|
+
recipient: XID,
|
|
525
|
+
allPackages: Map<string, [XID, unknown][]>,
|
|
526
|
+
): [XID, unknown][] {
|
|
527
|
+
const result: [XID, unknown][] = [];
|
|
528
|
+
|
|
529
|
+
for (const [senderUrString, packages] of allPackages) {
|
|
530
|
+
for (const [rcpt, pkg] of packages) {
|
|
531
|
+
if (rcpt.urString() === recipient.urString()) {
|
|
532
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-require-imports, no-undef
|
|
533
|
+
const { XID: XIDClass } = require("@bcts/components");
|
|
534
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
535
|
+
const sender = XIDClass.fromURString(senderUrString) as XID;
|
|
536
|
+
result.push([sender, pkg]);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (result.length === 0) {
|
|
542
|
+
throw new Error(`No round2 packages found for recipient ${recipient.urString()}`);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return result;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Build a finalize request for a participant.
|
|
550
|
+
*
|
|
551
|
+
* Port of `build_finalize_request_for_participant()` from round2.rs lines 575-594.
|
|
552
|
+
*/
|
|
553
|
+
export function buildFinalizeRequestForParticipant(
|
|
554
|
+
sender: XIDDocument,
|
|
555
|
+
groupId: ARID,
|
|
556
|
+
responseArid: ARID,
|
|
557
|
+
packages: [XID, unknown][],
|
|
558
|
+
): SealedRequest {
|
|
559
|
+
let request = SealedRequest.new("dkgFinalize", ARID.new(), sender)
|
|
560
|
+
.withParameter("group", groupId)
|
|
561
|
+
.withParameter("responseArid", responseArid);
|
|
562
|
+
|
|
563
|
+
for (const [pkgSender, pkg] of packages) {
|
|
564
|
+
const encoded = JSON.stringify(pkg);
|
|
565
|
+
const pkgEnvelope = Envelope.new(encoded).addAssertion("sender", pkgSender);
|
|
566
|
+
request = request.withParameter("round2Package", pkgEnvelope);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return request;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// -----------------------------------------------------------------------------
|
|
573
|
+
// Send finalize requests (sequential)
|
|
574
|
+
// -----------------------------------------------------------------------------
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Send finalize requests sequentially.
|
|
578
|
+
*
|
|
579
|
+
* Port of `send_finalize_requests()` from round2.rs lines 444-550.
|
|
580
|
+
*/
|
|
581
|
+
async function sendFinalizeRequests(
|
|
582
|
+
client: StorageClient,
|
|
583
|
+
registryPath: string,
|
|
584
|
+
registry: Registry,
|
|
585
|
+
coordinator: XIDDocument,
|
|
586
|
+
groupId: ARID,
|
|
587
|
+
collection: Round2Collection,
|
|
588
|
+
preview: boolean,
|
|
589
|
+
): Promise<[string, string] | undefined> {
|
|
590
|
+
const signerPrivateKeys = coordinator.inceptionPrivateKeys();
|
|
591
|
+
if (signerPrivateKeys === undefined) {
|
|
592
|
+
throw new Error("Coordinator XID document has no signing keys");
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const validUntil = new Date(Date.now() + 60 * 60 * 1000); // 1 hour
|
|
596
|
+
|
|
597
|
+
// Build participant info: [XID, XIDDocument, sendToArid, collectFromArid]
|
|
598
|
+
const participantInfo: [XID, XIDDocument, ARID, ARID][] = [];
|
|
599
|
+
|
|
600
|
+
for (const [xid, sendToArid] of collection.nextResponseArids) {
|
|
601
|
+
const record = registry.participant(xid);
|
|
602
|
+
if (record === undefined) {
|
|
603
|
+
throw new Error(`Participant ${xid.urString()} not found in registry`);
|
|
604
|
+
}
|
|
605
|
+
const collectFromArid = ARID.new();
|
|
606
|
+
participantInfo.push([xid, record.xidDocument(), sendToArid, collectFromArid]);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if (isVerbose()) {
|
|
610
|
+
console.error(`Sending finalize packages to ${participantInfo.length} participants...`);
|
|
611
|
+
} else {
|
|
612
|
+
console.error(); // Blank line to separate get phase from put phase
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
let previewOutput: [string, string] | undefined;
|
|
616
|
+
|
|
617
|
+
for (const [xid, recipientDoc, sendToArid, collectFromArid] of participantInfo) {
|
|
618
|
+
const record = registry.participant(xid);
|
|
619
|
+
const participantName = record?.petName() ?? xid.urString();
|
|
620
|
+
|
|
621
|
+
if (isVerbose()) {
|
|
622
|
+
console.error(`${participantName}...`);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// Gather packages FOR this recipient
|
|
626
|
+
const packagesForRecipient = gatherPackagesForRecipient(xid, collection.packages);
|
|
627
|
+
|
|
628
|
+
const request = buildFinalizeRequestForParticipant(
|
|
629
|
+
coordinator,
|
|
630
|
+
groupId,
|
|
631
|
+
collectFromArid,
|
|
632
|
+
packagesForRecipient,
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
if (preview && previewOutput === undefined) {
|
|
636
|
+
const unsealedEnvelope = request.toEnvelope(validUntil, signerPrivateKeys, undefined);
|
|
637
|
+
previewOutput = [participantName, unsealedEnvelope.urString()];
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const sealedEnvelope = request.toEnvelopeForRecipients(validUntil, signerPrivateKeys, [
|
|
641
|
+
recipientDoc,
|
|
642
|
+
]);
|
|
643
|
+
|
|
644
|
+
await putWithIndicator(client, sendToArid, sealedEnvelope, participantName, isVerbose());
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Build pending requests for finalize response collection
|
|
648
|
+
const newPendingRequests = new PendingRequests();
|
|
649
|
+
for (const [xid, , , collectFromArid] of participantInfo) {
|
|
650
|
+
newPendingRequests.addCollectOnly(xid, collectFromArid);
|
|
651
|
+
}
|
|
652
|
+
const groupRecord = registry.group(groupId);
|
|
653
|
+
if (groupRecord === undefined) {
|
|
654
|
+
throw new Error("Group not found in registry");
|
|
655
|
+
}
|
|
656
|
+
groupRecord.setPendingRequests(newPendingRequests);
|
|
657
|
+
registry.save(registryPath);
|
|
658
|
+
|
|
659
|
+
return previewOutput;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// -----------------------------------------------------------------------------
|
|
663
|
+
// Dispatch finalize requests (parallel)
|
|
664
|
+
// -----------------------------------------------------------------------------
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Dispatch finalize requests in parallel.
|
|
668
|
+
*
|
|
669
|
+
* Port of `dispatch_finalize_requests_parallel()` from round2.rs lines 780-900.
|
|
670
|
+
*/
|
|
671
|
+
export async function dispatchFinalizeRequestsParallel(
|
|
672
|
+
client: StorageClient,
|
|
673
|
+
registry: Registry,
|
|
674
|
+
registryPath: string,
|
|
675
|
+
coordinator: XIDDocument,
|
|
676
|
+
groupId: ARID,
|
|
677
|
+
successes: [XID, Round2ResponseData][],
|
|
678
|
+
preview: boolean,
|
|
679
|
+
): Promise<[string, string] | undefined> {
|
|
680
|
+
const signerPrivateKeys = coordinator.inceptionPrivateKeys();
|
|
681
|
+
if (signerPrivateKeys === undefined) {
|
|
682
|
+
throw new Error("Coordinator XID document has no signing keys");
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const validUntil = new Date(Date.now() + 60 * 60 * 1000); // 1 hour
|
|
686
|
+
|
|
687
|
+
// Build all_packages map for gatherPackagesForRecipient
|
|
688
|
+
const allPackages = new Map<string, [XID, unknown][]>();
|
|
689
|
+
for (const [xid, data] of successes) {
|
|
690
|
+
allPackages.set(xid.urString(), data.packages);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Build messages
|
|
694
|
+
const messages: [XID, ARID, Envelope, string][] = [];
|
|
695
|
+
const collectArids: [XID, ARID][] = [];
|
|
696
|
+
let previewOutput: [string, string] | undefined;
|
|
697
|
+
|
|
698
|
+
for (const [xid, data] of successes) {
|
|
699
|
+
const record = registry.participant(xid);
|
|
700
|
+
if (record === undefined) {
|
|
701
|
+
throw new Error(`Participant ${xid.urString()} not found in registry`);
|
|
702
|
+
}
|
|
703
|
+
const recipientDoc = record.xidDocument();
|
|
704
|
+
const participantName = record.petName() ?? xid.urString();
|
|
705
|
+
|
|
706
|
+
const collectFromArid = ARID.new();
|
|
707
|
+
collectArids.push([xid, collectFromArid]);
|
|
708
|
+
|
|
709
|
+
const packagesForRecipient = gatherPackagesForRecipient(xid, allPackages);
|
|
710
|
+
|
|
711
|
+
const request = buildFinalizeRequestForParticipant(
|
|
712
|
+
coordinator,
|
|
713
|
+
groupId,
|
|
714
|
+
collectFromArid,
|
|
715
|
+
packagesForRecipient,
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
if (preview && previewOutput === undefined) {
|
|
719
|
+
const unsealedEnvelope = request.toEnvelope(validUntil, signerPrivateKeys, undefined);
|
|
720
|
+
previewOutput = [participantName, unsealedEnvelope.urString()];
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const sealedEnvelope = request.toEnvelopeForRecipients(validUntil, signerPrivateKeys, [
|
|
724
|
+
recipientDoc,
|
|
725
|
+
]);
|
|
726
|
+
|
|
727
|
+
messages.push([xid, data.nextResponseArid, sealedEnvelope, participantName]);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// Blank line to separate get phase from put phase
|
|
731
|
+
console.error();
|
|
732
|
+
|
|
733
|
+
// Send all messages in parallel
|
|
734
|
+
const sendResults = await parallelSend(client, messages, isVerbose());
|
|
735
|
+
|
|
736
|
+
// Check for send failures
|
|
737
|
+
const failures = sendResults.filter(([, err]) => err !== null);
|
|
738
|
+
|
|
739
|
+
if (failures.length > 0) {
|
|
740
|
+
for (const [xid, error] of failures) {
|
|
741
|
+
if (error !== null) {
|
|
742
|
+
console.error(`Failed to send to ${xid.urString()}: ${error.message}`);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
throw new Error(`Failed to send finalize requests to ${failures.length} participants`);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Update pending requests for finalize response collection
|
|
749
|
+
const newPendingRequests = new PendingRequests();
|
|
750
|
+
for (const [xid, collectFromArid] of collectArids) {
|
|
751
|
+
newPendingRequests.addCollectOnly(xid, collectFromArid);
|
|
752
|
+
}
|
|
753
|
+
const groupRecord = registry.group(groupId);
|
|
754
|
+
if (groupRecord === undefined) {
|
|
755
|
+
throw new Error("Group not found in registry");
|
|
756
|
+
}
|
|
757
|
+
groupRecord.setPendingRequests(newPendingRequests);
|
|
758
|
+
registry.save(registryPath);
|
|
759
|
+
|
|
760
|
+
return previewOutput;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// -----------------------------------------------------------------------------
|
|
764
|
+
// Summary printing
|
|
765
|
+
// -----------------------------------------------------------------------------
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Print summary for parallel collection.
|
|
769
|
+
*
|
|
770
|
+
* Port of `print_summary_parallel()` from round2.rs lines 903-964.
|
|
771
|
+
*/
|
|
772
|
+
function printSummaryParallel(
|
|
773
|
+
collection: CollectionResult<Round2ResponseData>,
|
|
774
|
+
displayPath: string,
|
|
775
|
+
preview: [string, string] | undefined,
|
|
776
|
+
): void {
|
|
777
|
+
// Report any failures
|
|
778
|
+
if (collection.rejections.length > 0) {
|
|
779
|
+
console.error();
|
|
780
|
+
console.error("Rejections:");
|
|
781
|
+
for (const [xid, reason] of collection.rejections) {
|
|
782
|
+
console.error(` ${xid.urString()}: ${reason}`);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (collection.errors.length > 0) {
|
|
786
|
+
console.error();
|
|
787
|
+
console.error("Errors:");
|
|
788
|
+
for (const [xid, error] of collection.errors) {
|
|
789
|
+
console.error(` ${xid.urString()}: ${error}`);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (collection.timeouts.length > 0) {
|
|
793
|
+
console.error();
|
|
794
|
+
console.error("Timeouts:");
|
|
795
|
+
for (const xid of collection.timeouts) {
|
|
796
|
+
console.error(` ${xid.urString()}`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
if (!collection.allSucceeded()) {
|
|
801
|
+
console.error();
|
|
802
|
+
console.error(
|
|
803
|
+
`Round 2 collection incomplete: ${collection.successes.length} succeeded, ` +
|
|
804
|
+
`${collection.rejections.length} rejected, ${collection.errors.length} errors, ` +
|
|
805
|
+
`${collection.timeouts.length} timeouts`,
|
|
806
|
+
);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
if (preview !== undefined) {
|
|
811
|
+
const [participantName, ur] = preview;
|
|
812
|
+
if (isVerbose()) {
|
|
813
|
+
console.error(`# Finalize preview for ${participantName}`);
|
|
814
|
+
console.error();
|
|
815
|
+
}
|
|
816
|
+
console.error(
|
|
817
|
+
`Collected ${collection.successes.length} Round 2 responses to ${displayPath} and sent ${collection.successes.length} finalize requests.`,
|
|
818
|
+
);
|
|
819
|
+
console.log(ur);
|
|
820
|
+
} else if (isVerbose()) {
|
|
821
|
+
console.error();
|
|
822
|
+
console.error(
|
|
823
|
+
`Collected ${collection.successes.length} Round 2 responses to ${displayPath} and sent ${collection.successes.length} finalize requests.`,
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// -----------------------------------------------------------------------------
|
|
829
|
+
// Main entry point
|
|
830
|
+
// -----------------------------------------------------------------------------
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* Execute the DKG coordinator round 2 command.
|
|
834
|
+
*
|
|
835
|
+
* Collects Round 2 responses and sends finalize packages.
|
|
836
|
+
*
|
|
837
|
+
* Port of `CommandArgs::exec()` from cmd/dkg/coordinator/round2.rs lines 59-203.
|
|
838
|
+
*/
|
|
839
|
+
export async function round2(
|
|
840
|
+
client: StorageClient,
|
|
841
|
+
options: DkgRound2Options,
|
|
842
|
+
cwd: string,
|
|
843
|
+
): Promise<DkgRound2Result> {
|
|
844
|
+
const registryPath = resolveRegistryPath(options.registryPath, cwd);
|
|
845
|
+
const registry = Registry.load(registryPath);
|
|
846
|
+
|
|
847
|
+
const owner = registry.owner();
|
|
848
|
+
if (owner === undefined) {
|
|
849
|
+
throw new Error("Registry owner is required");
|
|
850
|
+
}
|
|
851
|
+
const ownerDoc = owner.xidDocument();
|
|
852
|
+
|
|
853
|
+
const groupId = parseAridUr(options.groupId);
|
|
854
|
+
const groupRecord = registry.group(groupId);
|
|
855
|
+
|
|
856
|
+
if (groupRecord === undefined) {
|
|
857
|
+
throw new Error(`Group ${options.groupId} not found in registry`);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// Verify we are the coordinator
|
|
861
|
+
validateCoordinator(groupRecord, owner.xid());
|
|
862
|
+
|
|
863
|
+
const pendingRequests = groupRecord.pendingRequests();
|
|
864
|
+
if (pendingRequests.isEmpty()) {
|
|
865
|
+
throw new Error(
|
|
866
|
+
"No pending requests for this group. Did you run 'frost dkg coordinator round1'?",
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const coordinatorKeys = ownerDoc.inceptionPrivateKeys();
|
|
871
|
+
if (coordinatorKeys === undefined) {
|
|
872
|
+
throw new Error("Coordinator XID document has no private keys");
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (options.parallel === true) {
|
|
876
|
+
// Parallel path with progress display
|
|
877
|
+
const collection = await collectRound2Parallel(
|
|
878
|
+
client,
|
|
879
|
+
registry,
|
|
880
|
+
pendingRequests,
|
|
881
|
+
coordinatorKeys,
|
|
882
|
+
groupId,
|
|
883
|
+
options.timeoutSeconds,
|
|
884
|
+
);
|
|
885
|
+
|
|
886
|
+
// Persist collected data
|
|
887
|
+
const displayPath = persistRound2Packages(registryPath, groupId, collection.successes);
|
|
888
|
+
|
|
889
|
+
updatePendingForFinalizeFromCollection(registry, registryPath, groupId, collection.successes);
|
|
890
|
+
|
|
891
|
+
const preview = await dispatchFinalizeRequestsParallel(
|
|
892
|
+
client,
|
|
893
|
+
registry,
|
|
894
|
+
registryPath,
|
|
895
|
+
ownerDoc,
|
|
896
|
+
groupId,
|
|
897
|
+
collection.successes,
|
|
898
|
+
options.preview ?? false,
|
|
899
|
+
);
|
|
900
|
+
|
|
901
|
+
printSummaryParallel(collection, displayPath, preview);
|
|
902
|
+
|
|
903
|
+
return {
|
|
904
|
+
accepted: collection.successes.length,
|
|
905
|
+
rejected: collection.rejections.length,
|
|
906
|
+
errors: collection.errors.length,
|
|
907
|
+
timeouts: collection.timeouts.length,
|
|
908
|
+
displayPath,
|
|
909
|
+
};
|
|
910
|
+
} else {
|
|
911
|
+
// Sequential path (original behavior)
|
|
912
|
+
// Phase 1: Collect Round 2 responses
|
|
913
|
+
const collection = await collectRound2(
|
|
914
|
+
client,
|
|
915
|
+
registryPath,
|
|
916
|
+
registry,
|
|
917
|
+
coordinatorKeys,
|
|
918
|
+
groupId,
|
|
919
|
+
pendingRequests,
|
|
920
|
+
options.timeoutSeconds,
|
|
921
|
+
);
|
|
922
|
+
|
|
923
|
+
// Phase 2: Send finalize packages
|
|
924
|
+
const preview = await sendFinalizeRequests(
|
|
925
|
+
client,
|
|
926
|
+
registryPath,
|
|
927
|
+
registry,
|
|
928
|
+
ownerDoc,
|
|
929
|
+
groupId,
|
|
930
|
+
collection,
|
|
931
|
+
options.preview ?? false,
|
|
932
|
+
);
|
|
933
|
+
|
|
934
|
+
if (preview !== undefined) {
|
|
935
|
+
const [participantName, ur] = preview;
|
|
936
|
+
if (isVerbose()) {
|
|
937
|
+
console.error(`# Finalize preview for ${participantName}`);
|
|
938
|
+
console.error();
|
|
939
|
+
}
|
|
940
|
+
console.error(
|
|
941
|
+
`Collected ${collection.packages.size} Round 2 responses to ${collection.displayPath} and sent ${collection.nextResponseArids.length} finalize requests.`,
|
|
942
|
+
);
|
|
943
|
+
console.log(ur);
|
|
944
|
+
} else if (isVerbose()) {
|
|
945
|
+
console.error();
|
|
946
|
+
console.error(
|
|
947
|
+
`Collected ${collection.packages.size} Round 2 responses to ${collection.displayPath} and sent ${collection.nextResponseArids.length} finalize requests.`,
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
return {
|
|
952
|
+
accepted: collection.packages.size,
|
|
953
|
+
rejected: 0,
|
|
954
|
+
errors: 0,
|
|
955
|
+
timeouts: 0,
|
|
956
|
+
displayPath: collection.displayPath,
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
}
|