Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: stateless keys #187

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ macro_rules! get_signed_channel_state {
}
}};
}
use crate::utils::SerialIds;
pub(crate) use get_signed_channel_state;

/// Creates an [`OfferedChannel`] and an associated [`OfferedContract`] using
Expand All @@ -74,6 +75,7 @@ pub fn offer_channel<C: Signing, W: Deref, B: Deref, T: Deref>(
cet_nsequence: u32,
refund_delay: u32,
wallet: &W,
seed: [u8; 32],
blockchain: &B,
time: &T,
) -> Result<(OfferedChannel, OfferedContract), Error>
Expand All @@ -82,8 +84,13 @@ where
B::Target: Blockchain,
T::Target: Time,
{
let (offer_params, _, funding_inputs_info) = crate::utils::get_party_params(
let temp_id = get_new_temporary_id();
let serial_ids = SerialIds::generate();
let funding_privkey = crate::utils::compute_secret_key(seed, temp_id, serial_ids);
let (offer_params, funding_inputs_info) = crate::utils::get_party_params(
secp,
funding_privkey,
serial_ids,
contract.offer_collateral,
contract.fee_rate,
wallet,
Expand All @@ -92,6 +99,7 @@ where
let party_points = crate::utils::get_party_base_points(secp, wallet)?;

let offered_contract = OfferedContract::new(
temp_id,
contract,
oracle_announcements.to_vec(),
&offer_params,
Expand Down Expand Up @@ -136,6 +144,7 @@ pub fn accept_channel_offer<W: Deref, B: Deref>(
offered_channel: &OfferedChannel,
offered_contract: &OfferedContract,
wallet: &W,
seed: [u8; 32],
blockchain: &B,
) -> Result<(AcceptedChannel, AcceptedContract, AcceptChannel), Error>
where
Expand All @@ -146,8 +155,12 @@ where

let total_collateral = offered_contract.total_collateral;

let (accept_params, _, funding_inputs) = crate::utils::get_party_params(
let serial_ids = SerialIds::generate();
let fund_secret_key = crate::utils::compute_secret_key(seed, offered_contract.id, serial_ids);
let (accept_params, funding_inputs) = crate::utils::get_party_params(
secp,
fund_secret_key,
serial_ids,
total_collateral - offered_contract.offer_params.collateral,
offered_contract.fee_rate_per_vb,
wallet,
Expand Down Expand Up @@ -209,7 +222,11 @@ where
&offered_channel.temporary_channel_id,
);

let own_fund_sk = wallet.get_secret_key_for_pubkey(&accept_params.fund_pubkey)?;
let serial_ids = SerialIds {
payout_serial_id: accept_params.payout_serial_id,
change_serial_id: accept_params.change_serial_id,
};
let own_fund_sk = crate::utils::compute_secret_key(seed, offered_contract.id, serial_ids);

let buffer_adaptor_signature = get_tx_adaptor_signature(
secp,
Expand Down Expand Up @@ -265,6 +282,7 @@ pub fn verify_and_sign_accepted_channel<S: Deref>(
offered_contract: &OfferedContract,
accept_channel: &AcceptChannel,
cet_nsequence: u32,
seed: [u8; 32],
signer: &S,
) -> Result<(SignedChannel, SignedContract, SignChannel), Error>
where
Expand Down Expand Up @@ -299,8 +317,11 @@ where
&offer_own_base_secret,
);

let offer_fund_sk =
signer.get_secret_key_for_pubkey(&offered_contract.offer_params.fund_pubkey)?;
let serial_ids = SerialIds {
payout_serial_id: accept_params.payout_serial_id,
change_serial_id: accept_params.change_serial_id,
};
let offer_fund_sk = crate::utils::compute_secret_key(seed, offered_contract.id, serial_ids);

let offer_revoke_params = offered_channel.party_points.get_revokable_params(
secp,
Expand Down Expand Up @@ -596,6 +617,7 @@ pub fn settle_channel_accept<S: Deref, T: Deref>(
lock_time: u32,
peer_timeout: u64,
signer: &S,
seed: [u8; 32],
time: &T,
) -> Result<SettleAccept, Error>
where
Expand Down Expand Up @@ -634,7 +656,13 @@ where
let fund_vout = channel.fund_output_index;
let funding_script_pubkey = &channel.fund_script_pubkey;

let own_fund_sk = signer.get_secret_key_for_pubkey(&channel.own_params.fund_pubkey)?;
let serial_ids = SerialIds {
payout_serial_id: channel.own_params.payout_serial_id,
change_serial_id: channel.own_params.change_serial_id,
};
// todo i think wrong id
let own_fund_sk =
crate::utils::compute_secret_key(seed, channel.temporary_channel_id, serial_ids);

let (settle_tx, settle_adaptor_signature) = get_settle_tx_and_adaptor_sig(
secp,
Expand Down Expand Up @@ -967,7 +995,9 @@ where
S::Target: Signer,
T::Target: Time,
{
let temp_id = get_new_temporary_id();
let mut offered_contract = OfferedContract::new(
temp_id,
contract_input,
oracle_announcements,
&signed_channel.own_params,
Expand Down
3 changes: 2 additions & 1 deletion dlc-manager/src/contract/offered_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl OfferedContract {

/// Creates a new [`OfferedContract`] from the given parameters.
pub fn new(
temp_id: [u8; 32],
contract: &ContractInput,
oracle_announcements: Vec<Vec<OracleAnnouncement>>,
offer_params: &PartyParams,
Expand Down Expand Up @@ -102,7 +103,7 @@ impl OfferedContract {
})
.collect::<Vec<ContractInfo>>();
OfferedContract {
id: crate::utils::get_new_temporary_id(),
id: temp_id,
is_offer_party: true,
contract_info,
offer_params: offer_params.clone(),
Expand Down
23 changes: 23 additions & 0 deletions dlc-manager/src/contract/signed_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::conversion_utils::PROTOCOL_VERSION;
use crate::ChannelId;

use super::accepted_contract::AcceptedContract;
use crate::utils::SerialIds;
use dlc_messages::CetAdaptorSignature;
use dlc_messages::CetAdaptorSignatures;
use dlc_messages::FundingSignatures;
Expand Down Expand Up @@ -46,4 +47,26 @@ impl SignedContract {
funding_signatures: self.funding_signatures.clone(),
}
}

pub(crate) fn get_serial_ids(&self) -> SerialIds {
if self.accepted_contract.offered_contract.is_offer_party {
SerialIds {
payout_serial_id: self
.accepted_contract
.offered_contract
.offer_params
.payout_serial_id,
change_serial_id: self
.accepted_contract
.offered_contract
.offer_params
.change_serial_id,
}
} else {
SerialIds {
payout_serial_id: self.accepted_contract.accept_params.payout_serial_id,
change_serial_id: self.accepted_contract.accept_params.change_serial_id,
}
}
}
}
50 changes: 28 additions & 22 deletions dlc-manager/src/contract_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use secp256k1_zkp::{
ecdsa::Signature, All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey, Signing,
};

use crate::utils::{get_new_temporary_id, SerialIds};
use crate::{
contract::{
accepted_contract::AcceptedContract, contract_info::ContractInfo,
Expand All @@ -32,6 +33,7 @@ pub fn offer_contract<C: Signing, W: Deref, B: Deref, T: Deref>(
refund_delay: u32,
counter_party: &PublicKey,
wallet: &W,
seed: [u8; 32],
blockchain: &B,
time: &T,
) -> Result<(OfferedContract, OfferDlc), Error>
Expand All @@ -42,15 +44,21 @@ where
{
contract_input.validate()?;

let (party_params, _, funding_inputs_info) = crate::utils::get_party_params(
let temp_id = get_new_temporary_id();
let serial_ids = SerialIds::generate();
let funding_privkey = crate::utils::compute_secret_key(seed, temp_id, serial_ids);
let (party_params, funding_inputs_info) = crate::utils::get_party_params(
secp,
funding_privkey,
serial_ids,
contract_input.offer_collateral,
contract_input.fee_rate,
wallet,
blockchain,
)?;

let offered_contract = OfferedContract::new(
temp_id,
contract_input,
oracle_announcements,
&party_params,
Expand All @@ -71,6 +79,7 @@ pub fn accept_contract<W: Deref, B: Deref>(
secp: &Secp256k1<All>,
offered_contract: &OfferedContract,
wallet: &W,
seed: [u8; 32],
blockchain: &B,
) -> Result<(AcceptedContract, AcceptDlc), crate::Error>
where
Expand All @@ -79,8 +88,12 @@ where
{
let total_collateral = offered_contract.total_collateral;

let (accept_params, fund_secret_key, funding_inputs) = crate::utils::get_party_params(
let serial_ids = SerialIds::generate();
let fund_secret_key = crate::utils::compute_secret_key(seed, offered_contract.id, serial_ids);
let (accept_params, funding_inputs) = crate::utils::get_party_params(
secp,
fund_secret_key,
serial_ids,
total_collateral - offered_contract.offer_params.collateral,
offered_contract.fee_rate_per_vb,
wallet,
Expand Down Expand Up @@ -636,41 +649,37 @@ where
}

/// Signs and return the CET that can be used to close the given contract.
pub fn get_signed_cet<C: Signing, S: Deref>(
pub fn get_signed_cet<C: Signing>(
secp: &Secp256k1<C>,
contract: &SignedContract,
contract_info: &ContractInfo,
adaptor_info: &AdaptorInfo,
attestations: &[(usize, OracleAttestation)],
signer: &S,
) -> Result<Transaction, Error>
where
S::Target: Signer,
{
seed: [u8; 32],
) -> Result<Transaction, Error> {
let (range_info, sigs) =
crate::utils::get_range_info_and_oracle_sigs(contract_info, adaptor_info, attestations)?;
let mut cet = contract.accepted_contract.dlc_transactions.cets[range_info.cet_index].clone();
let offered_contract = &contract.accepted_contract.offered_contract;

let (adaptor_sigs, fund_pubkey, other_pubkey) = if offered_contract.is_offer_party {
let (adaptor_sigs, other_pubkey) = if offered_contract.is_offer_party {
(
contract
.accepted_contract
.adaptor_signatures
.as_ref()
.unwrap(),
&offered_contract.offer_params.fund_pubkey,
&contract.accepted_contract.accept_params.fund_pubkey,
)
} else {
(
contract.adaptor_signatures.as_ref().unwrap(),
&contract.accepted_contract.accept_params.fund_pubkey,
&offered_contract.offer_params.fund_pubkey,
)
};

let funding_sk = signer.get_secret_key_for_pubkey(fund_pubkey)?;
let funding_sk =
crate::utils::compute_secret_key(seed, offered_contract.id, contract.get_serial_ids());

dlc::sign_cet(
secp,
Expand All @@ -694,33 +703,29 @@ where
}

/// Signs and return the refund transaction to refund the contract.
pub fn get_signed_refund<C: Signing, S: Deref>(
pub fn get_signed_refund<C: Signing>(
secp: &Secp256k1<C>,
contract: &SignedContract,
signer: &S,
) -> Result<Transaction, Error>
where
S::Target: Signer,
{
seed: [u8; 32],
) -> Result<Transaction, Error> {
let accepted_contract = &contract.accepted_contract;
let offered_contract = &accepted_contract.offered_contract;
let funding_script_pubkey = &accepted_contract.dlc_transactions.funding_script_pubkey;
let fund_output_value = accepted_contract.dlc_transactions.get_fund_output().value;
let (fund_pubkey, other_fund_pubkey, other_sig) = if offered_contract.is_offer_party {
let (other_fund_pubkey, other_sig) = if offered_contract.is_offer_party {
(
&offered_contract.offer_params.fund_pubkey,
&accepted_contract.accept_params.fund_pubkey,
&accepted_contract.accept_refund_signature,
)
} else {
(
&accepted_contract.accept_params.fund_pubkey,
&offered_contract.offer_params.fund_pubkey,
&contract.offer_refund_signature,
)
};

let fund_priv_key = signer.get_secret_key_for_pubkey(fund_pubkey)?;
let fund_priv_key =
crate::utils::compute_secret_key(seed, offered_contract.id, contract.get_serial_ids());
let mut refund = accepted_contract.dlc_transactions.refund.clone();
dlc::util::sign_multi_sig_input(
secp,
Expand Down Expand Up @@ -766,6 +771,7 @@ mod tests {
secp256k1_zkp::SECP256K1,
&offered_contract,
&wallet,
[0; 32],
&blockchain,
)
.expect("Not to fail");
Expand Down
Loading
Loading