diff --git a/cfd_protocol/src/lib.rs b/cfd_protocol/src/lib.rs index 64e9d55..5834019 100644 --- a/cfd_protocol/src/lib.rs +++ b/cfd_protocol/src/lib.rs @@ -58,7 +58,7 @@ where pub fn create_cfd_transactions( (maker, maker_punish_params): (PartyParams, PunishParams), (taker, taker_punish_params): (PartyParams, PunishParams), - oracle_params: OracleParams, + oracle_pk: schnorrsig::PublicKey, refund_timelock: u32, payouts: Vec, identity_sk: SecretKey, @@ -85,7 +85,7 @@ pub fn create_cfd_transactions( taker.address, taker_punish_params, ), - oracle_params, + oracle_pk, refund_timelock, payouts, identity_sk, @@ -106,7 +106,7 @@ pub fn renew_cfd_transactions( Address, PunishParams, ), - oracle_params: OracleParams, + oracle_pk: schnorrsig::PublicKey, refund_timelock: u32, payouts: Vec, identity_sk: SecretKey, @@ -125,7 +125,7 @@ pub fn renew_cfd_transactions( taker_address, taker_punish_params, ), - oracle_params, + oracle_pk, refund_timelock, payouts, identity_sk, @@ -146,7 +146,7 @@ fn build_cfds( Address, PunishParams, ), - oracle_params: OracleParams, + oracle_pk: schnorrsig::PublicKey, refund_timelock: u32, payouts: Vec, identity_sk: SecretKey, @@ -201,6 +201,7 @@ fn build_cfds( let cets = payouts .into_iter() .map(|payout| { + let nonce_pk = payout.nonce_pk; let message = payout.message.clone(); let cet = ContractExecutionTransaction::new( &commit_tx, @@ -210,7 +211,7 @@ fn build_cfds( CET_TIMELOCK, )?; - let encsig = cet.encsign(identity_sk, &oracle_params.pk, &oracle_params.nonce_pk)?; + let encsig = cet.encsign(identity_sk, &oracle_pk, &nonce_pk)?; Ok((cet.inner, encsig, message)) }) @@ -420,11 +421,6 @@ pub struct PunishParams { pub publish_pk: PublicKey, } -pub struct OracleParams { - pub pk: schnorrsig::PublicKey, - pub nonce_pk: schnorrsig::PublicKey, -} - pub struct CfdTransactions { pub lock: PartiallySignedTransaction, pub commit: (Transaction, EcdsaAdaptorSignature), @@ -432,17 +428,26 @@ pub struct CfdTransactions { pub refund: (Transaction, Signature), } +// TODO: We will very likely have multiple `(message, nonce_pk)` pairs +// per payout in the future #[derive(Debug, Clone)] pub struct Payout { message: Vec, + nonce_pk: schnorrsig::PublicKey, maker_amount: Amount, taker_amount: Amount, } impl Payout { - pub fn new(message: Vec, maker_amount: Amount, taker_amount: Amount) -> Self { + pub fn new( + message: Vec, + nonce_pk: schnorrsig::PublicKey, + maker_amount: Amount, + taker_amount: Amount, + ) -> Self { Self { message, + nonce_pk, maker_amount, taker_amount, } @@ -907,6 +912,9 @@ mod tests { #[test] fn test_fee_subtraction_bigger_than_dust() { + let nonce_pk = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" + .parse() + .unwrap(); let key = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af" .parse() .unwrap(); @@ -917,6 +925,7 @@ mod tests { let orig_taker_amount = 1000; let payout = Payout::new( b"win".to_vec(), + nonce_pk, Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), ); @@ -937,6 +946,9 @@ mod tests { #[test] fn test_fee_subtraction_smaller_than_dust() { + let nonce_pk = "18845781f631c48f1c9709e23092067d06837f30aa0cd0544ac887fe91ddd166" + .parse() + .unwrap(); let key = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af" .parse() .unwrap(); @@ -947,6 +959,7 @@ mod tests { let orig_taker_amount = 1000; let payout = Payout::new( b"win".to_vec(), + nonce_pk, Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), ); diff --git a/cfd_protocol/tests/cfds.rs b/cfd_protocol/tests/cfds.rs index 470245d..230db53 100644 --- a/cfd_protocol/tests/cfds.rs +++ b/cfd_protocol/tests/cfds.rs @@ -10,8 +10,7 @@ use bitcoin::Txid; use cfd_protocol::{ commit_descriptor, compute_signature_point, create_cfd_transactions, finalize_spend_transaction, lock_descriptor, punish_transaction, renew_cfd_transactions, - spending_tx_sighash, CfdTransactions, OracleParams, Payout, PunishParams, TransactionExt, - WalletExt, + spending_tx_sighash, CfdTransactions, Payout, PunishParams, TransactionExt, WalletExt, }; use rand::{CryptoRng, RngCore, SeedableRng}; use rand_chacha::ChaChaRng; @@ -34,11 +33,13 @@ fn create_cfd() { let payouts = vec![ Payout::new( b"win".to_vec(), + announcement.nonce_pk(), Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), ), Payout::new( b"lose".to_vec(), + announcement.nonce_pk(), Amount::ZERO, Amount::from_btc(2.0).unwrap(), ), @@ -50,7 +51,7 @@ fn create_cfd() { &mut rng, (&maker_wallet, maker_lock_amount), (&taker_wallet, taker_lock_amount), - (&oracle, announcement), + oracle.public_key(), payouts, refund_timelock, ); @@ -115,11 +116,13 @@ fn renew_cfd() { let payouts = vec![ Payout::new( b"win".to_vec(), + announcement.nonce_pk(), Amount::from_btc(2.0).unwrap(), Amount::ZERO, ), Payout::new( b"lose".to_vec(), + announcement.nonce_pk(), Amount::ZERO, Amount::from_btc(2.0).unwrap(), ), @@ -131,7 +134,7 @@ fn renew_cfd() { &mut rng, (&maker_wallet, maker_lock_amount), (&taker_wallet, taker_lock_amount), - (&oracle, announcement), + oracle.public_key(), payouts, refund_timelock, ); @@ -149,11 +152,13 @@ fn renew_cfd() { let payouts = vec![ Payout::new( b"win".to_vec(), + announcement.nonce_pk(), Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), ), Payout::new( b"lose".to_vec(), + announcement.nonce_pk(), Amount::from_btc(0.5).unwrap(), Amount::from_btc(1.5).unwrap(), ), @@ -179,10 +184,7 @@ fn renew_cfd() { publish_pk: taker_pub_pk, }, ), - OracleParams { - pk: oracle.public_key(), - nonce_pk: announcement.nonce_pk(), - }, + oracle.public_key(), refund_timelock, payouts.clone(), maker.sk, @@ -209,10 +211,7 @@ fn renew_cfd() { publish_pk: taker_pub_pk, }, ), - OracleParams { - pk: oracle.public_key(), - nonce_pk: announcement.nonce_pk(), - }, + oracle.public_key(), refund_timelock, payouts, taker.sk, @@ -267,7 +266,7 @@ fn create_cfd_txs( rng: &mut ChaChaRng, (maker_wallet, maker_lock_amount): (&bdk::Wallet<(), bdk::database::MemoryDatabase>, Amount), (taker_wallet, taker_lock_amount): (&bdk::Wallet<(), bdk::database::MemoryDatabase>, Amount), - (oracle, announcement): (&Oracle, Announcement), + oracle_pk: schnorrsig::PublicKey, payouts: Vec, refund_timelock: u32, ) -> ( @@ -309,10 +308,7 @@ fn create_cfd_txs( publish_pk: taker_pub_pk, }, ), - OracleParams { - pk: oracle.public_key(), - nonce_pk: announcement.nonce_pk(), - }, + oracle_pk, refund_timelock, payouts.clone(), maker_sk, @@ -333,10 +329,7 @@ fn create_cfd_txs( publish_pk: taker_pub_pk, }, ), - OracleParams { - pk: oracle.public_key(), - nonce_pk: announcement.nonce_pk(), - }, + oracle_pk, refund_timelock, payouts, taker_sk,