@devtion/actions 0.0.0-9614e0c → 0.0.0-a9e4cd4
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 +243 -229
- package/dist/index.node.js +241 -227
- 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 +3 -3
- 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 +268 -264
- package/src/helpers/vm.ts +11 -5
- package/src/index.ts +2 -2
- package/src/types/index.ts +12 -8
package/README.md
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @module @
|
|
3
|
-
* @version 1.
|
|
2
|
+
* @module @p0tion/actions
|
|
3
|
+
* @version 1.1.1
|
|
4
4
|
* @file A set of actions and helpers for CLI commands
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -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,199 +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
|
-
// where we storing the r1cs downloaded
|
|
1095
|
-
const localR1csPath = `./${circuitData.name}.r1cs`;
|
|
1096
|
-
// where we storing the wasm downloaded
|
|
1097
|
-
const localWasmPath = `./${circuitData.name}.wasm`;
|
|
1098
|
-
// check that the artifacts exist in S3
|
|
1099
|
-
// we don't need any privileges to download this
|
|
1100
|
-
// just the correct region
|
|
1101
|
-
const s3 = new S3Client({
|
|
1102
|
-
region: artifacts.region,
|
|
1103
|
-
credentials: undefined
|
|
1104
|
-
});
|
|
1105
|
-
// download the r1cs to extract the metadata
|
|
1106
|
-
const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath });
|
|
1107
|
-
const response = await s3.send(command);
|
|
1108
|
-
const streamPipeline = promisify(pipeline);
|
|
1109
|
-
if (response.$metadata.httpStatusCode !== 200)
|
|
1110
|
-
throw new Error(`There was an error while trying to download the r1cs file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
|
|
1111
|
-
if (response.Body instanceof Readable)
|
|
1112
|
-
await streamPipeline(response.Body, fs.createWriteStream(localR1csPath));
|
|
1113
|
-
// extract the metadata from the r1cs
|
|
1114
|
-
const metadata = getR1CSInfo(localR1csPath);
|
|
1115
|
-
// download wasm too to ensure it's available
|
|
1116
|
-
const wasmCommand = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.wasmStoragePath });
|
|
1117
|
-
const wasmResponse = await s3.send(wasmCommand);
|
|
1118
|
-
if (wasmResponse.$metadata.httpStatusCode !== 200)
|
|
1119
|
-
throw new Error(`There was an error while trying to download the wasm file for circuit ${circuitData.name}. Please check that the file has the correct permissions (public) set.`);
|
|
1120
|
-
if (wasmResponse.Body instanceof Readable)
|
|
1121
|
-
await streamPipeline(wasmResponse.Body, fs.createWriteStream(localWasmPath));
|
|
1122
|
-
// validate that the circuit hash and template links are valid
|
|
1123
|
-
const template = circuitData.template;
|
|
1124
|
-
const URLMatch = template.source.match(urlPattern);
|
|
1125
|
-
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
1126
|
-
throw new Error("You should provide the URL to the circuits templates on GitHub.");
|
|
1127
|
-
const hashMatch = template.commitHash.match(commitHashPattern);
|
|
1128
|
-
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
1129
|
-
throw new Error("You should provide a valid commit hash of the circuit templates.");
|
|
1130
|
-
// calculate the hash of the r1cs file
|
|
1131
|
-
const r1csBlake2bHash = await blake512FromPath(localR1csPath);
|
|
1132
|
-
const circuitPrefix = extractPrefix(circuitData.name);
|
|
1133
|
-
// filenames
|
|
1134
|
-
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
|
|
1135
|
-
const r1csCompleteFilename = `${circuitData.name}.r1cs`;
|
|
1136
|
-
const wasmCompleteFilename = `${circuitData.name}.wasm`;
|
|
1137
|
-
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
|
|
1138
|
-
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
|
|
1139
|
-
// storage paths
|
|
1140
|
-
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
|
|
1141
|
-
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
|
|
1142
|
-
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
|
|
1143
|
-
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
|
|
1144
|
-
const files = {
|
|
1145
|
-
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
1146
|
-
r1csFilename: r1csCompleteFilename,
|
|
1147
|
-
wasmFilename: wasmCompleteFilename,
|
|
1148
|
-
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
1149
|
-
potStoragePath: potStorageFilePath,
|
|
1150
|
-
r1csStoragePath: r1csStorageFilePath,
|
|
1151
|
-
wasmStoragePath: wasmStorageFilePath,
|
|
1152
|
-
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
1153
|
-
r1csBlake2bHash: r1csBlake2bHash
|
|
1154
|
-
};
|
|
1155
|
-
// validate that the compiler hash is a valid hash
|
|
1156
|
-
const compiler = circuitData.compiler;
|
|
1157
|
-
const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
|
|
1158
|
-
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
1159
|
-
throw new Error("You should provide a valid commit hash of the circuit compiler.");
|
|
1160
|
-
// validate that the verification options are valid
|
|
1161
|
-
const verification = circuitData.verification;
|
|
1162
|
-
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
1163
|
-
throw new Error("Please enter a valid verification mechanism: either CF or VM");
|
|
1164
|
-
// @todo VM parameters verification
|
|
1165
|
-
// if (verification['cfOrVM'] === "VM") {}
|
|
1166
|
-
// check that the timeout is provided for the correct configuration
|
|
1167
|
-
let dynamicThreshold;
|
|
1168
|
-
let fixedTimeWindow;
|
|
1169
|
-
let circuit = {};
|
|
1170
|
-
if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
|
|
1171
|
-
if (circuitData.dynamicThreshold <= 0)
|
|
1172
|
-
throw new Error("The dynamic threshold should be > 0.");
|
|
1173
|
-
dynamicThreshold = circuitData.dynamicThreshold;
|
|
1174
|
-
// the Circuit data for the ceremony setup
|
|
1175
|
-
circuit = {
|
|
1176
|
-
name: circuitData.name,
|
|
1177
|
-
description: circuitData.description,
|
|
1178
|
-
prefix: circuitPrefix,
|
|
1179
|
-
sequencePosition: i + 1,
|
|
1180
|
-
metadata: metadata,
|
|
1181
|
-
files: files,
|
|
1182
|
-
template: template,
|
|
1183
|
-
compiler: compiler,
|
|
1184
|
-
verification: verification,
|
|
1185
|
-
dynamicThreshold: dynamicThreshold,
|
|
1186
|
-
avgTimings: {
|
|
1187
|
-
contributionComputation: 0,
|
|
1188
|
-
fullContribution: 0,
|
|
1189
|
-
verifyCloudFunction: 0
|
|
1190
|
-
},
|
|
1191
|
-
};
|
|
1192
|
-
}
|
|
1193
|
-
if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
|
|
1194
|
-
if (circuitData.fixedTimeWindow <= 0)
|
|
1195
|
-
throw new Error("The fixed time window threshold should be > 0.");
|
|
1196
|
-
fixedTimeWindow = circuitData.fixedTimeWindow;
|
|
1197
|
-
// the Circuit data for the ceremony setup
|
|
1198
|
-
circuit = {
|
|
1199
|
-
name: circuitData.name,
|
|
1200
|
-
description: circuitData.description,
|
|
1201
|
-
prefix: circuitPrefix,
|
|
1202
|
-
sequencePosition: i + 1,
|
|
1203
|
-
metadata: metadata,
|
|
1204
|
-
files: files,
|
|
1205
|
-
template: template,
|
|
1206
|
-
compiler: compiler,
|
|
1207
|
-
verification: verification,
|
|
1208
|
-
fixedTimeWindow: fixedTimeWindow,
|
|
1209
|
-
avgTimings: {
|
|
1210
|
-
contributionComputation: 0,
|
|
1211
|
-
fullContribution: 0,
|
|
1212
|
-
verifyCloudFunction: 0
|
|
1213
|
-
},
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
circuits.push(circuit);
|
|
1217
|
-
// remove the local r1cs download (if used for verifying the config only vs setup)
|
|
1218
|
-
if (cleanup)
|
|
1219
|
-
fs.unlinkSync(localR1csPath);
|
|
1220
|
-
}
|
|
1221
|
-
const setupData = {
|
|
1222
|
-
ceremonyInputData: {
|
|
1223
|
-
title: data.title,
|
|
1224
|
-
description: data.description,
|
|
1225
|
-
startDate: startDate.valueOf(),
|
|
1226
|
-
endDate: endDate.valueOf(),
|
|
1227
|
-
timeoutMechanismType: data.timeoutMechanismType,
|
|
1228
|
-
penalty: data.penalty
|
|
1229
|
-
},
|
|
1230
|
-
ceremonyPrefix: extractPrefix(data.title),
|
|
1231
|
-
circuits: circuits,
|
|
1232
|
-
circuitArtifacts: circuitArtifacts
|
|
1233
|
-
};
|
|
1234
|
-
return setupData;
|
|
1235
|
-
}
|
|
1236
|
-
catch (error) {
|
|
1237
|
-
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
|
|
1238
|
-
}
|
|
1239
|
-
};
|
|
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();
|
|
1240
1070
|
/**
|
|
1241
1071
|
* Extract data from a R1CS metadata file generated with a custom file-based logger.
|
|
1242
1072
|
* @notice useful for extracting metadata circuits contained in the generated file using a logger
|
|
@@ -1293,17 +1123,6 @@ const formatZkeyIndex = (progress) => {
|
|
|
1293
1123
|
* @returns <number> - the amount of powers.
|
|
1294
1124
|
*/
|
|
1295
1125
|
const extractPoTFromFilename = (potCompleteFilename) => Number(potCompleteFilename.split("_").pop()?.split(".").at(0));
|
|
1296
|
-
/**
|
|
1297
|
-
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
1298
|
-
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
1299
|
-
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
1300
|
-
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
1301
|
-
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
1302
|
-
* @returns <string> - the resulting prefix.
|
|
1303
|
-
*/
|
|
1304
|
-
const extractPrefix = (str) =>
|
|
1305
|
-
// eslint-disable-next-line no-useless-escape
|
|
1306
|
-
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
|
|
1307
1126
|
/**
|
|
1308
1127
|
* Automate the generation of an entropy for a contribution.
|
|
1309
1128
|
* @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
|
|
@@ -1370,7 +1189,9 @@ const getContributionsValidityForContributor = async (firestoreDatabase, circuit
|
|
|
1370
1189
|
* @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
|
|
1371
1190
|
* @returns <string> - the public attestation preamble.
|
|
1372
1191
|
*/
|
|
1373
|
-
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:`;
|
|
1374
1195
|
/**
|
|
1375
1196
|
* Check and prepare public attestation for the contributor made only of its valid contributions.
|
|
1376
1197
|
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
|
|
@@ -1504,8 +1325,9 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1504
1325
|
const numberOfSections = utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, 8));
|
|
1505
1326
|
// Jump to first section.
|
|
1506
1327
|
pointer = 12;
|
|
1328
|
+
let found = false;
|
|
1507
1329
|
// For each section
|
|
1508
|
-
for (let i = 0; i < numberOfSections; i++) {
|
|
1330
|
+
for (let i = 0; i < numberOfSections && !found; i++) {
|
|
1509
1331
|
// Read section type.
|
|
1510
1332
|
const sectionType = utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer));
|
|
1511
1333
|
// Jump to section size.
|
|
@@ -1554,6 +1376,7 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1554
1376
|
labels = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
|
|
1555
1377
|
pointer += 8;
|
|
1556
1378
|
constraints = Number(utils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1379
|
+
found = true;
|
|
1557
1380
|
}
|
|
1558
1381
|
pointer += 8 + Number(sectionSize);
|
|
1559
1382
|
}
|
|
@@ -1573,11 +1396,194 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1573
1396
|
}
|
|
1574
1397
|
};
|
|
1575
1398
|
/**
|
|
1576
|
-
*
|
|
1577
|
-
* @
|
|
1578
|
-
* @
|
|
1399
|
+
* Parse and validate that the ceremony configuration is correct
|
|
1400
|
+
* @notice this does not upload any files to storage
|
|
1401
|
+
* @param path <string> - the path to the configuration file
|
|
1402
|
+
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
1403
|
+
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
1579
1404
|
*/
|
|
1580
|
-
const
|
|
1405
|
+
const parseCeremonyFile = async (path, cleanup = false) => {
|
|
1406
|
+
// check that the path exists
|
|
1407
|
+
if (!fs.existsSync(path))
|
|
1408
|
+
throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
|
|
1409
|
+
try {
|
|
1410
|
+
// read the data
|
|
1411
|
+
const data = JSON.parse(fs.readFileSync(path).toString());
|
|
1412
|
+
// verify that the data is correct
|
|
1413
|
+
if (data.timeoutMechanismType !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ &&
|
|
1414
|
+
data.timeoutMechanismType !== "FIXED" /* CeremonyTimeoutType.FIXED */)
|
|
1415
|
+
throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
|
|
1416
|
+
// validate that we have at least 1 circuit input data
|
|
1417
|
+
if (!data.circuits || data.circuits.length === 0)
|
|
1418
|
+
throw new Error("You need to provide the data for at least 1 circuit.");
|
|
1419
|
+
// validate that the end date is in the future
|
|
1420
|
+
let endDate;
|
|
1421
|
+
let startDate;
|
|
1422
|
+
try {
|
|
1423
|
+
endDate = new Date(data.endDate);
|
|
1424
|
+
startDate = new Date(data.startDate);
|
|
1425
|
+
}
|
|
1426
|
+
catch (error) {
|
|
1427
|
+
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
|
|
1428
|
+
}
|
|
1429
|
+
if (endDate <= startDate)
|
|
1430
|
+
throw new Error("The end date should be greater than the start date.");
|
|
1431
|
+
const currentDate = new Date();
|
|
1432
|
+
if (endDate <= currentDate || startDate <= currentDate)
|
|
1433
|
+
throw new Error("The start and end dates should be in the future.");
|
|
1434
|
+
// validate penalty
|
|
1435
|
+
if (data.penalty <= 0)
|
|
1436
|
+
throw new Error("The penalty should be greater than zero.");
|
|
1437
|
+
const circuits = [];
|
|
1438
|
+
const urlPattern = /(https?:\/\/[^\s]+)/g;
|
|
1439
|
+
const commitHashPattern = /^[a-f0-9]{40}$/i;
|
|
1440
|
+
const circuitArtifacts = [];
|
|
1441
|
+
for (let i = 0; i < data.circuits.length; i++) {
|
|
1442
|
+
const circuitData = data.circuits[i];
|
|
1443
|
+
const { artifacts } = circuitData;
|
|
1444
|
+
circuitArtifacts.push({
|
|
1445
|
+
artifacts
|
|
1446
|
+
});
|
|
1447
|
+
// where we storing the r1cs downloaded
|
|
1448
|
+
const localR1csPath = `./${circuitData.name}.r1cs`;
|
|
1449
|
+
// where we storing the wasm downloaded
|
|
1450
|
+
const localWasmPath = `./${circuitData.name}.wasm`;
|
|
1451
|
+
// download the r1cs to extract the metadata
|
|
1452
|
+
const streamPipeline = promisify(pipeline);
|
|
1453
|
+
// Make the call.
|
|
1454
|
+
const responseR1CS = await fetch(artifacts.r1csStoragePath);
|
|
1455
|
+
// Handle errors.
|
|
1456
|
+
if (!responseR1CS.ok && responseR1CS.status !== 200)
|
|
1457
|
+
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.`);
|
|
1458
|
+
await streamPipeline(responseR1CS.body, createWriteStream(localR1csPath));
|
|
1459
|
+
// Write the file locally
|
|
1460
|
+
// extract the metadata from the r1cs
|
|
1461
|
+
const metadata = getR1CSInfo(localR1csPath);
|
|
1462
|
+
// download wasm too to ensure it's available
|
|
1463
|
+
const responseWASM = await fetch(artifacts.wasmStoragePath);
|
|
1464
|
+
if (!responseWASM.ok && responseWASM.status !== 200)
|
|
1465
|
+
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.`);
|
|
1466
|
+
await streamPipeline(responseWASM.body, createWriteStream(localWasmPath));
|
|
1467
|
+
// validate that the circuit hash and template links are valid
|
|
1468
|
+
const { template } = circuitData;
|
|
1469
|
+
const URLMatch = template.source.match(urlPattern);
|
|
1470
|
+
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
1471
|
+
throw new Error("You should provide the URL to the circuits templates on GitHub.");
|
|
1472
|
+
const hashMatch = template.commitHash.match(commitHashPattern);
|
|
1473
|
+
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
1474
|
+
throw new Error("You should provide a valid commit hash of the circuit templates.");
|
|
1475
|
+
// calculate the hash of the r1cs file
|
|
1476
|
+
const r1csBlake2bHash = await blake512FromPath(localR1csPath);
|
|
1477
|
+
const circuitPrefix = extractPrefix(circuitData.name);
|
|
1478
|
+
// filenames
|
|
1479
|
+
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
|
|
1480
|
+
const r1csCompleteFilename = `${circuitData.name}.r1cs`;
|
|
1481
|
+
const wasmCompleteFilename = `${circuitData.name}.wasm`;
|
|
1482
|
+
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
|
|
1483
|
+
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
|
|
1484
|
+
// storage paths
|
|
1485
|
+
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
|
|
1486
|
+
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
|
|
1487
|
+
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
|
|
1488
|
+
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
|
|
1489
|
+
const files = {
|
|
1490
|
+
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
1491
|
+
r1csFilename: r1csCompleteFilename,
|
|
1492
|
+
wasmFilename: wasmCompleteFilename,
|
|
1493
|
+
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
1494
|
+
potStoragePath: potStorageFilePath,
|
|
1495
|
+
r1csStoragePath: r1csStorageFilePath,
|
|
1496
|
+
wasmStoragePath: wasmStorageFilePath,
|
|
1497
|
+
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
1498
|
+
r1csBlake2bHash
|
|
1499
|
+
};
|
|
1500
|
+
// validate that the compiler hash is a valid hash
|
|
1501
|
+
const { compiler } = circuitData;
|
|
1502
|
+
const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
|
|
1503
|
+
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
1504
|
+
throw new Error("You should provide a valid commit hash of the circuit compiler.");
|
|
1505
|
+
// validate that the verification options are valid
|
|
1506
|
+
const { verification } = circuitData;
|
|
1507
|
+
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
1508
|
+
throw new Error("Please enter a valid verification mechanism: either CF or VM");
|
|
1509
|
+
// @todo VM parameters verification
|
|
1510
|
+
// if (verification['cfOrVM'] === "VM") {}
|
|
1511
|
+
// check that the timeout is provided for the correct configuration
|
|
1512
|
+
let dynamicThreshold;
|
|
1513
|
+
let fixedTimeWindow;
|
|
1514
|
+
let circuit = {};
|
|
1515
|
+
if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
|
|
1516
|
+
if (circuitData.dynamicThreshold <= 0)
|
|
1517
|
+
throw new Error("The dynamic threshold should be > 0.");
|
|
1518
|
+
dynamicThreshold = circuitData.dynamicThreshold;
|
|
1519
|
+
// the Circuit data for the ceremony setup
|
|
1520
|
+
circuit = {
|
|
1521
|
+
name: circuitData.name,
|
|
1522
|
+
description: circuitData.description,
|
|
1523
|
+
prefix: circuitPrefix,
|
|
1524
|
+
sequencePosition: i + 1,
|
|
1525
|
+
metadata,
|
|
1526
|
+
files,
|
|
1527
|
+
template,
|
|
1528
|
+
compiler,
|
|
1529
|
+
verification,
|
|
1530
|
+
dynamicThreshold,
|
|
1531
|
+
avgTimings: {
|
|
1532
|
+
contributionComputation: 0,
|
|
1533
|
+
fullContribution: 0,
|
|
1534
|
+
verifyCloudFunction: 0
|
|
1535
|
+
}
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1538
|
+
if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
|
|
1539
|
+
if (circuitData.fixedTimeWindow <= 0)
|
|
1540
|
+
throw new Error("The fixed time window threshold should be > 0.");
|
|
1541
|
+
fixedTimeWindow = circuitData.fixedTimeWindow;
|
|
1542
|
+
// the Circuit data for the ceremony setup
|
|
1543
|
+
circuit = {
|
|
1544
|
+
name: circuitData.name,
|
|
1545
|
+
description: circuitData.description,
|
|
1546
|
+
prefix: circuitPrefix,
|
|
1547
|
+
sequencePosition: i + 1,
|
|
1548
|
+
metadata,
|
|
1549
|
+
files,
|
|
1550
|
+
template,
|
|
1551
|
+
compiler,
|
|
1552
|
+
verification,
|
|
1553
|
+
fixedTimeWindow,
|
|
1554
|
+
avgTimings: {
|
|
1555
|
+
contributionComputation: 0,
|
|
1556
|
+
fullContribution: 0,
|
|
1557
|
+
verifyCloudFunction: 0
|
|
1558
|
+
}
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
circuits.push(circuit);
|
|
1562
|
+
// remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
|
|
1563
|
+
if (cleanup) {
|
|
1564
|
+
fs.unlinkSync(localR1csPath);
|
|
1565
|
+
fs.unlinkSync(localWasmPath);
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
const setupData = {
|
|
1569
|
+
ceremonyInputData: {
|
|
1570
|
+
title: data.title,
|
|
1571
|
+
description: data.description,
|
|
1572
|
+
startDate: startDate.valueOf(),
|
|
1573
|
+
endDate: endDate.valueOf(),
|
|
1574
|
+
timeoutMechanismType: data.timeoutMechanismType,
|
|
1575
|
+
penalty: data.penalty
|
|
1576
|
+
},
|
|
1577
|
+
ceremonyPrefix: extractPrefix(data.title),
|
|
1578
|
+
circuits,
|
|
1579
|
+
circuitArtifacts
|
|
1580
|
+
};
|
|
1581
|
+
return setupData;
|
|
1582
|
+
}
|
|
1583
|
+
catch (error) {
|
|
1584
|
+
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
|
|
1585
|
+
}
|
|
1586
|
+
};
|
|
1581
1587
|
|
|
1582
1588
|
/**
|
|
1583
1589
|
* Verify that a zKey is valid
|
|
@@ -1826,7 +1832,7 @@ const getFirestoreDatabase = (app) => getFirestore(app);
|
|
|
1826
1832
|
* @param app <FirebaseApp> - the Firebase application.
|
|
1827
1833
|
* @returns <Functions> - the Cloud Functions associated to the application.
|
|
1828
1834
|
*/
|
|
1829
|
-
const getFirebaseFunctions = (app) => getFunctions(app,
|
|
1835
|
+
const getFirebaseFunctions = (app) => getFunctions(app, "europe-west1");
|
|
1830
1836
|
/**
|
|
1831
1837
|
* Retrieve the configuration variables for the AWS services (S3, EC2).
|
|
1832
1838
|
* @returns <AWSVariables> - the values of the AWS services configuration variables.
|
|
@@ -1835,14 +1841,14 @@ const getAWSVariables = () => {
|
|
|
1835
1841
|
if (!process.env.AWS_ACCESS_KEY_ID ||
|
|
1836
1842
|
!process.env.AWS_SECRET_ACCESS_KEY ||
|
|
1837
1843
|
!process.env.AWS_REGION ||
|
|
1838
|
-
!process.env.
|
|
1844
|
+
!process.env.AWS_INSTANCE_PROFILE_ARN ||
|
|
1839
1845
|
!process.env.AWS_AMI_ID)
|
|
1840
1846
|
throw new Error("Could not retrieve the AWS environment variables. Please, verify your environment configuration and retry");
|
|
1841
1847
|
return {
|
|
1842
1848
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
1843
1849
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
1844
1850
|
region: process.env.AWS_REGION || "us-east-1",
|
|
1845
|
-
|
|
1851
|
+
instanceProfileArn: process.env.AWS_INSTANCE_PROFILE_ARN,
|
|
1846
1852
|
amiId: process.env.AWS_AMI_ID
|
|
1847
1853
|
};
|
|
1848
1854
|
};
|
|
@@ -2095,7 +2101,8 @@ const getGitHubStats = async (user) => {
|
|
|
2095
2101
|
following: jsonData.following,
|
|
2096
2102
|
followers: jsonData.followers,
|
|
2097
2103
|
publicRepos: jsonData.public_repos,
|
|
2098
|
-
avatarUrl: jsonData.avatar_url
|
|
2104
|
+
avatarUrl: jsonData.avatar_url,
|
|
2105
|
+
age: jsonData.created_at
|
|
2099
2106
|
};
|
|
2100
2107
|
return data;
|
|
2101
2108
|
};
|
|
@@ -2107,20 +2114,21 @@ const getGitHubStats = async (user) => {
|
|
|
2107
2114
|
* @param minimumAmountOfPublicRepos <number> The minimum amount of public repos the user should have
|
|
2108
2115
|
* @returns <any> Return the avatar URL of the user if the user is reputable, false otherwise
|
|
2109
2116
|
*/
|
|
2110
|
-
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos) => {
|
|
2117
|
+
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos, minimumAge) => {
|
|
2111
2118
|
if (!process.env.GITHUB_ACCESS_TOKEN)
|
|
2112
2119
|
throw new Error("The GitHub access token is missing. Please insert a valid token to be used for anti-sybil checks on user registation, and then try again.");
|
|
2113
|
-
const { following, followers, publicRepos, avatarUrl } = await getGitHubStats(userLogin);
|
|
2120
|
+
const { following, followers, publicRepos, avatarUrl, age } = await getGitHubStats(userLogin);
|
|
2114
2121
|
if (following < minimumAmountOfFollowing ||
|
|
2115
2122
|
publicRepos < minimumAmountOfPublicRepos ||
|
|
2116
|
-
followers < minimumAmountOfFollowers
|
|
2123
|
+
followers < minimumAmountOfFollowers ||
|
|
2124
|
+
new Date(age) > new Date(Date.now() - minimumAge))
|
|
2117
2125
|
return {
|
|
2118
2126
|
reputable: false,
|
|
2119
2127
|
avatarUrl: ""
|
|
2120
2128
|
};
|
|
2121
2129
|
return {
|
|
2122
2130
|
reputable: true,
|
|
2123
|
-
avatarUrl
|
|
2131
|
+
avatarUrl
|
|
2124
2132
|
};
|
|
2125
2133
|
};
|
|
2126
2134
|
|
|
@@ -2329,8 +2337,13 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2329
2337
|
// eslint-disable-next-line no-template-curly-in-string
|
|
2330
2338
|
"touch ${MARKER_FILE}",
|
|
2331
2339
|
"sudo yum update -y",
|
|
2332
|
-
"curl -
|
|
2333
|
-
"
|
|
2340
|
+
"curl -O https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz",
|
|
2341
|
+
"tar -xf node-v16.13.0-linux-x64.tar.xz",
|
|
2342
|
+
"mv node-v16.13.0-linux-x64 nodejs",
|
|
2343
|
+
"sudo mv nodejs /opt/",
|
|
2344
|
+
"echo 'export NODEJS_HOME=/opt/nodejs' >> /etc/profile",
|
|
2345
|
+
"echo 'export PATH=$NODEJS_HOME/bin:$PATH' >> /etc/profile",
|
|
2346
|
+
"source /etc/profile",
|
|
2334
2347
|
"npm install -g snarkjs",
|
|
2335
2348
|
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
|
|
2336
2349
|
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
|
|
@@ -2349,6 +2362,7 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2349
2362
|
* @returns Array<string> - the list of commands for contribution verification.
|
|
2350
2363
|
*/
|
|
2351
2364
|
const vmContributionVerificationCommand = (bucketName, lastZkeyStoragePath, verificationTranscriptStoragePathAndFilename) => [
|
|
2365
|
+
`source /etc/profile`,
|
|
2352
2366
|
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
|
|
2353
2367
|
`snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
|
|
2354
2368
|
`aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
|
|
@@ -2375,7 +2389,7 @@ const computeDiskSizeForVM = (zKeySizeInBytes, pot) => Math.ceil(2 * convertByte
|
|
|
2375
2389
|
*/
|
|
2376
2390
|
const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskType) => {
|
|
2377
2391
|
// Get the AWS variables.
|
|
2378
|
-
const { amiId,
|
|
2392
|
+
const { amiId, instanceProfileArn } = getAWSVariables();
|
|
2379
2393
|
// Parametrize the VM EC2 instance.
|
|
2380
2394
|
const params = {
|
|
2381
2395
|
ImageId: amiId,
|
|
@@ -2384,7 +2398,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
|
|
|
2384
2398
|
MinCount: 1,
|
|
2385
2399
|
// nb. to find this: iam -> roles -> role_name.
|
|
2386
2400
|
IamInstanceProfile: {
|
|
2387
|
-
Arn:
|
|
2401
|
+
Arn: instanceProfileArn
|
|
2388
2402
|
},
|
|
2389
2403
|
// nb. for running commands at the startup.
|
|
2390
2404
|
UserData: Buffer.from(commands.join("\n")).toString("base64"),
|