Skip to content

Commit 890e3cb

Browse files
committed
Update for new rust-bitcoin API, avoid some duplicate hashing
1 parent 2018782 commit 890e3cb

File tree

6 files changed

+57
-61
lines changed

6 files changed

+57
-61
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ non_bitcoin_chain_hash_routing = []
1515
fuzztarget = ["secp256k1/fuzztarget", "bitcoin/fuzztarget"]
1616

1717
[dependencies]
18-
bitcoin = "0.12"
18+
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin" }
1919
rust-crypto = "0.2"
2020
rand = "0.4"
2121
secp256k1 = "0.9"

fuzz/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ honggfuzz_fuzz = ["honggfuzz"]
1313

1414
[dependencies]
1515
lightning = { path = "..", features = ["fuzztarget"] }
16-
bitcoin = { version = "0.12", features = ["fuzztarget"] }
16+
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", features = ["fuzztarget"] }
1717
secp256k1 = { version = "0.9", features = ["fuzztarget"] }
1818
honggfuzz = { version = "0.5", optional = true }
1919
afl = { version = "0.3", optional = true }

fuzz/fuzz_targets/channel_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn do_test(data: &[u8]) {
163163

164164
let their_pubkey = get_pubkey!();
165165

166-
let tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new(), witness: Vec::new() };
166+
let tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
167167
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
168168

169169
let mut channel = if get_slice!(1)[0] != 0 {

src/ln/channel.rs

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,11 @@ impl Channel {
511511
prev_hash: self.channel_monitor.get_funding_txo().unwrap().0,
512512
prev_index: self.channel_monitor.get_funding_txo().unwrap().1 as u32,
513513
script_sig: Script::new(),
514-
sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32)
514+
sequence: ((0x80 as u32) << 8*3) | ((obscured_commitment_transaction_number >> 3*8) as u32),
515+
witness: Vec::new(),
515516
});
516517
ins
517518
};
518-
let mut witness: Vec<Vec<Vec<u8>>> = Vec::new();
519-
witness.push(Vec::new());
520519

521520
let mut txouts: Vec<(TxOut, Option<HTLCOutputInCommitment>)> = Vec::new();
522521

@@ -596,7 +595,6 @@ impl Channel {
596595
lock_time: ((0x20 as u32) << 8*3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
597596
input: txins,
598597
output: outputs,
599-
witness: witness
600598
}, htlcs_used))
601599
}
602600

@@ -646,30 +644,30 @@ impl Channel {
646644
if tx.input.len() != 1 {
647645
panic!("Tried to sign commitment transaction that had input count != 1!");
648646
}
649-
if tx.witness.len() != 1 || tx.witness[0].len() != 0 {
647+
if tx.input[0].witness.len() != 0 {
650648
panic!("Tried to re-sign commitment transaction");
651649
}
652650

653651
let funding_redeemscript = self.get_funding_redeemscript();
654652

655-
let sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&tx).sighash_all(&tx, 0, &funding_redeemscript, self.channel_value_satoshis)[..]));
653+
let sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]));
656654
let our_sig = secp_call!(self.secp_ctx.sign(&sighash, &self.local_keys.funding_key));
657655

658-
tx.witness[0].push(Vec::new()); // First is the multisig dummy
656+
tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
659657

660658
let our_funding_key = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).unwrap().serialize();
661659
let their_funding_key = self.their_funding_pubkey.serialize();
662660
if our_funding_key[..] < their_funding_key[..] {
663-
tx.witness[0].push(our_sig.serialize_der(&self.secp_ctx).to_vec());
664-
tx.witness[0].push(their_sig.serialize_der(&self.secp_ctx).to_vec());
661+
tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
662+
tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
665663
} else {
666-
tx.witness[0].push(their_sig.serialize_der(&self.secp_ctx).to_vec());
667-
tx.witness[0].push(our_sig.serialize_der(&self.secp_ctx).to_vec());
664+
tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
665+
tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
668666
}
669-
tx.witness[0][1].push(SigHashType::All as u8);
670-
tx.witness[0][2].push(SigHashType::All as u8);
667+
tx.input[0].witness[1].push(SigHashType::All as u8);
668+
tx.input[0].witness[2].push(SigHashType::All as u8);
671669

672-
tx.witness[0].push(funding_redeemscript.into_vec());
670+
tx.input[0].witness.push(funding_redeemscript.into_vec());
673671

674672
Ok(())
675673
}
@@ -683,12 +681,10 @@ impl Channel {
683681
prev_hash: prev_hash.clone(),
684682
prev_index: htlc.transaction_output_index,
685683
script_sig: Script::new(),
686-
sequence: 0
684+
sequence: 0,
685+
witness: Vec::new(),
687686
});
688687

689-
let mut witnesses: Vec<Vec<Vec<u8>>> = Vec::new();
690-
witnesses.push(Vec::new());
691-
692688
let total_fee = if htlc.offered {
693689
self.feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000
694690
} else {
@@ -708,7 +704,6 @@ impl Channel {
708704
lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 },
709705
input: txins,
710706
output: txouts,
711-
witness: witnesses
712707
})
713708
}
714709

@@ -718,37 +713,37 @@ impl Channel {
718713
if tx.input.len() != 1 {
719714
panic!("Tried to sign HTLC transaction that had input count != 1!");
720715
}
721-
if tx.witness.len() != 1 || tx.witness[0].len() != 0 {
716+
if tx.input[0].witness.len() != 0 {
722717
panic!("Tried to re-sign HTLC transaction");
723718
}
724719

725720
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys, htlc.offered);
726721

727722
let our_htlc_key = secp_call!(chan_utils::derive_private_key(&self.secp_ctx, &keys.per_commitment_point, &self.local_keys.htlc_base_key));
728-
let sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&tx).sighash_all(&tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
723+
let sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
729724
let our_sig = secp_call!(self.secp_ctx.sign(&sighash, &our_htlc_key));
730725

731726
let local_tx = PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key).unwrap() == keys.a_htlc_key;
732727

733-
tx.witness[0].push(Vec::new()); // First is the multisig dummy
728+
tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
734729

735730
if local_tx { // b, then a
736-
tx.witness[0].push(their_sig.serialize_der(&self.secp_ctx).to_vec());
737-
tx.witness[0].push(our_sig.serialize_der(&self.secp_ctx).to_vec());
731+
tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
732+
tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
738733
} else {
739-
tx.witness[0].push(our_sig.serialize_der(&self.secp_ctx).to_vec());
740-
tx.witness[0].push(their_sig.serialize_der(&self.secp_ctx).to_vec());
734+
tx.input[0].witness.push(our_sig.serialize_der(&self.secp_ctx).to_vec());
735+
tx.input[0].witness.push(their_sig.serialize_der(&self.secp_ctx).to_vec());
741736
}
742-
tx.witness[0][1].push(SigHashType::All as u8);
743-
tx.witness[0][2].push(SigHashType::All as u8);
737+
tx.input[0].witness[1].push(SigHashType::All as u8);
738+
tx.input[0].witness[2].push(SigHashType::All as u8);
744739

745740
if htlc.offered {
746-
tx.witness[0].push(Vec::new());
741+
tx.input[0].witness.push(Vec::new());
747742
} else {
748-
tx.witness[0].push(preimage.unwrap().to_vec());
743+
tx.input[0].witness.push(preimage.unwrap().to_vec());
749744
}
750745

751-
tx.witness[0].push(htlc_redeemscript.into_vec());
746+
tx.input[0].witness.push(htlc_redeemscript.into_vec());
752747

753748
Ok(())
754749
}
@@ -880,11 +875,11 @@ impl Channel {
880875

881876
let remote_keys = self.build_remote_transaction_keys()?;
882877
let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)?.0;
883-
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..]));
878+
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]));
884879

885880
let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
886881
let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?.0;
887-
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..]));
882+
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]));
888883

889884
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
890885
secp_call!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey));
@@ -946,7 +941,7 @@ impl Channel {
946941

947942
let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
948943
let local_initial_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?.0;
949-
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..]));
944+
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]));
950945

951946
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
952947
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey));
@@ -1167,17 +1162,18 @@ impl Channel {
11671162

11681163
let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
11691164
let local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false)?;
1170-
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0, 0, &funding_script, self.channel_value_satoshis)[..]));
1165+
let local_commitment_txid = local_commitment_tx.0.txid();
1166+
let local_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]));
11711167
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey));
11721168

11731169
if msg.htlc_signatures.len() != local_commitment_tx.1.len() {
11741170
return Err(HandleError{err: "Got wrong number of HTLC signatures from remote", msg: None});
11751171
}
11761172

11771173
for (idx, ref htlc) in local_commitment_tx.1.iter().enumerate() {
1178-
let htlc_tx = self.build_htlc_transaction(&local_commitment_tx.0.txid(), htlc, true, &local_keys)?;
1174+
let htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys)?;
11791175
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys, htlc.offered);
1180-
let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
1176+
let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
11811177
secp_call!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key));
11821178
}
11831179

@@ -1464,7 +1460,7 @@ impl Channel {
14641460

14651461
let remote_keys = self.build_remote_transaction_keys()?;
14661462
let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false)?.0;
1467-
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx, 0, &funding_script, self.channel_value_satoshis)[..]));
1463+
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_initial_commitment_tx).sighash_all(&remote_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]));
14681464

14691465
// We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
14701466
Ok(secp_call!(self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key)))
@@ -1639,15 +1635,16 @@ impl Channel {
16391635

16401636
let remote_keys = self.build_remote_transaction_keys()?;
16411637
let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true)?;
1642-
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0, 0, &funding_script, self.channel_value_satoshis)[..]));
1638+
let remote_commitment_txid = remote_commitment_tx.0.txid();
1639+
let remote_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]));
16431640
let our_sig = secp_call!(self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key));
16441641

16451642
let mut htlc_sigs = Vec::new();
16461643

16471644
for ref htlc in remote_commitment_tx.1.iter() {
1648-
let htlc_tx = self.build_htlc_transaction(&remote_commitment_tx.0.txid(), htlc, false, &remote_keys)?;
1645+
let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys)?;
16491646
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys, htlc.offered);
1650-
let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
1647+
let htlc_sighash = secp_call!(Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]));
16511648
let our_htlc_key = secp_call!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key));
16521649
htlc_sigs.push(secp_call!(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key)));
16531650
}
@@ -1749,7 +1746,7 @@ mod tests {
17491746
( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
17501747
unsigned_tx = chan.build_commitment_transaction(42, &keys, true, false).unwrap();
17511748
let their_signature = Signature::from_der(&secp_ctx, &hex_bytes($their_sig_hex).unwrap()[..]).unwrap();
1752-
let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0, 0, &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
1749+
let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
17531750
secp_ctx.verify(&sighash, &their_signature, &chan.their_funding_pubkey).unwrap();
17541751

17551752
chan.sign_commitment_transaction(&mut unsigned_tx.0, &their_signature).unwrap();
@@ -1766,7 +1763,7 @@ mod tests {
17661763
let ref htlc = unsigned_tx.1[$htlc_idx];
17671764
let mut htlc_tx = chan.build_htlc_transaction(&unsigned_tx.0.txid(), &htlc, true, &keys).unwrap();
17681765
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys, htlc.offered);
1769-
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx, 0, &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
1766+
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
17701767
secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.b_htlc_key).unwrap();
17711768

17721769
let mut preimage: Option<[u8; 32]> = None;

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ mod tests {
15761576
node_a.handle_accept_channel(&node_b.get_our_node_id(), &accept_chan).unwrap();
15771577

15781578
let chan_id = unsafe { CHAN_COUNT };
1579-
let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: Vec::new(), witness: Vec::new() };
1579+
let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: Vec::new() };
15801580
let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), chan_id);
15811581

15821582
let events_1 = node_a.get_and_clear_pending_events();

0 commit comments

Comments
 (0)