@devtion/actions 0.0.0-4f2b2be → 0.0.0-521e678

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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @module @devtion/actions
3
- * @version 1.0.6
2
+ * @module @p0tion/actions
3
+ * @version 1.1.1
4
4
  * @file A set of actions and helpers for CLI commands
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -15,10 +15,8 @@ import { onSnapshot, query, collection, getDocs, doc, getDoc, where, Timestamp,
15
15
  import { zKey, groth16 } from 'snarkjs';
16
16
  import crypto from 'crypto';
17
17
  import blake from 'blakejs';
18
- import { utils } from 'ffjavascript';
19
18
  import winston from 'winston';
20
- import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
21
- import { pipeline, Readable } from 'stream';
19
+ import { pipeline } from 'stream';
22
20
  import { promisify } from 'util';
23
21
  import { initializeApp } from 'firebase/app';
24
22
  import { signInWithCredential, initializeAuth, getAuth } from 'firebase/auth';
@@ -340,7 +338,7 @@ const commonTerms = {
340
338
  finalizeCircuit: "finalizeCircuit",
341
339
  finalizeCeremony: "finalizeCeremony",
342
340
  downloadCircuitArtifacts: "downloadCircuitArtifacts",
343
- transferObject: "transferObject",
341
+ transferObject: "transferObject"
344
342
  }
345
343
  };
346
344
 
@@ -691,11 +689,15 @@ const getChunksAndPreSignedUrls = async (cloudFunctions, bucketName, objectKey,
691
689
  * @param cloudFunctions <Functions> - the Firebase Cloud Functions service instance.
692
690
  * @param ceremonyId <string> - the unique identifier of the ceremony.
693
691
  * @param alreadyUploadedChunks Array<ETagWithPartNumber> - the temporary information about the already uploaded chunks.
692
+ * @param logger <GenericBar> - an optional logger to show progress.
694
693
  * @returns <Promise<Array<ETagWithPartNumber>>> - the completed (uploaded) chunks information.
695
694
  */
696
- const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks) => {
695
+ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks, logger) => {
697
696
  // Keep track of uploaded chunks.
698
697
  const uploadedChunks = alreadyUploadedChunks || [];
698
+ // if we were passed a logger, start it
699
+ if (logger)
700
+ logger.start(chunksWithUrls.length, 0);
699
701
  // Loop through remaining chunks.
700
702
  for (let i = alreadyUploadedChunks ? alreadyUploadedChunks.length : 0; i < chunksWithUrls.length; i += 1) {
701
703
  // Consume the pre-signed url to upload the chunk.
@@ -727,6 +729,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
727
729
  // nb. this must be done only when contributing (not finalizing).
728
730
  if (!!ceremonyId && !!cloudFunctions)
729
731
  await temporaryStoreCurrentContributionUploadedChunkData(cloudFunctions, ceremonyId, chunk);
732
+ // increment the count on the logger
733
+ if (logger)
734
+ logger.increment();
730
735
  }
731
736
  return uploadedChunks;
732
737
  };
@@ -747,8 +752,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
747
752
  * @param configStreamChunkSize <number> - size of each chunk into which the artifact is going to be splitted (nb. will be converted in MB).
748
753
  * @param [ceremonyId] <string> - the unique identifier of the ceremony (used as a double-edge sword - as identifier and as a check if current contributor is the coordinator finalizing the ceremony).
749
754
  * @param [temporaryDataToResumeMultiPartUpload] <TemporaryParticipantContributionData> - the temporary information necessary to resume an already started multi-part upload.
755
+ * @param logger <GenericBar> - an optional logger to show progress.
750
756
  */
751
- const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload) => {
757
+ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload, logger) => {
752
758
  // The unique identifier of the multi-part upload.
753
759
  let multiPartUploadId = "";
754
760
  // The list of already uploaded chunks.
@@ -772,7 +778,7 @@ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFileP
772
778
  const chunksWithUrlsZkey = await getChunksAndPreSignedUrls(cloudFunctions, bucketName, objectKey, localFilePath, multiPartUploadId, configStreamChunkSize, ceremonyId);
773
779
  // Step (2).
774
780
  const partNumbersAndETagsZkey = await uploadParts(chunksWithUrlsZkey, mime.lookup(localFilePath), // content-type.
775
- cloudFunctions, ceremonyId, alreadyUploadedChunks);
781
+ cloudFunctions, ceremonyId, alreadyUploadedChunks, logger);
776
782
  // Step (3).
777
783
  await completeMultiPartUpload(cloudFunctions, bucketName, objectKey, multiPartUploadId, partNumbersAndETagsZkey, ceremonyId);
778
784
  };
@@ -1044,199 +1050,22 @@ const compareHashes = async (path1, path2) => {
1044
1050
  };
1045
1051
 
1046
1052
  /**
1047
- * Parse and validate that the ceremony configuration is correct
1048
- * @notice this does not upload any files to storage
1049
- * @param path <string> - the path to the configuration file
1050
- * @param cleanup <boolean> - whether to delete the r1cs file after parsing
1051
- * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
1053
+ * Return a string with double digits if the provided input is one digit only.
1054
+ * @param in <number> - the input number to be converted.
1055
+ * @returns <string> - the two digits stringified number derived from the conversion.
1052
1056
  */
1053
- const parseCeremonyFile = async (path, cleanup = false) => {
1054
- // check that the path exists
1055
- if (!fs.existsSync(path))
1056
- throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
1057
- try {
1058
- // read the data
1059
- const data = JSON.parse(fs.readFileSync(path).toString());
1060
- // verify that the data is correct
1061
- if (data['timeoutMechanismType'] !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ && data['timeoutMechanismType'] !== "FIXED" /* CeremonyTimeoutType.FIXED */)
1062
- throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
1063
- // validate that we have at least 1 circuit input data
1064
- if (!data.circuits || data.circuits.length === 0)
1065
- throw new Error("You need to provide the data for at least 1 circuit.");
1066
- // validate that the end date is in the future
1067
- let endDate;
1068
- let startDate;
1069
- try {
1070
- endDate = new Date(data.endDate);
1071
- startDate = new Date(data.startDate);
1072
- }
1073
- catch (error) {
1074
- throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
1075
- }
1076
- if (endDate <= startDate)
1077
- throw new Error("The end date should be greater than the start date.");
1078
- const currentDate = new Date();
1079
- if (endDate <= currentDate || startDate <= currentDate)
1080
- throw new Error("The start and end dates should be in the future.");
1081
- // validate penalty
1082
- if (data.penalty <= 0)
1083
- throw new Error("The penalty should be greater than zero.");
1084
- const circuits = [];
1085
- const urlPattern = /(https?:\/\/[^\s]+)/g;
1086
- const commitHashPattern = /^[a-f0-9]{40}$/i;
1087
- const circuitArtifacts = [];
1088
- for (let i = 0; i < data.circuits.length; i++) {
1089
- const circuitData = data.circuits[i];
1090
- const artifacts = circuitData.artifacts;
1091
- circuitArtifacts.push({
1092
- artifacts: artifacts
1093
- });
1094
- // where we storing the r1cs downloaded
1095
- const localR1csPath = `./${circuitData.name}.r1cs`;
1096
- // where we storing the wasm downloaded
1097
- const localWasmPath = `./${circuitData.name}.wasm`;
1098
- // check that the artifacts exist in S3
1099
- // we don't need any privileges to download this
1100
- // just the correct region
1101
- const s3 = new S3Client({
1102
- region: artifacts.region,
1103
- credentials: undefined
1104
- });
1105
- // download the r1cs to extract the metadata
1106
- const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath });
1107
- const response = await s3.send(command);
1108
- const streamPipeline = promisify(pipeline);
1109
- if (response.$metadata.httpStatusCode !== 200)
1110
- throw new Error(`There was an error while trying to download the r1cs file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
1111
- if (response.Body instanceof Readable)
1112
- await streamPipeline(response.Body, fs.createWriteStream(localR1csPath));
1113
- // extract the metadata from the r1cs
1114
- const metadata = getR1CSInfo(localR1csPath);
1115
- // download wasm too to ensure it's available
1116
- const wasmCommand = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.wasmStoragePath });
1117
- const wasmResponse = await s3.send(wasmCommand);
1118
- if (wasmResponse.$metadata.httpStatusCode !== 200)
1119
- throw new Error(`There was an error while trying to download the wasm file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
1120
- if (wasmResponse.Body instanceof Readable)
1121
- await streamPipeline(wasmResponse.Body, fs.createWriteStream(localWasmPath));
1122
- // validate that the circuit hash and template links are valid
1123
- const template = circuitData.template;
1124
- const URLMatch = template.source.match(urlPattern);
1125
- if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
1126
- throw new Error("You should provide the URL to the circuits templates on GitHub.");
1127
- const hashMatch = template.commitHash.match(commitHashPattern);
1128
- if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
1129
- throw new Error("You should provide a valid commit hash of the circuit templates.");
1130
- // calculate the hash of the r1cs file
1131
- const r1csBlake2bHash = await blake512FromPath(localR1csPath);
1132
- const circuitPrefix = extractPrefix(circuitData.name);
1133
- // filenames
1134
- const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
1135
- const r1csCompleteFilename = `${circuitData.name}.r1cs`;
1136
- const wasmCompleteFilename = `${circuitData.name}.wasm`;
1137
- const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
1138
- const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
1139
- // storage paths
1140
- const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
1141
- const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
1142
- const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
1143
- const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
1144
- const files = {
1145
- potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
1146
- r1csFilename: r1csCompleteFilename,
1147
- wasmFilename: wasmCompleteFilename,
1148
- initialZkeyFilename: firstZkeyCompleteFilename,
1149
- potStoragePath: potStorageFilePath,
1150
- r1csStoragePath: r1csStorageFilePath,
1151
- wasmStoragePath: wasmStorageFilePath,
1152
- initialZkeyStoragePath: zkeyStorageFilePath,
1153
- r1csBlake2bHash: r1csBlake2bHash
1154
- };
1155
- // validate that the compiler hash is a valid hash
1156
- const compiler = circuitData.compiler;
1157
- const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
1158
- if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
1159
- throw new Error("You should provide a valid commit hash of the circuit compiler.");
1160
- // validate that the verification options are valid
1161
- const verification = circuitData.verification;
1162
- if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
1163
- throw new Error("Please enter a valid verification mechanism: either CF or VM");
1164
- // @todo VM parameters verification
1165
- // if (verification['cfOrVM'] === "VM") {}
1166
- // check that the timeout is provided for the correct configuration
1167
- let dynamicThreshold;
1168
- let fixedTimeWindow;
1169
- let circuit = {};
1170
- if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
1171
- if (circuitData.dynamicThreshold <= 0)
1172
- throw new Error("The dynamic threshold should be > 0.");
1173
- dynamicThreshold = circuitData.dynamicThreshold;
1174
- // the Circuit data for the ceremony setup
1175
- circuit = {
1176
- name: circuitData.name,
1177
- description: circuitData.description,
1178
- prefix: circuitPrefix,
1179
- sequencePosition: i + 1,
1180
- metadata: metadata,
1181
- files: files,
1182
- template: template,
1183
- compiler: compiler,
1184
- verification: verification,
1185
- dynamicThreshold: dynamicThreshold,
1186
- avgTimings: {
1187
- contributionComputation: 0,
1188
- fullContribution: 0,
1189
- verifyCloudFunction: 0
1190
- },
1191
- };
1192
- }
1193
- if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
1194
- if (circuitData.fixedTimeWindow <= 0)
1195
- throw new Error("The fixed time window threshold should be > 0.");
1196
- fixedTimeWindow = circuitData.fixedTimeWindow;
1197
- // the Circuit data for the ceremony setup
1198
- circuit = {
1199
- name: circuitData.name,
1200
- description: circuitData.description,
1201
- prefix: circuitPrefix,
1202
- sequencePosition: i + 1,
1203
- metadata: metadata,
1204
- files: files,
1205
- template: template,
1206
- compiler: compiler,
1207
- verification: verification,
1208
- fixedTimeWindow: fixedTimeWindow,
1209
- avgTimings: {
1210
- contributionComputation: 0,
1211
- fullContribution: 0,
1212
- verifyCloudFunction: 0
1213
- },
1214
- };
1215
- }
1216
- circuits.push(circuit);
1217
- // remove the local r1cs download (if used for verifying the config only vs setup)
1218
- if (cleanup)
1219
- fs.unlinkSync(localR1csPath);
1220
- }
1221
- const setupData = {
1222
- ceremonyInputData: {
1223
- title: data.title,
1224
- description: data.description,
1225
- startDate: startDate.valueOf(),
1226
- endDate: endDate.valueOf(),
1227
- timeoutMechanismType: data.timeoutMechanismType,
1228
- penalty: data.penalty
1229
- },
1230
- ceremonyPrefix: extractPrefix(data.title),
1231
- circuits: circuits,
1232
- circuitArtifacts: circuitArtifacts
1233
- };
1234
- return setupData;
1235
- }
1236
- catch (error) {
1237
- throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
1238
- }
1239
- };
1057
+ const convertToDoubleDigits = (amount) => (amount < 10 ? `0${amount}` : amount.toString());
1058
+ /**
1059
+ * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
1060
+ * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
1061
+ * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
1062
+ * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
1063
+ * @param str <string> - the arbitrary string from which to extract the prefix.
1064
+ * @returns <string> - the resulting prefix.
1065
+ */
1066
+ const extractPrefix = (str) =>
1067
+ // eslint-disable-next-line no-useless-escape
1068
+ str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
1240
1069
  /**
1241
1070
  * Extract data from a R1CS metadata file generated with a custom file-based logger.
1242
1071
  * @notice useful for extracting metadata circuits contained in the generated file using a logger
@@ -1293,17 +1122,6 @@ const formatZkeyIndex = (progress) => {
1293
1122
  * @returns <number> - the amount of powers.
1294
1123
  */
1295
1124
  const extractPoTFromFilename = (potCompleteFilename) => Number(potCompleteFilename.split("_").pop()?.split(".").at(0));
1296
- /**
1297
- * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
1298
- * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
1299
- * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
1300
- * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
1301
- * @param str <string> - the arbitrary string from which to extract the prefix.
1302
- * @returns <string> - the resulting prefix.
1303
- */
1304
- const extractPrefix = (str) =>
1305
- // eslint-disable-next-line no-useless-escape
1306
- str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
1307
1125
  /**
1308
1126
  * Automate the generation of an entropy for a contribution.
1309
1127
  * @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
@@ -1370,7 +1188,9 @@ const getContributionsValidityForContributor = async (firestoreDatabase, circuit
1370
1188
  * @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
1371
1189
  * @returns <string> - the public attestation preamble.
1372
1190
  */
1373
- const getPublicAttestationPreambleForContributor = (contributorIdentifier, ceremonyName, isFinalizing) => `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName} MPC Phase2 Trusted Setup ceremony.\nThe following are my contribution signatures:`;
1191
+ const getPublicAttestationPreambleForContributor = (contributorIdentifier, ceremonyName, isFinalizing) => `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}${ceremonyName.toLowerCase().includes("trusted setup") || ceremonyName.toLowerCase().includes("ceremony")
1192
+ ? "."
1193
+ : " MPC Phase2 Trusted Setup ceremony."}\nThe following are my contribution signatures:`;
1374
1194
  /**
1375
1195
  * Check and prepare public attestation for the contributor made only of its valid contributions.
1376
1196
  * @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
@@ -1441,6 +1261,41 @@ const readBytesFromFile = (localFilePath, offset, length, position) => {
1441
1261
  // Return the read bytes.
1442
1262
  return buffer;
1443
1263
  };
1264
+ /**
1265
+ * Given a buffer in little endian format, convert it to bigint
1266
+ * @param buffer
1267
+ * @returns
1268
+ */
1269
+ function leBufferToBigint(buffer) {
1270
+ return BigInt(`0x${buffer.reverse().toString("hex")}`);
1271
+ }
1272
+ /**
1273
+ * Given an input containing string values, convert them
1274
+ * to bigint
1275
+ * @param input - The input to convert
1276
+ * @returns the input with string values converted to bigint
1277
+ */
1278
+ const unstringifyBigInts = (input) => {
1279
+ if (typeof input === "string" && /^[0-9]+$/.test(input)) {
1280
+ return BigInt(input);
1281
+ }
1282
+ if (typeof input === "string" && /^0x[0-9a-fA-F]+$/.test(input)) {
1283
+ return BigInt(input);
1284
+ }
1285
+ if (Array.isArray(input)) {
1286
+ return input.map(unstringifyBigInts);
1287
+ }
1288
+ if (input === null) {
1289
+ return null;
1290
+ }
1291
+ if (typeof input === "object") {
1292
+ return Object.entries(input).reduce((acc, [key, value]) => {
1293
+ acc[key] = unstringifyBigInts(value);
1294
+ return acc;
1295
+ }, {});
1296
+ }
1297
+ return input;
1298
+ };
1444
1299
  /**
1445
1300
  * Return the info about the R1CS file.ù
1446
1301
  * @dev this method was built taking inspiration from
@@ -1501,17 +1356,17 @@ const getR1CSInfo = (localR1CSFilePath) => {
1501
1356
  let constraints = 0;
1502
1357
  try {
1503
1358
  // Get 'number of section' (jump magic r1cs and version1 data).
1504
- const numberOfSections = utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, 8));
1359
+ const numberOfSections = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, 8));
1505
1360
  // Jump to first section.
1506
1361
  pointer = 12;
1507
1362
  // For each section
1508
1363
  for (let i = 0; i < numberOfSections; i++) {
1509
1364
  // Read section type.
1510
- const sectionType = utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer));
1365
+ const sectionType = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer));
1511
1366
  // Jump to section size.
1512
1367
  pointer += 4;
1513
1368
  // Read section size
1514
- const sectionSize = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
1369
+ const sectionSize = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
1515
1370
  // If at header section (0x00000001 : Header Section).
1516
1371
  if (sectionType === BigInt(1)) {
1517
1372
  // Read info from header section.
@@ -1543,17 +1398,17 @@ const getR1CSInfo = (localR1CSFilePath) => {
1543
1398
  */
1544
1399
  pointer += sectionSize - 20;
1545
1400
  // Read R1CS info.
1546
- wires = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1401
+ wires = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1547
1402
  pointer += 4;
1548
- publicOutputs = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1403
+ publicOutputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1549
1404
  pointer += 4;
1550
- publicInputs = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1405
+ publicInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1551
1406
  pointer += 4;
1552
- privateInputs = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1407
+ privateInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1553
1408
  pointer += 4;
1554
- labels = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
1409
+ labels = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
1555
1410
  pointer += 8;
1556
- constraints = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1411
+ constraints = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
1557
1412
  }
1558
1413
  pointer += 8 + Number(sectionSize);
1559
1414
  }
@@ -1573,11 +1428,194 @@ const getR1CSInfo = (localR1CSFilePath) => {
1573
1428
  }
1574
1429
  };
1575
1430
  /**
1576
- * Return a string with double digits if the provided input is one digit only.
1577
- * @param in <number> - the input number to be converted.
1578
- * @returns <string> - the two digits stringified number derived from the conversion.
1431
+ * Parse and validate that the ceremony configuration is correct
1432
+ * @notice this does not upload any files to storage
1433
+ * @param path <string> - the path to the configuration file
1434
+ * @param cleanup <boolean> - whether to delete the r1cs file after parsing
1435
+ * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
1579
1436
  */
1580
- const convertToDoubleDigits = (amount) => (amount < 10 ? `0${amount}` : amount.toString());
1437
+ const parseCeremonyFile = async (path, cleanup = false) => {
1438
+ // check that the path exists
1439
+ if (!fs.existsSync(path))
1440
+ throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
1441
+ try {
1442
+ // read the data
1443
+ const data = JSON.parse(fs.readFileSync(path).toString());
1444
+ // verify that the data is correct
1445
+ if (data.timeoutMechanismType !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ &&
1446
+ data.timeoutMechanismType !== "FIXED" /* CeremonyTimeoutType.FIXED */)
1447
+ throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
1448
+ // validate that we have at least 1 circuit input data
1449
+ if (!data.circuits || data.circuits.length === 0)
1450
+ throw new Error("You need to provide the data for at least 1 circuit.");
1451
+ // validate that the end date is in the future
1452
+ let endDate;
1453
+ let startDate;
1454
+ try {
1455
+ endDate = new Date(data.endDate);
1456
+ startDate = new Date(data.startDate);
1457
+ }
1458
+ catch (error) {
1459
+ throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
1460
+ }
1461
+ if (endDate <= startDate)
1462
+ throw new Error("The end date should be greater than the start date.");
1463
+ const currentDate = new Date();
1464
+ if (endDate <= currentDate || startDate <= currentDate)
1465
+ throw new Error("The start and end dates should be in the future.");
1466
+ // validate penalty
1467
+ if (data.penalty <= 0)
1468
+ throw new Error("The penalty should be greater than zero.");
1469
+ const circuits = [];
1470
+ const urlPattern = /(https?:\/\/[^\s]+)/g;
1471
+ const commitHashPattern = /^[a-f0-9]{40}$/i;
1472
+ const circuitArtifacts = [];
1473
+ for (let i = 0; i < data.circuits.length; i++) {
1474
+ const circuitData = data.circuits[i];
1475
+ const { artifacts } = circuitData;
1476
+ circuitArtifacts.push({
1477
+ artifacts
1478
+ });
1479
+ // where we storing the r1cs downloaded
1480
+ const localR1csPath = `./${circuitData.name}.r1cs`;
1481
+ // where we storing the wasm downloaded
1482
+ const localWasmPath = `./${circuitData.name}.wasm`;
1483
+ // download the r1cs to extract the metadata
1484
+ const streamPipeline = promisify(pipeline);
1485
+ // Make the call.
1486
+ const responseR1CS = await fetch(artifacts.r1csStoragePath);
1487
+ // Handle errors.
1488
+ if (!responseR1CS.ok && responseR1CS.status !== 200)
1489
+ throw new Error(`There was an error while trying to download the r1cs file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
1490
+ await streamPipeline(responseR1CS.body, createWriteStream(localR1csPath));
1491
+ // Write the file locally
1492
+ // extract the metadata from the r1cs
1493
+ const metadata = getR1CSInfo(localR1csPath);
1494
+ // download wasm too to ensure it's available
1495
+ const responseWASM = await fetch(artifacts.wasmStoragePath);
1496
+ if (!responseWASM.ok && responseWASM.status !== 200)
1497
+ throw new Error(`There was an error while trying to download the WASM file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
1498
+ await streamPipeline(responseWASM.body, createWriteStream(localWasmPath));
1499
+ // validate that the circuit hash and template links are valid
1500
+ const { template } = circuitData;
1501
+ const URLMatch = template.source.match(urlPattern);
1502
+ if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
1503
+ throw new Error("You should provide the URL to the circuits templates on GitHub.");
1504
+ const hashMatch = template.commitHash.match(commitHashPattern);
1505
+ if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
1506
+ throw new Error("You should provide a valid commit hash of the circuit templates.");
1507
+ // calculate the hash of the r1cs file
1508
+ const r1csBlake2bHash = await blake512FromPath(localR1csPath);
1509
+ const circuitPrefix = extractPrefix(circuitData.name);
1510
+ // filenames
1511
+ const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
1512
+ const r1csCompleteFilename = `${circuitData.name}.r1cs`;
1513
+ const wasmCompleteFilename = `${circuitData.name}.wasm`;
1514
+ const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
1515
+ const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
1516
+ // storage paths
1517
+ const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
1518
+ const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
1519
+ const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
1520
+ const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
1521
+ const files = {
1522
+ potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
1523
+ r1csFilename: r1csCompleteFilename,
1524
+ wasmFilename: wasmCompleteFilename,
1525
+ initialZkeyFilename: firstZkeyCompleteFilename,
1526
+ potStoragePath: potStorageFilePath,
1527
+ r1csStoragePath: r1csStorageFilePath,
1528
+ wasmStoragePath: wasmStorageFilePath,
1529
+ initialZkeyStoragePath: zkeyStorageFilePath,
1530
+ r1csBlake2bHash
1531
+ };
1532
+ // validate that the compiler hash is a valid hash
1533
+ const { compiler } = circuitData;
1534
+ const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
1535
+ if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
1536
+ throw new Error("You should provide a valid commit hash of the circuit compiler.");
1537
+ // validate that the verification options are valid
1538
+ const { verification } = circuitData;
1539
+ if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
1540
+ throw new Error("Please enter a valid verification mechanism: either CF or VM");
1541
+ // @todo VM parameters verification
1542
+ // if (verification['cfOrVM'] === "VM") {}
1543
+ // check that the timeout is provided for the correct configuration
1544
+ let dynamicThreshold;
1545
+ let fixedTimeWindow;
1546
+ let circuit = {};
1547
+ if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
1548
+ if (circuitData.dynamicThreshold <= 0)
1549
+ throw new Error("The dynamic threshold should be > 0.");
1550
+ dynamicThreshold = circuitData.dynamicThreshold;
1551
+ // the Circuit data for the ceremony setup
1552
+ circuit = {
1553
+ name: circuitData.name,
1554
+ description: circuitData.description,
1555
+ prefix: circuitPrefix,
1556
+ sequencePosition: i + 1,
1557
+ metadata,
1558
+ files,
1559
+ template,
1560
+ compiler,
1561
+ verification,
1562
+ dynamicThreshold,
1563
+ avgTimings: {
1564
+ contributionComputation: 0,
1565
+ fullContribution: 0,
1566
+ verifyCloudFunction: 0
1567
+ }
1568
+ };
1569
+ }
1570
+ if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
1571
+ if (circuitData.fixedTimeWindow <= 0)
1572
+ throw new Error("The fixed time window threshold should be > 0.");
1573
+ fixedTimeWindow = circuitData.fixedTimeWindow;
1574
+ // the Circuit data for the ceremony setup
1575
+ circuit = {
1576
+ name: circuitData.name,
1577
+ description: circuitData.description,
1578
+ prefix: circuitPrefix,
1579
+ sequencePosition: i + 1,
1580
+ metadata,
1581
+ files,
1582
+ template,
1583
+ compiler,
1584
+ verification,
1585
+ fixedTimeWindow,
1586
+ avgTimings: {
1587
+ contributionComputation: 0,
1588
+ fullContribution: 0,
1589
+ verifyCloudFunction: 0
1590
+ }
1591
+ };
1592
+ }
1593
+ circuits.push(circuit);
1594
+ // remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
1595
+ if (cleanup) {
1596
+ fs.unlinkSync(localR1csPath);
1597
+ fs.unlinkSync(localWasmPath);
1598
+ }
1599
+ }
1600
+ const setupData = {
1601
+ ceremonyInputData: {
1602
+ title: data.title,
1603
+ description: data.description,
1604
+ startDate: startDate.valueOf(),
1605
+ endDate: endDate.valueOf(),
1606
+ timeoutMechanismType: data.timeoutMechanismType,
1607
+ penalty: data.penalty
1608
+ },
1609
+ ceremonyPrefix: extractPrefix(data.title),
1610
+ circuits,
1611
+ circuitArtifacts
1612
+ };
1613
+ return setupData;
1614
+ }
1615
+ catch (error) {
1616
+ throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
1617
+ }
1618
+ };
1581
1619
 
1582
1620
  /**
1583
1621
  * Verify that a zKey is valid
@@ -1826,7 +1864,7 @@ const getFirestoreDatabase = (app) => getFirestore(app);
1826
1864
  * @param app <FirebaseApp> - the Firebase application.
1827
1865
  * @returns <Functions> - the Cloud Functions associated to the application.
1828
1866
  */
1829
- const getFirebaseFunctions = (app) => getFunctions(app, 'europe-west1');
1867
+ const getFirebaseFunctions = (app) => getFunctions(app, "europe-west1");
1830
1868
  /**
1831
1869
  * Retrieve the configuration variables for the AWS services (S3, EC2).
1832
1870
  * @returns <AWSVariables> - the values of the AWS services configuration variables.
@@ -1835,14 +1873,14 @@ const getAWSVariables = () => {
1835
1873
  if (!process.env.AWS_ACCESS_KEY_ID ||
1836
1874
  !process.env.AWS_SECRET_ACCESS_KEY ||
1837
1875
  !process.env.AWS_REGION ||
1838
- !process.env.AWS_ROLE_ARN ||
1876
+ !process.env.AWS_INSTANCE_PROFILE_ARN ||
1839
1877
  !process.env.AWS_AMI_ID)
1840
1878
  throw new Error("Could not retrieve the AWS environment variables. Please, verify your environment configuration and retry");
1841
1879
  return {
1842
1880
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
1843
1881
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
1844
1882
  region: process.env.AWS_REGION || "us-east-1",
1845
- roleArn: process.env.AWS_ROLE_ARN,
1883
+ instanceProfileArn: process.env.AWS_INSTANCE_PROFILE_ARN,
1846
1884
  amiId: process.env.AWS_AMI_ID
1847
1885
  };
1848
1886
  };
@@ -1923,11 +1961,11 @@ const p256 = (proofPart) => {
1923
1961
  */
1924
1962
  const formatSolidityCalldata = (circuitInput, _proof) => {
1925
1963
  try {
1926
- const proof = utils.unstringifyBigInts(_proof);
1964
+ const proof = unstringifyBigInts(_proof);
1927
1965
  // format the public inputs to the circuit
1928
1966
  const formattedCircuitInput = [];
1929
1967
  for (const cInput of circuitInput) {
1930
- formattedCircuitInput.push(p256(utils.unstringifyBigInts(cInput)));
1968
+ formattedCircuitInput.push(p256(unstringifyBigInts(cInput)));
1931
1969
  }
1932
1970
  // construct calldata
1933
1971
  const calldata = {
@@ -2095,7 +2133,8 @@ const getGitHubStats = async (user) => {
2095
2133
  following: jsonData.following,
2096
2134
  followers: jsonData.followers,
2097
2135
  publicRepos: jsonData.public_repos,
2098
- avatarUrl: jsonData.avatar_url
2136
+ avatarUrl: jsonData.avatar_url,
2137
+ age: jsonData.created_at
2099
2138
  };
2100
2139
  return data;
2101
2140
  };
@@ -2107,20 +2146,21 @@ const getGitHubStats = async (user) => {
2107
2146
  * @param minimumAmountOfPublicRepos <number> The minimum amount of public repos the user should have
2108
2147
  * @returns <any> Return the avatar URL of the user if the user is reputable, false otherwise
2109
2148
  */
2110
- const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos) => {
2149
+ const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos, minimumAge) => {
2111
2150
  if (!process.env.GITHUB_ACCESS_TOKEN)
2112
2151
  throw new Error("The GitHub access token is missing. Please insert a valid token to be used for anti-sybil checks on user registation, and then try again.");
2113
- const { following, followers, publicRepos, avatarUrl } = await getGitHubStats(userLogin);
2152
+ const { following, followers, publicRepos, avatarUrl, age } = await getGitHubStats(userLogin);
2114
2153
  if (following < minimumAmountOfFollowing ||
2115
2154
  publicRepos < minimumAmountOfPublicRepos ||
2116
- followers < minimumAmountOfFollowers)
2155
+ followers < minimumAmountOfFollowers ||
2156
+ new Date(age) > new Date(Date.now() - minimumAge))
2117
2157
  return {
2118
2158
  reputable: false,
2119
2159
  avatarUrl: ""
2120
2160
  };
2121
2161
  return {
2122
2162
  reputable: true,
2123
- avatarUrl: avatarUrl
2163
+ avatarUrl
2124
2164
  };
2125
2165
  };
2126
2166
 
@@ -2329,8 +2369,13 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
2329
2369
  // eslint-disable-next-line no-template-curly-in-string
2330
2370
  "touch ${MARKER_FILE}",
2331
2371
  "sudo yum update -y",
2332
- "curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash - ",
2333
- "sudo yum install -y nodejs",
2372
+ "curl -O https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz",
2373
+ "tar -xf node-v16.13.0-linux-x64.tar.xz",
2374
+ "mv node-v16.13.0-linux-x64 nodejs",
2375
+ "sudo mv nodejs /opt/",
2376
+ "echo 'export NODEJS_HOME=/opt/nodejs' >> /etc/profile",
2377
+ "echo 'export PATH=$NODEJS_HOME/bin:$PATH' >> /etc/profile",
2378
+ "source /etc/profile",
2334
2379
  "npm install -g snarkjs",
2335
2380
  `aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
2336
2381
  `aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
@@ -2349,6 +2394,7 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
2349
2394
  * @returns Array<string> - the list of commands for contribution verification.
2350
2395
  */
2351
2396
  const vmContributionVerificationCommand = (bucketName, lastZkeyStoragePath, verificationTranscriptStoragePathAndFilename) => [
2397
+ `source /etc/profile`,
2352
2398
  `aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
2353
2399
  `snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
2354
2400
  `aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
@@ -2375,7 +2421,7 @@ const computeDiskSizeForVM = (zKeySizeInBytes, pot) => Math.ceil(2 * convertByte
2375
2421
  */
2376
2422
  const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskType) => {
2377
2423
  // Get the AWS variables.
2378
- const { amiId, roleArn } = getAWSVariables();
2424
+ const { amiId, instanceProfileArn } = getAWSVariables();
2379
2425
  // Parametrize the VM EC2 instance.
2380
2426
  const params = {
2381
2427
  ImageId: amiId,
@@ -2384,7 +2430,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
2384
2430
  MinCount: 1,
2385
2431
  // nb. to find this: iam -> roles -> role_name.
2386
2432
  IamInstanceProfile: {
2387
- Arn: roleArn
2433
+ Arn: instanceProfileArn
2388
2434
  },
2389
2435
  // nb. for running commands at the startup.
2390
2436
  UserData: Buffer.from(commands.join("\n")).toString("base64"),