Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed Apr 12, 2024
1 parent f2aa285 commit b618464
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
79 changes: 34 additions & 45 deletions src/swaps/liquidv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,79 +553,72 @@ impl LBtcSwapTxV2 {

let secp = Secp256k1::new();

// Unblind the funding utxo
let unblinded_utxo = self
let unblined_utxo = self
.funding_utxo
.unblind(&secp, self.swap_script.blinding_key.secret_key())?;
let asset_id = unblined_utxo.asset;
let out_abf = AssetBlindingFactor::new(&mut thread_rng());
let exp_asset = Asset::Explicit(asset_id);

let output_value = Amount::from_sat(unblinded_utxo.value) - absolute_fees;
let exp_asset = Asset::Explicit(unblinded_utxo.asset);
let exp_value = elements::confidential::Value::Explicit(output_value.to_sat());
let (blinded_asset, asset_surjection_proof) =
exp_asset.blind(&mut thread_rng(), &secp, out_abf, &[unblined_utxo])?;

// Create new Blinding Factors
let asset_bf = AssetBlindingFactor::new(&mut thread_rng());
let msg = elements::RangeProofMessage {
asset: unblinded_utxo.asset,
bf: asset_bf,
};
let value_bf = ValueBlindingFactor::last(
let output_value = Amount::from_sat(unblined_utxo.value) - absolute_fees;

let final_vbf = ValueBlindingFactor::last(
&secp,
output_value.to_sat(),
asset_bf,
out_abf,
&[(
unblinded_utxo.value,
unblinded_utxo.asset_bf,
unblinded_utxo.value_bf,
unblined_utxo.value,
unblined_utxo.asset_bf,
unblined_utxo.value_bf,
)],
&[(
absolute_fees.to_sat(),
AssetBlindingFactor::zero(),
ValueBlindingFactor::zero(),
)],
);
let explicit_value = elements::confidential::Value::Explicit(output_value.to_sat());
let msg = elements::RangeProofMessage {
asset: asset_id,
bf: out_abf,
};
let ephemeral_sk = SecretKey::new(&mut thread_rng());

// Blind the Value
let blinding_key = self.output_address.blinding_pubkey.ok_or(Error::Protocol(
"We can only send to blinded address.".to_string(),
))?;

let (blinded_value, nonce, range_proof) = exp_value.blind(
// assuming we always use a blinded address that has an extractable blinding pub
let blinding_key = self
.output_address
.blinding_pubkey
.ok_or(Error::Protocol("No blinding key in tx.".to_string()))?;
let (blinded_value, nonce, rangeproof) = explicit_value.blind(
&secp,
value_bf,
final_vbf,
blinding_key,
SecretKey::new(&mut thread_rng()),
ephemeral_sk,
&self.output_address.script_pubkey(),
&msg,
)?;

// Blind the Asset
let (blinded_asset, surjection_proof) = exp_asset.blind(
&mut thread_rng(),
&secp,
AssetBlindingFactor::new(&mut thread_rng()),
&[unblinded_utxo],
)?;

// Create the witness and the outputs
let tx_out_witness = TxOutWitness {
surjection_proof: Some(Box::new(surjection_proof)), // from asset blinding
rangeproof: Some(Box::new(range_proof)), // from value blinding
surjection_proof: Some(Box::new(asset_surjection_proof)), // from asset blinding
rangeproof: Some(Box::new(rangeproof)), // from value blinding
};

let payment_output: TxOut = TxOut {
script_pubkey: self.output_address.script_pubkey(),
value: blinded_value,
asset: blinded_asset,
nonce: nonce,
witness: tx_out_witness,
};
let fee_output: TxOut = TxOut::new_fee(absolute_fees.to_sat(), unblinded_utxo.asset);
let fee_output: TxOut = TxOut::new_fee(absolute_fees.to_sat(), asset_id);

let mut claim_tx = Transaction {
version: 2,
lock_time: LockTime::ZERO,
input: vec![claim_txin],
output: vec![fee_output, payment_output],
output: vec![payment_output, fee_output],
};

// If its a cooperative claim, compute the Musig2 Aggregate Signature and use Keypath spending
Expand Down Expand Up @@ -749,6 +742,7 @@ impl LBtcSwapTxV2 {

let mut script_witness = Witness::new();
script_witness.push(final_sig.to_vec());
script_witness.push(&preimage.bytes.unwrap());
script_witness.push(claim_script.as_bytes());
script_witness.push(control_block.serialize());

Expand Down Expand Up @@ -856,7 +850,7 @@ impl LBtcSwapTxV2 {
.filter_map(|i| {
let ins = i.unwrap();
if let Instruction::PushBytes(bytes) = ins {
if bytes.len() == 3 as usize {
if bytes.len() < 5 as usize {
Some(LockTime::from_consensus(bytes_to_u32_little_endian(&bytes)))
} else {
None
Expand All @@ -877,18 +871,13 @@ impl LBtcSwapTxV2 {

let leaf_hash = TapLeafHash::from_script(&refund_script, LeafVersion::default());

let electrum = ElectrumConfig::default_liquid().build_client()?;

let genesis_blockhash =
elements::BlockHash::from_raw_hash(electrum.block_header(0)?.block_hash().into());

let sighash = SighashCache::new(&refund_tx)
.taproot_script_spend_signature_hash(
0,
&Prevouts::All(&[&self.funding_utxo]),
leaf_hash,
SchnorrSighashType::Default,
genesis_blockhash,
self.genesis_hash,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion tests/regtest_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn lbtc_submarine_refund() {
};

// Send coin the swapscript address and confirm tx
let swap_addrs = swap_script.to_address(Chain::LiquidTestnet).unwrap();
let swap_addrs = swap_script.to_address(Chain::LiquidRegtest).unwrap();
test_framework.send_coins(&swap_addrs, Amount::from_sat(10000));
test_framework.generate_blocks(1);

Expand Down
13 changes: 9 additions & 4 deletions tests/test_framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,20 @@ impl LbtcTestFramework {
)
.unwrap();

let unspents = scan_result
let unspents = if let Some(value) = scan_result
.as_object()
.unwrap()
.get("unspents")
.unwrap()
.as_array()
.unwrap()[0]
.as_object()
.unwrap();
.unwrap()
.get(0)
{
let value = value.as_object().unwrap().clone();
value
} else {
return None;
};

let txid = unspents.get("txid").unwrap();

Expand Down

0 comments on commit b618464

Please sign in to comment.