@@ -10,11 +10,7 @@ use clap::Parser;
10
10
use color_eyre:: Result ;
11
11
use rand:: SeedableRng ;
12
12
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 ;
18
14
19
15
use ed25519_dalek:: * ;
20
16
use std:: error:: Error ;
@@ -24,10 +20,9 @@ use crate::fragment::{compose_encrypted_vote_part, generate_vote_fragment};
24
20
pub mod fragment;
25
21
pub mod network;
26
22
27
-
28
23
///
29
24
/// Args defines and declares CLI behaviour within the context of clap
30
- ///
25
+ ///
31
26
#[ derive( Parser , Debug , Clone ) ]
32
27
#[ clap( about, version, author) ]
33
28
pub enum Cli {
@@ -65,21 +60,22 @@ pub struct CliArgs {
65
60
choice : u8 ,
66
61
}
67
62
68
-
69
63
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
70
64
color_eyre:: install ( ) ?;
71
65
72
66
let cli = Cli :: parse ( ) ;
73
67
68
+ let mut rng = ChaCha20Rng :: from_entropy ( ) ;
74
69
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 ) ,
77
72
}
78
73
}
79
74
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 ;
82
77
78
+ fn v1_exec ( args : CliArgs , rng : & mut ChaCha20Rng ) -> Result < ( ) , Box < dyn Error > > {
83
79
let pk = hex:: decode ( args. public_key ) ?;
84
80
let mut sk = hex:: decode ( args. private_key ) ?;
85
81
@@ -96,15 +92,15 @@ fn v1_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
96
92
97
93
let choice = args. choice ;
98
94
99
- let vote = chain_vote:: Vote :: new ( 2 , choice. into ( ) ) ?;
95
+ let vote = chain_vote:: Vote :: new ( VOTING_OPTIONS . into ( ) , choice. into ( ) ) ?;
100
96
// common reference string
101
97
let crs = chain_vote:: Crs :: from_hash ( & hex:: decode ( args. vote_plan_id . clone ( ) ) ?) ;
102
98
103
99
// parse ek key
104
100
let ek = ElectionPublicKey :: from_bytes ( & election_pk)
105
101
. ok_or ( "unable to parse election pub key" . to_string ( ) ) ?;
106
102
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) ;
108
104
let ( proof, encrypted_vote) = compose_encrypted_vote_part ( ciphertexts. clone ( ) , proof) ?;
109
105
110
106
let fragment_bytes = generate_vote_fragment (
@@ -123,50 +119,46 @@ fn v1_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
123
119
Ok ( ( ) )
124
120
}
125
121
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 ) ?;
131
124
132
125
// Election pub key published as a Bech32_encoded address
133
126
// which consists of 3 parts: A Human-Readable Part (HRP) + Separator + Data:
134
127
let ( _hrp, data, _variant) =
135
128
bech32:: decode ( & args. election_pub_key ) . map_err ( Bech32Error :: from) ?;
136
129
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 ;
145
148
let choice = args. choice ;
146
149
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,
166
158
) ?;
167
159
168
160
// fragment in hex: output consumed as input to another program
169
- println ! ( "{:?}" , hex:: encode( fragment_bytes . clone ( ) ) ) ;
161
+ println ! ( "{:?}" , hex:: encode( tx . to_bytes ( ) ) ) ;
170
162
171
163
Ok ( ( ) )
172
164
}
0 commit comments