Skip to content

Commit 49dfa82

Browse files
committed
add v2 impl
1 parent 3c64728 commit 49dfa82

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

src/sign/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ serde_json = "1.0"
3535
serde_yaml = "0.8.17"
3636
rand = "0.8.3"
3737
bech32 = "0.8"
38-
rand_core = { version = "0.5.1", default-features = false }
38+
rand_core = { version = "0.5.1", default-features = false, features = ["getrandom"] }
3939
ed25519-dalek = "1.0.1"
4040
reqwest = { version = "*", default_features = false, features = [ "blocking","json", "rustls-tls" ] }

src/sign/src/main.rs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ use clap::Parser;
1010
use color_eyre::Result;
1111
use rand::SeedableRng;
1212
use rand_chacha::ChaCha20Rng;
13-
use catalyst_voting::{
14-
crypto::{ed25519::PrivateKey},
15-
txs::v1::Tx,
16-
vote_protocol::committee::ElectionSecretKey,
17-
};
13+
use std::convert::TryInto;
1814

1915
use ed25519_dalek::*;
2016
use std::error::Error;
@@ -24,10 +20,9 @@ use crate::fragment::{compose_encrypted_vote_part, generate_vote_fragment};
2420
pub mod fragment;
2521
pub mod network;
2622

27-
2823
///
2924
/// Args defines and declares CLI behaviour within the context of clap
30-
///
25+
///
3126
#[derive(Parser, Debug, Clone)]
3227
#[clap(about, version, author)]
3328
pub enum Cli {
@@ -65,21 +60,22 @@ pub struct CliArgs {
6560
choice: u8,
6661
}
6762

68-
6963
fn main() -> Result<(), Box<dyn Error>> {
7064
color_eyre::install()?;
7165

7266
let cli = Cli::parse();
7367

68+
let mut rng = ChaCha20Rng::from_entropy();
7469
match cli {
75-
Cli::V1(args) => v1_exec(args),
76-
Cli::V2(args) => v2_exec(args),
70+
Cli::V1(args) => v1_exec(args, &mut rng),
71+
Cli::V2(args) => v2_exec(args, &mut rng),
7772
}
7873
}
7974

80-
fn v1_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
81-
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
75+
/// Number of voting options
76+
const VOTING_OPTIONS: u8 = 2;
8277

78+
fn v1_exec(args: CliArgs, rng: &mut ChaCha20Rng) -> Result<(), Box<dyn Error>> {
8379
let pk = hex::decode(args.public_key)?;
8480
let mut sk = hex::decode(args.private_key)?;
8581

@@ -96,15 +92,15 @@ fn v1_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
9692

9793
let choice = args.choice;
9894

99-
let vote = chain_vote::Vote::new(2, choice.into())?;
95+
let vote = chain_vote::Vote::new(VOTING_OPTIONS.into(), choice.into())?;
10096
// common reference string
10197
let crs = chain_vote::Crs::from_hash(&hex::decode(args.vote_plan_id.clone())?);
10298

10399
// parse ek key
104100
let ek = ElectionPublicKey::from_bytes(&election_pk)
105101
.ok_or("unable to parse election pub key".to_string())?;
106102

107-
let (ciphertexts, proof) = ek.encrypt_and_prove_vote(&mut rng, &crs, vote);
103+
let (ciphertexts, proof) = ek.encrypt_and_prove_vote(rng, &crs, vote);
108104
let (proof, encrypted_vote) = compose_encrypted_vote_part(ciphertexts.clone(), proof)?;
109105

110106
let fragment_bytes = generate_vote_fragment(
@@ -123,50 +119,46 @@ fn v1_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
123119
Ok(())
124120
}
125121

126-
fn v2_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
127-
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
128-
129-
// let pk = hex::decode(args.public_key)?;
130-
let mut sk = hex::decode(args.private_key)?;
122+
fn v2_exec(args: CliArgs, rng: &mut ChaCha20Rng) -> Result<(), Box<dyn Error>> {
123+
let sk_bytes = hex::decode(args.private_key)?;
131124

132125
// Election pub key published as a Bech32_encoded address
133126
// which consists of 3 parts: A Human-Readable Part (HRP) + Separator + Data:
134127
let (_hrp, data, _variant) =
135128
bech32::decode(&args.election_pub_key).map_err(Bech32Error::from)?;
136129

137-
let election_pk = Vec::<u8>::from_base32(&data).map_err(Bech32Error::from)?;
138-
139-
140-
141-
// join sk+pk together, api requirement
142-
sk.extend(pk.clone());
143-
let keypair: Keypair = Keypair::from_bytes(&sk)?;
144-
130+
let election_pk_bytes = Vec::<u8>::from_base32(&data).map_err(Bech32Error::from)?;
131+
132+
let private_key = catalyst_voting::crypto::ed25519::PrivateKey::from_bytes(
133+
&sk_bytes
134+
.try_into()
135+
.map_err(|_| "private key invalid length")?,
136+
);
137+
let election_public_key =
138+
catalyst_voting::vote_protocol::committee::ElectionPublicKey::from_bytes(
139+
&election_pk_bytes
140+
.try_into()
141+
.map_err(|_| "election public key invalid length")?,
142+
)?;
143+
144+
let vote_plan_id = hex::decode(args.vote_plan_id.clone())?
145+
.try_into()
146+
.map_err(|_| "vote plan id invalid length")?;
147+
let proposal_index = args.proposal;
145148
let choice = args.choice;
146149

147-
let vote = chain_vote::Vote::new(2, choice.into())?;
148-
// common reference string
149-
let crs = chain_vote::Crs::from_hash(&hex::decode(args.vote_plan_id.clone())?);
150-
151-
// parse ek key
152-
let ek = ElectionPublicKey::from_bytes(&election_pk)
153-
.ok_or("unable to parse election pub key".to_string())?;
154-
155-
let (ciphertexts, proof) = ek.encrypt_and_prove_vote(&mut rng, &crs, vote);
156-
let (proof, encrypted_vote) = compose_encrypted_vote_part(ciphertexts.clone(), proof)?;
157-
158-
let fragment_bytes = generate_vote_fragment(
159-
keypair,
160-
encrypted_vote,
161-
proof,
162-
args.proposal,
163-
&hex::decode(args.vote_plan_id)?,
164-
args.epoch,
165-
args.slot,
150+
let tx = catalyst_voting::txs::v1::Tx::new_private(
151+
vote_plan_id,
152+
proposal_index,
153+
VOTING_OPTIONS,
154+
choice,
155+
&election_public_key,
156+
&private_key,
157+
rng,
166158
)?;
167159

168160
// fragment in hex: output consumed as input to another program
169-
println!("{:?}", hex::encode(fragment_bytes.clone()));
161+
println!("{:?}", hex::encode(tx.to_bytes()));
170162

171163
Ok(())
172164
}

0 commit comments

Comments
 (0)