From 34c0b9c4d9bc0c4277ec3f47e797e010ff5894e3 Mon Sep 17 00:00:00 2001 From: cong-or Date: Thu, 12 Oct 2023 22:14:47 +0100 Subject: [PATCH] refactor(bech32 decoding): election pub key --- Cargo.lock | 1 + src/sign/Cargo.toml | 1 + src/sign/README.md | 4 ++-- src/sign/src/fragment.rs | 10 +++++----- src/sign/src/main.rs | 12 ++++++++++-- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2012f93739..b85ebd47a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6526,6 +6526,7 @@ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" name = "sign" version = "0.1.0" dependencies = [ + "bech32 0.8.1", "chain-addr", "chain-core", "chain-crypto", diff --git a/src/sign/Cargo.toml b/src/sign/Cargo.toml index 6728f4d93f..bd8060caa1 100644 --- a/src/sign/Cargo.toml +++ b/src/sign/Cargo.toml @@ -33,6 +33,7 @@ serde = "1.0" 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 } diff --git a/src/sign/README.md b/src/sign/README.md index d3e1415a6f..199360735a 100644 --- a/src/sign/README.md +++ b/src/sign/README.md @@ -23,12 +23,12 @@ cargo build --release -p sign ```bash -ELECTION_PUB_KEY=bed88887abe0a84f64691fe0bdfa3daf1a6cd697a13f07ae07588910ce39c927 +ELECTION_PUB_KEY=ristretto255_votepk1ppxnuxrqa4728evnp2ues000uvwvwtxmtf77ejc29lknjuqqu44s4cfmja ALICE_SK=56e367979579e2ce27fbd305892b0706b7dede999a534a864a7430a5c6aefd3c ALICE_PK=ea084d2d80ed0ab681333d934efc56df3868d13d46a2de3b7f27f40b62e5344d PROPOSAL=5 VOTE_PLAN_ID=36ad42885189a0ac3438cdb57bc8ac7f6542e05a59d1f2e4d1d38194c9d4ac7b -./target/release/signer --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 ``` \ No newline at end of file diff --git a/src/sign/src/fragment.rs b/src/sign/src/fragment.rs index 4265d49cc6..1baf477814 100644 --- a/src/sign/src/fragment.rs +++ b/src/sign/src/fragment.rs @@ -76,7 +76,7 @@ pub fn generate_vote_fragment( vote_cast.put_be_u32(data.len() as u32 + PADDING_AND_TAG_SIZE)?; vote_cast.put_u8(PADDING)?; vote_cast.put_u8(VOTE_CAST_TAG)?; - vote_cast.put_bytes(&data.as_slice())?; + vote_cast.put_bytes(data.as_slice())?; Ok(vote_cast.into_inner()) } @@ -130,7 +130,7 @@ pub fn compose_encrypted_vote_part( // prepend with SIZE-ELEMENT-8BIT let mut encrypted_vote = Codec::new(Vec::new()); encrypted_vote.put_u8(size_element as u8)?; - encrypted_vote.put_bytes(&encrypted_bytes.as_slice())?; + encrypted_vote.put_bytes(encrypted_bytes.as_slice())?; let mut proof_bytes = Codec::new(Vec::new()); @@ -138,15 +138,15 @@ pub fn compose_encrypted_vote_part( proof_bytes.put_bytes(&announcement.to_bytes())?; } - for cipher in proof.ds().into_iter() { + for cipher in proof.ds() { proof_bytes.put_bytes(&cipher.to_bytes())?; } - for response in proof.zwvs().into_iter() { + for response in proof.zwvs() { proof_bytes.put_bytes(&response.to_bytes())?; } - proof_bytes.put_bytes(&proof.r().as_bytes())?; + proof_bytes.put_bytes(proof.r().as_bytes())?; // prepend with SIZE-ELEMENT-8BIT let mut proof_vote = Codec::new(Vec::new()); diff --git a/src/sign/src/main.rs b/src/sign/src/main.rs index dd3c477240..781e495f6e 100644 --- a/src/sign/src/main.rs +++ b/src/sign/src/main.rs @@ -2,6 +2,8 @@ //! Fragment generator //! +use bech32::Error as Bech32Error; +use bech32::FromBase32; use chain_vote::ElectionPublicKey; use clap::Parser; use color_eyre::Result; @@ -46,14 +48,20 @@ fn main() -> Result<(), Box> { let pk = hex::decode(args.public_key)?; let mut sk = hex::decode(args.private_key)?; - let election_pk = hex::decode(args.election_pub_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 = Vec::::from_base32(&data).map_err(Bech32Error::from)?; // join sk+pk together, api requirement sk.extend(pk.clone()); let keypair: Keypair = Keypair::from_bytes(&sk)?; // vote - let vote = chain_vote::Vote::new(2, 1 as usize).unwrap(); + let vote = chain_vote::Vote::new(2, 1_usize)?; let crs = chain_vote::Crs::from_hash(args.vote_plan_id.clone().as_bytes()); // parse ek key