Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiba committed Aug 29, 2019
1 parent f21a4ba commit ecc6a66
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
6 changes: 1 addition & 5 deletions gotham-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ uuid = { version = "0.7", features = ["v4"] }
electrumx_client = { git = "https://github.com/KZen-networks/rust-electrumx-client" }
itertools = "0.8.0"
hex = "0.3.2"
bitcoin = "0.16.0"
bitcoin = "0.20.0"
config = "0.9"

[dependencies.secp256k1]
version = "0.12"
features = ["rand", "serde"]

[dependencies.zk-paillier]
git = "https://github.com/KZen-networks/zk-paillier"
branch = "feature/libra-support"
Expand Down
2 changes: 1 addition & 1 deletion gotham-client/src/ecdsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub extern "C" fn sign_message(
Err(e) => return error_to_c_string(format_err!("decoding raw id failed: {}", e))
};

let x: BigInt = BigInt::from(c_x_pos);;
let x: BigInt = BigInt::from(c_x_pos);

let y: BigInt = BigInt::from(c_y_pos);

Expand Down
1 change: 0 additions & 1 deletion gotham-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extern crate bitcoin;
extern crate electrumx_client;
extern crate hex;
extern crate itertools;
extern crate secp256k1;
extern crate time;
extern crate uuid;

Expand Down
53 changes: 37 additions & 16 deletions gotham-client/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use bitcoin::consensus::encode::serialize;
use bitcoin::network::constants::Network;
use bitcoin::util::bip143::SighashComponents;
use bitcoin::{TxIn, TxOut};
use bitcoin::hashes::{sha256d, hex::FromHex};
use bitcoin::secp256k1::Signature;
use curv::elliptic::curves::traits::ECPoint;
use curv::{BigInt, GE};
use electrumx_client::{electrumx_client::ElectrumxClient, interface::Electrumx};
Expand All @@ -33,7 +35,6 @@ use super::ClientShim;
use curv::arithmetic::traits::Converter;
use hex;
use itertools::Itertools;
use secp256k1::Signature;
use std::collections::HashMap;
use std::str::FromStr;

Expand Down Expand Up @@ -251,7 +252,7 @@ impl Wallet {
.into_iter()
.map(|s| bitcoin::TxIn {
previous_output: bitcoin::OutPoint {
txid: bitcoin::util::hash::Sha256dHash::from_hex(&s.tx_hash).unwrap(),
txid: sha256d::Hash::from_hex(&s.tx_hash).unwrap(),
vout: s.tx_pos as u32,
},
script_sig: bitcoin::Script::default(),
Expand Down Expand Up @@ -303,13 +304,15 @@ impl Wallet {
let comp = SighashComponents::new(&transaction);
let sig_hash = comp.sighash_all(
&transaction.input[i],
&bitcoin::Address::p2pkh(&pk, self.get_bitcoin_network()).script_pubkey(),
&bitcoin::Address::p2pkh(
&to_bitcoin_public_key(pk),
self.get_bitcoin_network()).script_pubkey(),
(selected[i].value as u32).into(),
);

let signature = ecdsa::sign(
client_shim,
BigInt::from_hex(&sig_hash.le_hex_string()),
BigInt::from_hex(&hex::encode(&sig_hash[..])),
&mk,
BigInt::from(0),
BigInt::from(address_derivation.pos),
Expand All @@ -319,14 +322,15 @@ impl Wallet {
let mut v = BigInt::to_vec(&signature.r);
v.extend(BigInt::to_vec(&signature.s));

let mut sig = Signature::from_compact(&v[..]).unwrap().serialize_der();
let mut sig_vec = Signature::from_compact(&v[..])
.unwrap()
.serialize_der()
.to_vec();
sig_vec.push(01);

sig.push(01);
let mut witness = Vec::new();
witness.push(sig);
witness.push(pk.serialize().to_vec());
let pk_vec = pk.serialize().to_vec();

signed_transaction.input[i].witness = witness;
signed_transaction.input[i].witness = vec![sig_vec, pk_vec];
}

let mut electrum = ElectrumxClient::new(ELECTRUM_HOST).unwrap();
Expand All @@ -340,7 +344,10 @@ impl Wallet {
pub fn get_new_bitcoin_address(&mut self) -> bitcoin::Address {
let (pos, mk) = Self::derive_new_key(&self.private_share, self.last_derived_pos);
let pk = mk.public.q.get_element();
let address = bitcoin::Address::p2wpkh(&pk, self.get_bitcoin_network());
let address = bitcoin::Address::p2wpkh(
&to_bitcoin_public_key(pk),
self.get_bitcoin_network()
);

self.addresses_derivation_map
.insert(address.to_string(), AddressDerivation { mk, pos });
Expand All @@ -355,7 +362,10 @@ impl Wallet {
let (pos, mk) = Self::derive_new_key(&self.private_share, i);

let address =
bitcoin::Address::p2wpkh(&mk.public.q.get_element(), self.get_bitcoin_network());
bitcoin::Address::p2wpkh(
&to_bitcoin_public_key(mk.public.q.get_element()),
self.get_bitcoin_network()
);

self.addresses_derivation_map
.insert(address.to_string(), AddressDerivation { mk, pos });
Expand All @@ -382,7 +392,7 @@ impl Wallet {
let list_unspent: Vec<GetListUnspentResponse> = self
.get_all_addresses_balance()
.into_iter()
.filter(|b| b.confirmed > 0)
// .filter(|b| b.confirmed > 0)
.map(|a| self.list_unspent_for_addresss(a.address.to_string()))
.flatten()
.sorted_by(|a, b| a.value.partial_cmp(&b.value).unwrap())
Expand Down Expand Up @@ -488,13 +498,24 @@ impl Wallet {
}

fn to_bitcoin_address(mk: &MasterKey2, network: Network) -> bitcoin::Address {
bitcoin::Address::p2wpkh(&mk.public.q.get_element(), network)
bitcoin::Address::p2wpkh(
&to_bitcoin_public_key(mk.public.q.get_element()),
network
)
}
}

// type conversion
fn to_bitcoin_public_key(pk: curv::PK) -> bitcoin::util::key::PublicKey {
bitcoin::util::key::PublicKey {
compressed: true,
key: pk
}
}

#[cfg(test)]
mod tests {
use bitcoin::util::hash::Sha256dHash;
use bitcoin::hashes::sha256d;
use curv::arithmetic::traits::Converter;
use curv::BigInt;

Expand All @@ -506,7 +527,7 @@ mod tests {
];

// 14abf5ed107ff58bf844ee7f447bec317c276b00905c09a45434f8848599597e
let hash = Sha256dHash::from_data(&message);
let hash = sha256d::Hash::from_slice(&message);

// 7e59998584f83454a4095c90006b277c31ec7b447fee44f88bf57f10edf5ab14
let ser = hash.le_hex_string();
Expand Down
11 changes: 8 additions & 3 deletions integration-tests/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ mod tests {
.get_child(vec![x_pos.clone(), y_pos.clone()]);

let msg: BigInt = BigInt::from(1234); // arbitrary message
let signature = ecdsa::sign(&client_shim, msg, &child_master_key, x_pos, y_pos, &ps.id);
let signature = ecdsa::sign(&client_shim, msg, &child_master_key, x_pos, y_pos, &ps.id)
.expect("ECDSA signature failed");

println!(
"signature = (r: {}, s: {})",
signature.r.to_hex(),
Expand All @@ -39,7 +41,9 @@ mod tests {
let share: schnorr::Share = schnorr::generate_key(&client_shim).unwrap();

let msg: BigInt = BigInt::from(1234); // arbitrary message
let signature: schnorr::Signature = schnorr::sign(&client_shim, msg, &share).unwrap();
let signature = schnorr::sign(&client_shim, msg, &share)
.expect("Schnorr signature failed");

println!(
"signature = (e: {:?}, s: {:?})",
signature.e,
Expand All @@ -60,7 +64,8 @@ mod tests {

let message = BigInt::from(1234);
let signature = client_lib::eddsa::sign(&client_shim, message, &key_pair, &key_agg, &id)
.expect("Error while signing");
.expect("EdDSA signature failed");

println!(
"signature = (R: {}, s: {})",
signature.R.bytes_compressed_to_big_int().to_hex(),
Expand Down

0 comments on commit ecc6a66

Please sign in to comment.