From: Tamas Blummer Date: Mon, 4 Mar 2019 17:02:02 +0000 (+0100) Subject: migration to rust-bitcoin 0.17 X-Git-Tag: v0.0.12~223^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=02b541607bb7a8234675b1e2101c1e31d372706f;p=rust-lightning migration to rust-bitcoin 0.17 typedef Sha256dHash with bitcoin_hashes::sha256d::Hash SecretKey -> PrivateKey.key assume compressed public keys --- diff --git a/Cargo.toml b/Cargo.toml index caf8b35b3..df2808e90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,13 +22,13 @@ max_level_info = [] max_level_debug = [] [dependencies] -bitcoin = "0.16" +bitcoin = "0.17" bitcoin_hashes = "0.3" rand = "0.4" secp256k1 = "0.12" [dev-dependencies.bitcoin] -version = "0.16" +version = "0.17" features = ["bitcoinconsensus"] [dev-dependencies] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 21f3bac22..1f307ab85 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -18,8 +18,8 @@ libfuzzer_fuzz = ["libfuzzer-sys"] [dependencies] afl = { version = "0.4", optional = true } lightning = { path = "..", features = ["fuzztarget"] } -bitcoin = { version = "0.16", features = ["fuzztarget"] } -bitcoin_hashes = { version = "0.2", features=["fuzztarget"] } +bitcoin = { version = "0.17", features = ["fuzztarget"] } +bitcoin_hashes = { version = "0.3", features=["fuzztarget"] } hex = "0.3" honggfuzz = { version = "0.5", optional = true } secp256k1 = { version = "0.12", features=["fuzztarget"] } diff --git a/fuzz/fuzz_targets/chanmon_deser_target.rs b/fuzz/fuzz_targets/chanmon_deser_target.rs index fb8f0bf6f..4a1044478 100644 --- a/fuzz/fuzz_targets/chanmon_deser_target.rs +++ b/fuzz/fuzz_targets/chanmon_deser_target.rs @@ -2,9 +2,10 @@ // To modify it, modify msg_target_template.txt and run gen_target.sh instead. extern crate bitcoin; +extern crate bitcoin_hashes; extern crate lightning; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use lightning::ln::channelmonitor; use lightning::util::reset_rng_state; diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 07ee7abac..937c73506 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -18,12 +18,13 @@ use bitcoin::blockdata::script::{Builder, Script}; use bitcoin::blockdata::opcodes; use bitcoin::consensus::encode::deserialize; use bitcoin::network::constants::Network; -use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; use bitcoin_hashes::Hash as TraitImport; use bitcoin_hashes::HashEngine as TraitImportEngine; use bitcoin_hashes::sha256::Hash as Sha256; use bitcoin_hashes::hash160::Hash as Hash160; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil}; use lightning::chain::transaction::OutPoint; diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index 3a40d3985..70164985b 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -1,8 +1,9 @@ extern crate bitcoin; +extern crate bitcoin_hashes; extern crate lightning; extern crate secp256k1; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::blockdata::script::{Script, Builder}; use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener}; diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs index ef09cbbaa..d1995b442 100644 --- a/src/chain/chaininterface.rs +++ b/src/chain/chaininterface.rs @@ -8,7 +8,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; use bitcoin::blockdata::constants::genesis_block; -use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::network::constants::Network; use util::logger::Logger; diff --git a/src/chain/keysinterface.rs b/src/chain/keysinterface.rs index 9369d3c35..013a3388f 100644 --- a/src/chain/keysinterface.rs +++ b/src/chain/keysinterface.rs @@ -141,23 +141,23 @@ impl KeysManager { let secp_ctx = Secp256k1::signing_only(); match ExtendedPrivKey::new_master(network.clone(), seed) { Ok(master_key) => { - let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0)).expect("Your RNG is busted").secret_key; - let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1)) { + let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key; + let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) { Ok(destination_key) => { - let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.serialize()[..]); + let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.key.serialize()[..]); Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0) .push_slice(&pubkey_hash160.into_inner()) .into_script() }, Err(_) => panic!("Your RNG is busted"), }; - let shutdown_pubkey = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(2)) { - Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key).public_key, + let shutdown_pubkey = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(2).unwrap()) { + Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key).public_key.key, Err(_) => panic!("Your RNG is busted"), }; - let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3)).expect("Your RNG is busted"); - let session_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(4)).expect("Your RNG is busted"); - let channel_id_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5)).expect("Your RNG is busted"); + let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3).unwrap()).expect("Your RNG is busted"); + let session_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(4).unwrap()).expect("Your RNG is busted"); + let channel_id_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5).unwrap()).expect("Your RNG is busted"); KeysManager { secp_ctx, node_secret, @@ -207,8 +207,8 @@ impl KeysInterface for KeysManager { sha.input(&byte_utils::be64_to_array(now.as_secs())); let child_ix = self.channel_child_index.fetch_add(1, Ordering::AcqRel); - let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted"); - sha.input(&child_privkey.secret_key[..]); + let child_privkey = self.channel_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted"); + sha.input(&child_privkey.private_key.key[..]); seed = Sha256::from_engine(sha).into_inner(); @@ -251,8 +251,8 @@ impl KeysInterface for KeysManager { sha.input(&byte_utils::be64_to_array(now.as_secs())); let child_ix = self.session_child_index.fetch_add(1, Ordering::AcqRel); - let child_privkey = self.session_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted"); - sha.input(&child_privkey.secret_key[..]); + let child_privkey = self.session_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted"); + sha.input(&child_privkey.private_key.key[..]); SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("Your RNG is busted") } @@ -264,8 +264,8 @@ impl KeysInterface for KeysManager { sha.input(&byte_utils::be64_to_array(now.as_secs())); let child_ix = self.channel_id_child_index.fetch_add(1, Ordering::AcqRel); - let child_privkey = self.channel_id_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted"); - sha.input(&child_privkey.secret_key[..]); + let child_privkey = self.channel_id_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted"); + sha.input(&child_privkey.private_key.key[..]); (Sha256::from_engine(sha).into_inner()) } diff --git a/src/chain/transaction.rs b/src/chain/transaction.rs index a9b314dd0..ce43984eb 100644 --- a/src/chain/transaction.rs +++ b/src/chain/transaction.rs @@ -1,6 +1,6 @@ //! Contains simple structs describing parts of transactions on the chain. -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; /// A reference to a transaction output. diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs index e7945a508..eab80a54e 100644 --- a/src/ln/chan_utils.rs +++ b/src/ln/chan_utils.rs @@ -1,12 +1,12 @@ use bitcoin::blockdata::script::{Script,Builder}; use bitcoin::blockdata::opcodes; use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction}; -use bitcoin::util::hash::{Sha256dHash}; use bitcoin_hashes::{Hash, HashEngine}; use bitcoin_hashes::sha256::Hash as Sha256; use bitcoin_hashes::ripemd160::Hash as Ripemd160; use bitcoin_hashes::hash160::Hash as Hash160; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use ln::channelmanager::PaymentHash; diff --git a/src/ln/channel.rs b/src/ln/channel.rs index beab7ffc6..64da86e58 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -2,13 +2,14 @@ use bitcoin::blockdata::block::BlockHeader; use bitcoin::blockdata::script::{Script,Builder}; use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType}; use bitcoin::blockdata::opcodes; -use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; use bitcoin::util::bip143; use bitcoin::consensus::encode::{self, Encodable, Decodable}; use bitcoin_hashes::{Hash, HashEngine}; use bitcoin_hashes::sha256::Hash as Sha256; use bitcoin_hashes::hash160::Hash as Hash160; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::{Secp256k1,Signature}; @@ -3143,7 +3144,7 @@ impl Channel { excess_data: Vec::new(), }; - let msghash = hash_to_message!(&Sha256dHash::from_data(&msg.encode()[..])[..]); + let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]); let sig = self.secp_ctx.sign(&msghash, &self.local_keys.funding_key); Ok((msg, sig)) @@ -3954,12 +3955,12 @@ impl ReadableArgs> for Channel { #[cfg(test)] mod tests { - use bitcoin::util::hash::{Sha256dHash, Hash160}; use bitcoin::util::bip143; use bitcoin::consensus::encode::serialize; use bitcoin::blockdata::script::{Script, Builder}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::opcodes; + use bitcoin_hashes::hex::FromHex; use hex; use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash}; use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys}; @@ -3974,6 +3975,8 @@ mod tests { use secp256k1::{Secp256k1,Message,Signature}; use secp256k1::key::{SecretKey,PublicKey}; use bitcoin_hashes::sha256::Hash as Sha256; + use bitcoin_hashes::sha256d::Hash as Sha256dHash; + use bitcoin_hashes::hash160::Hash as Hash160; use bitcoin_hashes::Hash; use std::sync::Arc; @@ -4000,7 +4003,7 @@ mod tests { fn get_destination_script(&self) -> Script { let secp_ctx = Secp256k1::signing_only(); let channel_monitor_claim_key = SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(); - let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize()); + let our_channel_monitor_claim_key_hash = Hash160::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize()); Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script() } diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 23d5eb21f..e3de3c09e 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -12,11 +12,12 @@ use bitcoin::blockdata::block::BlockHeader; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::constants::genesis_block; use bitcoin::network::constants::Network; -use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; use bitcoin_hashes::{Hash, HashEngine}; use bitcoin_hashes::hmac::{Hmac, HmacEngine}; use bitcoin_hashes::sha256::Hash as Sha256; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin_hashes::cmp::fixed_time_eq; use secp256k1::key::{SecretKey,PublicKey}; @@ -964,7 +965,7 @@ impl ChannelManager { excess_data: Vec::new(), }; - let msg_hash = Sha256dHash::from_data(&unsigned.encode()[..]); + let msg_hash = Sha256dHash::hash(&unsigned.encode()[..]); let sig = self.secp_ctx.sign(&hash_to_message!(&msg_hash[..]), &self.our_network_key); Ok(msgs::ChannelUpdate { @@ -1158,7 +1159,7 @@ impl ChannelManager { Ok(res) => res, Err(_) => return None, // Only in case of state precondition violations eg channel is closing }; - let msghash = hash_to_message!(&Sha256dHash::from_data(&announcement.encode()[..])[..]); + let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]); let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key); Some(msgs::AnnouncementSignatures { @@ -2182,7 +2183,7 @@ impl ChannelManager { try_chan_entry!(self, chan.get_mut().get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()), channel_state, chan); let were_node_one = announcement.node_id_1 == our_node_id; - let msghash = hash_to_message!(&Sha256dHash::from_data(&announcement.encode()[..])[..]); + let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]); if self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }).is_err() || self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }).is_err() { try_chan_entry!(self, Err(ChannelError::Close("Bad announcement_signatures node_signature")), channel_state, chan); diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs index 3b5905e70..8900b5078 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -17,12 +17,13 @@ use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; use bitcoin::blockdata::script::{Script, Builder}; use bitcoin::blockdata::opcodes; use bitcoin::consensus::encode::{self, Decodable, Encodable}; -use bitcoin::util::hash::{BitcoinHash,Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; use bitcoin::util::bip143; use bitcoin_hashes::Hash; use bitcoin_hashes::sha256::Hash as Sha256; use bitcoin_hashes::hash160::Hash as Hash160; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use secp256k1::{Secp256k1,Signature}; use secp256k1::key::{SecretKey,PublicKey}; @@ -1179,7 +1180,7 @@ impl ChannelMonitor { // on-chain claims, so we can do that at the same time. macro_rules! check_htlc_fails { ($txid: expr, $commitment_tx: expr) => { - if let Some(ref outpoints) = self.remote_claimable_outpoints.get(&$txid) { + if let Some(ref outpoints) = self.remote_claimable_outpoints.get($txid) { for &(ref htlc, ref source_option) in outpoints.iter() { if let &Some(ref source) = source_option { log_trace!(self, "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction", log_bytes!(htlc.payment_hash.0), $commitment_tx); @@ -1243,7 +1244,7 @@ impl ChannelMonitor { // on-chain claims, so we can do that at the same time. macro_rules! check_htlc_fails { ($txid: expr, $commitment_tx: expr, $id: tt) => { - if let Some(ref latest_outpoints) = self.remote_claimable_outpoints.get(&$txid) { + if let Some(ref latest_outpoints) = self.remote_claimable_outpoints.get($txid) { $id: for &(ref htlc, ref source_option) in latest_outpoints.iter() { if let &Some(ref source) = source_option { // Check if the HTLC is present in the commitment transaction that was diff --git a/src/ln/functional_tests.rs b/src/ln/functional_tests.rs index 58c8cd629..049777dad 100644 --- a/src/ln/functional_tests.rs +++ b/src/ln/functional_tests.rs @@ -21,7 +21,8 @@ use util::ser::{Writeable, ReadableArgs}; use util::config::UserConfig; use util::rng; -use bitcoin::util::hash::{BitcoinHash, Sha256dHash}; +use bitcoin::util::hash::BitcoinHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::util::bip143; use bitcoin::util::address::Address; use bitcoin::util::bip32::{ChildNumber, ExtendedPubKey, ExtendedPrivKey}; @@ -2486,7 +2487,7 @@ fn test_force_close_fail_back() { // Now check that if we add the preimage to ChannelMonitor it broadcasts our HTLC-Success.. { let mut monitors = nodes[2].chan_monitor.simple_monitor.monitors.lock().unwrap(); - monitors.get_mut(&OutPoint::new(Sha256dHash::from(&payment_event.commitment_msg.channel_id[..]), 0)).unwrap() + monitors.get_mut(&OutPoint::new(Sha256dHash::from_slice(&payment_event.commitment_msg.channel_id[..]).unwrap(), 0)).unwrap() .provide_payment_preimage(&our_payment_hash, &our_payment_preimage); } nodes[2].chain_monitor.block_connected_checked(&header, 1, &[&tx], &[1]); @@ -3018,7 +3019,7 @@ fn test_invalid_channel_announcement() { macro_rules! sign_msg { ($unsigned_msg: expr) => { - let msghash = Message::from_slice(&Sha256dHash::from_data(&$unsigned_msg.encode()[..])[..]).unwrap(); + let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap(); let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key); let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key); let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret()); @@ -3045,7 +3046,7 @@ fn test_invalid_channel_announcement() { assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); let mut unsigned_msg = dummy_unsigned_msg!(); - unsigned_msg.chain_hash = Sha256dHash::from_data(&[1,2,3,4,5,6,7,8,9]); + unsigned_msg.chain_hash = Sha256dHash::hash(&[1,2,3,4,5,6,7,8,9]); sign_msg!(unsigned_msg); assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); } @@ -3267,7 +3268,7 @@ macro_rules! check_spendable_outputs { }; let secp_ctx = Secp256k1::new(); let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key); - let witness_script = Address::p2pkh(&remotepubkey, Network::Testnet).script_pubkey(); + let witness_script = Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey(); let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap(); let remotesig = secp_ctx.sign(&sighash, key); spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec()); @@ -3322,7 +3323,7 @@ macro_rules! check_spendable_outputs { let secret = { match ExtendedPrivKey::new_master(Network::Testnet, &$node.node_seed) { Ok(master_key) => { - match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx)) { + match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx($der_idx).expect("key space exhausted")) { Ok(key) => key, Err(_) => panic!("Your RNG is busted"), } @@ -3333,10 +3334,10 @@ macro_rules! check_spendable_outputs { let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key; let witness_script = Address::p2pkh(&pubkey, Network::Testnet).script_pubkey(); let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap(); - let sig = secp_ctx.sign(&sighash, &secret.secret_key); + let sig = secp_ctx.sign(&sighash, &secret.private_key.key); spend_tx.input[0].witness.push(sig.serialize_der().to_vec()); spend_tx.input[0].witness[0].push(SigHashType::All as u8); - spend_tx.input[0].witness.push(pubkey.serialize().to_vec()); + spend_tx.input[0].witness.push(pubkey.key.serialize().to_vec()); txn.push(spend_tx); }, } @@ -4456,7 +4457,7 @@ impl msgs::ChannelUpdate { msgs::ChannelUpdate { signature: Signature::from(FFISignature::new()), contents: msgs::UnsignedChannelUpdate { - chain_hash: Sha256dHash::from_data(&vec![0u8][..]), + chain_hash: Sha256dHash::hash(&vec![0u8][..]), short_channel_id: 0, timestamp: 0, flags: 0, diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 125316d3f..6aa9e05b2 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -18,7 +18,7 @@ use secp256k1::key::PublicKey; use secp256k1::Signature; use secp256k1; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::blockdata::script::Script; use std::error::Error; @@ -1386,7 +1386,8 @@ mod tests { use ln::channelmanager::{PaymentPreimage, PaymentHash}; use util::ser::Writeable; - use bitcoin::util::hash::Sha256dHash; + use bitcoin_hashes::sha256d::Hash as Sha256dHash; + use bitcoin_hashes::hex::FromHex; use bitcoin::util::address::Address; use bitcoin::network::constants::Network; use bitcoin::blockdata::script::Builder; @@ -1712,7 +1713,7 @@ mod tests { htlc_basepoint: pubkey_5, first_per_commitment_point: pubkey_6, channel_flags: if random_bit { 1 << 5 } else { 0 }, - shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { OptionalField::Absent } + shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent } }; let encoded_value = open_channel.encode(); let mut target_value = Vec::new(); @@ -1765,7 +1766,7 @@ mod tests { delayed_payment_basepoint: pubkey_4, htlc_basepoint: pubkey_5, first_per_commitment_point: pubkey_6, - shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { OptionalField::Absent } + shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent } }; let encoded_value = accept_channel.encode(); let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap(); @@ -1830,7 +1831,7 @@ mod tests { let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script(); let shutdown = msgs::Shutdown { channel_id: [2; 32], - scriptpubkey: if script_type == 1 { Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&pubkey_1, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() }, + scriptpubkey: if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() }, }; let encoded_value = shutdown.encode(); let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap(); diff --git a/src/ln/router.rs b/src/ln/router.rs index 1d932ea99..dc58f6d20 100644 --- a/src/ln/router.rs +++ b/src/ln/router.rs @@ -7,7 +7,8 @@ use secp256k1::key::PublicKey; use secp256k1::Secp256k1; use secp256k1; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; +use bitcoin_hashes::Hash; use bitcoin::blockdata::script::Builder; use bitcoin::blockdata::opcodes; @@ -410,7 +411,7 @@ macro_rules! secp_verify_sig { impl RoutingMessageHandler for Router { fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result { - let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]); + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id); if msg.contents.features.requires_unknown_bits() { @@ -443,7 +444,7 @@ impl RoutingMessageHandler for Router { return Err(HandleError{err: "Channel announcement node had a channel with itself", action: Some(ErrorAction::IgnoreError)}); } - let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]); + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &msg.contents.node_id_2); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &msg.contents.bitcoin_key_1); @@ -619,7 +620,7 @@ impl RoutingMessageHandler for Router { }; } } - let msg_hash = hash_to_message!(&Sha256dHash::from_data(&msg.contents.encode()[..])[..]); + let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]); if msg.contents.flags & 1 == 1 { dest_node_id = channel.one_to_two.src_node_id.clone(); secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &channel.two_to_one.src_node_id); @@ -1020,7 +1021,8 @@ mod tests { use util::logger::Logger; use util::ser::{Writeable, Readable}; - use bitcoin::util::hash::Sha256dHash; + use bitcoin_hashes::sha256d::Hash as Sha256dHash; + use bitcoin_hashes::Hash; use bitcoin::network::constants::Network; use hex; @@ -1104,7 +1106,7 @@ mod tests { let node7 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0808080808080808080808080808080808080808080808080808080808080808").unwrap()[..]).unwrap()); let node8 = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0909090909090909090909090909090909090909090909090909090909090909").unwrap()[..]).unwrap()); - let zero_hash = Sha256dHash::from_data(&[0; 32]); + let zero_hash = Sha256dHash::hash(&[0; 32]); { let mut network = router.network_map.write().unwrap(); diff --git a/src/util/macro_logger.rs b/src/util/macro_logger.rs index fb38e347c..1f6854277 100644 --- a/src/util/macro_logger.rs +++ b/src/util/macro_logger.rs @@ -1,6 +1,6 @@ use chain::transaction::OutPoint; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use secp256k1::key::PublicKey; use ln::router::Route; diff --git a/src/util/ser.rs b/src/util/ser.rs index d832c7018..1b10a3930 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -8,8 +8,8 @@ use std::hash::Hash; use secp256k1::Signature; use secp256k1::key::{PublicKey, SecretKey}; -use bitcoin::util::hash::Sha256dHash; use bitcoin::blockdata::script::Script; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use std::marker::Sized; use ln::msgs::DecodeError; use ln::channelmanager::{PaymentPreimage, PaymentHash}; @@ -342,14 +342,16 @@ impl Readable for SecretKey { impl Writeable for Sha256dHash { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { - self.as_bytes().write(w) + w.write_all(&self[..]) } } impl Readable for Sha256dHash { fn read(r: &mut R) -> Result { + use bitcoin_hashes::Hash; + let buf: [u8; 32] = Readable::read(r)?; - Ok(From::from(&buf[..])) + Ok(Sha256dHash::from_slice(&buf[..]).unwrap()) } } diff --git a/src/util/test_utils.rs b/src/util/test_utils.rs index 4360c5701..b9c12a076 100644 --- a/src/util/test_utils.rs +++ b/src/util/test_utils.rs @@ -12,7 +12,7 @@ use util::ser::{ReadableArgs, Writer}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; -use bitcoin::util::hash::Sha256dHash; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use bitcoin::network::constants::Network; use secp256k1::{SecretKey, PublicKey}; diff --git a/src/util/transaction_utils.rs b/src/util/transaction_utils.rs index 33072f605..74ea81adb 100644 --- a/src/util/transaction_utils.rs +++ b/src/util/transaction_utils.rs @@ -1,36 +1,34 @@ use bitcoin::blockdata::transaction::{TxIn, TxOut}; +use bitcoin_hashes::sha256d::Hash as Sha256dHash; use std::cmp::Ordering; pub fn sort_outputs(outputs: &mut Vec<(TxOut, T)>) { outputs.sort_unstable_by(|a, b| { - if a.0.value < b.0.value { - Ordering::Less - } else if b.0.value < a.0.value { - Ordering::Greater - } else if a.0.script_pubkey[..] < b.0.script_pubkey[..] { - Ordering::Less - } else if b.0.script_pubkey[..] < a.0.script_pubkey[..] { - Ordering::Greater - } else { - Ordering::Equal - } + a.0.value.cmp(&b.0.value).then( + a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]) + ) }); } +fn cmp(a: &Sha256dHash, b: &Sha256dHash) -> Ordering { + use bitcoin_hashes::Hash; + + let av = a.into_inner(); + let bv = b.into_inner(); + for i in (0..32).rev() { + let cmp = av[i].cmp(&bv[i]); + if cmp != Ordering::Equal { + return cmp; + } + } + Ordering::Equal +} + pub fn sort_inputs(inputs: &mut Vec<(TxIn, T)>) { inputs.sort_unstable_by(|a, b| { - if a.0.previous_output.txid.into_le() < b.0.previous_output.txid.into_le() { - Ordering::Less - } else if b.0.previous_output.txid.into_le() < a.0.previous_output.txid.into_le() { - Ordering::Greater - } else if a.0.previous_output.vout < b.0.previous_output.vout { - Ordering::Less - } else if b.0.previous_output.vout < a.0.previous_output.vout { - Ordering::Greater - } else { - Ordering::Equal - } + cmp( &a.0.previous_output.txid, &b.0.previous_output.txid).then( + a.0.previous_output.vout.cmp(&b.0.previous_output.vout)) }); } @@ -40,7 +38,8 @@ mod tests { use bitcoin::blockdata::script::{Script, Builder}; use bitcoin::blockdata::transaction::{TxOut, OutPoint}; - use bitcoin::util::hash::Sha256dHash; + use bitcoin_hashes::sha256d::Hash as Sha256dHash; + use bitcoin_hashes::hex::FromHex; use hex::decode;