Skip to content

Commit

Permalink
Revert "wip"
Browse files Browse the repository at this point in the history
This reverts commit 6da2b29.
  • Loading branch information
weijiekoh committed Sep 30, 2020
1 parent 6da2b29 commit 7c4a807
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 227 deletions.
5 changes: 3 additions & 2 deletions circuits/ts/__tests__/BatchUpdateStateTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('State tree root update verification circuit', () => {
'prod/batchUpdateStateTree_small.circom'
)

// Sign up the user
maciState.signUp(user.pubKey, initialVoiceCreditBalance)
})

it('BatchUpdateStateTree should produce the correct state root from a partially filled batch', async () => {
Expand All @@ -68,8 +70,7 @@ describe('State tree root update verification circuit', () => {
const sharedKey = Keypair.genEcdhSharedKey(user.privKey, coordinator.pubKey)
const message = command.encrypt(signature, sharedKey)

// Sign up and publish
maciState.signUp(user.pubKey, initialVoiceCreditBalance, message, user.pubKey)
maciState.publishMessage(message, user.pubKey)

const randomStateLeaf = StateLeaf.genRandomLeaf()

Expand Down
7 changes: 5 additions & 2 deletions circuits/ts/__tests__/UpdateStateTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ describe('State tree root update verification circuit', () => {
beforeAll(async () => {
circuit = await compileAndLoadCircuit('test/updateStateTree_test.circom')

// Sign up the user and publish a message
maciState.signUp(user.pubKey, initialVoiceCreditBalance, message, user.pubKey)
// Sign up the user
maciState.signUp(user.pubKey, initialVoiceCreditBalance)

// Publish a message
maciState.publishMessage(message, user.pubKey)

// Generate circuit inputs
circuitInputs = maciState.genUpdateStateTreeCircuitInputs(0)
Expand Down
12 changes: 2 additions & 10 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ Fields that the user has to set:
| Prompt for the user's Ethereum private key | `-dp` or `--prompt-for-eth-privkey` | If specified, ignores `-d / --eth-privkey` and prompts the user to input their Ethereum private key |
| Signup gatekeeper proxy data | `-s` or `--sg-data` | A hex string to pass to the sign-up gatekeeper proxy contract which may use it to determine whether to allow the user to sign up. Default: an empty bytestring. |
| Initial voice credit proxy data | `-v` or `--ivcp-data` | A hex string to pass to the initial voice credit proxy contract which may use it to determine how many voice credits to assign to the user. Default: an empty bytestring. |
| The user's MACI private key | `-sk` or `--pubkey` | This should not be an Ethereum private key |
| Prompt for the user's MACI private key | `-dsk` or `--prompt-for-maci-privkey` | If specified, ignores `-sk / --privkey` and prompts the user to input thier MACI private key |
| State index | `-pi` or `--state-index` | The state index of the user |
| The user's new or current MACI public key | `-pp` or `--pubkey` | This should be a serialised BabyJub public key which should replace the user\'s public key in the state tree if the command is valid|
| Vote option index | `-pv` or `--vote-option-index` | The index of the option to vote for |
| New vote weight | `-pw` or `--new-vote-weight` | The vote weight to assign to said vote option |
| Nonce | `-pn` or `--nonce` | The nonce of the message |
| Salt | `-ps` or `--salt` | The salt of the message. If unspecified, this command will randomly generate a salt |

### User: Change key / vote

Expand All @@ -149,10 +141,10 @@ Fields that the user has to set:
|-|-|-|
| Ethereum provider | `-e` or `--eth-provider` | A connection string to the Ethereum provider. Default: `http://localhost:8545` |
| MACI contract address | `-x` or `--contract` | The address of the deployed MACI contract |
| User's Ethereum private key | `-d` or `--eth-privkey` | A private key of the Ethereum account to use to perform the transaction |
| Prompt for the user's Ethereum private key | `-dp` or `--prompt-for-eth-privkey` | If specified, ignores `-d / --eth-privkey` and prompts the user to input their Ethereum private key |
| The user's MACI private key | `-sk` or `--pubkey` | This should not be an Ethereum private key |
| Prompt for the user's MACI private key | `-dsk` or `--prompt-for-maci-privkey` | If specified, ignores `-sk / --privkey` and prompts the user to input thier MACI private key |
| User's Ethereum private key | `-d` or `--eth-privkey` | A private key of the Ethereum account to use to perform the transaction |
| Prompt for the user's Ethereum private key | `-dp` or `--prompt-for-eth-privkey` | If specified, ignores `-d / --eth-privkey` and prompts the user to input their Ethereum private key |
| State index | `-i` or `--state-index` | The state index of the user |
| The user's new or current MACI public key | `-p` or `--pubkey` | This should be a serialised BabyJub public key which should replace the user\'s public key in the state tree if the command is valid|
| Vote option index | `-v` or `--vote-option-index` | The index of the option to vote for |
Expand Down
126 changes: 56 additions & 70 deletions cli/ts/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
DEFAULT_ETH_PROVIDER,
} from './defaults'

const DEFAULT_SALT = genRandomSalt()

const configureSubparser = (subparsers: any) => {
const parser = subparsers.addParser(
'publish',
Expand All @@ -44,6 +46,15 @@ const configureSubparser = (subparsers: any) => {
}
)

parser.addArgument(
['-p', '--pubkey'],
{
required: true,
type: 'string',
help: 'The MACI public key which should replace the user\'s public key in the state tree',
}
)

parser.addArgument(
['-x', '--contract'],
{
Expand Down Expand Up @@ -91,15 +102,6 @@ const configureSubparser = (subparsers: any) => {
}
)

parser.addArgument(
['-p', '--pubkey'],
{
required: true,
type: 'string',
help: 'The MACI public key which should replace the user\'s public key in the state tree',
}
)

parser.addArgument(
['-i', '--state-index'],
{
Expand Down Expand Up @@ -204,28 +206,46 @@ const publish = async (args: any) => {

const userMaciPrivkey = PrivKey.unserialize(serializedPrivkey)

let stateIndex, voteOptionIndex, nonce, salt
try {
const results = validateArgs(
args.state_index,
args.vote_option_index,
args.nonce,
args.salt,
)
// State index
const stateIndex = BigInt(args.state_index)
if (stateIndex < 0) {
console.error('Error: the state index must be greater than 0')
return
}

stateIndex = results.stateIndex
voteOptionIndex = results.voteOptionIndex
nonce = results.nonce
salt = results.salt
// Vote option index
const voteOptionIndex = BigInt(args.vote_option_index)

} catch (e) {
console.error(e.message)
if (voteOptionIndex < 0) {
console.error('Error: the vote option index should be 0 or greater')
return
}

// The new vote weight
// TODO: validate this
const newVoteWeight = BigInt(args.new_vote_weight)
// The nonce
const nonce = BigInt(args.nonce)

if (nonce < 0) {
console.error('Error: the nonce should be 0 or greater')
return
}

// The salt
let salt
if (args.salt) {
if (!validateSaltFormat(args.salt)) {
console.error('Error: the salt should be a 32-byte hexadecimal string')
return
}

salt = BigInt(args.salt)

if (!validateSaltSize(args.salt)) {
console.error('Error: the salt should less than the BabyJub field size')
return
}
} else {
salt = DEFAULT_SALT
}

if (! (await checkDeployerProviderConnection(ethSk, ethProvider))) {
console.error('Error: unable to connect to the Ethereum provider at', ethProvider)
Expand All @@ -246,13 +266,23 @@ const publish = async (args: any) => {
wallet,
)

// Validate the state index against the number of signups on-chain
const numSignUps = (await maciContract.numSignUps()).toNumber()
if (numSignUps < stateIndex) {
console.error('Error: the state index is invalid')
return
}

// Validate the vote option index against the max leaf index on-chain
const maxVoteOptions = (await maciContract.voteOptionsMaxLeafIndex()).toNumber()
if (maxVoteOptions < voteOptionIndex) {
console.error('Error: the vote option index is invalid')
return
}

// The new vote weight
const newVoteWeight = BigInt(args.new_vote_weight)

const coordinatorPubKeyOnChain = await maciContract.coordinatorPubKey()
const coordinatorPubKey = new PubKey([
BigInt(coordinatorPubKeyOnChain.x.toString()),
Expand Down Expand Up @@ -298,51 +328,7 @@ const publish = async (args: any) => {
await tx.wait()
}

const validateArgs = (
stateIndex: BigInt,
voteOptionIndex: BigInt,
nonce: BigInt,
saltStr: string,
) => {

// State index
if (stateIndex < BigInt(0)) {
throw new Error('Error: the state index must be greater than 0')
}

// Vote option index

if (voteOptionIndex < BigInt(0)) {
throw new Error('Error: the vote option index should be 0 or greater')
}

// The nonce
if (nonce < BigInt(0)) {
throw new Error('Error: the nonce should be 0 or greater')
}

// The salt
let salt
if (saltStr) {
if (!validateSaltFormat(saltStr)) {
throw new Error('Error: the salt should be a 32-byte hexadecimal string')
}

if (!validateSaltSize(saltStr)) {
throw new Error('Error: the salt should less than the BabyJub field size')
}

salt = BigInt(saltStr)
} else {
salt = genRandomSalt()
}

return { stateIndex, voteOptionIndex, nonce, salt }

}

export {
publish,
validateArgs,
configureSubparser,
}
Loading

0 comments on commit 7c4a807

Please sign in to comment.