Skip to content

Commit

Permalink
Fragment generator add epoch slot (#610)
Browse files Browse the repository at this point in the history
add epoch and slot for testing
  • Loading branch information
cong-or authored Oct 31, 2023
1 parent 45dcf7c commit 2e58820
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/sign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- Alice private key
- proposal to vote on
- vote plan id (hash of voteplan)
- epoch
- slot

*Example usage:*

Expand All @@ -28,7 +30,9 @@ ALICE_SK=56e367979579e2ce27fbd305892b0706b7dede999a534a864a7430a5c6aefd3c
ALICE_PK=ea084d2d80ed0ab681333d934efc56df3868d13d46a2de3b7f27f40b62e5344d
PROPOSAL=5
VOTE_PLAN_ID=36ad42885189a0ac3438cdb57bc8ac7f6542e05a59d1f2e4d1d38194c9d4ac7b
EPOCH=0
SLOT=0

./target/release/sign --election-pub-key $ELECTION_PUB_KEY --private-key $ALICE_SK --public-key $ALICE_PK --proposal $PROPOSAL --vote-plan-id $VOTE_PLAN_ID
./target/release/sign --election-pub-key $ELECTION_PUB_KEY --private-key $ALICE_SK --public-key $ALICE_PK --proposal $PROPOSAL --vote-plan-id $VOTE_PLAN_ID --epoch $EPOCH --slot $SLOT

```
18 changes: 10 additions & 8 deletions src/sign/src/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ const VOTE_CAST_TAG: u8 = 11;
/// INPUT-ACCOUNT = %xff VALUE UNTAG-ACCOUNT-ID
const INPUT_ACCOUNT: u8 = 255;

/// Block epoch + slot
/// This is redundant as time checks have been removed
const EPOCH: u32 = 0;
const SLOT: u32 = 0;

/// Only 1 input (subsequently 1 witness), no output
/// VoteCast TX should have only 1 input, 0 output and 1 witness (signature).
const INPUT: u8 = 1;
Expand Down Expand Up @@ -51,6 +46,8 @@ pub fn generate_vote_fragment(
proof: Vec<u8>,
proposal: u8,
vote_plan_id: &[u8],
epoch: u32,
slot: u32,
) -> Result<Vec<u8>, Box<dyn error::Error>> {
let mut vote_cast = Codec::new(Vec::new());

Expand All @@ -62,7 +59,8 @@ pub fn generate_vote_fragment(

let data_to_sign = vote_cast.into_inner().clone();

let (inputs, witness) = compose_inputs_and_witnesses(keypair, data_to_sign.clone())?;
let (inputs, witness) =
compose_inputs_and_witnesses(keypair, data_to_sign.clone(), epoch, slot)?;

let mut vote_cast = Codec::new(Vec::new());
vote_cast.put_bytes(&data_to_sign)?;
Expand All @@ -85,11 +83,13 @@ pub fn generate_vote_fragment(
fn compose_inputs_and_witnesses(
keypair: Keypair,
data_to_sign: Vec<u8>,
epoch: u32,
slot: u32,
) -> Result<(Vec<u8>, Vec<u8>), Box<dyn error::Error>> {
let mut inputs = Codec::new(Vec::new());

inputs.put_be_u32(EPOCH)?;
inputs.put_be_u32(SLOT)?;
inputs.put_be_u32(epoch)?;
inputs.put_be_u32(slot)?;
inputs.put_u8(INPUT)?;
inputs.put_u8(OUTPUT)?;

Expand Down Expand Up @@ -219,6 +219,8 @@ mod tests {
proof,
5,
&hex::decode(vote_plan_id.clone()).unwrap(),
0,
0,
)
.unwrap();

Expand Down
8 changes: 8 additions & 0 deletions src/sign/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub struct Args {
/// proposal to vote on
#[clap(short, long)]
proposal: u8,
/// Epoch
#[clap(short, long)]
epoch: u32,
/// Slot
#[clap(short, long)]
slot: u32,
/// vote plan hash
#[clap(short, long)]
vote_plan_id: String,
Expand Down Expand Up @@ -77,6 +83,8 @@ fn main() -> Result<(), Box<dyn Error>> {
proof,
args.proposal,
&hex::decode(args.vote_plan_id)?,
args.epoch,
args.slot,
)?;

// fragment in hex: output consumed as input to another program
Expand Down

0 comments on commit 2e58820

Please sign in to comment.