Skip to content

Commit

Permalink
refactor(refactor verify.ts): refactor verify cli command for Tally a…
Browse files Browse the repository at this point in the history
…nd Subsidy contracts
  • Loading branch information
chaosma committed Mar 9, 2023
1 parent 36446fc commit dedfda6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
9 changes: 3 additions & 6 deletions cli/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,10 @@ import {
configureSubparser as configureSubparserForProveOnChain,
} from './proveOnChain'

/*
import {
verify,
configureSubparser as configureSubparserForVerify,
} from './verify'
*/

import {
checkVerifyingKey,
Expand Down Expand Up @@ -150,8 +148,7 @@ const main = async () => {
configureSubparserForProveOnChain(subparsers)

// Subcommand: verify
// hehe, temp comment out
//configureSubparserForVerify(subparsers)
configureSubparserForVerify(subparsers)

// Subcommand: checkVerifyKey
configureSubparserForCheckVerifyKey(subparsers)
Expand Down Expand Up @@ -189,9 +186,9 @@ const main = async () => {
await genProofs(args)
} else if (args.subcommand === 'proveOnChain') {
await proveOnChain(args)
} /* else if (args.subcommand === 'verify') {
} else if (args.subcommand === 'verify') {
await verify(args)
} hehe temp comment out */
}
else if (args.subcommand === 'checkVerifyingKey') {
await checkVerifyingKey(args)
}
Expand Down
64 changes: 47 additions & 17 deletions cli/ts/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,75 @@ const configureSubparser = (subparsers: any) => {
)

parser.addArgument(
['-q', '--ppt'],
['-tc', '--tally-contract'],
{
type: 'string',
help: 'The PollProcessorAndTallyer contract address',
help: 'The Tally contract address',
}
)

parser.addArgument(
['-sc', '--subsidy-contract'],
{
type: 'string',
help: 'The Subsidy contract address',
}
)
}


const verify = async (args: any) => {
const signer = await getDefaultSigner()

const pollId = Number(args.poll_id)

// check existence of MACI and ppt contract addresses
// check existence of MACI, Tally and Subsidy contract addresses
let contractAddrs = readJSONFile(contractFilepath)
if ((!contractAddrs||!contractAddrs["MACI"]) && !args.contract) {
console.error('Error: MACI contract address is empty')
return 1
}
if ((!contractAddrs||!contractAddrs["PollProcessorAndTally-"+pollId]) && !args.ppt) {
console.error('Error: PollProcessorAndTally contract address is empty')
if ((!contractAddrs||!contractAddrs["Tally-"+pollId]) && !args.tally_contract) {
console.error('Error: Tally contract address is empty')
return 1
}
if ((!contractAddrs||!contractAddrs["Subsidy-"+pollId]) && !args.subsidy_contract) {
console.error('Error: Subsidy contract address is empty')
return 1
}

const maciAddress = args.contract ? args.contract: contractAddrs["MACI"]
const pptAddress = args.ppt ? args.ppt: contractAddrs["PollProcessorAndTally-"+pollId]
const tallyAddress = args.tally_contract? args.tally_contract: contractAddrs["Tally-"+pollId]
const subsidyAddress = args.subsidy_contract? args.subsidy_contract: contractAddrs["Subsidy-"+pollId]

// MACI contract
if (!validateEthAddress(maciAddress)) {
console.error('Error: invalid MACI contract address')
return 0
}

// PollProcessorAndTallyer contract
if (!validateEthAddress(pptAddress)) {
console.error('Error: invalid PollProcessorAndTallyer contract address')
// Tally contract
if (!validateEthAddress(tallyAddress)) {
console.error('Error: invalid Tally contract address')
return 0
}

// Subsidy contract
if (!validateEthAddress(subsidyAddress)) {
console.error('Error: invalid Subsidy contract address')
return 0
}
const [ maciContractAbi ] = parseArtifact('MACI')
const [ pollContractAbi ] = parseArtifact('Poll')
const [ pptContractAbi ] = parseArtifact('PollProcessorAndTallyer')
const [ tallyContractAbi ] = parseArtifact('Tally')
const [ subsidyContractAbi ] = parseArtifact('Subsidy')

if (! (await contractExists(signer.provider, pptAddress))) {
console.error(`Error: there is no contract deployed at ${pptAddress}.`)
if (! (await contractExists(signer.provider, tallyAddress))) {
console.error(`Error: there is no contract deployed at ${tallyAddress}.`)
return 1
}
if (! (await contractExists(signer.provider, subsidyAddress))) {
console.error(`Error: there is no contract deployed at ${subsidyAddress}.`)
return 1
}

Expand All @@ -128,15 +152,21 @@ const verify = async (args: any) => {
signer,
)

const pptContract = new ethers.Contract(
pptAddress,
pptContractAbi,
const tallyContract = new ethers.Contract(
tallyAddress,
tallyContractAbi,
signer,
)

const subsidyContract = new ethers.Contract(
subsidyAddress,
subsidyContractAbi,
signer,
)

// ----------------------------------------------
// verify tally result
const onChainTallyCommitment = BigInt(await pptContract.tallyCommitment())
const onChainTallyCommitment = BigInt(await tallyContract.tallyCommitment())
console.log(onChainTallyCommitment.toString(16))

// Read the tally file
Expand Down Expand Up @@ -227,7 +257,7 @@ const verify = async (args: any) => {
// verify subsidy result

if (args.subsidy_file) {
const onChainSubsidyCommitment = BigInt(await pptContract.subsidyCommitment())
const onChainSubsidyCommitment = BigInt(await subsidyContract.subsidyCommitment())
console.log(onChainSubsidyCommitment.toString(16))
// Read the subsidy file
try {
Expand Down

0 comments on commit dedfda6

Please sign in to comment.