@devtion/actions 0.0.0-bfc9ee4 → 0.0.0-c749be4

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
2
  * @module @p0tion/actions
3
- * @version 1.0.5
3
+ * @version 1.1.0
4
4
  * @file A set of actions and helpers for CLI commands
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -17,8 +17,7 @@ import crypto from 'crypto';
17
17
  import blake from 'blakejs';
18
18
  import { utils } from 'ffjavascript';
19
19
  import winston from 'winston';
20
- import { S3Client, HeadObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
21
- import { pipeline, Readable } from 'stream';
20
+ import { pipeline } from 'stream';
22
21
  import { promisify } from 'util';
23
22
  import { initializeApp } from 'firebase/app';
24
23
  import { signInWithCredential, initializeAuth, getAuth } from 'firebase/auth';
@@ -244,6 +243,12 @@ const commonTerms = {
244
243
  verificationStartedAt: "verificationStartedAt"
245
244
  }
246
245
  },
246
+ avatars: {
247
+ name: "avatars",
248
+ fields: {
249
+ avatarUrl: "avatarUrl"
250
+ }
251
+ },
247
252
  ceremonies: {
248
253
  name: "ceremonies",
249
254
  fields: {
@@ -334,7 +339,7 @@ const commonTerms = {
334
339
  finalizeCircuit: "finalizeCircuit",
335
340
  finalizeCeremony: "finalizeCeremony",
336
341
  downloadCircuitArtifacts: "downloadCircuitArtifacts",
337
- transferObject: "transferObject",
342
+ transferObject: "transferObject"
338
343
  }
339
344
  };
340
345
 
@@ -685,11 +690,15 @@ const getChunksAndPreSignedUrls = async (cloudFunctions, bucketName, objectKey,
685
690
  * @param cloudFunctions <Functions> - the Firebase Cloud Functions service instance.
686
691
  * @param ceremonyId <string> - the unique identifier of the ceremony.
687
692
  * @param alreadyUploadedChunks Array<ETagWithPartNumber> - the temporary information about the already uploaded chunks.
693
+ * @param logger <GenericBar> - an optional logger to show progress.
688
694
  * @returns <Promise<Array<ETagWithPartNumber>>> - the completed (uploaded) chunks information.
689
695
  */
690
- const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks) => {
696
+ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks, logger) => {
691
697
  // Keep track of uploaded chunks.
692
698
  const uploadedChunks = alreadyUploadedChunks || [];
699
+ // if we were passed a logger, start it
700
+ if (logger)
701
+ logger.start(chunksWithUrls.length, 0);
693
702
  // Loop through remaining chunks.
694
703
  for (let i = alreadyUploadedChunks ? alreadyUploadedChunks.length : 0; i < chunksWithUrls.length; i += 1) {
695
704
  // Consume the pre-signed url to upload the chunk.
@@ -721,6 +730,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
721
730
  // nb. this must be done only when contributing (not finalizing).
722
731
  if (!!ceremonyId && !!cloudFunctions)
723
732
  await temporaryStoreCurrentContributionUploadedChunkData(cloudFunctions, ceremonyId, chunk);
733
+ // increment the count on the logger
734
+ if (logger)
735
+ logger.increment();
724
736
  }
725
737
  return uploadedChunks;
726
738
  };
@@ -741,8 +753,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
741
753
  * @param configStreamChunkSize <number> - size of each chunk into which the artifact is going to be splitted (nb. will be converted in MB).
742
754
  * @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).
743
755
  * @param [temporaryDataToResumeMultiPartUpload] <TemporaryParticipantContributionData> - the temporary information necessary to resume an already started multi-part upload.
756
+ * @param logger <GenericBar> - an optional logger to show progress.
744
757
  */
745
- const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload) => {
758
+ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload, logger) => {
746
759
  // The unique identifier of the multi-part upload.
747
760
  let multiPartUploadId = "";
748
761
  // The list of already uploaded chunks.
@@ -766,7 +779,7 @@ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFileP
766
779
  const chunksWithUrlsZkey = await getChunksAndPreSignedUrls(cloudFunctions, bucketName, objectKey, localFilePath, multiPartUploadId, configStreamChunkSize, ceremonyId);
767
780
  // Step (2).
768
781
  const partNumbersAndETagsZkey = await uploadParts(chunksWithUrlsZkey, mime.lookup(localFilePath), // content-type.
769
- cloudFunctions, ceremonyId, alreadyUploadedChunks);
782
+ cloudFunctions, ceremonyId, alreadyUploadedChunks, logger);
770
783
  // Step (3).
771
784
  await completeMultiPartUpload(cloudFunctions, bucketName, objectKey, multiPartUploadId, partNumbersAndETagsZkey, ceremonyId);
772
785
  };
@@ -1038,207 +1051,22 @@ const compareHashes = async (path1, path2) => {
1038
1051
  };
1039
1052
 
1040
1053
  /**
1041
- * Parse and validate that the ceremony configuration is correct
1042
- * @notice this does not upload any files to storage
1043
- * @param path <string> - the path to the configuration file
1044
- * @param cleanup <boolean> - whether to delete the r1cs file after parsing
1045
- * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
1054
+ * Return a string with double digits if the provided input is one digit only.
1055
+ * @param in <number> - the input number to be converted.
1056
+ * @returns <string> - the two digits stringified number derived from the conversion.
1046
1057
  */
1047
- const parseCeremonyFile = async (path, cleanup = false) => {
1048
- // check that the path exists
1049
- if (!fs.existsSync(path))
1050
- throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
1051
- try {
1052
- // read the data
1053
- const data = JSON.parse(fs.readFileSync(path).toString());
1054
- // verify that the data is correct
1055
- if (data['timeoutMechanismType'] !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ && data['timeoutMechanismType'] !== "FIXED" /* CeremonyTimeoutType.FIXED */)
1056
- throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
1057
- // validate that we have at least 1 circuit input data
1058
- if (!data.circuits || data.circuits.length === 0)
1059
- throw new Error("You need to provide the data for at least 1 circuit.");
1060
- // validate that the end date is in the future
1061
- let endDate;
1062
- let startDate;
1063
- try {
1064
- endDate = new Date(data.endDate);
1065
- startDate = new Date(data.startDate);
1066
- }
1067
- catch (error) {
1068
- throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
1069
- }
1070
- if (endDate <= startDate)
1071
- throw new Error("The end date should be greater than the start date.");
1072
- const currentDate = new Date();
1073
- if (endDate <= currentDate || startDate <= currentDate)
1074
- throw new Error("The start and end dates should be in the future.");
1075
- // validate penalty
1076
- if (data.penalty <= 0)
1077
- throw new Error("The penalty should be greater than zero.");
1078
- const circuits = [];
1079
- const urlPattern = /(https?:\/\/[^\s]+)/g;
1080
- const commitHashPattern = /^[a-f0-9]{40}$/i;
1081
- const circuitArtifacts = [];
1082
- for (let i = 0; i < data.circuits.length; i++) {
1083
- const circuitData = data.circuits[i];
1084
- const artifacts = circuitData.artifacts;
1085
- circuitArtifacts.push({
1086
- artifacts: artifacts
1087
- });
1088
- const r1csPath = artifacts.r1csStoragePath;
1089
- const wasmPath = artifacts.wasmStoragePath;
1090
- // where we storing the r1cs downloaded
1091
- const localR1csPath = `./${circuitData.name}.r1cs`;
1092
- // check that the artifacts exist in S3
1093
- // we don't need any privileges to download this
1094
- // just the correct region
1095
- const s3 = new S3Client({ region: artifacts.region });
1096
- try {
1097
- await s3.send(new HeadObjectCommand({
1098
- Bucket: artifacts.bucket,
1099
- Key: r1csPath
1100
- }));
1101
- }
1102
- catch (error) {
1103
- throw new Error(`The r1cs file (${r1csPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`);
1104
- }
1105
- try {
1106
- await s3.send(new HeadObjectCommand({
1107
- Bucket: artifacts.bucket,
1108
- Key: wasmPath
1109
- }));
1110
- }
1111
- catch (error) {
1112
- throw new Error(`The wasm file (${wasmPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`);
1113
- }
1114
- // download the r1cs to extract the metadata
1115
- const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath });
1116
- const response = await s3.send(command);
1117
- const streamPipeline = promisify(pipeline);
1118
- if (response.$metadata.httpStatusCode !== 200)
1119
- throw new Error("There was an error while trying to download the r1cs file. Please check that the file has the correct permissions (public) set.");
1120
- if (response.Body instanceof Readable)
1121
- await streamPipeline(response.Body, fs.createWriteStream(localR1csPath));
1122
- // extract the metadata from the r1cs
1123
- const metadata = getR1CSInfo(localR1csPath);
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
- };
1058
+ const convertToDoubleDigits = (amount) => (amount < 10 ? `0${amount}` : amount.toString());
1059
+ /**
1060
+ * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
1061
+ * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
1062
+ * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
1063
+ * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
1064
+ * @param str <string> - the arbitrary string from which to extract the prefix.
1065
+ * @returns <string> - the resulting prefix.
1066
+ */
1067
+ const extractPrefix = (str) =>
1068
+ // eslint-disable-next-line no-useless-escape
1069
+ str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
1242
1070
  /**
1243
1071
  * Extract data from a R1CS metadata file generated with a custom file-based logger.
1244
1072
  * @notice useful for extracting metadata circuits contained in the generated file using a logger
@@ -1295,17 +1123,6 @@ const formatZkeyIndex = (progress) => {
1295
1123
  * @returns <number> - the amount of powers.
1296
1124
  */
1297
1125
  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
1126
  /**
1310
1127
  * Automate the generation of an entropy for a contribution.
1311
1128
  * @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
@@ -1372,7 +1189,9 @@ const getContributionsValidityForContributor = async (firestoreDatabase, circuit
1372
1189
  * @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
1373
1190
  * @returns <string> - the public attestation preamble.
1374
1191
  */
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:`;
1192
+ 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")
1193
+ ? "."
1194
+ : " MPC Phase2 Trusted Setup ceremony."}\nThe following are my contribution signatures:`;
1376
1195
  /**
1377
1196
  * Check and prepare public attestation for the contributor made only of its valid contributions.
1378
1197
  * @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
@@ -1575,11 +1394,193 @@ const getR1CSInfo = (localR1CSFilePath) => {
1575
1394
  }
1576
1395
  };
1577
1396
  /**
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.
1397
+ * Parse and validate that the ceremony configuration is correct
1398
+ * @notice this does not upload any files to storage
1399
+ * @param path <string> - the path to the configuration file
1400
+ * @param cleanup <boolean> - whether to delete the r1cs file after parsing
1401
+ * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
1581
1402
  */
1582
- const convertToDoubleDigits = (amount) => (amount < 10 ? `0${amount}` : amount.toString());
1403
+ const parseCeremonyFile = async (path, cleanup = false) => {
1404
+ // check that the path exists
1405
+ if (!fs.existsSync(path))
1406
+ throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
1407
+ try {
1408
+ // read the data
1409
+ const data = JSON.parse(fs.readFileSync(path).toString());
1410
+ // verify that the data is correct
1411
+ if (data.timeoutMechanismType !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ &&
1412
+ data.timeoutMechanismType !== "FIXED" /* CeremonyTimeoutType.FIXED */)
1413
+ throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
1414
+ // validate that we have at least 1 circuit input data
1415
+ if (!data.circuits || data.circuits.length === 0)
1416
+ throw new Error("You need to provide the data for at least 1 circuit.");
1417
+ // validate that the end date is in the future
1418
+ let endDate;
1419
+ let startDate;
1420
+ try {
1421
+ endDate = new Date(data.endDate);
1422
+ startDate = new Date(data.startDate);
1423
+ }
1424
+ catch (error) {
1425
+ throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
1426
+ }
1427
+ if (endDate <= startDate)
1428
+ throw new Error("The end date should be greater than the start date.");
1429
+ const currentDate = new Date();
1430
+ if (endDate <= currentDate || startDate <= currentDate)
1431
+ throw new Error("The start and end dates should be in the future.");
1432
+ // validate penalty
1433
+ if (data.penalty <= 0)
1434
+ throw new Error("The penalty should be greater than zero.");
1435
+ const circuits = [];
1436
+ const urlPattern = /(https?:\/\/[^\s]+)/g;
1437
+ const commitHashPattern = /^[a-f0-9]{40}$/i;
1438
+ const circuitArtifacts = [];
1439
+ for (let i = 0; i < data.circuits.length; i++) {
1440
+ const circuitData = data.circuits[i];
1441
+ const { artifacts } = circuitData;
1442
+ circuitArtifacts.push({
1443
+ artifacts
1444
+ });
1445
+ // where we storing the r1cs downloaded
1446
+ const localR1csPath = `./${circuitData.name}.r1cs`;
1447
+ // where we storing the wasm downloaded
1448
+ const localWasmPath = `./${circuitData.name}.wasm`;
1449
+ // download the r1cs to extract the metadata
1450
+ const streamPipeline = promisify(pipeline);
1451
+ // Make the call.
1452
+ const responseR1CS = await fetch(artifacts.r1csStoragePath);
1453
+ // Handle errors.
1454
+ if (!responseR1CS.ok && responseR1CS.status !== 200)
1455
+ 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.`);
1456
+ await streamPipeline(responseR1CS.body, createWriteStream(localR1csPath));
1457
+ // Write the file locally
1458
+ // extract the metadata from the r1cs
1459
+ const metadata = getR1CSInfo(localR1csPath);
1460
+ // download wasm too to ensure it's available
1461
+ const responseWASM = await fetch(artifacts.wasmStoragePath);
1462
+ if (!responseWASM.ok && responseWASM.status !== 200)
1463
+ 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.`);
1464
+ await streamPipeline(responseWASM.body, createWriteStream(localWasmPath));
1465
+ // validate that the circuit hash and template links are valid
1466
+ const { template } = circuitData;
1467
+ const URLMatch = template.source.match(urlPattern);
1468
+ if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
1469
+ throw new Error("You should provide the URL to the circuits templates on GitHub.");
1470
+ const hashMatch = template.commitHash.match(commitHashPattern);
1471
+ if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
1472
+ throw new Error("You should provide a valid commit hash of the circuit templates.");
1473
+ // calculate the hash of the r1cs file
1474
+ const r1csBlake2bHash = await blake512FromPath(localR1csPath);
1475
+ const circuitPrefix = extractPrefix(circuitData.name);
1476
+ // filenames
1477
+ const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
1478
+ const r1csCompleteFilename = `${circuitData.name}.r1cs`;
1479
+ const wasmCompleteFilename = `${circuitData.name}.wasm`;
1480
+ const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
1481
+ const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
1482
+ // storage paths
1483
+ const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
1484
+ const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
1485
+ const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
1486
+ const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
1487
+ const files = {
1488
+ potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
1489
+ r1csFilename: r1csCompleteFilename,
1490
+ wasmFilename: wasmCompleteFilename,
1491
+ initialZkeyFilename: firstZkeyCompleteFilename,
1492
+ potStoragePath: potStorageFilePath,
1493
+ r1csStoragePath: r1csStorageFilePath,
1494
+ wasmStoragePath: wasmStorageFilePath,
1495
+ initialZkeyStoragePath: zkeyStorageFilePath,
1496
+ r1csBlake2bHash
1497
+ };
1498
+ // validate that the compiler hash is a valid hash
1499
+ const { compiler } = circuitData;
1500
+ const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
1501
+ if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
1502
+ throw new Error("You should provide a valid commit hash of the circuit compiler.");
1503
+ // validate that the verification options are valid
1504
+ const { verification } = circuitData;
1505
+ if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
1506
+ throw new Error("Please enter a valid verification mechanism: either CF or VM");
1507
+ // @todo VM parameters verification
1508
+ // if (verification['cfOrVM'] === "VM") {}
1509
+ // check that the timeout is provided for the correct configuration
1510
+ let dynamicThreshold;
1511
+ let fixedTimeWindow;
1512
+ let circuit = {};
1513
+ if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
1514
+ if (circuitData.dynamicThreshold <= 0)
1515
+ throw new Error("The dynamic threshold should be > 0.");
1516
+ dynamicThreshold = circuitData.dynamicThreshold;
1517
+ // the Circuit data for the ceremony setup
1518
+ circuit = {
1519
+ name: circuitData.name,
1520
+ description: circuitData.description,
1521
+ prefix: circuitPrefix,
1522
+ sequencePosition: i + 1,
1523
+ metadata,
1524
+ files,
1525
+ template,
1526
+ compiler,
1527
+ verification,
1528
+ dynamicThreshold,
1529
+ avgTimings: {
1530
+ contributionComputation: 0,
1531
+ fullContribution: 0,
1532
+ verifyCloudFunction: 0
1533
+ }
1534
+ };
1535
+ }
1536
+ if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
1537
+ if (circuitData.fixedTimeWindow <= 0)
1538
+ throw new Error("The fixed time window threshold should be > 0.");
1539
+ fixedTimeWindow = circuitData.fixedTimeWindow;
1540
+ // the Circuit data for the ceremony setup
1541
+ circuit = {
1542
+ name: circuitData.name,
1543
+ description: circuitData.description,
1544
+ prefix: circuitPrefix,
1545
+ sequencePosition: i + 1,
1546
+ metadata,
1547
+ files,
1548
+ template,
1549
+ compiler,
1550
+ verification,
1551
+ fixedTimeWindow,
1552
+ avgTimings: {
1553
+ contributionComputation: 0,
1554
+ fullContribution: 0,
1555
+ verifyCloudFunction: 0
1556
+ }
1557
+ };
1558
+ }
1559
+ circuits.push(circuit);
1560
+ // remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
1561
+ if (cleanup)
1562
+ fs.unlinkSync(localR1csPath);
1563
+ fs.unlinkSync(localWasmPath);
1564
+ }
1565
+ const setupData = {
1566
+ ceremonyInputData: {
1567
+ title: data.title,
1568
+ description: data.description,
1569
+ startDate: startDate.valueOf(),
1570
+ endDate: endDate.valueOf(),
1571
+ timeoutMechanismType: data.timeoutMechanismType,
1572
+ penalty: data.penalty
1573
+ },
1574
+ ceremonyPrefix: extractPrefix(data.title),
1575
+ circuits,
1576
+ circuitArtifacts
1577
+ };
1578
+ return setupData;
1579
+ }
1580
+ catch (error) {
1581
+ throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
1582
+ }
1583
+ };
1583
1584
 
1584
1585
  /**
1585
1586
  * Verify that a zKey is valid
@@ -1828,7 +1829,7 @@ const getFirestoreDatabase = (app) => getFirestore(app);
1828
1829
  * @param app <FirebaseApp> - the Firebase application.
1829
1830
  * @returns <Functions> - the Cloud Functions associated to the application.
1830
1831
  */
1831
- const getFirebaseFunctions = (app) => getFunctions(app, 'europe-west1');
1832
+ const getFirebaseFunctions = (app) => getFunctions(app, "europe-west1");
1832
1833
  /**
1833
1834
  * Retrieve the configuration variables for the AWS services (S3, EC2).
1834
1835
  * @returns <AWSVariables> - the values of the AWS services configuration variables.
@@ -1837,14 +1838,14 @@ const getAWSVariables = () => {
1837
1838
  if (!process.env.AWS_ACCESS_KEY_ID ||
1838
1839
  !process.env.AWS_SECRET_ACCESS_KEY ||
1839
1840
  !process.env.AWS_REGION ||
1840
- !process.env.AWS_ROLE_ARN ||
1841
+ !process.env.AWS_INSTANCE_PROFILE_ARN ||
1841
1842
  !process.env.AWS_AMI_ID)
1842
1843
  throw new Error("Could not retrieve the AWS environment variables. Please, verify your environment configuration and retry");
1843
1844
  return {
1844
1845
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
1845
1846
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
1846
1847
  region: process.env.AWS_REGION || "us-east-1",
1847
- roleArn: process.env.AWS_ROLE_ARN,
1848
+ instanceProfileArn: process.env.AWS_INSTANCE_PROFILE_ARN,
1848
1849
  amiId: process.env.AWS_AMI_ID
1849
1850
  };
1850
1851
  };
@@ -2079,55 +2080,28 @@ const verifyCeremony = async (functions, firestore, ceremonyPrefix, outputDirect
2079
2080
  };
2080
2081
 
2081
2082
  /**
2082
- * This function will return the number of public repos of a user
2083
- * @param user <string> The username of the user
2084
- * @returns <number> The number of public repos
2085
- */
2086
- const getNumberOfPublicReposGitHub = async (user) => {
2087
- const response = await fetch(`https://api.github.com/user/${user}/repos`, {
2088
- method: "GET",
2089
- headers: {
2090
- Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN}`
2091
- }
2092
- });
2093
- if (response.status !== 200)
2094
- throw new Error("It was not possible to retrieve the number of public repositories. Please try again.");
2095
- const jsonData = await response.json();
2096
- return jsonData.length;
2097
- };
2098
- /**
2099
- * This function will return the number of followers of a user
2100
- * @param user <string> The username of the user
2101
- * @returns <number> The number of followers
2102
- */
2103
- const getNumberOfFollowersGitHub = async (user) => {
2104
- const response = await fetch(`https://api.github.com/user/${user}/followers`, {
2105
- method: "GET",
2106
- headers: {
2107
- Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN}`
2108
- }
2109
- });
2110
- if (response.status !== 200)
2111
- throw new Error("It was not possible to retrieve the number of followers. Please try again.");
2112
- const jsonData = await response.json();
2113
- return jsonData.length;
2114
- };
2115
- /**
2116
- * This function will return the number of following of a user
2117
- * @param user <string> The username of the user
2118
- * @returns <number> The number of following users
2083
+ * This function queries the GitHub API to fetch users statistics
2084
+ * @param user {string} the user uid
2085
+ * @returns {any} the stats from the GitHub API
2119
2086
  */
2120
- const getNumberOfFollowingGitHub = async (user) => {
2121
- const response = await fetch(`https://api.github.com/user/${user}/following`, {
2087
+ const getGitHubStats = async (user) => {
2088
+ const response = await fetch(`https://api.github.com/user/${user}`, {
2122
2089
  method: "GET",
2123
2090
  headers: {
2124
2091
  Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN}`
2125
2092
  }
2126
2093
  });
2127
2094
  if (response.status !== 200)
2128
- throw new Error("It was not possible to retrieve the number of following. Please try again.");
2095
+ throw new Error("It was not possible to retrieve the user's statistic. Please try again.");
2129
2096
  const jsonData = await response.json();
2130
- return jsonData.length;
2097
+ const data = {
2098
+ following: jsonData.following,
2099
+ followers: jsonData.followers,
2100
+ publicRepos: jsonData.public_repos,
2101
+ avatarUrl: jsonData.avatar_url,
2102
+ age: jsonData.created_at
2103
+ };
2104
+ return data;
2131
2105
  };
2132
2106
  /**
2133
2107
  * This function will check if the user is reputable enough to be able to use the app
@@ -2135,19 +2109,24 @@ const getNumberOfFollowingGitHub = async (user) => {
2135
2109
  * @param minimumAmountOfFollowing <number> The minimum amount of following the user should have
2136
2110
  * @param minimumAmountOfFollowers <number> The minimum amount of followers the user should have
2137
2111
  * @param minimumAmountOfPublicRepos <number> The minimum amount of public repos the user should have
2138
- * @returns <boolean> True if the user is reputable enough, false otherwise
2112
+ * @returns <any> Return the avatar URL of the user if the user is reputable, false otherwise
2139
2113
  */
2140
- const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos) => {
2114
+ const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos, minimumAge) => {
2141
2115
  if (!process.env.GITHUB_ACCESS_TOKEN)
2142
2116
  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.");
2143
- const following = await getNumberOfFollowingGitHub(userLogin);
2144
- const repos = await getNumberOfPublicReposGitHub(userLogin);
2145
- const followers = await getNumberOfFollowersGitHub(userLogin);
2117
+ const { following, followers, publicRepos, avatarUrl, age } = await getGitHubStats(userLogin);
2146
2118
  if (following < minimumAmountOfFollowing ||
2147
- repos < minimumAmountOfPublicRepos ||
2148
- followers < minimumAmountOfFollowers)
2149
- return false;
2150
- return true;
2119
+ publicRepos < minimumAmountOfPublicRepos ||
2120
+ followers < minimumAmountOfFollowers ||
2121
+ new Date(age) > new Date(Date.now() - minimumAge))
2122
+ return {
2123
+ reputable: false,
2124
+ avatarUrl: ""
2125
+ };
2126
+ return {
2127
+ reputable: true,
2128
+ avatarUrl
2129
+ };
2151
2130
  };
2152
2131
 
2153
2132
  /**
@@ -2355,8 +2334,13 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
2355
2334
  // eslint-disable-next-line no-template-curly-in-string
2356
2335
  "touch ${MARKER_FILE}",
2357
2336
  "sudo yum update -y",
2358
- "curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash - ",
2359
- "sudo yum install -y nodejs",
2337
+ "curl -O https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz",
2338
+ "tar -xf node-v16.13.0-linux-x64.tar.xz",
2339
+ "mv node-v16.13.0-linux-x64 nodejs",
2340
+ "sudo mv nodejs /opt/",
2341
+ "echo 'export NODEJS_HOME=/opt/nodejs' >> /etc/profile",
2342
+ "echo 'export PATH=$NODEJS_HOME/bin:$PATH' >> /etc/profile",
2343
+ "source /etc/profile",
2360
2344
  "npm install -g snarkjs",
2361
2345
  `aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
2362
2346
  `aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
@@ -2375,6 +2359,7 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
2375
2359
  * @returns Array<string> - the list of commands for contribution verification.
2376
2360
  */
2377
2361
  const vmContributionVerificationCommand = (bucketName, lastZkeyStoragePath, verificationTranscriptStoragePathAndFilename) => [
2362
+ `source /etc/profile`,
2378
2363
  `aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
2379
2364
  `snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
2380
2365
  `aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
@@ -2401,7 +2386,7 @@ const computeDiskSizeForVM = (zKeySizeInBytes, pot) => Math.ceil(2 * convertByte
2401
2386
  */
2402
2387
  const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskType) => {
2403
2388
  // Get the AWS variables.
2404
- const { amiId, roleArn } = getAWSVariables();
2389
+ const { amiId, instanceProfileArn } = getAWSVariables();
2405
2390
  // Parametrize the VM EC2 instance.
2406
2391
  const params = {
2407
2392
  ImageId: amiId,
@@ -2410,7 +2395,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
2410
2395
  MinCount: 1,
2411
2396
  // nb. to find this: iam -> roles -> role_name.
2412
2397
  IamInstanceProfile: {
2413
- Arn: roleArn
2398
+ Arn: instanceProfileArn
2414
2399
  },
2415
2400
  // nb. for running commands at the startup.
2416
2401
  UserData: Buffer.from(commands.join("\n")).toString("base64"),