Skip to content

Commit

Permalink
change browser-electron and wallet-js too
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Jan 28, 2021
1 parent 80e0769 commit 01db3b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bindings/wallet-cordova/src/electron/bindings.js
Expand Up @@ -146,7 +146,7 @@ async function proposalNewPrivate (successCallback, errorCallback, opts) {
const id = m.VotePlanId.new_from_bytes(new Uint8Array(votePlanId));

const options = m.Options.new_length(numChoices);
const key = m.EncryptingVoteKey.from_bytes(new Uint8Array(encryptionKey));
const key = m.EncryptingVoteKey.from_bech32(encryptionKey);
const proposal = m.Proposal.new_private(id, index, options, key);
successCallback(proposal.ptr.toString());
} catch (err) {
Expand Down
21 changes: 21 additions & 0 deletions bindings/wallet-js/src/lib.rs
Expand Up @@ -8,6 +8,8 @@ use wasm_bindgen::JsCast as _;

mod utils;

const ENCRYPTION_VOTE_KEY_HRP: &str = "p256k1_votepk";

// `set_panic_hook` function can be called at least once during initialization,
// to get better error messages if the code ever panics.
pub use utils::set_panic_hook;
Expand Down Expand Up @@ -395,4 +397,23 @@ impl EncryptingVoteKey {
.ok_or_else(|| JsValue::from_str("invalid binary format"))
.map(Self)
}

pub fn from_bech32(bech32_str: &str) -> Result<EncryptingVoteKey, JsValue> {
use bech32::FromBase32;

bech32::decode(bech32_str)
.map_err(|e| JsValue::from_str(&format!("invalid bech32 string {}", e)))
.and_then(|(hrp, raw_key)| {
if hrp != ENCRYPTION_VOTE_KEY_HRP {
return Err(JsValue::from_str(&format!(
"expected hrp to be {} instead found {}",
ENCRYPTION_VOTE_KEY_HRP, hrp
)));
}

let bytes = Vec::<u8>::from_base32(&raw_key).unwrap();

Self::from_bytes(&bytes)
})
}
}

0 comments on commit 01db3b7

Please sign in to comment.