Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (fragment generator): add epoch and slot | NPG-000 #610

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading