From d465dcd630d13bbb4da2d25d4eac90c45e5a30c4 Mon Sep 17 00:00:00 2001 From: "J. Ayo Akinyele" Date: Sun, 11 Aug 2019 05:48:46 -0400 Subject: [PATCH] More clean up and better error handling --- examples/bolt_test.rs | 7 ++++++- src/ffishim.rs | 14 +++++++++++++- src/lib.rs | 36 ++++++++++++++++++++++-------------- src/util.rs | 1 - 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/examples/bolt_test.rs b/examples/bolt_test.rs index 1119da9..b710cf8 100644 --- a/examples/bolt_test.rs +++ b/examples/bolt_test.rs @@ -68,7 +68,12 @@ fn main() { println!(">> Time to generate proof for establish: {} ms", est_time); // obtain close token for closing out channel - let close_token = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let option = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let close_token= match option { + Ok(n) => n.unwrap(), + Err(e) => panic!("Failed - bidirectional::establish_merchant_issue_close_token(): {}", e) + }; + assert!(cust_wallet.verify_close_token(&channel_state, &close_token)); // wait for funding tx to be confirmed, etc diff --git a/src/ffishim.rs b/src/ffishim.rs index e4d2bc8..bddbb51 100644 --- a/src/ffishim.rs +++ b/src/ffishim.rs @@ -14,6 +14,18 @@ pub mod ffishim { use std::mem; use serialization_wrappers; + fn error_message(s: String) -> *mut c_char { + let ser = ["{\'error\':\'", serde_json::to_string(&s).unwrap().as_str(), "\'}"].concat(); + let cser = CString::new(ser).unwrap(); + cser.into_raw() + } + + macro_rules! bolt_try { + ($e:expr) => (match $e { + Ok(val) => val.unwrap(), + Err(err) => return error_message(err), + }); + } fn deserialize_object<'a, T>(serialized: *mut c_char) -> T where @@ -135,7 +147,7 @@ pub mod ffishim { // Deserialize the merchant wallet let merch_wallet: bidirectional::MerchantWallet = deserialize_object(ser_merch_wallet); - let close_token = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let close_token = bolt_try!(bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet)); let ser = ["{\'close_token\':\'", serde_json::to_string(&close_token).unwrap().as_str(), "\'}"].concat(); let cser = CString::new(ser).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 661bf77..d377a0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,6 +183,8 @@ pub fn hash_pub_key_to_fr(wpk: &secp256k1::PublicKey) -> Fr { return Fr::interpret(&hash_buf); } +pub type BoltResult = Result, String>; + ////////////////////////////////// Utilities ////////////////////////////////// /////////////////////////////// Bidirectional //////////////////////////////// @@ -190,9 +192,6 @@ pub mod bidirectional { use std::fmt; use rand::{rngs::OsRng, Rng}; use rand_core::RngCore; - pub use channels::{ChannelState, ChannelToken, CustomerWallet, MerchantWallet, PubKeyMap, ChannelParams, BoltError, ResultBoltSig}; - pub use nizk::Proof; - pub use util::CommitmentProof; use util; use wallet; use pairing::{Engine, CurveProjective}; @@ -204,17 +203,16 @@ pub mod bidirectional { use sodiumoxide::crypto::hash::sha512; use sha2::Sha512; -// use curve25519_dalek::scalar::Scalar; -// use curve25519_dalek::ristretto::RistrettoPoint; -// use merlin::Transcript; -// use bulletproofs::{BulletproofGens, PedersenGens, RangeProof}; - use serialization_wrappers; use serde::{Serialize, Deserialize}; use std::sync::mpsc::channel; + use util::RevokedMessage; pub use ped92::Commitment; pub use cl::{PublicKey, Signature}; - use util::RevokedMessage; + pub use BoltResult; + pub use channels::{ChannelState, ChannelToken, CustomerWallet, MerchantWallet, PubKeyMap, ChannelParams, BoltError, ResultBoltSig}; + pub use nizk::Proof; + pub use util::CommitmentProof; #[derive(Clone, Serialize, Deserialize)] #[serde(bound(serialize = "::Fr: serde::Serialize, \ @@ -305,10 +303,12 @@ pub mod bidirectional { /// signature) over the contents of the customer's wallet. /// pub fn establish_merchant_issue_close_token(csprng: &mut R, channel_state: &ChannelState, - com: &Commitment, com_proof: &CommitmentProof, merch_wallet: &MerchantWallet) -> cl::Signature { + com: &Commitment, com_proof: &CommitmentProof, merch_wallet: &MerchantWallet) -> BoltResult> { // verifies proof of committed values and derives blind signature on the committed values to the customer's initial wallet - let (close_token, _) = merch_wallet.verify_proof(csprng, channel_state, com, com_proof).unwrap(); - return close_token; + match merch_wallet.verify_proof(csprng, channel_state, com, com_proof) { + Ok(n) => Ok(Some(n.0)), // just close token + Err(err) => Err(String::from(err.to_string())) + } } /// @@ -583,7 +583,11 @@ mod tests { let (com, com_proof) = bidirectional::establish_customer_generate_proof(rng, channel_token, cust_wallet); // obtain close token for closing out channel - let close_token = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let option = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let close_token= match option { + Ok(n) => n.unwrap(), + Err(e) => panic!("Failed - bidirectional::establish_merchant_issue_close_token(): {}", e) + }; assert!(cust_wallet.verify_close_token(&channel_state, &close_token)); // wait for funding tx to be confirmed, etc @@ -642,7 +646,11 @@ mod tests { let (com, com_proof) = bidirectional::establish_customer_generate_proof(rng, &mut channel_token, &mut cust_wallet); // obtain close token for closing out channel - let close_token = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let option = bidirectional::establish_merchant_issue_close_token(rng, &channel_state, &com, &com_proof, &merch_wallet); + let close_token= match option { + Ok(n) => n.unwrap(), + Err(e) => panic!("Failed - bidirectional::establish_merchant_issue_close_token(): {}", e) + }; assert!(cust_wallet.verify_close_token(&channel_state, &close_token)); // wait for funding tx to be confirmed, etc diff --git a/src/util.rs b/src/util.rs index aa2e55e..e9abc6c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -43,7 +43,6 @@ pub fn hash_g2_to_fr(x: &E::G2) -> E::Fr { hash_to_fr::(x_vec) } -// TODO: very buggy - revisit asap pub fn fmt_bytes_to_int(bytearray: [u8; 64]) -> String { let mut result: String = "".to_string(); for byte in bytearray.iter() {