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: Update sign tool v2 using new catalyst-voting crate | NPG-000 #718

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
153 changes: 144 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/sign/Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@ chain-impl-mockchain = { path = "../chain-libs/chain-impl-mockchain" ,features=
chain-ser = { path = "../chain-libs/chain-ser" }
chain-storage = { path = "../chain-libs/chain-storage" }

catalyst-voting = { git = "https://github.com/input-output-hk/catalyst-libs.git", rev = "d0b5784208abb7c4f2eddf8237099368e070620a" }


hex = "0.4"
cryptoxide = "0.4.2"
@@ -32,6 +34,6 @@ serde_json = "1.0"
serde_yaml = "0.8.17"
rand = "0.8.3"
bech32 = "0.8"
rand_core = { version = "0.5.1", default-features = false }
rand_core = { version = "0.5.1", default-features = false, features = ["getrandom"] }
ed25519-dalek = "1.0.1"
reqwest = { version = "*", default_features = false, features = [ "blocking","json", "rustls-tls" ] }
21 changes: 19 additions & 2 deletions src/sign/README.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ Generates vote fragments and signs them accordingly
cargo build --release -p sign
```

*Generate raw vote fragment in byte representation*
*Generate raw vote fragment in byte representation using original jorm implementation*

```bash

@@ -35,6 +35,23 @@ EPOCH=0
SLOT=0
CHOICE=1

./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 --choice $CHOICE
./target/release/sign v1 --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 --choice $CHOICE

```

*Generate raw vote fragment in byte representation using `catalyst-voting` crate*

```bash

ELECTION_PUB_KEY=ristretto255_votepk1ppxnuxrqa4728evnp2ues000uvwvwtxmtf77ejc29lknjuqqu44s4cfmja
ALICE_SK=56e367979579e2ce27fbd305892b0706b7dede999a534a864a7430a5c6aefd3c
ALICE_PK=ea084d2d80ed0ab681333d934efc56df3868d13d46a2de3b7f27f40b62e5344d
PROPOSAL=5
VOTE_PLAN_ID=36ad42885189a0ac3438cdb57bc8ac7f6542e05a59d1f2e4d1d38194c9d4ac7b
EPOCH=0
SLOT=0
CHOICE=1

./target/release/sign v2 --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 --choice $CHOICE

```
70 changes: 66 additions & 4 deletions src/sign/src/main.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use bech32::Error as Bech32Error;
use bech32::FromBase32;
use chain_vote::ElectionPublicKey;
use clap::Args;
use clap::Parser;
use color_eyre::Result;
use rand::SeedableRng;
@@ -23,7 +24,15 @@ pub mod network;
///
#[derive(Parser, Debug, Clone)]
#[clap(about, version, author)]
pub struct Args {
pub enum Cli {
/// Original jormungandr transaction generation
V1(CliArgs),
/// `catalyst-voting` transaction generation
V2(CliArgs),
}

#[derive(Args, Debug, Clone)]
pub struct CliArgs {
/// Election public key issued by Trent
#[clap(short, long)]
pub election_pub_key: String,
@@ -53,9 +62,19 @@ pub struct Args {
fn main() -> Result<(), Box<dyn Error>> {
color_eyre::install()?;

let args = Args::parse();
let mut rng = ChaCha20Rng::from_seed([0u8; 32]);
let cli = Cli::parse();

match cli {
Cli::V1(args) => v1_exec(args),
Cli::V2(args) => v2_exec(args),
}
}

/// Number of voting options
const VOTING_OPTIONS: u8 = 2;

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

@@ -72,7 +91,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let choice = args.choice;

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

@@ -98,3 +117,46 @@ fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

fn v2_exec(args: CliArgs) -> Result<(), Box<dyn Error>> {
let sk_bytes = hex::decode(args.private_key)?;

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

let election_pk_bytes = Vec::<u8>::from_base32(&data).map_err(Bech32Error::from)?;

let private_key = catalyst_voting::crypto::ed25519::PrivateKey::from_bytes(
&sk_bytes
.try_into()
.map_err(|_| "private key invalid length")?,
);
let election_public_key =
catalyst_voting::vote_protocol::committee::ElectionPublicKey::from_bytes(
&election_pk_bytes
.try_into()
.map_err(|_| "election public key invalid length")?,
)?;

let vote_plan_id = hex::decode(args.vote_plan_id.clone())?
.try_into()
.map_err(|_| "vote plan id invalid length")?;
let proposal_index = args.proposal;
let choice = args.choice;

let tx = catalyst_voting::txs::v1::Tx::new_private_with_default_rng(
vote_plan_id,
proposal_index,
VOTING_OPTIONS,
choice,
&election_public_key,
&private_key,
)?;

// fragment in hex: output consumed as input to another program
println!("{:?}", hex::encode(tx.to_bytes()));

Ok(())
}