@devtion/actions 0.0.0-4088679 → 0.0.0-477457c
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 +2 -2
- package/dist/index.mjs +376 -268
- package/dist/index.node.js +376 -266
- package/dist/types/src/helpers/authentication.d.ts.map +1 -1
- package/dist/types/src/helpers/constants.d.ts +5 -2
- package/dist/types/src/helpers/constants.d.ts.map +1 -1
- package/dist/types/src/helpers/contracts.d.ts.map +1 -1
- package/dist/types/src/helpers/crypto.d.ts.map +1 -1
- package/dist/types/src/helpers/database.d.ts +8 -0
- package/dist/types/src/helpers/database.d.ts.map +1 -1
- package/dist/types/src/helpers/functions.d.ts +2 -1
- package/dist/types/src/helpers/functions.d.ts.map +1 -1
- 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/services.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 +33 -21
- package/dist/types/src/helpers/utils.d.ts.map +1 -1
- package/dist/types/src/helpers/verification.d.ts +3 -2
- package/dist/types/src/helpers/verification.d.ts.map +1 -1
- package/dist/types/src/helpers/vm.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +9 -4
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/test/data/generators.d.ts +32 -0
- package/dist/types/test/data/generators.d.ts.map +1 -0
- package/dist/types/test/data/samples.d.ts +40 -0
- package/dist/types/test/data/samples.d.ts.map +1 -0
- package/dist/types/test/utils/authentication.d.ts +72 -0
- package/dist/types/test/utils/authentication.d.ts.map +1 -0
- package/dist/types/test/utils/configs.d.ts +52 -0
- package/dist/types/test/utils/configs.d.ts.map +1 -0
- package/dist/types/test/utils/index.d.ts +4 -0
- package/dist/types/test/utils/index.d.ts.map +1 -0
- package/dist/types/test/utils/storage.d.ts +126 -0
- package/dist/types/test/utils/storage.d.ts.map +1 -0
- package/package.json +7 -8
- package/src/helpers/constants.ts +39 -31
- package/src/helpers/contracts.ts +3 -3
- package/src/helpers/crypto.ts +5 -1
- package/src/helpers/database.ts +13 -0
- package/src/helpers/functions.ts +1 -1
- package/src/helpers/security.ts +11 -8
- package/src/helpers/services.ts +3 -3
- package/src/helpers/storage.ts +15 -3
- package/src/helpers/utils.ts +335 -263
- package/src/helpers/verification.ts +6 -6
- package/src/helpers/vm.ts +28 -7
- package/src/index.ts +5 -3
- package/src/types/index.ts +32 -8
package/dist/index.node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @devtion/actions
|
|
3
|
-
* @version 1.
|
|
3
|
+
* @version 1.2.8
|
|
4
4
|
* @file A set of actions and helpers for CLI commands
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
@@ -17,7 +17,6 @@ var firestore = require('firebase/firestore');
|
|
|
17
17
|
var snarkjs = require('snarkjs');
|
|
18
18
|
var crypto = require('crypto');
|
|
19
19
|
var blake = require('blakejs');
|
|
20
|
-
var ffjavascript = require('ffjavascript');
|
|
21
20
|
var winston = require('winston');
|
|
22
21
|
var stream = require('stream');
|
|
23
22
|
var util = require('util');
|
|
@@ -29,10 +28,10 @@ var clientEc2 = require('@aws-sdk/client-ec2');
|
|
|
29
28
|
var clientSsm = require('@aws-sdk/client-ssm');
|
|
30
29
|
var dotenv = require('dotenv');
|
|
31
30
|
|
|
32
|
-
// Main part for the
|
|
33
|
-
const potFileDownloadMainUrl = `https://
|
|
34
|
-
// Main part for the
|
|
35
|
-
const potFilenameTemplate = `
|
|
31
|
+
// Main part for the PPoT Phase 1 Trusted Setup URLs to download PoT files.
|
|
32
|
+
const potFileDownloadMainUrl = `https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/`;
|
|
33
|
+
// Main part for the PPoT Phase 1 Trusted Setup PoT files to be downloaded.
|
|
34
|
+
const potFilenameTemplate = `ppot_0080_`;
|
|
36
35
|
// The genesis zKey index.
|
|
37
36
|
const genesisZkeyIndex = `00000`;
|
|
38
37
|
// The number of exponential iterations to be executed by SnarkJS when finalizing the ceremony.
|
|
@@ -49,6 +48,8 @@ const verifierSmartContractAcronym = "verifier";
|
|
|
49
48
|
const ec2InstanceTag = "p0tionec2instance";
|
|
50
49
|
// The name of the VM startup script file.
|
|
51
50
|
const vmBootstrapScriptFilename = "bootstrap.sh";
|
|
51
|
+
// Match hash output by snarkjs in transcript log
|
|
52
|
+
const contribHashRegex = /Contribution.+Hash.+\s+.+\s+.+\s+.+\s+.+\s*/;
|
|
52
53
|
/**
|
|
53
54
|
* Define the supported VM configuration types.
|
|
54
55
|
* @dev the VM configurations can be retrieved at https://aws.amazon.com/ec2/instance-types/
|
|
@@ -106,112 +107,116 @@ const vmConfigurationTypes = {
|
|
|
106
107
|
*/
|
|
107
108
|
const powersOfTauFiles = [
|
|
108
109
|
{
|
|
109
|
-
ref: "https://
|
|
110
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_01.ptau",
|
|
110
111
|
size: 0.000084
|
|
111
112
|
},
|
|
112
113
|
{
|
|
113
|
-
ref: "https://
|
|
114
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_02.ptau",
|
|
114
115
|
size: 0.000086
|
|
115
116
|
},
|
|
116
117
|
{
|
|
117
|
-
ref: "https://
|
|
118
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_03.ptau",
|
|
118
119
|
size: 0.000091
|
|
119
120
|
},
|
|
120
121
|
{
|
|
121
|
-
ref: "https://
|
|
122
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_04.ptau",
|
|
122
123
|
size: 0.0001
|
|
123
124
|
},
|
|
124
125
|
{
|
|
125
|
-
ref: "https://
|
|
126
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_05.ptau",
|
|
126
127
|
size: 0.000117
|
|
127
128
|
},
|
|
128
129
|
{
|
|
129
|
-
ref: "https://
|
|
130
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_06.ptau",
|
|
130
131
|
size: 0.000153
|
|
131
132
|
},
|
|
132
133
|
{
|
|
133
|
-
ref: "https://
|
|
134
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_07.ptau",
|
|
134
135
|
size: 0.000225
|
|
135
136
|
},
|
|
136
137
|
{
|
|
137
|
-
ref: "https://
|
|
138
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_08.ptau",
|
|
138
139
|
size: 0.0004
|
|
139
140
|
},
|
|
140
141
|
{
|
|
141
|
-
ref: "https://
|
|
142
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_09.ptau",
|
|
142
143
|
size: 0.000658
|
|
143
144
|
},
|
|
144
145
|
{
|
|
145
|
-
ref: "https://
|
|
146
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_10.ptau",
|
|
146
147
|
size: 0.0013
|
|
147
148
|
},
|
|
148
149
|
{
|
|
149
|
-
ref: "https://
|
|
150
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_11.ptau",
|
|
150
151
|
size: 0.0023
|
|
151
152
|
},
|
|
152
153
|
{
|
|
153
|
-
ref: "https://
|
|
154
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_12.ptau",
|
|
154
155
|
size: 0.0046
|
|
155
156
|
},
|
|
156
157
|
{
|
|
157
|
-
ref: "https://
|
|
158
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_13.ptau",
|
|
158
159
|
size: 0.0091
|
|
159
160
|
},
|
|
160
161
|
{
|
|
161
|
-
ref: "https://
|
|
162
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_14.ptau",
|
|
162
163
|
size: 0.0181
|
|
163
164
|
},
|
|
164
165
|
{
|
|
165
|
-
ref: "https://
|
|
166
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_15.ptau",
|
|
166
167
|
size: 0.0361
|
|
167
168
|
},
|
|
168
169
|
{
|
|
169
|
-
ref: "https://
|
|
170
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_16.ptau",
|
|
170
171
|
size: 0.0721
|
|
171
172
|
},
|
|
172
173
|
{
|
|
173
|
-
ref: "https://
|
|
174
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_17.ptau",
|
|
174
175
|
size: 0.144
|
|
175
176
|
},
|
|
176
177
|
{
|
|
177
|
-
ref: "https://
|
|
178
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_18.ptau",
|
|
178
179
|
size: 0.288
|
|
179
180
|
},
|
|
180
181
|
{
|
|
181
|
-
ref: "https://
|
|
182
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_19.ptau",
|
|
182
183
|
size: 0.576
|
|
183
184
|
},
|
|
184
185
|
{
|
|
185
|
-
ref: "https://
|
|
186
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_20.ptau",
|
|
186
187
|
size: 1.1
|
|
187
188
|
},
|
|
188
189
|
{
|
|
189
|
-
ref: "https://
|
|
190
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_21.ptau",
|
|
190
191
|
size: 2.3
|
|
191
192
|
},
|
|
192
193
|
{
|
|
193
|
-
ref: "https://
|
|
194
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_22.ptau",
|
|
194
195
|
size: 4.5
|
|
195
196
|
},
|
|
196
197
|
{
|
|
197
|
-
ref: "https://
|
|
198
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_23.ptau",
|
|
198
199
|
size: 9.0
|
|
199
200
|
},
|
|
200
201
|
{
|
|
201
|
-
ref: "https://
|
|
202
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_24.ptau",
|
|
202
203
|
size: 18.0
|
|
203
204
|
},
|
|
204
205
|
{
|
|
205
|
-
ref: "https://
|
|
206
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_25.ptau",
|
|
206
207
|
size: 36.0
|
|
207
208
|
},
|
|
208
209
|
{
|
|
209
|
-
ref: "https://
|
|
210
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_26.ptau",
|
|
210
211
|
size: 72.0
|
|
211
212
|
},
|
|
212
213
|
{
|
|
213
|
-
ref: "https://
|
|
214
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_27.ptau",
|
|
214
215
|
size: 144.0
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
ref: "https://pse-trusted-setup-ppot.s3.eu-central-1.amazonaws.com/pot28_0080/ppot_0080_final.ptau",
|
|
219
|
+
size: 288.0
|
|
215
220
|
}
|
|
216
221
|
];
|
|
217
222
|
/**
|
|
@@ -342,6 +347,8 @@ const commonTerms = {
|
|
|
342
347
|
finalizeCeremony: "finalizeCeremony",
|
|
343
348
|
downloadCircuitArtifacts: "downloadCircuitArtifacts",
|
|
344
349
|
transferObject: "transferObject",
|
|
350
|
+
bandadaValidateProof: "bandadaValidateProof",
|
|
351
|
+
checkNonceOfSIWEAddress: "checkNonceOfSIWEAddress"
|
|
345
352
|
}
|
|
346
353
|
};
|
|
347
354
|
|
|
@@ -692,19 +699,23 @@ const getChunksAndPreSignedUrls = async (cloudFunctions, bucketName, objectKey,
|
|
|
692
699
|
* @param cloudFunctions <Functions> - the Firebase Cloud Functions service instance.
|
|
693
700
|
* @param ceremonyId <string> - the unique identifier of the ceremony.
|
|
694
701
|
* @param alreadyUploadedChunks Array<ETagWithPartNumber> - the temporary information about the already uploaded chunks.
|
|
702
|
+
* @param logger <GenericBar> - an optional logger to show progress.
|
|
695
703
|
* @returns <Promise<Array<ETagWithPartNumber>>> - the completed (uploaded) chunks information.
|
|
696
704
|
*/
|
|
697
|
-
const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks) => {
|
|
705
|
+
const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremonyId, alreadyUploadedChunks, logger) => {
|
|
698
706
|
// Keep track of uploaded chunks.
|
|
699
707
|
const uploadedChunks = alreadyUploadedChunks || [];
|
|
708
|
+
// if we were passed a logger, start it
|
|
709
|
+
if (logger)
|
|
710
|
+
logger.start(chunksWithUrls.length, 0);
|
|
700
711
|
// Loop through remaining chunks.
|
|
701
712
|
for (let i = alreadyUploadedChunks ? alreadyUploadedChunks.length : 0; i < chunksWithUrls.length; i += 1) {
|
|
702
713
|
// Consume the pre-signed url to upload the chunk.
|
|
703
714
|
// @ts-ignore
|
|
704
715
|
const response = await fetch(chunksWithUrls[i].preSignedUrl, {
|
|
705
716
|
retryOptions: {
|
|
706
|
-
retryInitialDelay: 500,
|
|
707
|
-
socketTimeout: 60000,
|
|
717
|
+
retryInitialDelay: 500, // 500 ms.
|
|
718
|
+
socketTimeout: 60000, // 60 seconds.
|
|
708
719
|
retryMaxDuration: 300000 // 5 minutes.
|
|
709
720
|
},
|
|
710
721
|
method: "PUT",
|
|
@@ -728,6 +739,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
|
|
|
728
739
|
// nb. this must be done only when contributing (not finalizing).
|
|
729
740
|
if (!!ceremonyId && !!cloudFunctions)
|
|
730
741
|
await temporaryStoreCurrentContributionUploadedChunkData(cloudFunctions, ceremonyId, chunk);
|
|
742
|
+
// increment the count on the logger
|
|
743
|
+
if (logger)
|
|
744
|
+
logger.increment();
|
|
731
745
|
}
|
|
732
746
|
return uploadedChunks;
|
|
733
747
|
};
|
|
@@ -748,8 +762,9 @@ const uploadParts = async (chunksWithUrls, contentType, cloudFunctions, ceremony
|
|
|
748
762
|
* @param configStreamChunkSize <number> - size of each chunk into which the artifact is going to be splitted (nb. will be converted in MB).
|
|
749
763
|
* @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).
|
|
750
764
|
* @param [temporaryDataToResumeMultiPartUpload] <TemporaryParticipantContributionData> - the temporary information necessary to resume an already started multi-part upload.
|
|
765
|
+
* @param logger <GenericBar> - an optional logger to show progress.
|
|
751
766
|
*/
|
|
752
|
-
const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload) => {
|
|
767
|
+
const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFilePath, configStreamChunkSize, ceremonyId, temporaryDataToResumeMultiPartUpload, logger) => {
|
|
753
768
|
// The unique identifier of the multi-part upload.
|
|
754
769
|
let multiPartUploadId = "";
|
|
755
770
|
// The list of already uploaded chunks.
|
|
@@ -773,7 +788,7 @@ const multiPartUpload = async (cloudFunctions, bucketName, objectKey, localFileP
|
|
|
773
788
|
const chunksWithUrlsZkey = await getChunksAndPreSignedUrls(cloudFunctions, bucketName, objectKey, localFilePath, multiPartUploadId, configStreamChunkSize, ceremonyId);
|
|
774
789
|
// Step (2).
|
|
775
790
|
const partNumbersAndETagsZkey = await uploadParts(chunksWithUrlsZkey, mime.lookup(localFilePath), // content-type.
|
|
776
|
-
cloudFunctions, ceremonyId, alreadyUploadedChunks);
|
|
791
|
+
cloudFunctions, ceremonyId, alreadyUploadedChunks, logger);
|
|
777
792
|
// Step (3).
|
|
778
793
|
await completeMultiPartUpload(cloudFunctions, bucketName, objectKey, multiPartUploadId, partNumbersAndETagsZkey, ceremonyId);
|
|
779
794
|
};
|
|
@@ -997,6 +1012,17 @@ const getClosedCeremonies = async (firestoreDatabase) => {
|
|
|
997
1012
|
]);
|
|
998
1013
|
return fromQueryToFirebaseDocumentInfo(closedCeremoniesQuerySnap.docs);
|
|
999
1014
|
};
|
|
1015
|
+
/**
|
|
1016
|
+
* Query all ceremonies
|
|
1017
|
+
* @notice get all ceremonies from the database.
|
|
1018
|
+
* @dev this is a helper for the CLI ceremony methods.
|
|
1019
|
+
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
|
|
1020
|
+
* @returns <Promise<Array<FirebaseDocumentInfo>>> - the list of all ceremonies.
|
|
1021
|
+
*/
|
|
1022
|
+
const getAllCeremonies = async (firestoreDatabase) => {
|
|
1023
|
+
const ceremoniesQuerySnap = await queryCollection(firestoreDatabase, commonTerms.collections.ceremonies.name, []);
|
|
1024
|
+
return fromQueryToFirebaseDocumentInfo(ceremoniesQuerySnap.docs);
|
|
1025
|
+
};
|
|
1000
1026
|
|
|
1001
1027
|
/**
|
|
1002
1028
|
* @hidden
|
|
@@ -1018,6 +1044,9 @@ const blake512FromPath = async (path) => {
|
|
|
1018
1044
|
const hash = await new Promise((resolve) => {
|
|
1019
1045
|
fs.createReadStream(path)
|
|
1020
1046
|
.on("data", (chunk) => {
|
|
1047
|
+
if (typeof chunk === "string") {
|
|
1048
|
+
chunk = Buffer.from(chunk);
|
|
1049
|
+
}
|
|
1021
1050
|
blake.blake2bUpdate(context, chunk);
|
|
1022
1051
|
})
|
|
1023
1052
|
.on("end", () => {
|
|
@@ -1045,192 +1074,22 @@ const compareHashes = async (path1, path2) => {
|
|
|
1045
1074
|
};
|
|
1046
1075
|
|
|
1047
1076
|
/**
|
|
1048
|
-
*
|
|
1049
|
-
* @
|
|
1050
|
-
* @
|
|
1051
|
-
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
1052
|
-
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
1077
|
+
* Return a string with double digits if the provided input is one digit only.
|
|
1078
|
+
* @param in <number> - the input number to be converted.
|
|
1079
|
+
* @returns <string> - the two digits stringified number derived from the conversion.
|
|
1053
1080
|
*/
|
|
1054
|
-
const
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
throw new Error("You need to provide the data for at least 1 circuit.");
|
|
1067
|
-
// validate that the end date is in the future
|
|
1068
|
-
let endDate;
|
|
1069
|
-
let startDate;
|
|
1070
|
-
try {
|
|
1071
|
-
endDate = new Date(data.endDate);
|
|
1072
|
-
startDate = new Date(data.startDate);
|
|
1073
|
-
}
|
|
1074
|
-
catch (error) {
|
|
1075
|
-
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
|
|
1076
|
-
}
|
|
1077
|
-
if (endDate <= startDate)
|
|
1078
|
-
throw new Error("The end date should be greater than the start date.");
|
|
1079
|
-
const currentDate = new Date();
|
|
1080
|
-
if (endDate <= currentDate || startDate <= currentDate)
|
|
1081
|
-
throw new Error("The start and end dates should be in the future.");
|
|
1082
|
-
// validate penalty
|
|
1083
|
-
if (data.penalty <= 0)
|
|
1084
|
-
throw new Error("The penalty should be greater than zero.");
|
|
1085
|
-
const circuits = [];
|
|
1086
|
-
const urlPattern = /(https?:\/\/[^\s]+)/g;
|
|
1087
|
-
const commitHashPattern = /^[a-f0-9]{40}$/i;
|
|
1088
|
-
const circuitArtifacts = [];
|
|
1089
|
-
for (let i = 0; i < data.circuits.length; i++) {
|
|
1090
|
-
const circuitData = data.circuits[i];
|
|
1091
|
-
const artifacts = circuitData.artifacts;
|
|
1092
|
-
circuitArtifacts.push({
|
|
1093
|
-
artifacts: artifacts
|
|
1094
|
-
});
|
|
1095
|
-
// where we storing the r1cs downloaded
|
|
1096
|
-
const localR1csPath = `./${circuitData.name}.r1cs`;
|
|
1097
|
-
// where we storing the wasm downloaded
|
|
1098
|
-
const localWasmPath = `./${circuitData.name}.wasm`;
|
|
1099
|
-
// download the r1cs to extract the metadata
|
|
1100
|
-
const streamPipeline = util.promisify(stream.pipeline);
|
|
1101
|
-
// Make the call.
|
|
1102
|
-
const responseR1CS = await fetch(artifacts.r1csStoragePath);
|
|
1103
|
-
// Handle errors.
|
|
1104
|
-
if (!responseR1CS.ok && responseR1CS.status !== 200)
|
|
1105
|
-
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.`);
|
|
1106
|
-
await streamPipeline(responseR1CS.body, fs.createWriteStream(localR1csPath));
|
|
1107
|
-
// Write the file locally
|
|
1108
|
-
// extract the metadata from the r1cs
|
|
1109
|
-
const metadata = getR1CSInfo(localR1csPath);
|
|
1110
|
-
// download wasm too to ensure it's available
|
|
1111
|
-
const responseWASM = await fetch(artifacts.wasmStoragePath);
|
|
1112
|
-
if (!responseWASM.ok && responseWASM.status !== 200)
|
|
1113
|
-
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.`);
|
|
1114
|
-
await streamPipeline(responseWASM.body, fs.createWriteStream(localWasmPath));
|
|
1115
|
-
// validate that the circuit hash and template links are valid
|
|
1116
|
-
const template = circuitData.template;
|
|
1117
|
-
const URLMatch = template.source.match(urlPattern);
|
|
1118
|
-
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
1119
|
-
throw new Error("You should provide the URL to the circuits templates on GitHub.");
|
|
1120
|
-
const hashMatch = template.commitHash.match(commitHashPattern);
|
|
1121
|
-
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
1122
|
-
throw new Error("You should provide a valid commit hash of the circuit templates.");
|
|
1123
|
-
// calculate the hash of the r1cs file
|
|
1124
|
-
const r1csBlake2bHash = await blake512FromPath(localR1csPath);
|
|
1125
|
-
const circuitPrefix = extractPrefix(circuitData.name);
|
|
1126
|
-
// filenames
|
|
1127
|
-
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
|
|
1128
|
-
const r1csCompleteFilename = `${circuitData.name}.r1cs`;
|
|
1129
|
-
const wasmCompleteFilename = `${circuitData.name}.wasm`;
|
|
1130
|
-
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
|
|
1131
|
-
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
|
|
1132
|
-
// storage paths
|
|
1133
|
-
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
|
|
1134
|
-
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
|
|
1135
|
-
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
|
|
1136
|
-
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
|
|
1137
|
-
const files = {
|
|
1138
|
-
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
1139
|
-
r1csFilename: r1csCompleteFilename,
|
|
1140
|
-
wasmFilename: wasmCompleteFilename,
|
|
1141
|
-
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
1142
|
-
potStoragePath: potStorageFilePath,
|
|
1143
|
-
r1csStoragePath: r1csStorageFilePath,
|
|
1144
|
-
wasmStoragePath: wasmStorageFilePath,
|
|
1145
|
-
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
1146
|
-
r1csBlake2bHash: r1csBlake2bHash
|
|
1147
|
-
};
|
|
1148
|
-
// validate that the compiler hash is a valid hash
|
|
1149
|
-
const compiler = circuitData.compiler;
|
|
1150
|
-
const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
|
|
1151
|
-
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
1152
|
-
throw new Error("You should provide a valid commit hash of the circuit compiler.");
|
|
1153
|
-
// validate that the verification options are valid
|
|
1154
|
-
const verification = circuitData.verification;
|
|
1155
|
-
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
1156
|
-
throw new Error("Please enter a valid verification mechanism: either CF or VM");
|
|
1157
|
-
// @todo VM parameters verification
|
|
1158
|
-
// if (verification['cfOrVM'] === "VM") {}
|
|
1159
|
-
// check that the timeout is provided for the correct configuration
|
|
1160
|
-
let dynamicThreshold;
|
|
1161
|
-
let fixedTimeWindow;
|
|
1162
|
-
let circuit = {};
|
|
1163
|
-
if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
|
|
1164
|
-
if (circuitData.dynamicThreshold <= 0)
|
|
1165
|
-
throw new Error("The dynamic threshold should be > 0.");
|
|
1166
|
-
dynamicThreshold = circuitData.dynamicThreshold;
|
|
1167
|
-
// the Circuit data for the ceremony setup
|
|
1168
|
-
circuit = {
|
|
1169
|
-
name: circuitData.name,
|
|
1170
|
-
description: circuitData.description,
|
|
1171
|
-
prefix: circuitPrefix,
|
|
1172
|
-
sequencePosition: i + 1,
|
|
1173
|
-
metadata: metadata,
|
|
1174
|
-
files: files,
|
|
1175
|
-
template: template,
|
|
1176
|
-
compiler: compiler,
|
|
1177
|
-
verification: verification,
|
|
1178
|
-
dynamicThreshold: dynamicThreshold,
|
|
1179
|
-
avgTimings: {
|
|
1180
|
-
contributionComputation: 0,
|
|
1181
|
-
fullContribution: 0,
|
|
1182
|
-
verifyCloudFunction: 0
|
|
1183
|
-
},
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
|
|
1187
|
-
if (circuitData.fixedTimeWindow <= 0)
|
|
1188
|
-
throw new Error("The fixed time window threshold should be > 0.");
|
|
1189
|
-
fixedTimeWindow = circuitData.fixedTimeWindow;
|
|
1190
|
-
// the Circuit data for the ceremony setup
|
|
1191
|
-
circuit = {
|
|
1192
|
-
name: circuitData.name,
|
|
1193
|
-
description: circuitData.description,
|
|
1194
|
-
prefix: circuitPrefix,
|
|
1195
|
-
sequencePosition: i + 1,
|
|
1196
|
-
metadata: metadata,
|
|
1197
|
-
files: files,
|
|
1198
|
-
template: template,
|
|
1199
|
-
compiler: compiler,
|
|
1200
|
-
verification: verification,
|
|
1201
|
-
fixedTimeWindow: fixedTimeWindow,
|
|
1202
|
-
avgTimings: {
|
|
1203
|
-
contributionComputation: 0,
|
|
1204
|
-
fullContribution: 0,
|
|
1205
|
-
verifyCloudFunction: 0
|
|
1206
|
-
},
|
|
1207
|
-
};
|
|
1208
|
-
}
|
|
1209
|
-
circuits.push(circuit);
|
|
1210
|
-
// remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
|
|
1211
|
-
if (cleanup)
|
|
1212
|
-
fs.unlinkSync(localR1csPath);
|
|
1213
|
-
fs.unlinkSync(localWasmPath);
|
|
1214
|
-
}
|
|
1215
|
-
const setupData = {
|
|
1216
|
-
ceremonyInputData: {
|
|
1217
|
-
title: data.title,
|
|
1218
|
-
description: data.description,
|
|
1219
|
-
startDate: startDate.valueOf(),
|
|
1220
|
-
endDate: endDate.valueOf(),
|
|
1221
|
-
timeoutMechanismType: data.timeoutMechanismType,
|
|
1222
|
-
penalty: data.penalty
|
|
1223
|
-
},
|
|
1224
|
-
ceremonyPrefix: extractPrefix(data.title),
|
|
1225
|
-
circuits: circuits,
|
|
1226
|
-
circuitArtifacts: circuitArtifacts
|
|
1227
|
-
};
|
|
1228
|
-
return setupData;
|
|
1229
|
-
}
|
|
1230
|
-
catch (error) {
|
|
1231
|
-
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
|
|
1232
|
-
}
|
|
1233
|
-
};
|
|
1081
|
+
const convertToDoubleDigits = (amount) => (amount < 10 ? `0${amount}` : amount.toString());
|
|
1082
|
+
/**
|
|
1083
|
+
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
1084
|
+
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
1085
|
+
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
1086
|
+
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
1087
|
+
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
1088
|
+
* @returns <string> - the resulting prefix.
|
|
1089
|
+
*/
|
|
1090
|
+
const extractPrefix = (str) =>
|
|
1091
|
+
// eslint-disable-next-line no-useless-escape
|
|
1092
|
+
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
|
|
1234
1093
|
/**
|
|
1235
1094
|
* Extract data from a R1CS metadata file generated with a custom file-based logger.
|
|
1236
1095
|
* @notice useful for extracting metadata circuits contained in the generated file using a logger
|
|
@@ -1287,17 +1146,6 @@ const formatZkeyIndex = (progress) => {
|
|
|
1287
1146
|
* @returns <number> - the amount of powers.
|
|
1288
1147
|
*/
|
|
1289
1148
|
const extractPoTFromFilename = (potCompleteFilename) => Number(potCompleteFilename.split("_").pop()?.split(".").at(0));
|
|
1290
|
-
/**
|
|
1291
|
-
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
1292
|
-
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
1293
|
-
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
1294
|
-
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
1295
|
-
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
1296
|
-
* @returns <string> - the resulting prefix.
|
|
1297
|
-
*/
|
|
1298
|
-
const extractPrefix = (str) =>
|
|
1299
|
-
// eslint-disable-next-line no-useless-escape
|
|
1300
|
-
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase();
|
|
1301
1149
|
/**
|
|
1302
1150
|
* Automate the generation of an entropy for a contribution.
|
|
1303
1151
|
* @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
|
|
@@ -1364,7 +1212,9 @@ const getContributionsValidityForContributor = async (firestoreDatabase, circuit
|
|
|
1364
1212
|
* @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
|
|
1365
1213
|
* @returns <string> - the public attestation preamble.
|
|
1366
1214
|
*/
|
|
1367
|
-
const getPublicAttestationPreambleForContributor = (contributorIdentifier, ceremonyName, isFinalizing) => `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}
|
|
1215
|
+
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")
|
|
1216
|
+
? "."
|
|
1217
|
+
: " MPC Phase2 Trusted Setup ceremony."}\nThe following are my contribution signatures:`;
|
|
1368
1218
|
/**
|
|
1369
1219
|
* Check and prepare public attestation for the contributor made only of its valid contributions.
|
|
1370
1220
|
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
|
|
@@ -1435,6 +1285,41 @@ const readBytesFromFile = (localFilePath, offset, length, position) => {
|
|
|
1435
1285
|
// Return the read bytes.
|
|
1436
1286
|
return buffer;
|
|
1437
1287
|
};
|
|
1288
|
+
/**
|
|
1289
|
+
* Given a buffer in little endian format, convert it to bigint
|
|
1290
|
+
* @param buffer
|
|
1291
|
+
* @returns
|
|
1292
|
+
*/
|
|
1293
|
+
function leBufferToBigint(buffer) {
|
|
1294
|
+
return BigInt(`0x${buffer.reverse().toString("hex")}`);
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Given an input containing string values, convert them
|
|
1298
|
+
* to bigint
|
|
1299
|
+
* @param input - The input to convert
|
|
1300
|
+
* @returns the input with string values converted to bigint
|
|
1301
|
+
*/
|
|
1302
|
+
const unstringifyBigInts = (input) => {
|
|
1303
|
+
if (typeof input === "string" && /^[0-9]+$/.test(input)) {
|
|
1304
|
+
return BigInt(input);
|
|
1305
|
+
}
|
|
1306
|
+
if (typeof input === "string" && /^0x[0-9a-fA-F]+$/.test(input)) {
|
|
1307
|
+
return BigInt(input);
|
|
1308
|
+
}
|
|
1309
|
+
if (Array.isArray(input)) {
|
|
1310
|
+
return input.map(unstringifyBigInts);
|
|
1311
|
+
}
|
|
1312
|
+
if (input === null) {
|
|
1313
|
+
return null;
|
|
1314
|
+
}
|
|
1315
|
+
if (typeof input === "object") {
|
|
1316
|
+
return Object.entries(input).reduce((acc, [key, value]) => {
|
|
1317
|
+
acc[key] = unstringifyBigInts(value);
|
|
1318
|
+
return acc;
|
|
1319
|
+
}, {});
|
|
1320
|
+
}
|
|
1321
|
+
return input;
|
|
1322
|
+
};
|
|
1438
1323
|
/**
|
|
1439
1324
|
* Return the info about the R1CS file.ù
|
|
1440
1325
|
* @dev this method was built taking inspiration from
|
|
@@ -1495,17 +1380,17 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1495
1380
|
let constraints = 0;
|
|
1496
1381
|
try {
|
|
1497
1382
|
// Get 'number of section' (jump magic r1cs and version1 data).
|
|
1498
|
-
const numberOfSections =
|
|
1383
|
+
const numberOfSections = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, 8));
|
|
1499
1384
|
// Jump to first section.
|
|
1500
1385
|
pointer = 12;
|
|
1501
1386
|
// For each section
|
|
1502
1387
|
for (let i = 0; i < numberOfSections; i++) {
|
|
1503
1388
|
// Read section type.
|
|
1504
|
-
const sectionType =
|
|
1389
|
+
const sectionType = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer));
|
|
1505
1390
|
// Jump to section size.
|
|
1506
1391
|
pointer += 4;
|
|
1507
1392
|
// Read section size
|
|
1508
|
-
const sectionSize = Number(
|
|
1393
|
+
const sectionSize = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
|
|
1509
1394
|
// If at header section (0x00000001 : Header Section).
|
|
1510
1395
|
if (sectionType === BigInt(1)) {
|
|
1511
1396
|
// Read info from header section.
|
|
@@ -1537,22 +1422,22 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1537
1422
|
*/
|
|
1538
1423
|
pointer += sectionSize - 20;
|
|
1539
1424
|
// Read R1CS info.
|
|
1540
|
-
wires = Number(
|
|
1425
|
+
wires = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1541
1426
|
pointer += 4;
|
|
1542
|
-
publicOutputs = Number(
|
|
1427
|
+
publicOutputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1543
1428
|
pointer += 4;
|
|
1544
|
-
publicInputs = Number(
|
|
1429
|
+
publicInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1545
1430
|
pointer += 4;
|
|
1546
|
-
privateInputs = Number(
|
|
1431
|
+
privateInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1547
1432
|
pointer += 4;
|
|
1548
|
-
labels = Number(
|
|
1433
|
+
labels = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)));
|
|
1549
1434
|
pointer += 8;
|
|
1550
|
-
constraints = Number(
|
|
1435
|
+
constraints = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)));
|
|
1551
1436
|
}
|
|
1552
1437
|
pointer += 8 + Number(sectionSize);
|
|
1553
1438
|
}
|
|
1554
1439
|
return {
|
|
1555
|
-
curve: "bn-128",
|
|
1440
|
+
curve: "bn-128", /// @note currently default to bn-128 as we support only Groth16 proving system.
|
|
1556
1441
|
wires,
|
|
1557
1442
|
constraints,
|
|
1558
1443
|
privateInputs,
|
|
@@ -1567,11 +1452,212 @@ const getR1CSInfo = (localR1CSFilePath) => {
|
|
|
1567
1452
|
}
|
|
1568
1453
|
};
|
|
1569
1454
|
/**
|
|
1570
|
-
*
|
|
1571
|
-
* @
|
|
1572
|
-
* @
|
|
1455
|
+
* Parse and validate that the ceremony configuration is correct
|
|
1456
|
+
* @notice this does not upload any files to storage
|
|
1457
|
+
* @param path <string> - the path to the configuration file
|
|
1458
|
+
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
1459
|
+
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
1573
1460
|
*/
|
|
1574
|
-
const
|
|
1461
|
+
const parseCeremonyFile = async (path, cleanup = false) => {
|
|
1462
|
+
// check that the path exists
|
|
1463
|
+
if (!fs.existsSync(path))
|
|
1464
|
+
throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.");
|
|
1465
|
+
try {
|
|
1466
|
+
// read the data
|
|
1467
|
+
const data = JSON.parse(fs.readFileSync(path).toString());
|
|
1468
|
+
// verify that the data is correct
|
|
1469
|
+
if (data.timeoutMechanismType !== "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */ &&
|
|
1470
|
+
data.timeoutMechanismType !== "FIXED" /* CeremonyTimeoutType.FIXED */)
|
|
1471
|
+
throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.");
|
|
1472
|
+
// validate that we have at least 1 circuit input data
|
|
1473
|
+
if (!data.circuits || data.circuits.length === 0)
|
|
1474
|
+
throw new Error("You need to provide the data for at least 1 circuit.");
|
|
1475
|
+
// validate that the end date is in the future
|
|
1476
|
+
let endDate;
|
|
1477
|
+
let startDate;
|
|
1478
|
+
try {
|
|
1479
|
+
endDate = new Date(data.endDate);
|
|
1480
|
+
startDate = new Date(data.startDate);
|
|
1481
|
+
}
|
|
1482
|
+
catch (error) {
|
|
1483
|
+
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.");
|
|
1484
|
+
}
|
|
1485
|
+
if (endDate <= startDate)
|
|
1486
|
+
throw new Error("The end date should be greater than the start date.");
|
|
1487
|
+
const currentDate = new Date();
|
|
1488
|
+
if (endDate <= currentDate || startDate <= currentDate)
|
|
1489
|
+
throw new Error("The start and end dates should be in the future.");
|
|
1490
|
+
// validate penalty
|
|
1491
|
+
if (data.penalty <= 0)
|
|
1492
|
+
throw new Error("The penalty should be greater than zero.");
|
|
1493
|
+
const circuits = [];
|
|
1494
|
+
const urlPattern = /(https?:\/\/[^\s]+)/g;
|
|
1495
|
+
const commitHashPattern = /^[a-f0-9]{40}$/i;
|
|
1496
|
+
const circuitArtifacts = [];
|
|
1497
|
+
for (let i = 0; i < data.circuits.length; i++) {
|
|
1498
|
+
const circuitData = data.circuits[i];
|
|
1499
|
+
const { artifacts } = circuitData;
|
|
1500
|
+
circuitArtifacts.push({
|
|
1501
|
+
artifacts
|
|
1502
|
+
});
|
|
1503
|
+
// where we storing the r1cs downloaded
|
|
1504
|
+
const localR1csPath = `./${circuitData.name}.r1cs`;
|
|
1505
|
+
// where we storing the wasm downloaded
|
|
1506
|
+
const localWasmPath = `./${circuitData.name}.wasm`;
|
|
1507
|
+
// download the r1cs to extract the metadata
|
|
1508
|
+
const streamPipeline = util.promisify(stream.pipeline);
|
|
1509
|
+
// Check if r1cs file already exists
|
|
1510
|
+
let r1csExists = false;
|
|
1511
|
+
if (fs.existsSync(localR1csPath)) {
|
|
1512
|
+
console.log(`Found existing r1cs file for circuit ${circuitData.name}. Skipping download.`);
|
|
1513
|
+
r1csExists = true;
|
|
1514
|
+
}
|
|
1515
|
+
if (!r1csExists) {
|
|
1516
|
+
// Make the call to download r1cs.
|
|
1517
|
+
const responseR1CS = await fetch(artifacts.r1csStoragePath);
|
|
1518
|
+
// Handle errors.
|
|
1519
|
+
if (!responseR1CS.ok && responseR1CS.status !== 200)
|
|
1520
|
+
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.`);
|
|
1521
|
+
// Write the file locally
|
|
1522
|
+
await streamPipeline(responseR1CS.body, fs.createWriteStream(localR1csPath));
|
|
1523
|
+
console.log(`Downloaded r1cs file for circuit ${circuitData.name}.`);
|
|
1524
|
+
}
|
|
1525
|
+
// extract the metadata from the r1cs
|
|
1526
|
+
const metadata = getR1CSInfo(localR1csPath);
|
|
1527
|
+
// Check if wasm file already exists
|
|
1528
|
+
let wasmExists = false;
|
|
1529
|
+
if (fs.existsSync(localWasmPath)) {
|
|
1530
|
+
console.log(`Found existing wasm file for circuit ${circuitData.name}. Skipping download.`);
|
|
1531
|
+
wasmExists = true;
|
|
1532
|
+
}
|
|
1533
|
+
if (!wasmExists) {
|
|
1534
|
+
// download wasm if it's not available
|
|
1535
|
+
const responseWASM = await fetch(artifacts.wasmStoragePath);
|
|
1536
|
+
if (!responseWASM.ok && responseWASM.status !== 200)
|
|
1537
|
+
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.`);
|
|
1538
|
+
await streamPipeline(responseWASM.body, fs.createWriteStream(localWasmPath));
|
|
1539
|
+
console.log(`Downloaded wasm file for circuit ${circuitData.name}.`);
|
|
1540
|
+
}
|
|
1541
|
+
// validate that the circuit hash and template links are valid
|
|
1542
|
+
const { template } = circuitData;
|
|
1543
|
+
const URLMatch = template.source.match(urlPattern);
|
|
1544
|
+
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
1545
|
+
throw new Error("You should provide the URL to the circuits templates on GitHub.");
|
|
1546
|
+
const hashMatch = template.commitHash.match(commitHashPattern);
|
|
1547
|
+
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
1548
|
+
throw new Error("You should provide a valid commit hash of the circuit templates.");
|
|
1549
|
+
// calculate the hash of the r1cs file
|
|
1550
|
+
const r1csBlake2bHash = await blake512FromPath(localR1csPath);
|
|
1551
|
+
const circuitPrefix = extractPrefix(circuitData.name);
|
|
1552
|
+
// filenames
|
|
1553
|
+
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot);
|
|
1554
|
+
const r1csCompleteFilename = `${circuitData.name}.r1cs`;
|
|
1555
|
+
const wasmCompleteFilename = `${circuitData.name}.wasm`;
|
|
1556
|
+
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`;
|
|
1557
|
+
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`;
|
|
1558
|
+
// storage paths
|
|
1559
|
+
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename);
|
|
1560
|
+
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename);
|
|
1561
|
+
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit);
|
|
1562
|
+
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename);
|
|
1563
|
+
const files = {
|
|
1564
|
+
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
1565
|
+
r1csFilename: r1csCompleteFilename,
|
|
1566
|
+
wasmFilename: wasmCompleteFilename,
|
|
1567
|
+
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
1568
|
+
potStoragePath: potStorageFilePath,
|
|
1569
|
+
r1csStoragePath: r1csStorageFilePath,
|
|
1570
|
+
wasmStoragePath: wasmStorageFilePath,
|
|
1571
|
+
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
1572
|
+
r1csBlake2bHash
|
|
1573
|
+
};
|
|
1574
|
+
// validate that the compiler hash is a valid hash
|
|
1575
|
+
const { compiler } = circuitData;
|
|
1576
|
+
const compilerHashMatch = compiler.commitHash.match(commitHashPattern);
|
|
1577
|
+
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
1578
|
+
throw new Error("You should provide a valid commit hash of the circuit compiler.");
|
|
1579
|
+
// validate that the verification options are valid
|
|
1580
|
+
const { verification } = circuitData;
|
|
1581
|
+
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
1582
|
+
throw new Error("Please enter a valid verification mechanism: either CF or VM");
|
|
1583
|
+
// @todo VM parameters verification
|
|
1584
|
+
// if (verification['cfOrVM'] === "VM") {}
|
|
1585
|
+
// check that the timeout is provided for the correct configuration
|
|
1586
|
+
let dynamicThreshold;
|
|
1587
|
+
let fixedTimeWindow;
|
|
1588
|
+
let circuit = {};
|
|
1589
|
+
if (data.timeoutMechanismType === "DYNAMIC" /* CeremonyTimeoutType.DYNAMIC */) {
|
|
1590
|
+
if (circuitData.dynamicThreshold <= 0)
|
|
1591
|
+
throw new Error("The dynamic threshold should be > 0.");
|
|
1592
|
+
dynamicThreshold = circuitData.dynamicThreshold;
|
|
1593
|
+
// the Circuit data for the ceremony setup
|
|
1594
|
+
circuit = {
|
|
1595
|
+
name: circuitData.name,
|
|
1596
|
+
description: circuitData.description,
|
|
1597
|
+
prefix: circuitPrefix,
|
|
1598
|
+
sequencePosition: i + 1,
|
|
1599
|
+
metadata,
|
|
1600
|
+
files,
|
|
1601
|
+
template,
|
|
1602
|
+
compiler,
|
|
1603
|
+
verification,
|
|
1604
|
+
dynamicThreshold,
|
|
1605
|
+
avgTimings: {
|
|
1606
|
+
contributionComputation: 0,
|
|
1607
|
+
fullContribution: 0,
|
|
1608
|
+
verifyCloudFunction: 0
|
|
1609
|
+
}
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
if (data.timeoutMechanismType === "FIXED" /* CeremonyTimeoutType.FIXED */) {
|
|
1613
|
+
if (circuitData.fixedTimeWindow <= 0)
|
|
1614
|
+
throw new Error("The fixed time window threshold should be > 0.");
|
|
1615
|
+
fixedTimeWindow = circuitData.fixedTimeWindow;
|
|
1616
|
+
// the Circuit data for the ceremony setup
|
|
1617
|
+
circuit = {
|
|
1618
|
+
name: circuitData.name,
|
|
1619
|
+
description: circuitData.description,
|
|
1620
|
+
prefix: circuitPrefix,
|
|
1621
|
+
sequencePosition: i + 1,
|
|
1622
|
+
metadata,
|
|
1623
|
+
files,
|
|
1624
|
+
template,
|
|
1625
|
+
compiler,
|
|
1626
|
+
verification,
|
|
1627
|
+
fixedTimeWindow,
|
|
1628
|
+
avgTimings: {
|
|
1629
|
+
contributionComputation: 0,
|
|
1630
|
+
fullContribution: 0,
|
|
1631
|
+
verifyCloudFunction: 0
|
|
1632
|
+
}
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
circuits.push(circuit);
|
|
1636
|
+
// remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
|
|
1637
|
+
if (cleanup) {
|
|
1638
|
+
fs.unlinkSync(localR1csPath);
|
|
1639
|
+
fs.unlinkSync(localWasmPath);
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
const setupData = {
|
|
1643
|
+
ceremonyInputData: {
|
|
1644
|
+
title: data.title,
|
|
1645
|
+
description: data.description,
|
|
1646
|
+
startDate: startDate.valueOf(),
|
|
1647
|
+
endDate: endDate.valueOf(),
|
|
1648
|
+
timeoutMechanismType: data.timeoutMechanismType,
|
|
1649
|
+
penalty: data.penalty
|
|
1650
|
+
},
|
|
1651
|
+
ceremonyPrefix: extractPrefix(data.title),
|
|
1652
|
+
circuits,
|
|
1653
|
+
circuitArtifacts
|
|
1654
|
+
};
|
|
1655
|
+
return setupData;
|
|
1656
|
+
}
|
|
1657
|
+
catch (error) {
|
|
1658
|
+
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`);
|
|
1659
|
+
}
|
|
1660
|
+
};
|
|
1575
1661
|
|
|
1576
1662
|
/**
|
|
1577
1663
|
* Verify that a zKey is valid
|
|
@@ -1820,7 +1906,7 @@ const getFirestoreDatabase = (app) => firestore.getFirestore(app);
|
|
|
1820
1906
|
* @param app <FirebaseApp> - the Firebase application.
|
|
1821
1907
|
* @returns <Functions> - the Cloud Functions associated to the application.
|
|
1822
1908
|
*/
|
|
1823
|
-
const getFirebaseFunctions = (app) => functions.getFunctions(app,
|
|
1909
|
+
const getFirebaseFunctions = (app) => functions.getFunctions(app, "europe-west1");
|
|
1824
1910
|
/**
|
|
1825
1911
|
* Retrieve the configuration variables for the AWS services (S3, EC2).
|
|
1826
1912
|
* @returns <AWSVariables> - the values of the AWS services configuration variables.
|
|
@@ -1829,14 +1915,14 @@ const getAWSVariables = () => {
|
|
|
1829
1915
|
if (!process.env.AWS_ACCESS_KEY_ID ||
|
|
1830
1916
|
!process.env.AWS_SECRET_ACCESS_KEY ||
|
|
1831
1917
|
!process.env.AWS_REGION ||
|
|
1832
|
-
!process.env.
|
|
1918
|
+
!process.env.AWS_INSTANCE_PROFILE_ARN ||
|
|
1833
1919
|
!process.env.AWS_AMI_ID)
|
|
1834
1920
|
throw new Error("Could not retrieve the AWS environment variables. Please, verify your environment configuration and retry");
|
|
1835
1921
|
return {
|
|
1836
1922
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
1837
1923
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
1838
1924
|
region: process.env.AWS_REGION || "us-east-1",
|
|
1839
|
-
|
|
1925
|
+
instanceProfileArn: process.env.AWS_INSTANCE_PROFILE_ARN,
|
|
1840
1926
|
amiId: process.env.AWS_AMI_ID
|
|
1841
1927
|
};
|
|
1842
1928
|
};
|
|
@@ -1917,11 +2003,11 @@ const p256 = (proofPart) => {
|
|
|
1917
2003
|
*/
|
|
1918
2004
|
const formatSolidityCalldata = (circuitInput, _proof) => {
|
|
1919
2005
|
try {
|
|
1920
|
-
const proof =
|
|
2006
|
+
const proof = unstringifyBigInts(_proof);
|
|
1921
2007
|
// format the public inputs to the circuit
|
|
1922
2008
|
const formattedCircuitInput = [];
|
|
1923
2009
|
for (const cInput of circuitInput) {
|
|
1924
|
-
formattedCircuitInput.push(p256(
|
|
2010
|
+
formattedCircuitInput.push(p256(unstringifyBigInts(cInput)));
|
|
1925
2011
|
}
|
|
1926
2012
|
// construct calldata
|
|
1927
2013
|
const calldata = {
|
|
@@ -2089,7 +2175,8 @@ const getGitHubStats = async (user) => {
|
|
|
2089
2175
|
following: jsonData.following,
|
|
2090
2176
|
followers: jsonData.followers,
|
|
2091
2177
|
publicRepos: jsonData.public_repos,
|
|
2092
|
-
avatarUrl: jsonData.avatar_url
|
|
2178
|
+
avatarUrl: jsonData.avatar_url,
|
|
2179
|
+
age: jsonData.created_at
|
|
2093
2180
|
};
|
|
2094
2181
|
return data;
|
|
2095
2182
|
};
|
|
@@ -2101,20 +2188,21 @@ const getGitHubStats = async (user) => {
|
|
|
2101
2188
|
* @param minimumAmountOfPublicRepos <number> The minimum amount of public repos the user should have
|
|
2102
2189
|
* @returns <any> Return the avatar URL of the user if the user is reputable, false otherwise
|
|
2103
2190
|
*/
|
|
2104
|
-
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos) => {
|
|
2191
|
+
const githubReputation = async (userLogin, minimumAmountOfFollowing, minimumAmountOfFollowers, minimumAmountOfPublicRepos, minimumAge) => {
|
|
2105
2192
|
if (!process.env.GITHUB_ACCESS_TOKEN)
|
|
2106
2193
|
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.");
|
|
2107
|
-
const { following, followers, publicRepos, avatarUrl } = await getGitHubStats(userLogin);
|
|
2194
|
+
const { following, followers, publicRepos, avatarUrl, age } = await getGitHubStats(userLogin);
|
|
2108
2195
|
if (following < minimumAmountOfFollowing ||
|
|
2109
2196
|
publicRepos < minimumAmountOfPublicRepos ||
|
|
2110
|
-
followers < minimumAmountOfFollowers
|
|
2197
|
+
followers < minimumAmountOfFollowers ||
|
|
2198
|
+
new Date(age) > new Date(Date.now() - minimumAge))
|
|
2111
2199
|
return {
|
|
2112
2200
|
reputable: false,
|
|
2113
2201
|
avatarUrl: ""
|
|
2114
2202
|
};
|
|
2115
2203
|
return {
|
|
2116
2204
|
reputable: true,
|
|
2117
|
-
avatarUrl
|
|
2205
|
+
avatarUrl
|
|
2118
2206
|
};
|
|
2119
2207
|
};
|
|
2120
2208
|
|
|
@@ -2301,8 +2389,8 @@ const createSSMClient = async () => {
|
|
|
2301
2389
|
* @returns <Array<string>> - the list of startup commands to be executed.
|
|
2302
2390
|
*/
|
|
2303
2391
|
const vmBootstrapCommand = (bucketName) => [
|
|
2304
|
-
"#!/bin/bash",
|
|
2305
|
-
`aws s3 cp s3://${bucketName}/${vmBootstrapScriptFilename} ${vmBootstrapScriptFilename}`,
|
|
2392
|
+
"#!/bin/bash", // shabang.
|
|
2393
|
+
`aws s3 cp s3://${bucketName}/${vmBootstrapScriptFilename} ${vmBootstrapScriptFilename}`, // copy file from S3 bucket to VM.
|
|
2306
2394
|
`chmod +x ${vmBootstrapScriptFilename} && bash ${vmBootstrapScriptFilename}` // grant permission and execute.
|
|
2307
2395
|
];
|
|
2308
2396
|
/**
|
|
@@ -2323,8 +2411,13 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2323
2411
|
// eslint-disable-next-line no-template-curly-in-string
|
|
2324
2412
|
"touch ${MARKER_FILE}",
|
|
2325
2413
|
"sudo yum update -y",
|
|
2326
|
-
"curl -
|
|
2327
|
-
"
|
|
2414
|
+
"curl -O https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz",
|
|
2415
|
+
"tar -xf node-v16.13.0-linux-x64.tar.xz",
|
|
2416
|
+
"mv node-v16.13.0-linux-x64 nodejs",
|
|
2417
|
+
"sudo mv nodejs /opt/",
|
|
2418
|
+
"echo 'export NODEJS_HOME=/opt/nodejs' >> /etc/profile",
|
|
2419
|
+
"echo 'export PATH=$NODEJS_HOME/bin:$PATH' >> /etc/profile",
|
|
2420
|
+
"source /etc/profile",
|
|
2328
2421
|
"npm install -g snarkjs",
|
|
2329
2422
|
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
|
|
2330
2423
|
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
|
|
@@ -2343,6 +2436,7 @@ const vmDependenciesAndCacheArtifactsCommand = (zKeyPath, potPath, snsTopic, reg
|
|
|
2343
2436
|
* @returns Array<string> - the list of commands for contribution verification.
|
|
2344
2437
|
*/
|
|
2345
2438
|
const vmContributionVerificationCommand = (bucketName, lastZkeyStoragePath, verificationTranscriptStoragePathAndFilename) => [
|
|
2439
|
+
`source /etc/profile`,
|
|
2346
2440
|
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
|
|
2347
2441
|
`snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
|
|
2348
2442
|
`aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
|
|
@@ -2369,8 +2463,9 @@ const computeDiskSizeForVM = (zKeySizeInBytes, pot) => Math.ceil(2 * convertByte
|
|
|
2369
2463
|
*/
|
|
2370
2464
|
const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskType) => {
|
|
2371
2465
|
// Get the AWS variables.
|
|
2372
|
-
const { amiId,
|
|
2466
|
+
const { amiId, instanceProfileArn } = getAWSVariables();
|
|
2373
2467
|
// Parametrize the VM EC2 instance.
|
|
2468
|
+
console.log("\nLAUNCHING AWS EC2 INSTANCE\n");
|
|
2374
2469
|
const params = {
|
|
2375
2470
|
ImageId: amiId,
|
|
2376
2471
|
InstanceType: instanceType,
|
|
@@ -2378,7 +2473,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
|
|
|
2378
2473
|
MinCount: 1,
|
|
2379
2474
|
// nb. to find this: iam -> roles -> role_name.
|
|
2380
2475
|
IamInstanceProfile: {
|
|
2381
|
-
Arn:
|
|
2476
|
+
Arn: instanceProfileArn
|
|
2382
2477
|
},
|
|
2383
2478
|
// nb. for running commands at the startup.
|
|
2384
2479
|
UserData: Buffer.from(commands.join("\n")).toString("base64"),
|
|
@@ -2387,7 +2482,7 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
|
|
|
2387
2482
|
DeviceName: "/dev/xvda",
|
|
2388
2483
|
Ebs: {
|
|
2389
2484
|
DeleteOnTermination: true,
|
|
2390
|
-
VolumeSize: volumeSize,
|
|
2485
|
+
VolumeSize: volumeSize, // disk size in GB.
|
|
2391
2486
|
VolumeType: diskType
|
|
2392
2487
|
}
|
|
2393
2488
|
}
|
|
@@ -2404,6 +2499,19 @@ const createEC2Instance = async (ec2, commands, instanceType, volumeSize, diskTy
|
|
|
2404
2499
|
{
|
|
2405
2500
|
Key: "Initialized",
|
|
2406
2501
|
Value: "false"
|
|
2502
|
+
},
|
|
2503
|
+
{
|
|
2504
|
+
Key: "Project",
|
|
2505
|
+
Value: "trusted-setup"
|
|
2506
|
+
}
|
|
2507
|
+
]
|
|
2508
|
+
},
|
|
2509
|
+
{
|
|
2510
|
+
ResourceType: "volume",
|
|
2511
|
+
Tags: [
|
|
2512
|
+
{
|
|
2513
|
+
Key: "Project",
|
|
2514
|
+
Value: "trusted-setup"
|
|
2407
2515
|
}
|
|
2408
2516
|
]
|
|
2409
2517
|
}
|
|
@@ -2587,6 +2695,7 @@ exports.completeMultiPartUpload = completeMultiPartUpload;
|
|
|
2587
2695
|
exports.computeDiskSizeForVM = computeDiskSizeForVM;
|
|
2588
2696
|
exports.computeSHA256ToHex = computeSHA256ToHex;
|
|
2589
2697
|
exports.computeSmallestPowersOfTauForCircuit = computeSmallestPowersOfTauForCircuit;
|
|
2698
|
+
exports.contribHashRegex = contribHashRegex;
|
|
2590
2699
|
exports.convertBytesOrKbToGb = convertBytesOrKbToGb;
|
|
2591
2700
|
exports.convertToDoubleDigits = convertToDoubleDigits;
|
|
2592
2701
|
exports.createCustomLoggerForFile = createCustomLoggerForFile;
|
|
@@ -2615,6 +2724,7 @@ exports.generatePreSignedUrlsParts = generatePreSignedUrlsParts;
|
|
|
2615
2724
|
exports.generateValidContributionsAttestation = generateValidContributionsAttestation;
|
|
2616
2725
|
exports.generateZkeyFromScratch = generateZkeyFromScratch;
|
|
2617
2726
|
exports.genesisZkeyIndex = genesisZkeyIndex;
|
|
2727
|
+
exports.getAllCeremonies = getAllCeremonies;
|
|
2618
2728
|
exports.getAllCollectionDocs = getAllCollectionDocs;
|
|
2619
2729
|
exports.getBucketName = getBucketName;
|
|
2620
2730
|
exports.getCeremonyCircuits = getCeremonyCircuits;
|