@devtion/actions 0.0.0-bfc9ee4 → 0.0.0-c749be4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.mjs +265 -280
- package/dist/index.node.js +265 -280
- package/dist/types/src/helpers/constants.d.ts +6 -0
- package/dist/types/src/helpers/constants.d.ts.map +1 -1
- package/dist/types/src/helpers/security.d.ts +2 -2
- package/dist/types/src/helpers/security.d.ts.map +1 -1
- package/dist/types/src/helpers/storage.d.ts +5 -2
- package/dist/types/src/helpers/storage.d.ts.map +1 -1
- package/dist/types/src/helpers/utils.d.ts +19 -19
- package/dist/types/src/helpers/utils.d.ts.map +1 -1
- package/dist/types/src/helpers/vm.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +2 -2
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/package.json +2 -6
- package/src/helpers/constants.ts +7 -1
- package/src/helpers/functions.ts +1 -1
- package/src/helpers/security.ts +33 -52
- package/src/helpers/services.ts +3 -3
- package/src/helpers/storage.ts +15 -3
- package/src/helpers/utils.ts +262 -268
- package/src/helpers/vm.ts +11 -5
- package/src/index.ts +2 -2
- package/src/types/index.ts +11 -7
package/src/helpers/utils.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { Firestore } from "firebase/firestore"
|
|
2
|
-
import fs, { ReadPosition } from "fs"
|
|
2
|
+
import fs, { ReadPosition, createWriteStream } from "fs"
|
|
3
3
|
import { utils as ffUtils } from "ffjavascript"
|
|
4
4
|
import winston, { Logger } from "winston"
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
import fetch from "@adobe/node-fetch-retry"
|
|
6
|
+
import { pipeline } from "stream"
|
|
7
|
+
import { promisify } from "util"
|
|
8
|
+
import {
|
|
9
|
+
CircuitMetadata,
|
|
10
|
+
Contribution,
|
|
11
|
+
CircuitDocument,
|
|
10
12
|
CircuitInputData,
|
|
11
|
-
ContributionValidity,
|
|
12
|
-
FirebaseDocumentInfo,
|
|
13
|
-
SetupCeremonyData,
|
|
13
|
+
ContributionValidity,
|
|
14
|
+
FirebaseDocumentInfo,
|
|
15
|
+
SetupCeremonyData,
|
|
14
16
|
CeremonySetupTemplate,
|
|
15
|
-
CeremonySetupTemplateCircuitArtifacts
|
|
17
|
+
CeremonySetupTemplateCircuitArtifacts
|
|
16
18
|
} from "../types/index"
|
|
17
19
|
import { finalContributionIndex, genesisZkeyIndex, potFilenameTemplate } from "./constants"
|
|
18
20
|
import {
|
|
@@ -22,252 +24,32 @@ import {
|
|
|
22
24
|
getContributionsCollectionPath
|
|
23
25
|
} from "./database"
|
|
24
26
|
import { CeremonyTimeoutType } from "../types/enums"
|
|
25
|
-
import {
|
|
26
|
-
getPotStorageFilePath,
|
|
27
|
-
getR1csStorageFilePath,
|
|
28
|
-
getWasmStorageFilePath,
|
|
27
|
+
import {
|
|
28
|
+
getPotStorageFilePath,
|
|
29
|
+
getR1csStorageFilePath,
|
|
30
|
+
getWasmStorageFilePath,
|
|
29
31
|
getZkeyStorageFilePath
|
|
30
32
|
} from "./storage"
|
|
31
33
|
import { blake512FromPath } from "./crypto"
|
|
32
|
-
import { Readable, pipeline } from "stream"
|
|
33
|
-
import { promisify } from "util"
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* @
|
|
39
|
-
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
40
|
-
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
36
|
+
* Return a string with double digits if the provided input is one digit only.
|
|
37
|
+
* @param in <number> - the input number to be converted.
|
|
38
|
+
* @returns <string> - the two digits stringified number derived from the conversion.
|
|
41
39
|
*/
|
|
42
|
-
export const
|
|
43
|
-
// check that the path exists
|
|
44
|
-
if (!fs.existsSync(path)) throw new Error("The provided path to the configuration file does not exist. Please provide an absolute path and try again.")
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
// read the data
|
|
48
|
-
const data: CeremonySetupTemplate = JSON.parse(fs.readFileSync(path).toString())
|
|
49
|
-
|
|
50
|
-
// verify that the data is correct
|
|
51
|
-
if (data['timeoutMechanismType'] !== CeremonyTimeoutType.DYNAMIC && data['timeoutMechanismType'] !== CeremonyTimeoutType.FIXED)
|
|
52
|
-
throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.")
|
|
53
|
-
|
|
54
|
-
// validate that we have at least 1 circuit input data
|
|
55
|
-
if (!data.circuits || data.circuits.length === 0)
|
|
56
|
-
throw new Error("You need to provide the data for at least 1 circuit.")
|
|
57
|
-
|
|
58
|
-
// validate that the end date is in the future
|
|
59
|
-
let endDate: Date
|
|
60
|
-
let startDate: Date
|
|
61
|
-
try {
|
|
62
|
-
endDate = new Date(data.endDate)
|
|
63
|
-
startDate = new Date(data.startDate)
|
|
64
|
-
} catch (error: any) {
|
|
65
|
-
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.")
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (endDate <= startDate) throw new Error("The end date should be greater than the start date.")
|
|
69
|
-
|
|
70
|
-
const currentDate = new Date()
|
|
71
|
-
|
|
72
|
-
if (endDate <= currentDate || startDate <= currentDate)
|
|
73
|
-
throw new Error("The start and end dates should be in the future.")
|
|
74
|
-
|
|
75
|
-
// validate penalty
|
|
76
|
-
if (data.penalty <= 0) throw new Error("The penalty should be greater than zero.")
|
|
77
|
-
|
|
78
|
-
const circuits: CircuitDocument[] = []
|
|
79
|
-
const urlPattern = /(https?:\/\/[^\s]+)/g
|
|
80
|
-
const commitHashPattern = /^[a-f0-9]{40}$/i
|
|
81
|
-
|
|
82
|
-
const circuitArtifacts: CeremonySetupTemplateCircuitArtifacts[] = []
|
|
83
|
-
|
|
84
|
-
for (let i = 0; i < data.circuits.length; i++) {
|
|
85
|
-
const circuitData = data.circuits[i]
|
|
86
|
-
const artifacts = circuitData.artifacts
|
|
87
|
-
circuitArtifacts.push({
|
|
88
|
-
artifacts: artifacts
|
|
89
|
-
})
|
|
90
|
-
const r1csPath = artifacts.r1csStoragePath
|
|
91
|
-
const wasmPath = artifacts.wasmStoragePath
|
|
92
|
-
|
|
93
|
-
// where we storing the r1cs downloaded
|
|
94
|
-
const localR1csPath = `./${circuitData.name}.r1cs`
|
|
95
|
-
|
|
96
|
-
// check that the artifacts exist in S3
|
|
97
|
-
// we don't need any privileges to download this
|
|
98
|
-
// just the correct region
|
|
99
|
-
const s3 = new S3Client({region: artifacts.region})
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
await s3.send(new HeadObjectCommand({
|
|
103
|
-
Bucket: artifacts.bucket,
|
|
104
|
-
Key: r1csPath
|
|
105
|
-
}))
|
|
106
|
-
} catch (error: any) {
|
|
107
|
-
throw new Error(`The r1cs file (${r1csPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
await s3.send(new HeadObjectCommand({
|
|
112
|
-
Bucket: artifacts.bucket,
|
|
113
|
-
Key: wasmPath
|
|
114
|
-
}))
|
|
115
|
-
} catch (error: any) {
|
|
116
|
-
throw new Error(`The wasm file (${wasmPath}) seems to not exist. Please ensure this is correct and that the object is publicly available.`)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// download the r1cs to extract the metadata
|
|
120
|
-
const command = new GetObjectCommand({ Bucket: artifacts.bucket, Key: artifacts.r1csStoragePath })
|
|
121
|
-
const response = await s3.send(command)
|
|
122
|
-
const streamPipeline = promisify(pipeline)
|
|
123
|
-
|
|
124
|
-
if (response.$metadata.httpStatusCode !== 200)
|
|
125
|
-
throw new Error("There was an error while trying to download the r1cs file. Please check that the file has the correct permissions (public) set.")
|
|
126
|
-
|
|
127
|
-
if (response.Body instanceof Readable)
|
|
128
|
-
await streamPipeline(response.Body, fs.createWriteStream(localR1csPath))
|
|
129
|
-
|
|
130
|
-
// extract the metadata from the r1cs
|
|
131
|
-
const metadata = getR1CSInfo(localR1csPath)
|
|
132
|
-
|
|
133
|
-
// validate that the circuit hash and template links are valid
|
|
134
|
-
const template = circuitData.template
|
|
135
|
-
|
|
136
|
-
const URLMatch = template.source.match(urlPattern)
|
|
137
|
-
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1) throw new Error("You should provide the URL to the circuits templates on GitHub.")
|
|
138
|
-
|
|
139
|
-
const hashMatch = template.commitHash.match(commitHashPattern)
|
|
140
|
-
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit templates.")
|
|
141
|
-
|
|
142
|
-
// calculate the hash of the r1cs file
|
|
143
|
-
const r1csBlake2bHash = await blake512FromPath(localR1csPath)
|
|
144
|
-
|
|
145
|
-
const circuitPrefix = extractPrefix(circuitData.name)
|
|
146
|
-
|
|
147
|
-
// filenames
|
|
148
|
-
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
|
|
149
|
-
const r1csCompleteFilename = `${circuitData.name}.r1cs`
|
|
150
|
-
const wasmCompleteFilename = `${circuitData.name}.wasm`
|
|
151
|
-
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
|
|
152
|
-
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
|
|
40
|
+
export const convertToDoubleDigits = (amount: number): string => (amount < 10 ? `0${amount}` : amount.toString())
|
|
153
41
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
potStoragePath: potStorageFilePath,
|
|
166
|
-
r1csStoragePath: r1csStorageFilePath,
|
|
167
|
-
wasmStoragePath: wasmStorageFilePath,
|
|
168
|
-
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
169
|
-
r1csBlake2bHash: r1csBlake2bHash
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// validate that the compiler hash is a valid hash
|
|
173
|
-
const compiler = circuitData.compiler
|
|
174
|
-
const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
|
|
175
|
-
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit compiler.")
|
|
176
|
-
|
|
177
|
-
// validate that the verification options are valid
|
|
178
|
-
const verification = circuitData.verification
|
|
179
|
-
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
180
|
-
throw new Error("Please enter a valid verification mechanism: either CF or VM")
|
|
181
|
-
|
|
182
|
-
// @todo VM parameters verification
|
|
183
|
-
// if (verification['cfOrVM'] === "VM") {}
|
|
184
|
-
|
|
185
|
-
// check that the timeout is provided for the correct configuration
|
|
186
|
-
let dynamicThreshold: number | undefined
|
|
187
|
-
let fixedTimeWindow: number | undefined
|
|
188
|
-
|
|
189
|
-
let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
|
|
190
|
-
|
|
191
|
-
if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
|
|
192
|
-
if (circuitData.dynamicThreshold <= 0)
|
|
193
|
-
throw new Error("The dynamic threshold should be > 0.")
|
|
194
|
-
dynamicThreshold = circuitData.dynamicThreshold
|
|
195
|
-
|
|
196
|
-
// the Circuit data for the ceremony setup
|
|
197
|
-
circuit = {
|
|
198
|
-
name: circuitData.name,
|
|
199
|
-
description: circuitData.description,
|
|
200
|
-
prefix: circuitPrefix,
|
|
201
|
-
sequencePosition: i+1,
|
|
202
|
-
metadata: metadata,
|
|
203
|
-
files: files,
|
|
204
|
-
template: template,
|
|
205
|
-
compiler: compiler,
|
|
206
|
-
verification: verification,
|
|
207
|
-
dynamicThreshold: dynamicThreshold,
|
|
208
|
-
avgTimings: {
|
|
209
|
-
contributionComputation: 0,
|
|
210
|
-
fullContribution: 0,
|
|
211
|
-
verifyCloudFunction: 0
|
|
212
|
-
},
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
|
|
218
|
-
if (circuitData.fixedTimeWindow <= 0)
|
|
219
|
-
throw new Error("The fixed time window threshold should be > 0.")
|
|
220
|
-
fixedTimeWindow = circuitData.fixedTimeWindow
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// the Circuit data for the ceremony setup
|
|
224
|
-
circuit = {
|
|
225
|
-
name: circuitData.name,
|
|
226
|
-
description: circuitData.description,
|
|
227
|
-
prefix: circuitPrefix,
|
|
228
|
-
sequencePosition: i+1,
|
|
229
|
-
metadata: metadata,
|
|
230
|
-
files: files,
|
|
231
|
-
template: template,
|
|
232
|
-
compiler: compiler,
|
|
233
|
-
verification: verification,
|
|
234
|
-
fixedTimeWindow: fixedTimeWindow,
|
|
235
|
-
avgTimings: {
|
|
236
|
-
contributionComputation: 0,
|
|
237
|
-
fullContribution: 0,
|
|
238
|
-
verifyCloudFunction: 0
|
|
239
|
-
},
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
circuits.push(circuit)
|
|
246
|
-
|
|
247
|
-
// remove the local r1cs download (if used for verifying the config only vs setup)
|
|
248
|
-
if (cleanup) fs.unlinkSync(localR1csPath)
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const setupData: SetupCeremonyData = {
|
|
252
|
-
ceremonyInputData: {
|
|
253
|
-
title: data.title,
|
|
254
|
-
description: data.description,
|
|
255
|
-
startDate: startDate.valueOf(),
|
|
256
|
-
endDate: endDate.valueOf(),
|
|
257
|
-
timeoutMechanismType: data.timeoutMechanismType,
|
|
258
|
-
penalty: data.penalty
|
|
259
|
-
},
|
|
260
|
-
ceremonyPrefix: extractPrefix(data.title),
|
|
261
|
-
circuits: circuits,
|
|
262
|
-
circuitArtifacts: circuitArtifacts
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return setupData
|
|
266
|
-
|
|
267
|
-
} catch (error: any) {
|
|
268
|
-
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
|
|
269
|
-
}
|
|
270
|
-
}
|
|
42
|
+
/**
|
|
43
|
+
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
44
|
+
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
45
|
+
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
46
|
+
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
47
|
+
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
48
|
+
* @returns <string> - the resulting prefix.
|
|
49
|
+
*/
|
|
50
|
+
export const extractPrefix = (str: string): string =>
|
|
51
|
+
// eslint-disable-next-line no-useless-escape
|
|
52
|
+
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
|
|
271
53
|
|
|
272
54
|
/**
|
|
273
55
|
* Extract data from a R1CS metadata file generated with a custom file-based logger.
|
|
@@ -339,18 +121,6 @@ export const formatZkeyIndex = (progress: number): string => {
|
|
|
339
121
|
export const extractPoTFromFilename = (potCompleteFilename: string): number =>
|
|
340
122
|
Number(potCompleteFilename.split("_").pop()?.split(".").at(0))
|
|
341
123
|
|
|
342
|
-
/**
|
|
343
|
-
* Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
|
|
344
|
-
* @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
|
|
345
|
-
* @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
|
|
346
|
-
* NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
|
|
347
|
-
* @param str <string> - the arbitrary string from which to extract the prefix.
|
|
348
|
-
* @returns <string> - the resulting prefix.
|
|
349
|
-
*/
|
|
350
|
-
export const extractPrefix = (str: string): string =>
|
|
351
|
-
// eslint-disable-next-line no-useless-escape
|
|
352
|
-
str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
|
|
353
|
-
|
|
354
124
|
/**
|
|
355
125
|
* Automate the generation of an entropy for a contribution.
|
|
356
126
|
* @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
|
|
@@ -457,9 +227,11 @@ export const getPublicAttestationPreambleForContributor = (
|
|
|
457
227
|
ceremonyName: string,
|
|
458
228
|
isFinalizing: boolean
|
|
459
229
|
) =>
|
|
460
|
-
`Hey, I'm ${contributorIdentifier} and I have ${
|
|
461
|
-
|
|
462
|
-
|
|
230
|
+
`Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}${
|
|
231
|
+
ceremonyName.toLowerCase().includes("trusted setup") || ceremonyName.toLowerCase().includes("ceremony")
|
|
232
|
+
? "."
|
|
233
|
+
: " MPC Phase2 Trusted Setup ceremony."
|
|
234
|
+
}\nThe following are my contribution signatures:`
|
|
463
235
|
|
|
464
236
|
/**
|
|
465
237
|
* Check and prepare public attestation for the contributor made only of its valid contributions.
|
|
@@ -736,8 +508,230 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
|
|
|
736
508
|
}
|
|
737
509
|
|
|
738
510
|
/**
|
|
739
|
-
*
|
|
740
|
-
* @
|
|
741
|
-
* @
|
|
511
|
+
* Parse and validate that the ceremony configuration is correct
|
|
512
|
+
* @notice this does not upload any files to storage
|
|
513
|
+
* @param path <string> - the path to the configuration file
|
|
514
|
+
* @param cleanup <boolean> - whether to delete the r1cs file after parsing
|
|
515
|
+
* @returns any - the data to pass to the cloud function for setup and the circuit artifacts
|
|
742
516
|
*/
|
|
743
|
-
export const
|
|
517
|
+
export const parseCeremonyFile = async (path: string, cleanup: boolean = false): Promise<SetupCeremonyData> => {
|
|
518
|
+
// check that the path exists
|
|
519
|
+
if (!fs.existsSync(path))
|
|
520
|
+
throw new Error(
|
|
521
|
+
"The provided path to the configuration file does not exist. Please provide an absolute path and try again."
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
try {
|
|
525
|
+
// read the data
|
|
526
|
+
const data: CeremonySetupTemplate = JSON.parse(fs.readFileSync(path).toString())
|
|
527
|
+
|
|
528
|
+
// verify that the data is correct
|
|
529
|
+
if (
|
|
530
|
+
data.timeoutMechanismType !== CeremonyTimeoutType.DYNAMIC &&
|
|
531
|
+
data.timeoutMechanismType !== CeremonyTimeoutType.FIXED
|
|
532
|
+
)
|
|
533
|
+
throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.")
|
|
534
|
+
|
|
535
|
+
// validate that we have at least 1 circuit input data
|
|
536
|
+
if (!data.circuits || data.circuits.length === 0)
|
|
537
|
+
throw new Error("You need to provide the data for at least 1 circuit.")
|
|
538
|
+
|
|
539
|
+
// validate that the end date is in the future
|
|
540
|
+
let endDate: Date
|
|
541
|
+
let startDate: Date
|
|
542
|
+
try {
|
|
543
|
+
endDate = new Date(data.endDate)
|
|
544
|
+
startDate = new Date(data.startDate)
|
|
545
|
+
} catch (error: any) {
|
|
546
|
+
throw new Error("The dates should follow this format: 2023-07-04T00:00:00.")
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (endDate <= startDate) throw new Error("The end date should be greater than the start date.")
|
|
550
|
+
|
|
551
|
+
const currentDate = new Date()
|
|
552
|
+
|
|
553
|
+
if (endDate <= currentDate || startDate <= currentDate)
|
|
554
|
+
throw new Error("The start and end dates should be in the future.")
|
|
555
|
+
|
|
556
|
+
// validate penalty
|
|
557
|
+
if (data.penalty <= 0) throw new Error("The penalty should be greater than zero.")
|
|
558
|
+
|
|
559
|
+
const circuits: CircuitDocument[] = []
|
|
560
|
+
const urlPattern = /(https?:\/\/[^\s]+)/g
|
|
561
|
+
const commitHashPattern = /^[a-f0-9]{40}$/i
|
|
562
|
+
|
|
563
|
+
const circuitArtifacts: CeremonySetupTemplateCircuitArtifacts[] = []
|
|
564
|
+
|
|
565
|
+
for (let i = 0; i < data.circuits.length; i++) {
|
|
566
|
+
const circuitData = data.circuits[i]
|
|
567
|
+
const { artifacts } = circuitData
|
|
568
|
+
circuitArtifacts.push({
|
|
569
|
+
artifacts
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
// where we storing the r1cs downloaded
|
|
573
|
+
const localR1csPath = `./${circuitData.name}.r1cs`
|
|
574
|
+
// where we storing the wasm downloaded
|
|
575
|
+
const localWasmPath = `./${circuitData.name}.wasm`
|
|
576
|
+
|
|
577
|
+
// download the r1cs to extract the metadata
|
|
578
|
+
const streamPipeline = promisify(pipeline)
|
|
579
|
+
|
|
580
|
+
// Make the call.
|
|
581
|
+
const responseR1CS = await fetch(artifacts.r1csStoragePath)
|
|
582
|
+
|
|
583
|
+
// Handle errors.
|
|
584
|
+
if (!responseR1CS.ok && responseR1CS.status !== 200)
|
|
585
|
+
throw new Error(
|
|
586
|
+
`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.`
|
|
587
|
+
)
|
|
588
|
+
|
|
589
|
+
await streamPipeline(responseR1CS.body!, createWriteStream(localR1csPath))
|
|
590
|
+
// Write the file locally
|
|
591
|
+
|
|
592
|
+
// extract the metadata from the r1cs
|
|
593
|
+
const metadata = getR1CSInfo(localR1csPath)
|
|
594
|
+
|
|
595
|
+
// download wasm too to ensure it's available
|
|
596
|
+
const responseWASM = await fetch(artifacts.wasmStoragePath)
|
|
597
|
+
if (!responseWASM.ok && responseWASM.status !== 200)
|
|
598
|
+
throw new Error(
|
|
599
|
+
`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.`
|
|
600
|
+
)
|
|
601
|
+
await streamPipeline(responseWASM.body!, createWriteStream(localWasmPath))
|
|
602
|
+
|
|
603
|
+
// validate that the circuit hash and template links are valid
|
|
604
|
+
const { template } = circuitData
|
|
605
|
+
|
|
606
|
+
const URLMatch = template.source.match(urlPattern)
|
|
607
|
+
if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
|
|
608
|
+
throw new Error("You should provide the URL to the circuits templates on GitHub.")
|
|
609
|
+
|
|
610
|
+
const hashMatch = template.commitHash.match(commitHashPattern)
|
|
611
|
+
if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
|
|
612
|
+
throw new Error("You should provide a valid commit hash of the circuit templates.")
|
|
613
|
+
|
|
614
|
+
// calculate the hash of the r1cs file
|
|
615
|
+
const r1csBlake2bHash = await blake512FromPath(localR1csPath)
|
|
616
|
+
|
|
617
|
+
const circuitPrefix = extractPrefix(circuitData.name)
|
|
618
|
+
|
|
619
|
+
// filenames
|
|
620
|
+
const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
|
|
621
|
+
const r1csCompleteFilename = `${circuitData.name}.r1cs`
|
|
622
|
+
const wasmCompleteFilename = `${circuitData.name}.wasm`
|
|
623
|
+
const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
|
|
624
|
+
const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
|
|
625
|
+
|
|
626
|
+
// storage paths
|
|
627
|
+
const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename)
|
|
628
|
+
const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename)
|
|
629
|
+
const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit)
|
|
630
|
+
const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename)
|
|
631
|
+
|
|
632
|
+
const files: any = {
|
|
633
|
+
potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
|
|
634
|
+
r1csFilename: r1csCompleteFilename,
|
|
635
|
+
wasmFilename: wasmCompleteFilename,
|
|
636
|
+
initialZkeyFilename: firstZkeyCompleteFilename,
|
|
637
|
+
potStoragePath: potStorageFilePath,
|
|
638
|
+
r1csStoragePath: r1csStorageFilePath,
|
|
639
|
+
wasmStoragePath: wasmStorageFilePath,
|
|
640
|
+
initialZkeyStoragePath: zkeyStorageFilePath,
|
|
641
|
+
r1csBlake2bHash
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// validate that the compiler hash is a valid hash
|
|
645
|
+
const { compiler } = circuitData
|
|
646
|
+
const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
|
|
647
|
+
if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
|
|
648
|
+
throw new Error("You should provide a valid commit hash of the circuit compiler.")
|
|
649
|
+
|
|
650
|
+
// validate that the verification options are valid
|
|
651
|
+
const { verification } = circuitData
|
|
652
|
+
if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
|
|
653
|
+
throw new Error("Please enter a valid verification mechanism: either CF or VM")
|
|
654
|
+
|
|
655
|
+
// @todo VM parameters verification
|
|
656
|
+
// if (verification['cfOrVM'] === "VM") {}
|
|
657
|
+
|
|
658
|
+
// check that the timeout is provided for the correct configuration
|
|
659
|
+
let dynamicThreshold: number | undefined
|
|
660
|
+
let fixedTimeWindow: number | undefined
|
|
661
|
+
|
|
662
|
+
let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
|
|
663
|
+
|
|
664
|
+
if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
|
|
665
|
+
if (circuitData.dynamicThreshold <= 0) throw new Error("The dynamic threshold should be > 0.")
|
|
666
|
+
dynamicThreshold = circuitData.dynamicThreshold
|
|
667
|
+
|
|
668
|
+
// the Circuit data for the ceremony setup
|
|
669
|
+
circuit = {
|
|
670
|
+
name: circuitData.name,
|
|
671
|
+
description: circuitData.description,
|
|
672
|
+
prefix: circuitPrefix,
|
|
673
|
+
sequencePosition: i + 1,
|
|
674
|
+
metadata,
|
|
675
|
+
files,
|
|
676
|
+
template,
|
|
677
|
+
compiler,
|
|
678
|
+
verification,
|
|
679
|
+
dynamicThreshold,
|
|
680
|
+
avgTimings: {
|
|
681
|
+
contributionComputation: 0,
|
|
682
|
+
fullContribution: 0,
|
|
683
|
+
verifyCloudFunction: 0
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
|
|
689
|
+
if (circuitData.fixedTimeWindow <= 0) throw new Error("The fixed time window threshold should be > 0.")
|
|
690
|
+
fixedTimeWindow = circuitData.fixedTimeWindow
|
|
691
|
+
|
|
692
|
+
// the Circuit data for the ceremony setup
|
|
693
|
+
circuit = {
|
|
694
|
+
name: circuitData.name,
|
|
695
|
+
description: circuitData.description,
|
|
696
|
+
prefix: circuitPrefix,
|
|
697
|
+
sequencePosition: i + 1,
|
|
698
|
+
metadata,
|
|
699
|
+
files,
|
|
700
|
+
template,
|
|
701
|
+
compiler,
|
|
702
|
+
verification,
|
|
703
|
+
fixedTimeWindow,
|
|
704
|
+
avgTimings: {
|
|
705
|
+
contributionComputation: 0,
|
|
706
|
+
fullContribution: 0,
|
|
707
|
+
verifyCloudFunction: 0
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
circuits.push(circuit)
|
|
713
|
+
|
|
714
|
+
// remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
|
|
715
|
+
if (cleanup) fs.unlinkSync(localR1csPath)
|
|
716
|
+
fs.unlinkSync(localWasmPath)
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const setupData: SetupCeremonyData = {
|
|
720
|
+
ceremonyInputData: {
|
|
721
|
+
title: data.title,
|
|
722
|
+
description: data.description,
|
|
723
|
+
startDate: startDate.valueOf(),
|
|
724
|
+
endDate: endDate.valueOf(),
|
|
725
|
+
timeoutMechanismType: data.timeoutMechanismType,
|
|
726
|
+
penalty: data.penalty
|
|
727
|
+
},
|
|
728
|
+
ceremonyPrefix: extractPrefix(data.title),
|
|
729
|
+
circuits,
|
|
730
|
+
circuitArtifacts
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return setupData
|
|
734
|
+
} catch (error: any) {
|
|
735
|
+
throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
|
|
736
|
+
}
|
|
737
|
+
}
|
package/src/helpers/vm.ts
CHANGED
|
@@ -82,7 +82,7 @@ export const vmDependenciesAndCacheArtifactsCommand = (
|
|
|
82
82
|
zKeyPath: string,
|
|
83
83
|
potPath: string,
|
|
84
84
|
snsTopic: string,
|
|
85
|
-
region: string
|
|
85
|
+
region: string
|
|
86
86
|
): Array<string> => [
|
|
87
87
|
"#!/bin/bash",
|
|
88
88
|
'MARKER_FILE="/var/run/my_script_ran"',
|
|
@@ -93,8 +93,13 @@ export const vmDependenciesAndCacheArtifactsCommand = (
|
|
|
93
93
|
// eslint-disable-next-line no-template-curly-in-string
|
|
94
94
|
"touch ${MARKER_FILE}",
|
|
95
95
|
"sudo yum update -y",
|
|
96
|
-
"curl -
|
|
97
|
-
"
|
|
96
|
+
"curl -O https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz",
|
|
97
|
+
"tar -xf node-v16.13.0-linux-x64.tar.xz",
|
|
98
|
+
"mv node-v16.13.0-linux-x64 nodejs",
|
|
99
|
+
"sudo mv nodejs /opt/",
|
|
100
|
+
"echo 'export NODEJS_HOME=/opt/nodejs' >> /etc/profile",
|
|
101
|
+
"echo 'export PATH=$NODEJS_HOME/bin:$PATH' >> /etc/profile",
|
|
102
|
+
"source /etc/profile",
|
|
98
103
|
"npm install -g snarkjs",
|
|
99
104
|
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
|
|
100
105
|
`aws s3 cp s3://${potPath} /var/tmp/pot.ptau`,
|
|
@@ -118,6 +123,7 @@ export const vmContributionVerificationCommand = (
|
|
|
118
123
|
lastZkeyStoragePath: string,
|
|
119
124
|
verificationTranscriptStoragePathAndFilename: string
|
|
120
125
|
): Array<string> => [
|
|
126
|
+
`source /etc/profile`,
|
|
121
127
|
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey > /var/tmp/log.txt`,
|
|
122
128
|
`snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`,
|
|
123
129
|
`aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`,
|
|
@@ -153,7 +159,7 @@ export const createEC2Instance = async (
|
|
|
153
159
|
diskType: DiskTypeForVM
|
|
154
160
|
): Promise<EC2Instance> => {
|
|
155
161
|
// Get the AWS variables.
|
|
156
|
-
const { amiId,
|
|
162
|
+
const { amiId, instanceProfileArn } = getAWSVariables()
|
|
157
163
|
|
|
158
164
|
// Parametrize the VM EC2 instance.
|
|
159
165
|
const params: RunInstancesCommandInput = {
|
|
@@ -163,7 +169,7 @@ export const createEC2Instance = async (
|
|
|
163
169
|
MinCount: 1,
|
|
164
170
|
// nb. to find this: iam -> roles -> role_name.
|
|
165
171
|
IamInstanceProfile: {
|
|
166
|
-
Arn:
|
|
172
|
+
Arn: instanceProfileArn
|
|
167
173
|
},
|
|
168
174
|
// nb. for running commands at the startup.
|
|
169
175
|
UserData: Buffer.from(commands.join("\n")).toString("base64"),
|
package/src/index.ts
CHANGED
|
@@ -87,7 +87,7 @@ export {
|
|
|
87
87
|
verifyContribution,
|
|
88
88
|
checkAndPrepareCoordinatorForFinalization,
|
|
89
89
|
finalizeCircuit,
|
|
90
|
-
finalizeCeremony
|
|
90
|
+
finalizeCeremony
|
|
91
91
|
} from "./helpers/functions"
|
|
92
92
|
export { toHex, blake512FromPath, computeSHA256ToHex, compareHashes } from "./helpers/crypto"
|
|
93
93
|
export {
|
|
@@ -159,4 +159,4 @@ export {
|
|
|
159
159
|
createEC2Client,
|
|
160
160
|
vmContributionVerificationCommand,
|
|
161
161
|
retrieveCommandStatus
|
|
162
|
-
} from "./helpers/vm"
|
|
162
|
+
} from "./helpers/vm"
|