@devtion/actions 0.0.0-9c50f66 → 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/README.md +1 -1
- package/dist/index.mjs +238 -235
- package/dist/index.node.js +238 -235
- package/dist/types/src/helpers/security.d.ts +1 -1
- package/dist/types/src/helpers/security.d.ts.map +1 -1
- package/dist/types/src/helpers/storage.d.ts +5 -2
- package/dist/types/src/helpers/storage.d.ts.map +1 -1
- package/dist/types/src/helpers/utils.d.ts +19 -19
- package/dist/types/src/helpers/utils.d.ts.map +1 -1
- package/dist/types/src/helpers/vm.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +2 -2
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/package.json +2 -6
- package/src/helpers/constants.ts +1 -1
- package/src/helpers/functions.ts +1 -1
- package/src/helpers/security.ts +11 -10
- package/src/helpers/services.ts +3 -3
- package/src/helpers/storage.ts +15 -3
- package/src/helpers/utils.ts +262 -268
- package/src/helpers/vm.ts +11 -5
- package/src/index.ts +2 -2
- package/src/types/index.ts +11 -7
package/README.md
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @p0tion/actions
|
|
3
|
-
* @version 1.0
|
|
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 {
|
|
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';
|
|
@@ -340,7 +339,7 @@ const commonTerms = {
|
|
|
340
339
|
finalizeCircuit: "finalizeCircuit",
|
|
341
340
|
finalizeCeremony: "finalizeCeremony",
|
|
342
341
|
downloadCircuitArtifacts: "downloadCircuitArtifacts",
|
|
343
|
-
transferObject: "transferObject"
|
|
342
|
+
transferObject: "transferObject"
|
|
344
343
|
}
|
|
345
344
|
};
|
|
346
345
|
|
|
@@ -691,11 +690,15 @@ const getChunksAndPreSignedUrls = async (cloudFunctions, bucketName, objectKey,
|
|
|
691
690
|
* @param cloudFunctions <Functions> - the Firebase Cloud Functions service instance.
|
|
692
691
|
* @param ceremonyId <string> - the unique identifier of the ceremony.
|
|
693
692
|
* @param alreadyUploadedChunks Array<ETagWithPartNumber> - the temporary information about the already uploaded chunks.
|
|
693
|
+
* @param logger <GenericBar> - an optional logger to show progress.
|
|
694
694
|
* @returns <Promise<Array<ETagWithPartNumber>>> - the completed (uploaded) chunks information.
|
|
695
695
|
*/
|
|
696
|
-
const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks) => {
|
|
696
|
+
const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks, logger) => {
|
|
697
697
|
// Keep track of uploaded chunks.
|
|
698
698
|
const uploadedChunks = alreadyUploadedChunks || [];
|
|
699
|
+
// if we were passed a logger, start it
|
|
700
|
+
if (logger)
|
|
701
|
+
logger.start(chunksWithUrls.length, 0);
|
|
699
702
|
// Loop through remaining chunks.
|
|
700
703
|
for (let i = alreadyUploadedChunks ? alreadyUploadedChunks.length : 0; i < chunksWithUrls.length; i += 1) {
|
|
701
704
|
// Consume the pre-signed url to upload the chunk.
|
|
@@ -727,6 +730,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
|
|
|
727
730
|
// nb. this must be done only when contributing (not finalizing).
|
|
728
731
|
if (!!ceremonyId && !!cloudFunctions)
|
|
729
732
|
await temporaryStoreCurrentContributionUploadedChunkData(cloudFunctions, ceremonyId, chunk);
|
|
733
|
+
// increment the count on the logger
|
|
734
|
+
if (logger)
|
|
735
|
+
logger.increment();
|
|
730
736
|
}
|
|
731
737
|
return uploadedChunks;
|
|
732
738
|
};
|
|
@@ -747,8 +753,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
|
|
|
747
753
|
* @param configStreamChunkSize <number> - size of each chunk into which the artifact is going to be splitted (nb. will be converted in MB).
|
|
748
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).
|
|
749
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.
|
|
750
757
|
*/
|
|
751
|
-
const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload) => {
|
|
758
|
+
const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload, logger) => {
|
|
752
759
|
// The unique identifier of the multi-part upload.
|
|
753
760
|
let multiPartUploadId = "";
|
|
754
761
|
// The list of already uploaded chunks.
|
|
@@ -772,7 +779,7 @@ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFileP
|
|
|
772
779
|
const chunksWithUrlsZkey = await getChunksAndPreSignedUrls(cloudFunctions, bucketName, objectKey, localFilePath, multiPartUploadId, configStreamChunkSize, ceremonyId);
|
|
773
780
|
// Step (2).
|
|
774
781
|
const partNumbersAndETagsZkey = await uploadParts(chunksWithUrlsZkey, mime.lookup(localFilePath), // content-type.
|
|
775
|
-
cloudFunctions, ceremonyId, alreadyUploadedChunks);
|
|
782
|
+
cloudFunctions, ceremonyId, alreadyUploadedChunks, logger);
|
|
776
783
|
// Step (3).
|
|
777
784
|
await completeMultiPartUpload(cloudFunctions, bucketName, objectKey, multiPartUploadId, partNumbersAndETagsZkey, ceremonyId);
|
|
778
785
|
};
|
|
@@ -1044,207 +1051,22 @@ const compareHashes = async (path1, path2) => {
|
|
|
1044
1051
|
};
|
|
1045
1052
|
|
|
1046
1053
|
/**
|
|
1047
|
-
*
|
|
1048
|
-
* @
|
|
1049
|
-
* @
|
|
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
|
|
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.
|
|
1052
1057
|
*/
|
|
1053
|
-
const
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
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
|
-
const r1csPath = artifacts.r1csStoragePath;
|
|
1095
|
-
const wasmPath = artifacts.wasmStoragePath;
|
|
1096
|
-
// where we storing the r1cs downloaded
|
|
1097
|
-
const localR1csPath = `./${circuitData.name}.r1cs`;
|
|
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({ region: artifacts.region });
|
|
1102
|
-
try {
|
|
1103
|
-
await s3.send(new HeadObjectCommand({
|
|
1104
|
-
Bucket: artifacts.bucket,
|
|
1105
|
-
Key: r1csPath
|
|
1106
|
-
}));
|
|
1107
|
-
}
|
|
1108
|
-
catch (error) {
|
|
1109
|
-
throw new Error(`The r1cs file (${r1csPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`);
|
|
1110
|
-
}
|
|
1111
|
-
try {
|
|
1112
|
-
await s3.send(new HeadObjectCommand({
|
|
1113
|
-
Bucket: artifacts.bucket,
|
|
1114
|
-
Key: wasmPath
|
|
1115
|
-
}));
|
|
1116
|
-
}
|
|
1117
|
-
catch (error) {
|
|
1118
|
-
throw new Error(`The wasm file (${wasmPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`);
|
|
1119
|
-
}
|
|
1120
|
-
// download the r1cs to extract the metadata
|
|
1121
|
-
const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath });
|
|
1122
|
-
const response = await s3.send(command);
|
|
1123
|
-
const streamPipeline = promisify(pipeline);
|
|
1124
|
-
if (response.$metadata.httpStatusCode !== 200)
|
|
1125
|
-
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.");
|
|
1126
|
-
if (response.Body instanceof Readable)
|
|
1127
|
-
await streamPipeline(response.Body, fs.createWriteStream(localR1csPath));
|
|
1128
|
-
// extract the metadata from the r1cs
|
|
1129
|
-
const metadata = getR1CSInfo(localR1csPath);
|
|
1130
|
-
// validate that the circuit hash and template links are valid
|
|
1131
|
-
const template = circuitData.template;
|
|
1132
|
-
const URLMatch = template.source.match(urlPattern);
|
|
1133
|
-
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
1134
|
-
throw new Error("You should provide the URL to the circuits templates on GitHub.");
|
|
1135
|
-
const hashMatch = template.commitHash.match(commitHashPattern);
|
|
1136
|
-
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
1137
|
-
throw new Error("You should provide a valid commit hash of the circuit templates.");
|
|
1138
|
-
// calculate the hash of the r1cs file
|
|
1139
|
-
const r1csBlake2bHash = await blake512FromPath(localR1csPath);
|
|
1140
|
-
const circuitPrefix = extractPrefix(circuitData.name);
|
|
1141
|
-
// filenames
|
|
1142
|
-
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
|
|
1143
|
-
const r1csCompleteFilename = `${circuitData.name}.r1cs`;
|
|
1144
|
-
const wasmCompleteFilename = `${circuitData.name}.wasm`;
|
|
1145
|
-
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
|
|
1146
|
-
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
|
|
1147
|
-
// storage paths
|
|
1148
|
-
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
|
|
1149
|
-
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
|
|
1150
|
-
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
|
|
1151
|
-
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
|
|
1152
|
-
const files = {
|
|
1153
|
-
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
1154
|
-
r1csFilename: r1csCompleteFilename,
|
|
1155
|
-
wasmFilename: wasmCompleteFilename,
|
|
1156
|
-
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
1157
|
-
potStoragePath: potStorageFilePath,
|
|
1158
|
-
r1csStoragePath: r1csStorageFilePath,
|
|
1159
|
-
wasmStoragePath: wasmStorageFilePath,
|
|
1160
|
-
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
1161
|
-
r1csBlake2bHash: r1csBlake2bHash
|
|
1162
|
-
};
|
|
1163
|
-
// validate that the compiler hash is a valid hash
|
|
1164
|
-
const compiler = circuitData.compiler;
|
|
1165
|
-
const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
|
|
1166
|
-
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
1167
|
-
throw new Error("You should provide a valid commit hash of the circuit compiler.");
|
|
1168
|
-
// validate that the verification options are valid
|
|
1169
|
-
const verification = circuitData.verification;
|
|
1170
|
-
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
1171
|
-
throw new Error("Please enter a valid verification mechanism: either CF or VM");
|
|
1172
|
-
// @todo VM parameters verification
|
|
1173
|
-
// if (verification['cfOrVM'] === "VM") {}
|
|
1174
|
-
// check that the timeout is provided for the correct configuration
|
|
1175
|
-
let dynamicThreshold;
|
|
1176
|
-
let fixedTimeWindow;
|
|
1177
|
-
let circuit = {};
|
|
1178
|
-
if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
|
|
1179
|
-
if (circuitData.dynamicThreshold <= 0)
|
|
1180
|
-
throw new Error("The dynamic threshold should be > 0.");
|
|
1181
|
-
dynamicThreshold = circuitData.dynamicThreshold;
|
|
1182
|
-
// the Circuit data for the ceremony setup
|
|
1183
|
-
circuit = {
|
|
1184
|
-
name: circuitData.name,
|
|
1185
|
-
description: circuitData.description,
|
|
1186
|
-
prefix: circuitPrefix,
|
|
1187
|
-
sequencePosition: i + 1,
|
|
1188
|
-
metadata: metadata,
|
|
1189
|
-
files: files,
|
|
1190
|
-
template: template,
|
|
1191
|
-
compiler: compiler,
|
|
1192
|
-
verification: verification,
|
|
1193
|
-
dynamicThreshold: dynamicThreshold,
|
|
1194
|
-
avgTimings: {
|
|
1195
|
-
contributionComputation: 0,
|
|
1196
|
-
fullContribution: 0,
|
|
1197
|
-
verifyCloudFunction: 0
|
|
1198
|
-
},
|
|
1199
|
-
};
|
|
1200
|
-
}
|
|
1201
|
-
if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
|
|
1202
|
-
if (circuitData.fixedTimeWindow <= 0)
|
|
1203
|
-
throw new Error("The fixed time window threshold should be > 0.");
|
|
1204
|
-
fixedTimeWindow = circuitData.fixedTimeWindow;
|
|
1205
|
-
// the Circuit data for the ceremony setup
|
|
1206
|
-
circuit = {
|
|
1207
|
-
name: circuitData.name,
|
|
1208
|
-
description: circuitData.description,
|
|
1209
|
-
prefix: circuitPrefix,
|
|
1210
|
-
sequencePosition: i + 1,
|
|
1211
|
-
metadata: metadata,
|
|
1212
|
-
files: files,
|
|
1213
|
-
template: template,
|
|
1214
|
-
compiler: compiler,
|
|
1215
|
-
verification: verification,
|
|
1216
|
-
fixedTimeWindow: fixedTimeWindow,
|
|
1217
|
-
avgTimings: {
|
|
1218
|
-
contributionComputation: 0,
|
|
1219
|
-
fullContribution: 0,
|
|
1220
|
-
verifyCloudFunction: 0
|
|
1221
|
-
},
|
|
1222
|
-
};
|
|
1223
|
-
}
|
|
1224
|
-
circuits.push(circuit);
|
|
1225
|
-
// remove the local r1cs download (if used for verifying the config only vs setup)
|
|
1226
|
-
if (cleanup)
|
|
1227
|
-
fs.unlinkSync(localR1csPath);
|
|
1228
|
-
}
|
|
1229
|
-
const setupData = {
|
|
1230
|
-
ceremonyInputData: {
|
|
1231
|
-
title: data.title,
|
|
1232
|
-
description: data.description,
|
|
1233
|
-
startDate: startDate.valueOf(),
|
|
1234
|
-
endDate: endDate.valueOf(),
|
|
1235
|
-
timeoutMechanismType: data.timeoutMechanismType,
|
|
1236
|
-
penalty: data.penalty
|
|
1237
|
-
},
|
|
1238
|
-
ceremonyPrefix: extractPrefix(data.title),
|
|
1239
|
-
circuits: circuits,
|
|
1240
|
-
circuitArtifacts: circuitArtifacts
|
|
1241
|
-
};
|
|
1242
|
-
return setupData;
|
|
1243
|
-
}
|
|
1244
|
-
catch (error) {
|
|
1245
|
-
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
|
|
1246
|
-
}
|
|
1247
|
-
};
|
|
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();
|
|
1248
1070
|
/**
|
|
1249
1071
|
* Extract data from a R1CS metadata file generated with a custom file-based logger.
|
|
1250
1072
|
* @notice useful for extracting metadata circuits contained in the generated file using a logger
|
|
@@ -1301,17 +1123,6 @@ const formatZkeyIndex = (progress) => {
|
|
|
1301
1123
|
* @returns <number> - the amount of powers.
|
|
1302
1124
|
*/
|
|
1303
1125
|
const extractPoTFromFilename = (potCompleteFilename) => Number(potCompleteFilename.split("_").pop()?.split(".").at(0));
|
|
1304
|
-
/**
|
|
1305
|
-
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
1306
|
-
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
1307
|
-
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
1308
|
-
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
1309
|
-
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
1310
|
-
* @returns <string> - the resulting prefix.
|
|
1311
|
-
*/
|
|
1312
|
-
const extractPrefix = (str) =>
|
|
1313
|
-
// eslint-disable-next-line no-useless-escape
|
|
1314
|
-
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
|
|
1315
1126
|
/**
|
|
1316
1127
|
* Automate the generation of an entropy for a contribution.
|
|
1317
1128
|
* @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
|
|
@@ -1378,7 +1189,9 @@ const getContributionsValidityForContributor = async (firestoreDatabase, circuit
|
|
|
1378
1189
|
* @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
|
|
1379
1190
|
* @returns <string> - the public attestation preamble.
|
|
1380
1191
|
*/
|
|
1381
|
-
const getPublicAttestationPreambleForContributor = (contributorIdentifier, ceremonyName, isFinalizing) => `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}
|
|
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:`;
|
|
1382
1195
|
/**
|
|
1383
1196
|
* Check and prepare public attestation for the contributor made only of its valid contributions.
|
|
1384
1197
|
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
|
|
@@ -1581,11 +1394,193 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1581
1394
|
}
|
|
1582
1395
|
};
|
|
1583
1396
|
/**
|
|
1584
|
-
*
|
|
1585
|
-
* @
|
|
1586
|
-
* @
|
|
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
|
|
1587
1402
|
*/
|
|
1588
|
-
const
|
|
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
|
+
};
|
|
1589
1584
|
|
|
1590
1585
|
/**
|
|
1591
1586
|
* Verify that a zKey is valid
|
|
@@ -1834,7 +1829,7 @@ const getFirestoreDatabase = (app) => getFirestore(app);
|
|
|
1834
1829
|
* @param app <FirebaseApp> - the Firebase application.
|
|
1835
1830
|
* @returns <Functions> - the Cloud Functions associated to the application.
|
|
1836
1831
|
*/
|
|
1837
|
-
const getFirebaseFunctions = (app) => getFunctions(app,
|
|
1832
|
+
const getFirebaseFunctions = (app) => getFunctions(app, "europe-west1");
|
|
1838
1833
|
/**
|
|
1839
1834
|
* Retrieve the configuration variables for the AWS services (S3, EC2).
|
|
1840
1835
|
* @returns <AWSVariables> - the values of the AWS services configuration variables.
|
|
@@ -1843,14 +1838,14 @@ const getAWSVariables = () => {
|
|
|
1843
1838
|
if (!process.env.AWS_ACCESS_KEY_ID ||
|
|
1844
1839
|
!process.env.AWS_SECRET_ACCESS_KEY ||
|
|
1845
1840
|
!process.env.AWS_REGION ||
|
|
1846
|
-
!process.env.
|
|
1841
|
+
!process.env.AWS_INSTANCE_PROFILE_ARN ||
|
|
1847
1842
|
!process.env.AWS_AMI_ID)
|
|
1848
1843
|
throw new Error("Could not retrieve the AWS environment variables. Please, verify your environment configuration and retry");
|
|
1849
1844
|
return {
|
|
1850
1845
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
1851
1846
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
1852
1847
|
region: process.env.AWS_REGION || "us-east-1",
|
|
1853
|
-
|
|
1848
|
+
instanceProfileArn: process.env.AWS_INSTANCE_PROFILE_ARN,
|
|
1854
1849
|
amiId: process.env.AWS_AMI_ID
|
|
1855
1850
|
};
|
|
1856
1851
|
};
|
|
@@ -2103,7 +2098,8 @@ const getGitHubStats = async (user) => {
|
|
|
2103
2098
|
following: jsonData.following,
|
|
2104
2099
|
followers: jsonData.followers,
|
|
2105
2100
|
publicRepos: jsonData.public_repos,
|
|
2106
|
-
avatarUrl: jsonData.avatar_url
|
|
2101
|
+
avatarUrl: jsonData.avatar_url,
|
|
2102
|
+
age: jsonData.created_at
|
|
2107
2103
|
};
|
|
2108
2104
|
return data;
|
|
2109
2105
|
};
|
|
@@ -2115,20 +2111,21 @@ const getGitHubStats = async (user) => {
|
|
|
2115
2111
|
* @param minimumAmountOfPublicRepos <number> The minimum amount of public repos the user should have
|
|
2116
2112
|
* @returns <any> Return the avatar URL of the user if the user is reputable, false otherwise
|
|
2117
2113
|
*/
|
|
2118
|
-
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos) => {
|
|
2114
|
+
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos, minimumAge) => {
|
|
2119
2115
|
if (!process.env.GITHUB_ACCESS_TOKEN)
|
|
2120
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.");
|
|
2121
|
-
const { following, followers, publicRepos, avatarUrl } = await getGitHubStats(userLogin);
|
|
2117
|
+
const { following, followers, publicRepos, avatarUrl, age } = await getGitHubStats(userLogin);
|
|
2122
2118
|
if (following < minimumAmountOfFollowing ||
|
|
2123
2119
|
publicRepos < minimumAmountOfPublicRepos ||
|
|
2124
|
-
followers < minimumAmountOfFollowers
|
|
2120
|
+
followers < minimumAmountOfFollowers ||
|
|
2121
|
+
new Date(age) > new Date(Date.now() - minimumAge))
|
|
2125
2122
|
return {
|
|
2126
2123
|
reputable: false,
|
|
2127
2124
|
avatarUrl: ""
|
|
2128
2125
|
};
|
|
2129
2126
|
return {
|
|
2130
2127
|
reputable: true,
|
|
2131
|
-
avatarUrl
|
|
2128
|
+
avatarUrl
|
|
2132
2129
|
};
|
|
2133
2130
|
};
|
|
2134
2131
|
|
|
@@ -2337,8 +2334,13 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2337
2334
|
// eslint-disable-next-line no-template-curly-in-string
|
|
2338
2335
|
"touch ${MARKER_FILE}",
|
|
2339
2336
|
"sudo yum update -y",
|
|
2340
|
-
"curl -
|
|
2341
|
-
"
|
|
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",
|
|
2342
2344
|
"npm install -g snarkjs",
|
|
2343
2345
|
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
|
|
2344
2346
|
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
|
|
@@ -2357,6 +2359,7 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2357
2359
|
* @returns Array<string> - the list of commands for contribution verification.
|
|
2358
2360
|
*/
|
|
2359
2361
|
const vmContributionVerificationCommand = (bucketName, lastZkeyStoragePath, verificationTranscriptStoragePathAndFilename) => [
|
|
2362
|
+
`source /etc/profile`,
|
|
2360
2363
|
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
|
|
2361
2364
|
`snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
|
|
2362
2365
|
`aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
|
|
@@ -2383,7 +2386,7 @@ const computeDiskSizeForVM = (zKeySizeInBytes, pot) => Math.ceil(2 * convertByte
|
|
|
2383
2386
|
*/
|
|
2384
2387
|
const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskType) => {
|
|
2385
2388
|
// Get the AWS variables.
|
|
2386
|
-
const { amiId,
|
|
2389
|
+
const { amiId, instanceProfileArn } = getAWSVariables();
|
|
2387
2390
|
// Parametrize the VM EC2 instance.
|
|
2388
2391
|
const params = {
|
|
2389
2392
|
ImageId: amiId,
|
|
@@ -2392,7 +2395,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
|
|
|
2392
2395
|
MinCount: 1,
|
|
2393
2396
|
// nb. to find this: iam -> roles -> role_name.
|
|
2394
2397
|
IamInstanceProfile: {
|
|
2395
|
-
Arn:
|
|
2398
|
+
Arn: instanceProfileArn
|
|
2396
2399
|
},
|
|
2397
2400
|
// nb. for running commands at the startup.
|
|
2398
2401
|
UserData: Buffer.from(commands.join("\n")).toString("base64"),
|