@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.
Files changed (52) hide show
  1. package/README.md +2 -2
  2. package/dist/index.mjs +376 -268
  3. package/dist/index.node.js +376 -266
  4. package/dist/types/src/helpers/authentication.d.ts.map +1 -1
  5. package/dist/types/src/helpers/constants.d.ts +5 -2
  6. package/dist/types/src/helpers/constants.d.ts.map +1 -1
  7. package/dist/types/src/helpers/contracts.d.ts.map +1 -1
  8. package/dist/types/src/helpers/crypto.d.ts.map +1 -1
  9. package/dist/types/src/helpers/database.d.ts +8 -0
  10. package/dist/types/src/helpers/database.d.ts.map +1 -1
  11. package/dist/types/src/helpers/functions.d.ts +2 -1
  12. package/dist/types/src/helpers/functions.d.ts.map +1 -1
  13. package/dist/types/src/helpers/security.d.ts +1 -1
  14. package/dist/types/src/helpers/security.d.ts.map +1 -1
  15. package/dist/types/src/helpers/services.d.ts.map +1 -1
  16. package/dist/types/src/helpers/storage.d.ts +5 -2
  17. package/dist/types/src/helpers/storage.d.ts.map +1 -1
  18. package/dist/types/src/helpers/utils.d.ts +33 -21
  19. package/dist/types/src/helpers/utils.d.ts.map +1 -1
  20. package/dist/types/src/helpers/verification.d.ts +3 -2
  21. package/dist/types/src/helpers/verification.d.ts.map +1 -1
  22. package/dist/types/src/helpers/vm.d.ts.map +1 -1
  23. package/dist/types/src/index.d.ts +2 -2
  24. package/dist/types/src/index.d.ts.map +1 -1
  25. package/dist/types/src/types/index.d.ts +9 -4
  26. package/dist/types/src/types/index.d.ts.map +1 -1
  27. package/dist/types/test/data/generators.d.ts +32 -0
  28. package/dist/types/test/data/generators.d.ts.map +1 -0
  29. package/dist/types/test/data/samples.d.ts +40 -0
  30. package/dist/types/test/data/samples.d.ts.map +1 -0
  31. package/dist/types/test/utils/authentication.d.ts +72 -0
  32. package/dist/types/test/utils/authentication.d.ts.map +1 -0
  33. package/dist/types/test/utils/configs.d.ts +52 -0
  34. package/dist/types/test/utils/configs.d.ts.map +1 -0
  35. package/dist/types/test/utils/index.d.ts +4 -0
  36. package/dist/types/test/utils/index.d.ts.map +1 -0
  37. package/dist/types/test/utils/storage.d.ts +126 -0
  38. package/dist/types/test/utils/storage.d.ts.map +1 -0
  39. package/package.json +7 -8
  40. package/src/helpers/constants.ts +39 -31
  41. package/src/helpers/contracts.ts +3 -3
  42. package/src/helpers/crypto.ts +5 -1
  43. package/src/helpers/database.ts +13 -0
  44. package/src/helpers/functions.ts +1 -1
  45. package/src/helpers/security.ts +11 -8
  46. package/src/helpers/services.ts +3 -3
  47. package/src/helpers/storage.ts +15 -3
  48. package/src/helpers/utils.ts +335 -263
  49. package/src/helpers/verification.ts +6 -6
  50. package/src/helpers/vm.ts +28 -7
  51. package/src/index.ts +5 -3
  52. package/src/types/index.ts +32 -8
@@ -1,18 +1,21 @@
1
1
  import { Firestore } from "firebase/firestore"
2
2
  import fs, { ReadPosition, createWriteStream } from "fs"
3
- import { utils as ffUtils } from "ffjavascript"
4
3
  import winston, { Logger } from "winston"
5
4
  import fetch from "@adobe/node-fetch-retry"
6
- import {
7
- CircuitMetadata,
8
- Contribution,
9
- CircuitDocument,
5
+ import { pipeline } from "stream"
6
+ import { promisify } from "util"
7
+ import {
8
+ CircuitMetadata,
9
+ Contribution,
10
+ CircuitDocument,
10
11
  CircuitInputData,
11
- ContributionValidity,
12
- FirebaseDocumentInfo,
13
- SetupCeremonyData,
12
+ ContributionValidity,
13
+ FirebaseDocumentInfo,
14
+ SetupCeremonyData,
14
15
  CeremonySetupTemplate,
15
16
  CeremonySetupTemplateCircuitArtifacts,
17
+ StringifiedBigInts,
18
+ BigIntVariants
16
19
  } from "../types/index"
17
20
  import { finalContributionIndex, genesisZkeyIndex, potFilenameTemplate } from "./constants"
18
21
  import {
@@ -22,239 +25,32 @@ import {
22
25
  getContributionsCollectionPath
23
26
  } from "./database"
24
27
  import { CeremonyTimeoutType } from "../types/enums"
25
- import {
26
- getPotStorageFilePath,
27
- getR1csStorageFilePath,
28
- getWasmStorageFilePath,
28
+ import {
29
+ getPotStorageFilePath,
30
+ getR1csStorageFilePath,
31
+ getWasmStorageFilePath,
29
32
  getZkeyStorageFilePath
30
33
  } from "./storage"
31
34
  import { blake512FromPath } from "./crypto"
32
- import { pipeline } from "stream"
33
- import { promisify } from "util"
34
35
 
35
36
  /**
36
- * Parse and validate that the ceremony configuration is correct
37
- * @notice this does not upload any files to storage
38
- * @param path <string> - the path to the configuration file
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
37
+ * Return a string with double digits if the provided input is one digit only.
38
+ * @param in <number> - the input number to be converted.
39
+ * @returns <string> - the two digits stringified number derived from the conversion.
41
40
  */
42
- export const parseCeremonyFile = async (path: string, cleanup: boolean = false): Promise<SetupCeremonyData> => {
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
-
91
- // where we storing the r1cs downloaded
92
- const localR1csPath = `./${circuitData.name}.r1cs`
93
- // where we storing the wasm downloaded
94
- const localWasmPath = `./${circuitData.name}.wasm`
95
-
96
- // download the r1cs to extract the metadata
97
- const streamPipeline = promisify(pipeline)
98
-
99
- // Make the call.
100
- const responseR1CS = await fetch(artifacts.r1csStoragePath)
101
-
102
- // Handle errors.
103
- if (!responseR1CS.ok && responseR1CS.status !== 200)
104
- 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.`)
105
-
106
- await streamPipeline(responseR1CS.body!, createWriteStream(localR1csPath))
107
- // Write the file locally
108
-
109
- // extract the metadata from the r1cs
110
- const metadata = getR1CSInfo(localR1csPath)
111
-
112
- // download wasm too to ensure it's available
113
- const responseWASM = await fetch(artifacts.wasmStoragePath)
114
- if (!responseWASM.ok && responseWASM.status !== 200)
115
- 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.`)
116
- await streamPipeline(responseWASM.body!, createWriteStream(localWasmPath))
117
-
118
- // validate that the circuit hash and template links are valid
119
- const template = circuitData.template
120
-
121
- const URLMatch = template.source.match(urlPattern)
122
- if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1) throw new Error("You should provide the URL to the circuits templates on GitHub.")
123
-
124
- const hashMatch = template.commitHash.match(commitHashPattern)
125
- if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit templates.")
126
-
127
- // calculate the hash of the r1cs file
128
- const r1csBlake2bHash = await blake512FromPath(localR1csPath)
129
-
130
- const circuitPrefix = extractPrefix(circuitData.name)
131
-
132
- // filenames
133
- const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
134
- const r1csCompleteFilename = `${circuitData.name}.r1cs`
135
- const wasmCompleteFilename = `${circuitData.name}.wasm`
136
- const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
137
- const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
138
-
139
- // storage paths
140
- const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename)
141
- const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename)
142
- const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit)
143
- const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename)
144
-
145
- const files: any = {
146
- potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
147
- r1csFilename: r1csCompleteFilename,
148
- wasmFilename: wasmCompleteFilename,
149
- initialZkeyFilename: firstZkeyCompleteFilename,
150
- potStoragePath: potStorageFilePath,
151
- r1csStoragePath: r1csStorageFilePath,
152
- wasmStoragePath: wasmStorageFilePath,
153
- initialZkeyStoragePath: zkeyStorageFilePath,
154
- r1csBlake2bHash: r1csBlake2bHash
155
- }
41
+ export const convertToDoubleDigits = (amount: number): string => (amount < 10 ? `0${amount}` : amount.toString())
156
42
 
157
- // validate that the compiler hash is a valid hash
158
- const compiler = circuitData.compiler
159
- const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
160
- if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1) throw new Error("You should provide a valid commit hash of the circuit compiler.")
161
-
162
- // validate that the verification options are valid
163
- const verification = circuitData.verification
164
- if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
165
- throw new Error("Please enter a valid verification mechanism: either CF or VM")
166
-
167
- // @todo VM parameters verification
168
- // if (verification['cfOrVM'] === "VM") {}
169
-
170
- // check that the timeout is provided for the correct configuration
171
- let dynamicThreshold: number | undefined
172
- let fixedTimeWindow: number | undefined
173
-
174
- let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
175
-
176
- if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
177
- if (circuitData.dynamicThreshold <= 0)
178
- throw new Error("The dynamic threshold should be > 0.")
179
- dynamicThreshold = circuitData.dynamicThreshold
180
-
181
- // the Circuit data for the ceremony setup
182
- circuit = {
183
- name: circuitData.name,
184
- description: circuitData.description,
185
- prefix: circuitPrefix,
186
- sequencePosition: i+1,
187
- metadata: metadata,
188
- files: files,
189
- template: template,
190
- compiler: compiler,
191
- verification: verification,
192
- dynamicThreshold: dynamicThreshold,
193
- avgTimings: {
194
- contributionComputation: 0,
195
- fullContribution: 0,
196
- verifyCloudFunction: 0
197
- },
198
-
199
- }
200
- }
201
-
202
- if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
203
- if (circuitData.fixedTimeWindow <= 0)
204
- throw new Error("The fixed time window threshold should be > 0.")
205
- fixedTimeWindow = circuitData.fixedTimeWindow
206
-
207
-
208
- // the Circuit data for the ceremony setup
209
- circuit = {
210
- name: circuitData.name,
211
- description: circuitData.description,
212
- prefix: circuitPrefix,
213
- sequencePosition: i+1,
214
- metadata: metadata,
215
- files: files,
216
- template: template,
217
- compiler: compiler,
218
- verification: verification,
219
- fixedTimeWindow: fixedTimeWindow,
220
- avgTimings: {
221
- contributionComputation: 0,
222
- fullContribution: 0,
223
- verifyCloudFunction: 0
224
- },
225
-
226
- }
227
- }
228
-
229
-
230
- circuits.push(circuit)
231
-
232
- // remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
233
- if (cleanup)
234
- fs.unlinkSync(localR1csPath)
235
- fs.unlinkSync(localWasmPath)
236
- }
237
-
238
- const setupData: SetupCeremonyData = {
239
- ceremonyInputData: {
240
- title: data.title,
241
- description: data.description,
242
- startDate: startDate.valueOf(),
243
- endDate: endDate.valueOf(),
244
- timeoutMechanismType: data.timeoutMechanismType,
245
- penalty: data.penalty
246
- },
247
- ceremonyPrefix: extractPrefix(data.title),
248
- circuits: circuits,
249
- circuitArtifacts: circuitArtifacts
250
- }
251
-
252
- return setupData
253
-
254
- } catch (error: any) {
255
- throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
256
- }
257
- }
43
+ /**
44
+ * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
45
+ * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
46
+ * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
47
+ * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
48
+ * @param str <string> - the arbitrary string from which to extract the prefix.
49
+ * @returns <string> - the resulting prefix.
50
+ */
51
+ export const extractPrefix = (str: string): string =>
52
+ // eslint-disable-next-line no-useless-escape
53
+ str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
258
54
 
259
55
  /**
260
56
  * Extract data from a R1CS metadata file generated with a custom file-based logger.
@@ -326,18 +122,6 @@ export const formatZkeyIndex = (progress: number): string => {
326
122
  export const extractPoTFromFilename = (potCompleteFilename: string): number =>
327
123
  Number(potCompleteFilename.split("_").pop()?.split(".").at(0))
328
124
 
329
- /**
330
- * Extract a prefix consisting of alphanumeric and underscore characters from a string with arbitrary characters.
331
- * @dev replaces all special symbols and whitespaces with an underscore char ('_'). Convert all uppercase chars to lowercase.
332
- * @notice example: str = 'Multiplier-2!2.4.zkey'; output prefix = 'multiplier_2_2_4.zkey'.
333
- * NB. Prefix extraction is a key process that conditions the name of the ceremony artifacts, download/upload from/to storage, collections paths.
334
- * @param str <string> - the arbitrary string from which to extract the prefix.
335
- * @returns <string> - the resulting prefix.
336
- */
337
- export const extractPrefix = (str: string): string =>
338
- // eslint-disable-next-line no-useless-escape
339
- str.replace(/[`\s~!@#$%^&*()|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "-").toLowerCase()
340
-
341
125
  /**
342
126
  * Automate the generation of an entropy for a contribution.
343
127
  * @dev Took inspiration from here https://github.com/glamperd/setup-mpc-ui/blob/master/client/src/state/Compute.tsx#L112.
@@ -444,9 +228,11 @@ export const getPublicAttestationPreambleForContributor = (
444
228
  ceremonyName: string,
445
229
  isFinalizing: boolean
446
230
  ) =>
447
- `Hey, I'm ${contributorIdentifier} and I have ${
448
- isFinalizing ? "finalized" : "contributed to"
449
- } the ${ceremonyName} MPC Phase2 Trusted Setup ceremony.\nThe following are my contribution signatures:`
231
+ `Hey, I'm ${contributorIdentifier} and I have ${isFinalizing ? "finalized" : "contributed to"} the ${ceremonyName}${
232
+ ceremonyName.toLowerCase().includes("trusted setup") || ceremonyName.toLowerCase().includes("ceremony")
233
+ ? "."
234
+ : " MPC Phase2 Trusted Setup ceremony."
235
+ }\nThe following are my contribution signatures:`
450
236
 
451
237
  /**
452
238
  * Check and prepare public attestation for the contributor made only of its valid contributions.
@@ -541,7 +327,7 @@ export const createCustomLoggerForFile = (filename: string, level: winston.Logge
541
327
  level,
542
328
  transports: new winston.transports.File({
543
329
  filename,
544
- format: winston.format.printf((log) => log.message),
330
+ format: winston.format.printf((log) => log.message as string),
545
331
  level
546
332
  })
547
333
  })
@@ -573,6 +359,48 @@ export const readBytesFromFile = (
573
359
  return buffer
574
360
  }
575
361
 
362
+ /**
363
+ * Given a buffer in little endian format, convert it to bigint
364
+ * @param buffer
365
+ * @returns
366
+ */
367
+ export function leBufferToBigint(buffer: Buffer): bigint {
368
+ return BigInt(`0x${buffer.reverse().toString("hex")}`)
369
+ }
370
+
371
+ /**
372
+ * Given an input containing string values, convert them
373
+ * to bigint
374
+ * @param input - The input to convert
375
+ * @returns the input with string values converted to bigint
376
+ */
377
+ export const unstringifyBigInts = (input: StringifiedBigInts): BigIntVariants => {
378
+ if (typeof input === "string" && /^[0-9]+$/.test(input)) {
379
+ return BigInt(input)
380
+ }
381
+
382
+ if (typeof input === "string" && /^0x[0-9a-fA-F]+$/.test(input)) {
383
+ return BigInt(input)
384
+ }
385
+
386
+ if (Array.isArray(input)) {
387
+ return input.map(unstringifyBigInts)
388
+ }
389
+
390
+ if (input === null) {
391
+ return null
392
+ }
393
+
394
+ if (typeof input === "object") {
395
+ return Object.entries(input).reduce<Record<string, bigint>>((acc, [key, value]) => {
396
+ acc[key] = unstringifyBigInts(value) as bigint
397
+ return acc
398
+ }, {})
399
+ }
400
+
401
+ return input
402
+ }
403
+
576
404
  /**
577
405
  * Return the info about the R1CS file.ù
578
406
  * @dev this method was built taking inspiration from
@@ -635,7 +463,7 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
635
463
 
636
464
  try {
637
465
  // Get 'number of section' (jump magic r1cs and version1 data).
638
- const numberOfSections = ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, 8))
466
+ const numberOfSections = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, 8))
639
467
 
640
468
  // Jump to first section.
641
469
  pointer = 12
@@ -643,13 +471,13 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
643
471
  // For each section
644
472
  for (let i = 0; i < numberOfSections; i++) {
645
473
  // Read section type.
646
- const sectionType = ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer))
474
+ const sectionType = leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer))
647
475
 
648
476
  // Jump to section size.
649
477
  pointer += 4
650
478
 
651
479
  // Read section size
652
- const sectionSize = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
480
+ const sectionSize = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
653
481
 
654
482
  // If at header section (0x00000001 : Header Section).
655
483
  if (sectionType === BigInt(1)) {
@@ -684,22 +512,22 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
684
512
  pointer += sectionSize - 20
685
513
 
686
514
  // Read R1CS info.
687
- wires = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
515
+ wires = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
688
516
  pointer += 4
689
517
 
690
- publicOutputs = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
518
+ publicOutputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
691
519
  pointer += 4
692
520
 
693
- publicInputs = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
521
+ publicInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
694
522
  pointer += 4
695
523
 
696
- privateInputs = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
524
+ privateInputs = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
697
525
  pointer += 4
698
526
 
699
- labels = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
527
+ labels = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 8, pointer)))
700
528
  pointer += 8
701
529
 
702
- constraints = Number(ffUtils.leBuff2int(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
530
+ constraints = Number(leBufferToBigint(readBytesFromFile(localR1CSFilePath, 0, 4, pointer)))
703
531
  }
704
532
 
705
533
  pointer += 8 + Number(sectionSize)
@@ -723,8 +551,252 @@ export const getR1CSInfo = (localR1CSFilePath: string): CircuitMetadata => {
723
551
  }
724
552
 
725
553
  /**
726
- * Return a string with double digits if the provided input is one digit only.
727
- * @param in <number> - the input number to be converted.
728
- * @returns <string> - the two digits stringified number derived from the conversion.
554
+ * Parse and validate that the ceremony configuration is correct
555
+ * @notice this does not upload any files to storage
556
+ * @param path <string> - the path to the configuration file
557
+ * @param cleanup <boolean> - whether to delete the r1cs file after parsing
558
+ * @returns any - the data to pass to the cloud function for setup and the circuit artifacts
729
559
  */
730
- export const convertToDoubleDigits = (amount: number): string => (amount < 10 ? `0${amount}` : amount.toString())
560
+ export const parseCeremonyFile = async (path: string, cleanup: boolean = false): Promise<SetupCeremonyData> => {
561
+ // check that the path exists
562
+ if (!fs.existsSync(path))
563
+ throw new Error(
564
+ "The provided path to the configuration file does not exist. Please provide an absolute path and try again."
565
+ )
566
+
567
+ try {
568
+ // read the data
569
+ const data: CeremonySetupTemplate = JSON.parse(fs.readFileSync(path).toString())
570
+
571
+ // verify that the data is correct
572
+ if (
573
+ data.timeoutMechanismType !== CeremonyTimeoutType.DYNAMIC &&
574
+ data.timeoutMechanismType !== CeremonyTimeoutType.FIXED
575
+ )
576
+ throw new Error("Invalid timeout type. Please choose between DYNAMIC and FIXED.")
577
+
578
+ // validate that we have at least 1 circuit input data
579
+ if (!data.circuits || data.circuits.length === 0)
580
+ throw new Error("You need to provide the data for at least 1 circuit.")
581
+
582
+ // validate that the end date is in the future
583
+ let endDate: Date
584
+ let startDate: Date
585
+ try {
586
+ endDate = new Date(data.endDate)
587
+ startDate = new Date(data.startDate)
588
+ } catch (error: any) {
589
+ throw new Error("The dates should follow this format: 2023-07-04T00:00:00.")
590
+ }
591
+
592
+ if (endDate <= startDate) throw new Error("The end date should be greater than the start date.")
593
+
594
+ const currentDate = new Date()
595
+
596
+ if (endDate <= currentDate || startDate <= currentDate)
597
+ throw new Error("The start and end dates should be in the future.")
598
+
599
+ // validate penalty
600
+ if (data.penalty <= 0) throw new Error("The penalty should be greater than zero.")
601
+
602
+ const circuits: CircuitDocument[] = []
603
+ const urlPattern = /(https?:\/\/[^\s]+)/g
604
+ const commitHashPattern = /^[a-f0-9]{40}$/i
605
+
606
+ const circuitArtifacts: CeremonySetupTemplateCircuitArtifacts[] = []
607
+
608
+ for (let i = 0; i < data.circuits.length; i++) {
609
+ const circuitData = data.circuits[i]
610
+ const { artifacts } = circuitData
611
+ circuitArtifacts.push({
612
+ artifacts
613
+ })
614
+
615
+ // where we storing the r1cs downloaded
616
+ const localR1csPath = `./${circuitData.name}.r1cs`
617
+ // where we storing the wasm downloaded
618
+ const localWasmPath = `./${circuitData.name}.wasm`
619
+
620
+ // download the r1cs to extract the metadata
621
+ const streamPipeline = promisify(pipeline)
622
+
623
+ // Check if r1cs file already exists
624
+ let r1csExists = false
625
+ if (fs.existsSync(localR1csPath)) {
626
+ console.log(`Found existing r1cs file for circuit ${circuitData.name}. Skipping download.`)
627
+ r1csExists = true
628
+ }
629
+
630
+ if (!r1csExists) {
631
+ // Make the call to download r1cs.
632
+ const responseR1CS = await fetch(artifacts.r1csStoragePath)
633
+
634
+ // Handle errors.
635
+ if (!responseR1CS.ok && responseR1CS.status !== 200)
636
+ throw new Error(
637
+ `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.`
638
+ )
639
+
640
+ // Write the file locally
641
+ await streamPipeline(responseR1CS.body!, createWriteStream(localR1csPath))
642
+ console.log(`Downloaded r1cs file for circuit ${circuitData.name}.`)
643
+ }
644
+
645
+ // extract the metadata from the r1cs
646
+ const metadata = getR1CSInfo(localR1csPath)
647
+
648
+ // Check if wasm file already exists
649
+ let wasmExists = false
650
+ if (fs.existsSync(localWasmPath)) {
651
+ console.log(`Found existing wasm file for circuit ${circuitData.name}. Skipping download.`)
652
+ wasmExists = true
653
+ }
654
+
655
+ if (!wasmExists) {
656
+ // download wasm if it's not available
657
+ const responseWASM = await fetch(artifacts.wasmStoragePath)
658
+ if (!responseWASM.ok && responseWASM.status !== 200)
659
+ throw new Error(
660
+ `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.`
661
+ )
662
+ await streamPipeline(responseWASM.body!, createWriteStream(localWasmPath))
663
+ console.log(`Downloaded wasm file for circuit ${circuitData.name}.`)
664
+ }
665
+
666
+ // validate that the circuit hash and template links are valid
667
+ const { template } = circuitData
668
+
669
+ const URLMatch = template.source.match(urlPattern)
670
+ if (!URLMatch || URLMatch.length === 0 || URLMatch.length > 1)
671
+ throw new Error("You should provide the URL to the circuits templates on GitHub.")
672
+
673
+ const hashMatch = template.commitHash.match(commitHashPattern)
674
+ if (!hashMatch || hashMatch.length === 0 || hashMatch.length > 1)
675
+ throw new Error("You should provide a valid commit hash of the circuit templates.")
676
+
677
+ // calculate the hash of the r1cs file
678
+ const r1csBlake2bHash = await blake512FromPath(localR1csPath)
679
+
680
+ const circuitPrefix = extractPrefix(circuitData.name)
681
+
682
+ // filenames
683
+ const doubleDigitsPowers = convertToDoubleDigits(metadata.pot!)
684
+ const r1csCompleteFilename = `${circuitData.name}.r1cs`
685
+ const wasmCompleteFilename = `${circuitData.name}.wasm`
686
+ const smallestPowersOfTauCompleteFilenameForCircuit = `${potFilenameTemplate}${doubleDigitsPowers}.ptau`
687
+ const firstZkeyCompleteFilename = `${circuitPrefix}_${genesisZkeyIndex}.zkey`
688
+
689
+ // storage paths
690
+ const r1csStorageFilePath = getR1csStorageFilePath(circuitPrefix, r1csCompleteFilename)
691
+ const wasmStorageFilePath = getWasmStorageFilePath(circuitPrefix, wasmCompleteFilename)
692
+ const potStorageFilePath = getPotStorageFilePath(smallestPowersOfTauCompleteFilenameForCircuit)
693
+ const zkeyStorageFilePath = getZkeyStorageFilePath(circuitPrefix, firstZkeyCompleteFilename)
694
+
695
+ const files: any = {
696
+ potFilename: smallestPowersOfTauCompleteFilenameForCircuit,
697
+ r1csFilename: r1csCompleteFilename,
698
+ wasmFilename: wasmCompleteFilename,
699
+ initialZkeyFilename: firstZkeyCompleteFilename,
700
+ potStoragePath: potStorageFilePath,
701
+ r1csStoragePath: r1csStorageFilePath,
702
+ wasmStoragePath: wasmStorageFilePath,
703
+ initialZkeyStoragePath: zkeyStorageFilePath,
704
+ r1csBlake2bHash
705
+ }
706
+
707
+ // validate that the compiler hash is a valid hash
708
+ const { compiler } = circuitData
709
+ const compilerHashMatch = compiler.commitHash.match(commitHashPattern)
710
+ if (!compilerHashMatch || compilerHashMatch.length === 0 || compilerHashMatch.length > 1)
711
+ throw new Error("You should provide a valid commit hash of the circuit compiler.")
712
+
713
+ // validate that the verification options are valid
714
+ const { verification } = circuitData
715
+ if (verification.cfOrVm !== "CF" && verification.cfOrVm !== "VM")
716
+ throw new Error("Please enter a valid verification mechanism: either CF or VM")
717
+
718
+ // @todo VM parameters verification
719
+ // if (verification['cfOrVM'] === "VM") {}
720
+
721
+ // check that the timeout is provided for the correct configuration
722
+ let dynamicThreshold: number | undefined
723
+ let fixedTimeWindow: number | undefined
724
+
725
+ let circuit: CircuitDocument | CircuitInputData = {} as CircuitDocument | CircuitInputData
726
+
727
+ if (data.timeoutMechanismType === CeremonyTimeoutType.DYNAMIC) {
728
+ if (circuitData.dynamicThreshold <= 0) throw new Error("The dynamic threshold should be > 0.")
729
+ dynamicThreshold = circuitData.dynamicThreshold
730
+
731
+ // the Circuit data for the ceremony setup
732
+ circuit = {
733
+ name: circuitData.name,
734
+ description: circuitData.description,
735
+ prefix: circuitPrefix,
736
+ sequencePosition: i + 1,
737
+ metadata,
738
+ files,
739
+ template,
740
+ compiler,
741
+ verification,
742
+ dynamicThreshold,
743
+ avgTimings: {
744
+ contributionComputation: 0,
745
+ fullContribution: 0,
746
+ verifyCloudFunction: 0
747
+ }
748
+ }
749
+ }
750
+
751
+ if (data.timeoutMechanismType === CeremonyTimeoutType.FIXED) {
752
+ if (circuitData.fixedTimeWindow <= 0) throw new Error("The fixed time window threshold should be > 0.")
753
+ fixedTimeWindow = circuitData.fixedTimeWindow
754
+
755
+ // the Circuit data for the ceremony setup
756
+ circuit = {
757
+ name: circuitData.name,
758
+ description: circuitData.description,
759
+ prefix: circuitPrefix,
760
+ sequencePosition: i + 1,
761
+ metadata,
762
+ files,
763
+ template,
764
+ compiler,
765
+ verification,
766
+ fixedTimeWindow,
767
+ avgTimings: {
768
+ contributionComputation: 0,
769
+ fullContribution: 0,
770
+ verifyCloudFunction: 0
771
+ }
772
+ }
773
+ }
774
+
775
+ circuits.push(circuit)
776
+
777
+ // remove the local r1cs and wasm downloads (if used for verifying the config only vs setup)
778
+ if (cleanup) {
779
+ fs.unlinkSync(localR1csPath)
780
+ fs.unlinkSync(localWasmPath)
781
+ }
782
+ }
783
+
784
+ const setupData: SetupCeremonyData = {
785
+ ceremonyInputData: {
786
+ title: data.title,
787
+ description: data.description,
788
+ startDate: startDate.valueOf(),
789
+ endDate: endDate.valueOf(),
790
+ timeoutMechanismType: data.timeoutMechanismType,
791
+ penalty: data.penalty
792
+ },
793
+ ceremonyPrefix: extractPrefix(data.title),
794
+ circuits,
795
+ circuitArtifacts
796
+ }
797
+
798
+ return setupData
799
+ } catch (error: any) {
800
+ throw new Error(`Error while parsing up the ceremony setup file. ${error.message}`)
801
+ }
802
+ }