Skip to content

Commit

Permalink
Merge branch 'quadratic-funding:dev' into dev
Browse files Browse the repository at this point in the history
ctrlc03 authored Feb 28, 2023

Unverified

The email in this signature doesn’t match the committer email.
2 parents e38fa4d + 8ab1996 commit aa3d437
Showing 24 changed files with 1,117 additions and 673 deletions.
Binary file added audits/P0T1ON_report_draft_1.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion jest.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
"moduleNameMapper": {
"@zkmpc/(.*)$": "<rootDir>/packages/$1"
},
"testTimeout": 1000000,
"testTimeout": 100000000,
"setupFiles": ["dotenv/config"],
"collectCoverageFrom": ["packages/**/*", "!**/dist/**", "!**/node_modules/**", "!**/build/**"],
"verbose": true,
49 changes: 0 additions & 49 deletions packages/actions/src/core/finalize/index.ts

This file was deleted.

9 changes: 8 additions & 1 deletion packages/actions/src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,10 @@ export const numExpIterations = 10
export const solidityVersion = "0.8.0"
// The index of the final zKey.
export const finalContributionIndex = "final"
// The acronym for verification key.
export const verificationKeyAcronym = "vkey"
// The acronym for Verifier smart contract.
export const verifierSmartContractAcronym = "verifier"

/**
* Commonly used terms.
@@ -125,6 +129,9 @@ export const commonTerms = {
generatePreSignedUrlsParts: "generatePreSignedUrlsParts",
completeMultiPartUpload: "completeMultiPartUpload",
checkIfObjectExist: "checkIfObjectExist",
verifyContribution: "verifycontribution"
verifyContribution: "verifycontribution",
checkAndPrepareCoordinatorForFinalization: "checkAndPrepareCoordinatorForFinalization",
finalizeCircuit: "finalizeCircuit",
finalizeCeremony: "finalizeCeremony"
}
}
9 changes: 8 additions & 1 deletion packages/actions/src/helpers/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs"

import crypto from "crypto"
import blake from "blakejs"

/**
@@ -33,3 +33,10 @@ export const blake512FromPath = async (path: fs.PathLike): Promise<string> => {
})
return hash
}

/**
* Return the SHA256 hash (HEX format) of a given value
* @param value <string> - the value to be hashed.
* @returns <string> - the HEX format of the SHA256 hash of the given value
*/
export const computeSHA256ToHex = (value: string): string => crypto.createHash("sha256").update(value).digest("hex")
39 changes: 15 additions & 24 deletions packages/actions/src/helpers/database.ts
Original file line number Diff line number Diff line change
@@ -200,31 +200,22 @@ export const getCurrentActiveParticipantTimeout = async (
return fromQueryToFirebaseDocumentInfo(participantTimeoutQuerySnap.docs)
}

/// @todo needs refactoring below.

/**
* Query for closed ceremonies documents and return their data (if any).
* @param firestoreDatabase <Firestore> - the Firestore database to query.
* @returns <Promise<Array<FirebaseDocumentInfo>>>
* Query for the closed ceremonies.
* @notice a ceremony is closed when the period for receiving new contributions has ended.
* @dev when the ceremony is closed it becomes ready for finalization.
* @param firestoreDatabase <Firestore> - the Firestore service instance associated to the current Firebase application.
* @returns <Promise<Array<FirebaseDocumentInfo>>> - the list of closed ceremonies.
*/
export const getClosedCeremonies = async (firestoreDatabase: Firestore): Promise<Array<FirebaseDocumentInfo>> => {
let closedStateCeremoniesQuerySnap: any

try {
closedStateCeremoniesQuerySnap = await queryCollection(
firestoreDatabase,
commonTerms.collections.ceremonies.name,
[
where(commonTerms.collections.ceremonies.fields.state, "==", CeremonyState.CLOSED),
where(commonTerms.collections.ceremonies.fields.endDate, "<=", Date.now())
]
)

if (closedStateCeremoniesQuerySnap.empty && closedStateCeremoniesQuerySnap.size === 0)
throw new Error("Queries-0001: There are no ceremonies ready to finalization")
} catch (err: any) {
throw new Error(err.toString())
}

return fromQueryToFirebaseDocumentInfo(closedStateCeremoniesQuerySnap.docs)
const closedCeremoniesQuerySnap = await queryCollection(
firestoreDatabase,
commonTerms.collections.ceremonies.name,
[
where(commonTerms.collections.ceremonies.fields.state, "==", CeremonyState.CLOSED),
where(commonTerms.collections.ceremonies.fields.endDate, "<=", Date.now())
]
)

return fromQueryToFirebaseDocumentInfo(closedCeremoniesQuerySnap.docs)
}
51 changes: 50 additions & 1 deletion packages/actions/src/helpers/functions.ts
Original file line number Diff line number Diff line change
@@ -292,7 +292,7 @@ export const checkIfObjectExist = async (

/**
* Request to verify the newest contribution for the circuit.
* @param functions <Functions> - the cloud functions.
* @param functions <Functions> - the Firebase cloud functions object instance.
* @param ceremonyId <string> - the unique identifier of the ceremony.
* @param circuitId <string> - the unique identifier of the circuit.
* @param bucketName <string> - the name of the ceremony bucket.
@@ -321,3 +321,52 @@ export const verifyContribution = async (

return contributionVerificationData
}

/**
* Prepare the coordinator for the finalization of the ceremony.
* @param functions <Functions> - the Firebase cloud functions object instance.
* @param ceremonyId <string> - the unique identifier of the ceremony.
* @returns <Promise<boolean>> - true when the coordinator is ready for finalization; otherwise false.
*/
export const checkAndPrepareCoordinatorForFinalization = async (
functions: Functions,
ceremonyId: string
): Promise<boolean> => {
const cf = httpsCallable(functions, commonTerms.cloudFunctionsNames.checkAndPrepareCoordinatorForFinalization)

const { data: isCoordinatorReadyForCeremonyFinalization }: any = await cf({
ceremonyId
})

return isCoordinatorReadyForCeremonyFinalization
}

/**
* Finalize the ceremony circuit.
* @param functions <Functions> - the Firebase cloud functions object instance.
* @param ceremonyId <string> - the unique identifier of the ceremony.
* @param circuitId <string> - the unique identifier of the circuit.
* @param bucketName <string> - the name of the ceremony bucket.
*/
export const finalizeCircuit = async (functions: Functions, ceremonyId: string, circuitId: any, bucketName: string) => {
const cf = httpsCallable(functions, commonTerms.cloudFunctionsNames.finalizeCircuit)

await cf({
ceremonyId,
circuitId,
bucketName
})
}

/**
* Conclude the finalization of the ceremony.
* @param functions <Functions> - the Firebase cloud functions object instance.
* @param ceremonyId <string> - the unique identifier of the ceremony.
*/
export const finalizeCeremony = async (functions: Functions, ceremonyId: string) => {
const cf = httpsCallable(functions, commonTerms.cloudFunctionsNames.finalizeCeremony)

await cf({
ceremonyId
})
}
17 changes: 14 additions & 3 deletions packages/actions/src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -220,10 +220,17 @@ export const getContributionsValidityForContributor = async (
* Return the public attestation preamble for given contributor.
* @param contributorIdentifier <string> - the identifier of the contributor (handle, name, uid).
* @param ceremonyName <string> - the name of the ceremony.
* @param isFinalizing <boolean> - true when the coordinator is finalizing the ceremony, otherwise false.
* @returns <string> - the public attestation preamble.
*/
export const getPublicAttestationPreambleForContributor = (contributorIdentifier: string, ceremonyName: string) =>
`Hey, I'm ${contributorIdentifier} and I have contributed to the ${ceremonyName} MPC Phase2 Trusted Setup ceremony.\nThe following are my contribution signatures:`
export const getPublicAttestationPreambleForContributor = (
contributorIdentifier: string,
ceremonyName: string,
isFinalizing: boolean
) =>
`Hey, I'm ${contributorIdentifier} and I have ${
isFinalizing ? "finalized" : "contributed"
} to the ${ceremonyName} MPC Phase2 Trusted Setup ceremony.\nThe following are my contribution signatures:`

/**
* Check and prepare public attestation for the contributor made only of its valid contributions.
@@ -248,7 +255,11 @@ export const generateValidContributionsAttestation = async (
isFinalizing: boolean
): Promise<string> => {
// Generate the attestation preamble for the contributor.
let publicAttestation = getPublicAttestationPreambleForContributor(contributorIdentifier, ceremonyName)
let publicAttestation = getPublicAttestationPreambleForContributor(
contributorIdentifier,
ceremonyName,
isFinalizing
)

// Get contributors' contributions validity.
const contributionsWithValidity = await getContributionsValidityForContributor(
16 changes: 8 additions & 8 deletions packages/actions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export {
checkAndPrepareCoordinatorForFinalization,
finalizeLastContribution,
finalizeCeremony
} from "./core/finalize/index"
export {
getBucketName,
multiPartUpload,
@@ -37,7 +32,9 @@ export {
genesisZkeyIndex,
numExpIterations,
solidityVersion,
finalContributionIndex
finalContributionIndex,
verificationKeyAcronym,
verifierSmartContractAcronym
} from "./helpers/constants"
export {
extractPrefix,
@@ -67,6 +64,9 @@ export {
generatePreSignedUrlsParts,
completeMultiPartUpload,
checkIfObjectExist,
verifyContribution
verifyContribution,
checkAndPrepareCoordinatorForFinalization,
finalizeCircuit,
finalizeCeremony
} from "./helpers/functions"
export { toHex, blake512FromPath } from "./helpers/crypto"
export { toHex, blake512FromPath, computeSHA256ToHex } from "./helpers/crypto"
5 changes: 2 additions & 3 deletions packages/actions/test/unit/database.test.ts
Original file line number Diff line number Diff line change
@@ -222,9 +222,8 @@ describe("Database", () => {
.doc(fakeCeremoniesData.fakeCeremonyClosedDynamic.uid)
.delete()

expect(getClosedCeremonies(userFirestore)).to.be.rejectedWith(
"Queries-0001: There are no ceremonies ready to finalization"
)
const closedCeremonies = await getClosedCeremonies(userFirestore)
expect(closedCeremonies.length).to.be.equal(0)
})
})

Loading

0 comments on commit aa3d437

Please sign in to comment.