Skip to content

Commit

Permalink
Merge pull request #230 from KnowWhoami/locktime
Browse files Browse the repository at this point in the history
set `Current height` as locktime for `Contract` & `Funding` tx.
  • Loading branch information
Shourya742 authored Sep 5, 2024
2 parents 39c9d65 + 02333ec commit 57bf238
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/protocol/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ mod test {
use core::panic;
use std::str::FromStr;

const TEST_CURRENT_HEIGHT: u32 = 100;

fn read_pubkeys_from_contract_reedimscript(
contract_script: &Script,
) -> Result<(PublicKey, PublicKey), &'static str> {
Expand Down Expand Up @@ -681,7 +683,7 @@ mod test {
value: Amount::from_sat(3000),
},
],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};

Expand Down Expand Up @@ -845,7 +847,7 @@ mod test {
script_pubkey: funding_spk,
value: Amount::from_sat(2000),
}],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};

Expand Down Expand Up @@ -1254,7 +1256,7 @@ mod test {
value: Amount::from_sat(3000),
},
],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};

Expand Down
3 changes: 2 additions & 1 deletion src/wallet/direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ impl Wallet {
}

// Set the Anti-Fee-Snipping locktime
let lock_time = LockTime::from_height(self.rpc.get_block_count().unwrap() as u32).unwrap();
let current_height = self.rpc.get_block_count()?;
let lock_time = LockTime::from_height(current_height as u32)?;

let mut tx = Transaction {
input: tx_inputs,
Expand Down
8 changes: 5 additions & 3 deletions src/wallet/fidelity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,16 @@ impl Wallet {
script_pubkey: change_addrs,
});
}

// Set the Anti-Fee Snipping Locktime
let current_height = self.rpc.get_block_count()?;
let anti_fee_snipping_locktime = LockTime::from_height(current_height as u32)?;
let lock_time = LockTime::from_height(current_height as u32)?;

let mut tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: anti_fee_snipping_locktime,
version: Version::TWO, // anti-fee-snipping
lock_time,
version: Version::TWO,
};

let mut input_info = selected_utxo
Expand Down
26 changes: 21 additions & 5 deletions src/wallet/funding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,16 @@ impl Wallet {
script_sig: ScriptBuf::new(),
})
.collect::<Vec<_>>();

// Set the Anti-Fee-Snipping locktime
let current_height = self.rpc.get_block_count()?;

let lock_time = LockTime::from_height(current_height as u32)?;

let mut funding_tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: LockTime::ZERO,
lock_time,
version: Version::TWO,
};
let mut input_info = selected_utxo
Expand Down Expand Up @@ -221,6 +227,10 @@ impl Wallet {
let mut destinations_iter = destinations.iter();
let first_tx_input = utxos.next().unwrap();

// Set the Anti-Fee-Snipping locktime
let current_height = self.rpc.get_block_count()?;
let lock_time = LockTime::from_height(current_height as u32)?;

for _ in 0..destinations.len() - 2 {
let (txid, vout, value) = utxos.next().unwrap();

Expand All @@ -241,10 +251,11 @@ impl Wallet {
script_pubkey: address.script_pubkey(),
});
}

let mut funding_tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: LockTime::ZERO,
lock_time,
version: Version::TWO,
};
self.sign_transaction(&mut funding_tx, &mut input_info)?;
Expand Down Expand Up @@ -289,10 +300,11 @@ impl Wallet {
script_pubkey: address.script_pubkey(),
});
}

let mut funding_tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: LockTime::ZERO,
lock_time,
version: Version::TWO,
};
let mut info = input_info.iter().cloned();
Expand Down Expand Up @@ -335,7 +347,7 @@ impl Wallet {
let mut funding_tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: LockTime::ZERO,
lock_time,
version: Version::TWO,
};
let mut info = iter::once(self.get_utxo((first_txid, first_vout))?.unwrap());
Expand Down Expand Up @@ -405,10 +417,14 @@ impl Wallet {
})
.collect::<Vec<_>>();

// Set the Anti-Fee-Snipping locktime
let current_height = self.rpc.get_block_count()?;
let lock_time = LockTime::from_height(current_height as u32)?;

let mut funding_tx = Transaction {
input: tx_inputs,
output: tx_outs,
lock_time: LockTime::ZERO,
lock_time,
version: Version::TWO,
};

Expand Down
10 changes: 6 additions & 4 deletions src/wallet/swapcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ mod tests {
use super::*;
use bitcoin::{NetworkKind, PrivateKey};

const TEST_CURRENT_HEIGHT: u32 = 100;

#[test]
fn test_apply_privkey_watchonly_swapcoin() {
let secp = Secp256k1::new();
Expand Down Expand Up @@ -844,7 +846,7 @@ mod tests {
let tx = Transaction {
input: vec![input.clone()],
output: vec![],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};

Expand Down Expand Up @@ -879,7 +881,7 @@ mod tests {
};
// Intentionally failing to sign with incomplete swapcoin
assert!(incoming_swapcoin
.sign_transaction_input(index, &tx, &mut input, &contract_redeemscript,)
.sign_transaction_input(index, &tx, &mut input, &contract_redeemscript)
.is_err());
let sign = bitcoin::ecdsa::Signature {
signature: secp256k1::ecdsa::Signature::from_compact(&[0; 64]).unwrap(),
Expand Down Expand Up @@ -954,7 +956,7 @@ mod tests {
incoming_swapcoin.contract_tx.output[0].value.to_sat() - miner_fee,
),
}],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};
let index = 0;
Expand Down Expand Up @@ -1034,7 +1036,7 @@ mod tests {
incoming_swapcoin.contract_tx.output[0].value.to_sat() - miner_fee,
),
}],
lock_time: LockTime::ZERO,
lock_time: LockTime::from_height(TEST_CURRENT_HEIGHT).unwrap(),
version: Version::TWO,
};
let index = 0;
Expand Down

0 comments on commit 57bf238

Please sign in to comment.