@devtion/actions 0.0.0-101d43f → 0.0.0-142ec0a

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