Browse Source

Move nonce_pk to Payout type

It makes sense to put the `nonce_pk` together with the `message`,
since they're both used to generate the attestation (signature) point.

We also remove the `OracleParams` type since it would only have one
field after this change.
integrate-protocol-maker-side
Lucas Soriano del Pino 3 years ago
parent
commit
585d000694
No known key found for this signature in database GPG Key ID: EE611E973A1530E7
  1. 37
      cfd_protocol/src/lib.rs
  2. 35
      cfd_protocol/tests/cfds.rs

37
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<Payout>,
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<Payout>,
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<Payout>,
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<u8>,
nonce_pk: schnorrsig::PublicKey,
maker_amount: Amount,
taker_amount: Amount,
}
impl Payout {
pub fn new(message: Vec<u8>, maker_amount: Amount, taker_amount: Amount) -> Self {
pub fn new(
message: Vec<u8>,
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),
);

35
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<Payout>,
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,

Loading…
Cancel
Save