Browse Source

Take raw lock transaction in CommitTransaction constructor

We remove the `LockTransaction` type accordingly.
integrate-protocol-maker-side
Lucas Soriano del Pino 3 years ago
parent
commit
2a9e49ca9a
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 180
      cfd_protocol/src/lib.rs

180
cfd_protocol/src/lib.rs

@ -70,17 +70,16 @@ pub fn build_cfd_transactions(
/// TODO: Should this be an argument to this function? /// TODO: Should this be an argument to this function?
const CET_TIMELOCK: u32 = 12; const CET_TIMELOCK: u32 = 12;
let lock_tx = LockTransaction::new( let lock_tx = lock_transaction(
maker.lock_psbt.clone(), maker.lock_psbt.clone(),
taker.lock_psbt.clone(), taker.lock_psbt.clone(),
maker.identity_pk, maker.identity_pk,
taker.identity_pk, taker.identity_pk,
maker.lock_amount + taker.lock_amount, maker.lock_amount + taker.lock_amount,
) );
.context("cannot build lock tx")?;
let commit_tx = CommitTransaction::new( let commit_tx = CommitTransaction::new(
&lock_tx, &lock_tx.global.unsigned_tx,
( (
maker.identity_pk, maker.identity_pk,
maker_punish_params.revocation_pk, maker_punish_params.revocation_pk,
@ -91,7 +90,8 @@ pub fn build_cfd_transactions(
taker_punish_params.revocation_pk, taker_punish_params.revocation_pk,
taker_punish_params.publish_pk, taker_punish_params.publish_pk,
), ),
); )
.context("cannot build commit tx")?;
let identity_pk = secp256k1_zkp::PublicKey::from_secret_key(SECP256K1, &identity_sk); let identity_pk = secp256k1_zkp::PublicKey::from_secret_key(SECP256K1, &identity_sk);
let commit_encsig = if identity_pk == maker.identity_pk.key { let commit_encsig = if identity_pk == maker.identity_pk.key {
@ -138,7 +138,7 @@ pub fn build_cfd_transactions(
.context("cannot build and sign all cets")?; .context("cannot build and sign all cets")?;
Ok(CfdTransactions { Ok(CfdTransactions {
lock: lock_tx.inner, lock: lock_tx,
commit: (commit_tx.inner, commit_encsig), commit: (commit_tx.inner, commit_encsig),
cets, cets,
refund, refund,
@ -645,24 +645,38 @@ impl CommitTransaction {
const SIGNED_VBYTES: f64 = 148.5 + (3.0 * 2.0) / 4.0; const SIGNED_VBYTES: f64 = 148.5 + (3.0 * 2.0) / 4.0;
fn new( fn new(
lock_tx: &LockTransaction, lock_tx: &Transaction,
(maker_own_pk, maker_rev_pk, maker_publish_pk): (PublicKey, PublicKey, PublicKey), (maker_pk, maker_rev_pk, maker_publish_pk): (PublicKey, PublicKey, PublicKey),
(taker_own_pk, taker_rev_pk, taker_publish_pk): (PublicKey, PublicKey, PublicKey), (taker_pk, taker_rev_pk, taker_publish_pk): (PublicKey, PublicKey, PublicKey),
) -> Self { ) -> Result<Self> {
let lock_tx_amount = lock_tx.amount().as_sat(); let lock_descriptor = lock_descriptor(maker_pk, taker_pk);
let (lock_outpoint, lock_amount) = {
let vout = lock_tx
.output
.iter()
.position(|out| out.script_pubkey == lock_descriptor.script_pubkey())
.context("lock script not found in lock tx")?;
let outpoint = OutPoint {
txid: lock_tx.txid(),
vout: vout as u32,
};
let amount = lock_tx.output[vout].value;
(outpoint, amount)
};
let lock_input = TxIn { let lock_input = TxIn {
previous_output: lock_tx.lock_outpoint(), previous_output: lock_outpoint,
..Default::default() ..Default::default()
}; };
let descriptor = commit_descriptor( let descriptor = commit_descriptor(
(maker_own_pk, maker_rev_pk, maker_publish_pk), (maker_pk, maker_rev_pk, maker_publish_pk),
(taker_own_pk, taker_rev_pk, taker_publish_pk), (taker_pk, taker_rev_pk, taker_publish_pk),
); );
let output = TxOut { let output = TxOut {
value: lock_tx_amount, value: lock_amount,
script_pubkey: descriptor.script_pubkey(), script_pubkey: descriptor.script_pubkey(),
}; };
@ -674,24 +688,24 @@ impl CommitTransaction {
}; };
let fee = (Self::SIGNED_VBYTES * SATS_PER_VBYTE as f64) as u64; let fee = (Self::SIGNED_VBYTES * SATS_PER_VBYTE as f64) as u64;
let commit_tx_amount = lock_tx_amount - fee as u64; let commit_tx_amount = lock_amount - fee as u64;
inner.output[0].value = commit_tx_amount; inner.output[0].value = commit_tx_amount;
let sighash = SigHashCache::new(&inner).signature_hash( let sighash = SigHashCache::new(&inner).signature_hash(
0, 0,
&lock_tx.descriptor().script_code(), &lock_descriptor.script_code(),
lock_tx.amount().as_sat(), lock_amount,
SigHashType::All, SigHashType::All,
); );
Self { Ok(Self {
inner, inner,
descriptor, descriptor,
lock_descriptor: lock_tx.descriptor(), lock_descriptor,
amount: Amount::from_sat(commit_tx_amount), amount: Amount::from_sat(commit_tx_amount),
sighash, sighash,
fee, fee,
} })
} }
fn encsign(&self, sk: SecretKey, publish_them_pk: &PublicKey) -> EcdsaAdaptorSignature { fn encsign(&self, sk: SecretKey, publish_them_pk: &PublicKey) -> EcdsaAdaptorSignature {
@ -731,95 +745,55 @@ impl CommitTransaction {
} }
} }
#[derive(Debug, Clone)] fn lock_transaction(
struct LockTransaction { maker_psbt: PartiallySignedTransaction,
inner: PartiallySignedTransaction, taker_psbt: PartiallySignedTransaction,
lock_descriptor: Descriptor<PublicKey>, maker_pk: PublicKey,
taker_pk: PublicKey,
amount: Amount, amount: Amount,
} ) -> PartiallySignedTransaction {
let lock_descriptor = lock_descriptor(maker_pk, taker_pk);
impl LockTransaction {
fn new(
maker_psbt: PartiallySignedTransaction,
taker_psbt: PartiallySignedTransaction,
maker_pk: PublicKey,
taker_pk: PublicKey,
amount: Amount,
) -> Result<Self> {
let lock_descriptor = lock_descriptor(maker_pk, taker_pk);
let maker_change = maker_psbt
.global
.unsigned_tx
.output
.into_iter()
.filter(|out| {
out.script_pubkey != DUMMY_2OF2_MULITISIG.parse().expect("To be a valid script")
})
.collect::<Vec<_>>();
let taker_change = taker_psbt
.global
.unsigned_tx
.output
.into_iter()
.filter(|out| {
out.script_pubkey != DUMMY_2OF2_MULITISIG.parse().expect("To be a valid script")
})
.collect();
let lock_output = TxOut {
value: amount.as_sat(),
script_pubkey: lock_descriptor.script_pubkey(),
};
let lock_tx = Transaction {
version: 2,
lock_time: 0,
input: vec![
maker_psbt.global.unsigned_tx.input,
taker_psbt.global.unsigned_tx.input,
]
.concat(),
output: vec![vec![lock_output], maker_change, taker_change].concat(),
};
let inner = PartiallySignedTransaction {
global: Global::from_unsigned_tx(lock_tx)?,
inputs: vec![maker_psbt.inputs, taker_psbt.inputs].concat(),
outputs: vec![maker_psbt.outputs, taker_psbt.outputs].concat(),
};
Ok(Self { let maker_change = maker_psbt
inner, .global
lock_descriptor, .unsigned_tx
amount, .output
.into_iter()
.filter(|out| {
out.script_pubkey != DUMMY_2OF2_MULITISIG.parse().expect("To be a valid script")
}) })
} .collect::<Vec<_>>();
fn lock_outpoint(&self) -> OutPoint { let taker_change = taker_psbt
let txid = self.inner.global.unsigned_tx.txid(); .global
let vout = self .unsigned_tx
.inner .output
.global .into_iter()
.unsigned_tx .filter(|out| {
.output out.script_pubkey != DUMMY_2OF2_MULITISIG.parse().expect("To be a valid script")
.iter() })
.position(|out| out.script_pubkey == self.lock_descriptor.script_pubkey()) .collect();
.expect("to find lock output in lock tx");
OutPoint { let lock_output = TxOut {
txid, value: amount.as_sat(),
vout: vout as u32, script_pubkey: lock_descriptor.script_pubkey(),
} };
}
fn descriptor(&self) -> Descriptor<PublicKey> { let lock_tx = Transaction {
self.lock_descriptor.clone() version: 2,
} lock_time: 0,
input: vec![
maker_psbt.global.unsigned_tx.input,
taker_psbt.global.unsigned_tx.input,
]
.concat(),
output: vec![vec![lock_output], maker_change, taker_change].concat(),
};
fn amount(&self) -> Amount { PartiallySignedTransaction {
self.amount global: Global::from_unsigned_tx(lock_tx).expect("to be unsigned"),
inputs: vec![maker_psbt.inputs, taker_psbt.inputs].concat(),
outputs: vec![maker_psbt.outputs, taker_psbt.outputs].concat(),
} }
} }

Loading…
Cancel
Save