Skip to content

Commit

Permalink
Merge branch 'citadel-tech:master' into sync_offer
Browse files Browse the repository at this point in the history
  • Loading branch information
KnowWhoami committed Sep 7, 2024
2 parents f6e5a7a + 57bf238 commit 7fd41b4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ jobs:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ${{ steps.coverage.outputs.report }}
directory: ./coverage/reports/
28 changes: 17 additions & 11 deletions src/protocol/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub fn check_reedemscript_is_multisig(redeemscript: &Script) -> Result<(), Contr
//pattern match to check redeemscript is really a 2of2 multisig
let mut ms_rs_bytes = redeemscript.to_bytes();
const PUB_PLACEHOLDER: [u8; 33] = [0x02; 33];
let pubkey_placeholder = PublicKey::from_slice(&PUB_PLACEHOLDER).unwrap();
let pubkey_placeholder = PublicKey::from_slice(&PUB_PLACEHOLDER)?;
let template_ms_rs =
create_multisig_redeemscript(&pubkey_placeholder, &pubkey_placeholder).into_bytes();
if ms_rs_bytes.len() != template_ms_rs.len() {
Expand Down Expand Up @@ -282,10 +282,10 @@ pub fn read_hashvalue_from_contract(redeemscript: &Script) -> Result<Hash160, Co
}
let mut instrs = redeemscript.instructions().skip(2);
// Unwrap Safety: length is checked
let Instruction::Op(opcodes::all::OP_HASH160) = instrs.next().unwrap()? else {
let Instruction::Op(opcodes::all::OP_HASH160) = instrs.next().expect("opcode expected")? else {
return Err(ContractError::Protocol("Hash is not present!"));
};
let Instruction::PushBytes(hash_b) = instrs.next().unwrap()? else {
let Instruction::PushBytes(hash_b) = instrs.next().expect("opcode expected")? else {
return Err(ContractError::Protocol("Invalid script!"));
};

Expand Down Expand Up @@ -322,7 +322,9 @@ pub fn read_contract_locktime(redeemscript: &Script) -> Result<u16, ContractErro
let (int_bytes, _rest) = locktime_bytes
.as_bytes()
.split_at(std::mem::size_of::<u16>());
Ok(u16::from_le_bytes(int_bytes.try_into().unwrap()))
Ok(u16::from_le_bytes(int_bytes.try_into().map_err(|_| {
ContractError::Protocol("Can't read locktime value from contract reedemscript")
})?))
}
_ => Err(ContractError::Protocol(
"Can't read locktime value from contract reedemscript",
Expand Down Expand Up @@ -446,11 +448,13 @@ pub fn validate_contract_tx(
"invalid number of inputs or outputs",
));
}
if funding_outpoint.is_some()
&& receivers_contract_tx.input[0].previous_output != *funding_outpoint.unwrap()
{
return Err(ContractError::Protocol("not spending the funding outpoint"));

if let Some(op) = funding_outpoint {
if receivers_contract_tx.input[0].previous_output != *op {
return Err(ContractError::Protocol("not spending the funding outpoint"));
}
}

if receivers_contract_tx.output[0].script_pubkey
!= redeemscript_to_scriptpubkey(contract_redeemscript)
{
Expand Down Expand Up @@ -521,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 @@ -677,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 @@ -841,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 @@ -1250,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 7fd41b4

Please sign in to comment.