-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
"smartContract": "TokenElection" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Mina, PublicKey, fetchAccount } from 'o1js'; | ||
import { TokenElection, IpfsHash } from './../../TokenElection.js'; | ||
|
||
import * as readline from 'node:readline/promises'; // This uses the promise-based APIs | ||
import { stdin as input, stdout as output } from 'node:process'; | ||
|
||
const rl = readline.createInterface({ input, output }); | ||
|
||
const zkAppAddressInput = await rl.question(`At what address is the ZK App Deployed?\n`); | ||
|
||
rl.close(); | ||
|
||
// set up Mina instance and contract we interact with | ||
const Network = Mina.Network('https://proxy.berkeley.minaexplorer.com/graphql'); | ||
Mina.setActiveInstance(Network); | ||
let zkAppAddress = PublicKey.fromBase58(zkAppAddressInput); | ||
let zkApp = new TokenElection(zkAppAddress); | ||
await fetchAccount({ publicKey: zkAppAddress }); | ||
const onChainIpfsHash = zkApp.electionDetailsIpfs.get(); | ||
const ipfsHash = IpfsHash.unpack(onChainIpfsHash.packed).map(x => x.toString()).join('') | ||
console.log(` | ||
Review the details of this election at: https://ipfs.io/ipfs/${ipfsHash} | ||
`) | ||
|
||
const ballot = zkApp.ballot.get(); | ||
const partial1 = ballot.partial1.toBigInts() | ||
const partial2 = ballot.partial2.toBigInts() | ||
const totalVotes = (partial1.concat(partial2)).map(x => String(x)).join(",") | ||
|
||
console.log(` | ||
Current Vote Tallies: ${totalVotes} | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { AccountUpdate, Mina, PrivateKey, PublicKey, fetchAccount } from 'o1js'; | ||
import { TokenElection, IpfsHash } from './../../TokenElection.js'; | ||
import fs from 'fs/promises'; | ||
|
||
import * as readline from 'node:readline/promises'; // This uses the promise-based APIs | ||
import { stdin as input, stdout as output } from 'node:process'; | ||
|
||
|
||
const rl = readline.createInterface({ input, output }); | ||
|
||
const zkAppAddressInput = await rl.question(`At what address is the ZK App Deployed?\n`); | ||
|
||
rl.close(); | ||
|
||
let deployAlias = 'berkeley' | ||
Error.stackTraceLimit = 1000; | ||
|
||
// parse config and private key from file | ||
type Config = { | ||
deployAliases: Record< | ||
string, | ||
{ | ||
url: string; | ||
keyPath: string; | ||
fee: string; | ||
feepayerKeyPath: string; | ||
feepayerAlias: string; | ||
} | ||
>; | ||
}; | ||
let configJson: Config = JSON.parse(await fs.readFile('config.json', 'utf8')); | ||
let config = configJson.deployAliases[deployAlias]; | ||
let feepayerKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( | ||
await fs.readFile(config.feepayerKeyPath, 'utf8') | ||
); | ||
|
||
let zkAppKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( | ||
await fs.readFile(config.keyPath, 'utf8') | ||
); | ||
let feepayerKey = PrivateKey.fromBase58(feepayerKeysBase58.privateKey); | ||
let feepayerAddress = feepayerKey.toPublicKey(); | ||
|
||
// set up Mina instance and contract we interact with | ||
const Network = Mina.Network(config.url); | ||
const fee = Number(config.fee) * 1e9; // in nanomina (1 billion = 1.0 mina) | ||
Mina.setActiveInstance(Network); | ||
let zkAppAddress = PublicKey.fromBase58(zkAppAddressInput) | ||
let zkApp = new TokenElection(zkAppAddress); | ||
await fetchAccount({ publicKey: zkAppAddress }); | ||
|
||
let sentTx; | ||
// compile the contract to create prover keys | ||
console.log('compile the contract...'); | ||
await TokenElection.compile(); | ||
try { | ||
// Join the election by minting tokens to yourself | ||
console.log('build transaction and create proof...'); | ||
let tx = await Mina.transaction({ sender: feepayerAddress, fee }, () => { | ||
let senderUpdate = AccountUpdate.fundNewAccount(feepayerAddress); | ||
zkApp.faucet(feepayerAddress); | ||
}); | ||
await tx.prove(); | ||
console.log('send transaction...'); | ||
sentTx = await tx.sign([feepayerKey]).send(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
if (sentTx?.hash() !== undefined) { | ||
console.log(` | ||
Success! Update transaction sent. | ||
Your smart contract state will be updated | ||
as soon as the transaction is included in a block: | ||
https://berkeley.minaexplorer.com/transaction/${sentTx.hash()} | ||
`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { AccountUpdate, Mina, PrivateKey, PublicKey, UInt32, fetchAccount, setArchiveGraphqlEndpoint } from 'o1js'; | ||
import { TokenElection, IpfsHash, PartialBallot, Ballot } from './../../TokenElection.js'; | ||
import fs from 'fs/promises'; | ||
|
||
import * as readline from 'node:readline/promises'; // This uses the promise-based APIs | ||
import { stdin as input, stdout as output } from 'node:process'; | ||
|
||
|
||
const rl = readline.createInterface({ input, output }); | ||
|
||
const zkAppAddressInput = await rl.question(`At what address is the ZK App Deployed?\n`); | ||
|
||
const confirm = await rl.question(`Votes in votes.json are correct? Y/n\n`); | ||
|
||
rl.close(); | ||
|
||
if (confirm.toLowerCase() != 'y') { | ||
throw new Error("Update votes.json, then continue") | ||
} | ||
|
||
let deployAlias = 'berkeley' | ||
Error.stackTraceLimit = 1000; | ||
|
||
// parse config and private key from file | ||
type Config = { | ||
deployAliases: Record< | ||
string, | ||
{ | ||
url: string; | ||
keyPath: string; | ||
fee: string; | ||
feepayerKeyPath: string; | ||
feepayerAlias: string; | ||
} | ||
>; | ||
}; | ||
let configJson: Config = JSON.parse(await fs.readFile('config.json', 'utf8')); | ||
let config = configJson.deployAliases[deployAlias]; | ||
let feepayerKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( | ||
await fs.readFile(config.feepayerKeyPath, 'utf8') | ||
); | ||
|
||
let zkAppKeysBase58: { privateKey: string; publicKey: string } = JSON.parse( | ||
await fs.readFile(config.keyPath, 'utf8') | ||
); | ||
let feepayerKey = PrivateKey.fromBase58(feepayerKeysBase58.privateKey); | ||
let feepayerAddress = feepayerKey.toPublicKey(); | ||
|
||
// set up Mina instance and contract we interact with | ||
const Network = Mina.Network(config.url); | ||
setArchiveGraphqlEndpoint('https://api.minascan.io/archive/berkeley/v1/graphql') | ||
const fee = Number(config.fee) * 1e9; // in nanomina (1 billion = 1.0 mina) | ||
Mina.setActiveInstance(Network); | ||
let zkAppAddress = PublicKey.fromBase58(zkAppAddressInput) | ||
let zkApp = new TokenElection(zkAppAddress); | ||
await fetchAccount({ publicKey: zkAppAddress }); | ||
|
||
const votes = JSON.parse(await fs.readFile('./src/examples/ZkIgnite/votes.json', 'utf8')); | ||
console.log(votes) | ||
const partialBallot1 = new Array(7).fill(0n); | ||
const partialBallot2 = new Array(7).fill(0n); | ||
let sum = 0; | ||
for (let key in votes) { | ||
const keyNum = Number(key) | ||
if (keyNum < 7) { | ||
partialBallot1[keyNum] = BigInt(votes[key]) | ||
sum += Number(votes[key]) | ||
} else if (keyNum < 14) { | ||
partialBallot2[keyNum - 7] = BigInt(votes[key]) | ||
sum += Number(votes[key]) | ||
} else { | ||
throw new Error("KeyError: Vote keys must be numeric between 0-13") | ||
} | ||
} | ||
const ballot = new Ballot({ | ||
partial1: PartialBallot.fromBigInts(partialBallot1), | ||
partial2: PartialBallot.fromBigInts(partialBallot2), | ||
}); | ||
console.log(`Submitting Votes: ${(partialBallot1.concat(partialBallot2)).map(x => String(x)).join(",")}`) | ||
let sentTx; | ||
// compile the contract to create prover keys | ||
console.log('compile the contract...'); | ||
await TokenElection.compile(); | ||
try { | ||
// Join the election by minting tokens to yourself | ||
console.log('build transaction and create proof...'); | ||
let tx = await Mina.transaction({ sender: feepayerAddress, fee }, () => { | ||
zkApp.reduceVotes(); | ||
zkApp.castVote(ballot, UInt32.from(sum)) | ||
}); | ||
await tx.prove(); | ||
console.log('send transaction...'); | ||
sentTx = await tx.sign([feepayerKey]).send(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
if (sentTx?.hash() !== undefined) { | ||
console.log(` | ||
Success! Update transaction sent. | ||
Your smart contract state will be updated | ||
as soon as the transaction is included in a block: | ||
https://berkeley.minaexplorer.com/transaction/${sentTx.hash()} | ||
`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"4": 3200, | ||
"12": 2500 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters