2
2
3
3
import { zKey } from "snarkjs"
4
4
import boxen from "boxen"
5
- import { createWriteStream , Dirent , renameSync } from "fs"
5
+ import { createWriteStream , Dirent , renameSync , createReadStream } from "fs"
6
6
import { pipeline } from "node:stream"
7
7
import { promisify } from "node:util"
8
8
import fetch from "node-fetch"
9
9
import { Functions } from "firebase/functions"
10
+ import { S3Client , GetObjectCommand } from "@aws-sdk/client-s3"
11
+ import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
10
12
import {
11
13
CeremonyTimeoutType ,
12
14
CircomCompilerData ,
@@ -62,6 +64,7 @@ import {
62
64
getFileStats ,
63
65
checkAndMakeNewDirectoryIfNonexistent
64
66
} from "../lib/files.js"
67
+ import { Readable } from "stream"
65
68
66
69
/**
67
70
* Handle whatever is needed to obtain the input data for a circuit that the coordinator would like to add to the ceremony.
@@ -501,38 +504,54 @@ const setup = async (cmd: { template?: string, auth?: string}) => {
501
504
// if there is the file option, then set up the non interactively
502
505
if ( cmd . template ) {
503
506
// 1. parse the file
504
- // tmp data
505
- const setupCeremonyData = parseCeremonyFile ( cmd . template ! )
507
+ // tmp data - do not cleanup files as we need them
508
+ const setupCeremonyData = await parseCeremonyFile ( cmd . template ! )
506
509
// final setup data
507
510
const ceremonySetupData = setupCeremonyData
508
511
509
512
// create a new bucket
510
513
const bucketName = await handleCeremonyBucketCreation ( firebaseFunctions , ceremonySetupData . ceremonyPrefix )
511
514
console . log ( `\n${ theme . symbols . success } Ceremony bucket name: ${ theme . text . bold ( bucketName ) } ` )
512
515
516
+ // create S3 clienbt
517
+ const s3 = new S3Client ( { region : 'us-east-1' } )
518
+
513
519
// loop through each circuit
514
- for ( const circuit of setupCeremonyData . circuits ) {
520
+ for await ( const circuit of setupCeremonyData . circuits ) {
515
521
// Local paths.
516
522
const index = ceremonySetupData . circuits . indexOf ( circuit )
517
- const r1csLocalPathAndFileName = setupCeremonyData . circuitArtifacts [ index ] . artifacts . r1csLocalFilePath
518
- const wasmLocalPathAndFileName = setupCeremonyData . circuitArtifacts [ index ] . artifacts . wasmLocalFilePath
523
+ const r1csLocalPathAndFileName = `./ ${ circuit . name } .r1cs`
524
+ const wasmLocalPathAndFileName = `./ ${ circuit . name } .wasm`
519
525
const potLocalPathAndFileName = getPotLocalFilePath ( circuit . files . potFilename )
520
526
const zkeyLocalPathAndFileName = getZkeyLocalFilePath ( circuit . files . initialZkeyFilename )
521
527
522
- // 2. download the pot
528
+ // 2. download the pot and wasm files
523
529
const streamPipeline = promisify ( pipeline )
524
- const response = await fetch ( `${ potFileDownloadMainUrl } ${ circuit . files . potFilename } ` )
530
+ const potResponse = await fetch ( `${ potFileDownloadMainUrl } ${ circuit . files . potFilename } ` )
525
531
526
532
// Handle errors.
527
- if ( ! response . ok && response . status !== 200 ) showError ( "Error while setting up the ceremony. Could not download the powers of tau file." , true )
533
+ if ( ! potResponse . ok && potResponse . status !== 200 ) showError ( "Error while setting up the ceremony. Could not download the powers of tau file." , true )
528
534
529
- await streamPipeline ( response . body ! , createWriteStream ( potLocalPathAndFileName ) )
535
+ await streamPipeline ( potResponse . body ! , createWriteStream ( potLocalPathAndFileName ) )
536
+
537
+ // download the wasm to calculate the hash
538
+ const command = new GetObjectCommand ( { Bucket : ceremonySetupData . circuitArtifacts [ index ] . artifacts . bucket , Key : ceremonySetupData . circuitArtifacts [ index ] . artifacts . wasmStoragePath } )
539
+
540
+ const response = await s3 . send ( command )
541
+
542
+ const fileStream = createWriteStream ( wasmLocalPathAndFileName )
543
+ if ( response . $metadata . httpStatusCode !== 200 ) {
544
+ throw new Error ( "There was an error while trying to download the wasm file. Please check that the file has the correct permissions (public) set." )
545
+ }
546
+ // const streamPipeline = promisify(pipeline)
547
+ if ( response . Body instanceof Readable ) {
548
+ response . Body . pipe ( fileStream )
549
+ }
530
550
531
551
// 3. generate the zKey
532
552
await zKey . newZKey ( r1csLocalPathAndFileName , potLocalPathAndFileName , zkeyLocalPathAndFileName , undefined )
533
553
534
554
// 4. calculate the hashes
535
- const r1csBlake2bHash = await blake512FromPath ( r1csLocalPathAndFileName )
536
555
const wasmBlake2bHash = await blake512FromPath ( wasmLocalPathAndFileName )
537
556
const potBlake2bHash = await blake512FromPath ( potLocalPathAndFileName )
538
557
const initialZkeyBlake2bHash = await blake512FromPath ( zkeyLocalPathAndFileName )
@@ -557,7 +576,7 @@ const setup = async (cmd: { template?: string, auth?: string}) => {
557
576
circuit . files . potFilename
558
577
)
559
578
560
- // Upload R1CS to Storage.
579
+ // Move r1cs between buckets
561
580
await handleCircuitArtifactUploadToStorage (
562
581
firebaseFunctions ,
563
582
bucketName ,
@@ -566,7 +585,7 @@ const setup = async (cmd: { template?: string, auth?: string}) => {
566
585
circuit . files . r1csFilename
567
586
)
568
587
569
- // Upload WASM to Storage .
588
+ // Move wasm between buckets .
570
589
await handleCircuitArtifactUploadToStorage (
571
590
firebaseFunctions ,
572
591
bucketName ,
@@ -575,11 +594,13 @@ const setup = async (cmd: { template?: string, auth?: string}) => {
575
594
circuit . files . wasmFilename
576
595
)
577
596
597
+ // @todo move the artifacts from the origin bucket to the new bucket
598
+ // instead of uploading r1cs and wasm
599
+
578
600
// 6 update the setup data object
579
601
ceremonySetupData . circuits [ index ] . files = {
580
602
...circuit . files ,
581
603
potBlake2bHash : potBlake2bHash ,
582
- r1csBlake2bHash : r1csBlake2bHash ,
583
604
wasmBlake2bHash : wasmBlake2bHash ,
584
605
initialZkeyBlake2bHash : initialZkeyBlake2bHash
585
606
}
0 commit comments