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

Remove global context use from dlc-manager #203

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
4 changes: 2 additions & 2 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ impl ContractSignerProvider for BitcoinCoreProvider {
// if not something has gone wrong
assert_eq!(label_map.len(), 1);

let pk = self
let sk = self
.client
.lock()
.unwrap()
.dump_private_key(address)
.map_err(rpc_err_to_manager_err)?;
Ok(SimpleSigner::new(pk.inner))
Ok(SimpleSigner::new(sk.inner))
} else {
let sk = SecretKey::new(&mut thread_rng());
let network = self.get_network()?;
Expand Down
2 changes: 2 additions & 0 deletions dlc-manager/src/channel_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ where
let keys_id = signer_provider.derive_signer_key_id(true, id);
let signer = signer_provider.derive_contract_signer(keys_id)?;
let (offer_params, funding_inputs_info) = crate::utils::get_party_params(
secp,
contract.offer_collateral,
contract.fee_rate,
wallet,
Expand Down Expand Up @@ -157,6 +158,7 @@ where

let signer = signer_provider.derive_contract_signer(offered_contract.keys_id)?;
let (accept_params, funding_inputs) = crate::utils::get_party_params(
secp,
total_collateral - offered_contract.offer_params.collateral,
offered_contract.fee_rate_per_vb,
wallet,
Expand Down
5 changes: 4 additions & 1 deletion dlc-manager/src/contract_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use crate::{

/// Creates an [`OfferedContract`] and [`OfferDlc`] message from the provided
/// contract and oracle information.
pub fn offer_contract<W: Deref, B: Deref, T: Deref, X: ContractSigner, SP: Deref>(
pub fn offer_contract<W: Deref, B: Deref, T: Deref, X: ContractSigner, SP: Deref, C: Signing>(
secp: &Secp256k1<C>,
contract_input: &ContractInput,
oracle_announcements: Vec<Vec<OracleAnnouncement>>,
refund_delay: u32,
Expand All @@ -48,6 +49,7 @@ where
let keys_id = signer_provider.derive_signer_key_id(true, id);
let signer = signer_provider.derive_contract_signer(keys_id)?;
let (party_params, funding_inputs_info) = crate::utils::get_party_params(
secp,
contract_input.offer_collateral,
contract_input.fee_rate,
wallet,
Expand Down Expand Up @@ -90,6 +92,7 @@ where

let signer = signer_provider.derive_contract_signer(offered_contract.keys_id)?;
let (accept_params, funding_inputs) = crate::utils::get_party_params(
secp,
total_collateral - offered_contract.offer_params.collateral,
offered_contract.fee_rate_per_vb,
wallet,
Expand Down
15 changes: 7 additions & 8 deletions dlc-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod payout_curve;
mod utils;

use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::secp256k1::SECP256K1;
use bitcoin::{Address, Block, OutPoint, Script, Transaction, TxOut, Txid};
use chain_monitor::ChainMonitor;
use channel::offered_channel::OfferedChannel;
Expand All @@ -50,8 +49,8 @@ use dlc_messages::ser_impls::{read_address, write_address};
use error::Error;
use lightning::ln::msgs::DecodeError;
use lightning::util::ser::{Readable, Writeable, Writer};
use secp256k1_zkp::XOnlyPublicKey;
use secp256k1_zkp::{PublicKey, SecretKey};
use secp256k1_zkp::{PublicKey, SecretKey, Signing};
use secp256k1_zkp::{Secp256k1, XOnlyPublicKey};
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::RwLock;
Expand Down Expand Up @@ -86,7 +85,7 @@ impl Time for SystemTimeProvider {
/// Provides signing related functionalities.
pub trait ContractSigner: Clone {
/// Get the public key associated with the [`ContractSigner`].
fn get_public_key(&self) -> Result<PublicKey, Error>;
fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error>;
/// Returns the secret key associated with the [`ContractSigner`].
// todo: remove this method and add create_adaptor_signature to the trait
fn get_secret_key(&self) -> Result<SecretKey, Error>;
Expand All @@ -106,8 +105,8 @@ impl SimpleSigner {
}

impl ContractSigner for SimpleSigner {
fn get_public_key(&self) -> Result<PublicKey, Error> {
Ok(self.secret_key.public_key(SECP256K1))
fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error> {
Ok(self.secret_key.public_key(secp))
}

fn get_secret_key(&self) -> Result<SecretKey, Error> {
Expand All @@ -116,8 +115,8 @@ impl ContractSigner for SimpleSigner {
}

impl ContractSigner for SecretKey {
fn get_public_key(&self) -> Result<PublicKey, Error> {
Ok(self.public_key(SECP256K1))
fn get_public_key<C: Signing>(&self, secp: &Secp256k1<C>) -> Result<PublicKey, Error> {
Ok(self.public_key(secp))
}

fn get_secret_key(&self) -> Result<SecretKey, Error> {
Expand Down
1 change: 1 addition & 0 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ where
oracle_announcements: Vec<Vec<OracleAnnouncement>>,
) -> Result<OfferDlc, Error> {
let (offered_contract, offer_msg) = crate::contract_updater::offer_contract(
&self.secp,
contract_input,
oracle_announcements,
REFUND_DELAY,
Expand Down
5 changes: 3 additions & 2 deletions dlc-manager/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub(crate) fn compute_id(
res
}

pub(crate) fn get_party_params<W: Deref, B: Deref, X: ContractSigner>(
pub(crate) fn get_party_params<W: Deref, B: Deref, X: ContractSigner, C: Signing>(
secp: &Secp256k1<C>,
own_collateral: u64,
fee_rate: u64,
wallet: &W,
Expand All @@ -70,7 +71,7 @@ where
W::Target: Wallet,
B::Target: Blockchain,
{
let funding_pubkey = signer.get_public_key()?;
let funding_pubkey = signer.get_public_key(secp)?;

let payout_addr = wallet.get_new_address()?;
let payout_spk = payout_addr.script_pubkey();
Expand Down
Loading